Welcome to Dream.In.Code
Become a Java Expert!

Join 149,575 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,700 people online right now. Registration is fast and FREE... Join Now!




Rock Paper Scissor..i cannot fix the problem..please help

 
Reply to this topicStart new topic

Rock Paper Scissor..i cannot fix the problem..please help, cuz this a brief..please have a look on it..i don't know why it

alexakakazaf
12 Oct, 2007 - 03:59 AM
Post #1

New D.I.C Head
*

Joined: 4 Sep, 2007
Posts: 3


My Contributions
the error of this java is sometime cannot read the correct statement when you won or lost..
it still shows the drawn statement
thanks for anyone's help
i hope i can more better next time..
cuz it's a brief so i deleted something that's not important

CODE
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;

public class rps extends Applet implements ActionListener
{
    Button Scissor;
    Button Paper;
    Button Rock;
    Button Reset;
    
    int count, random, userChosen, won, lost, draw, cpuChosen,total,result;
    
    String user, cpu;
    
    public void init()
    {
        Scissor = new Button ("Scissor");
        Paper = new Button ("Paper");
        Rock = new Button ("Rock");
        Reset = new Button("Reset");

        add(Paper);
        add(Scissor);
        add(Rock);
        add(Reset);
        
        Paper.addActionListener(this);
        Scissor.addActionListener(this);
        Rock.addActionListener(this);
        Reset.addActionListener(this);
        
        user = " nothing ";
        cpu = " nothing ";
        result = 0;
    }
    public void actionPerformed(ActionEvent e)
    {
        cpuChosen = (int) (Math.random()*3 +1);
        if (e.getSource() == Rock)
        {
            userChosen = 1;
            user = "Rock";
            count = 1;
        }
        if (e.getSource() == Paper){
            userChosen = 2;
            user = "Paper";
            count = 1;
        }
        if (e.getSource() == Scissor)
        {
            userChosen = 3;
            user = "Scissor";
            count = 1;
        }
        if (cpuChosen == 1)
        {
            cpu = "Rock";
        }
        if (cpuChosen == 2)
        {
            cpu = "Paper";
        }
        if (cpuChosen == 3)
        {
            cpu = "Scissor";
        }
        total++;
        if(e.getSource() == Reset)
        {
            user = "nothing";
            cpu = "nothing";
            total = 0;
            count = 0;
            won = 0;
            lost = 0;

        }
        repaint();
        }
    public void decide()
    {
        if(userChosen==1)
        {
            if (cpuChosen==1)
            {result = 2;
            draw++;
            }
        else if (cpuChosen==2)
            {
                result = 3;
                lost++;}
        else
            {
                result = 1;
                won++;}
            }
        if(userChosen==2)
            {
            if (cpuChosen==1)
            {
                result = 1;
                won++;}
            else if (cpuChosen==2)
            {result = 2;
            draw++;}
            else
            {result = 3;
            }
            lost++;}
        if(userChosen==3)
            {
            if (cpuChosen==1)
            {
            result = 3;
            lost++;
            }
            else if (cpuChosen==2)
            {result = 1;
            }
            won++;
            }
            else
            {
            result = 2;
            draw++;
            }
        
}
    public void paint (Graphics g)
    {
        decide();
        g.fillRect(0, 0, 1280, 800);
        g.setColor(Color.white);
        g.drawString("alex a.k.a kazaf, all rights reserved", 425, 660);
        g.drawString(""+draw   , 230, 300);
        g.drawString(""+lost    , 60, 300);
        g.drawString(""+won   , 150, 300);
        g.drawString("Ya chosen " + user, 600, 140);
        g.drawString("CPU chosen " + cpu, 600, 160);
         g.drawString("--result--" + result, 100, 360);
        g.drawString("--userChosen--" + userChosen, 100, 380);
        g.drawString("--cpuChosen-" + cpuChosen, 100, 400);
        if (result == 1)
        {              
        g.drawString("Yo, Ya won!", 600, 180);
        }
        else if (result ==2)
        {
        g.drawString("it's drawn, Come on. try again", 600, 180);
        }
        else
        {
            g.drawString("Oh, sorry! ya lost!", 600, 180);
        }
    }
}
EDIT: CODE TAGS

This post has been edited by PennyBoki: 12 Oct, 2007 - 04:31 AM
User is offlineProfile CardPM
+Quote Post

PennyBoki
RE: Rock Paper Scissor..i Cannot Fix The Problem..please Help
12 Oct, 2007 - 05:25 AM
Post #2

system("revolution");
Group Icon

Joined: 11 Dec, 2006
Posts: 2,010



Thanked: 7 times
Dream Kudos: 500
Expert In: Java,C++,C

My Contributions
OK.

I made small changes. Below are the codes, both for the .java and .html.

You need to have large enough applet window in the html page so that you could see the string Yo Ya won... etc

That is because of the coordinates where you draw the string. Now I also made some changes in the decide() function, but only with the position of the curly braces. Now your logic errors were made by bad formatting the code of the .java file.

Ok you'll see the differences, but since this is applet, I suggest you delete the rps.class file. Then use the codes below. If you have any questions or problems with it, you know where to ask. Enjoy smile.gif

rps.java

CODE
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;

public class rps extends Applet implements ActionListener
{
    Button Scissor;
    Button Paper;
    Button Rock;
    Button Reset;
    
    int count, random, userChosen, won, lost, draw, cpuChosen,total,result;
    
    String user, cpu;
    
    public void init()
    {
        Scissor = new Button ("Scissor");
        Paper = new Button ("Paper");
        Rock = new Button ("Rock");
        Reset = new Button("Reset");

        add(Paper);
        add(Scissor);
        add(Rock);
        add(Reset);
        
        Paper.addActionListener(this);
        Scissor.addActionListener(this);
        Rock.addActionListener(this);
        Reset.addActionListener(this);
        
        user = " nothing ";
        cpu = " nothing ";
        result = 0;
    }
    public void actionPerformed(ActionEvent e)
    {
        cpuChosen = (int) (Math.random()*3 +1);
        if (e.getSource() == Rock)
        {
            userChosen = 1;
            user = "Rock";
            count = 1;
        }
        if (e.getSource() == Paper){
            userChosen = 2;
            user = "Paper";
            count = 1;
        }
        if (e.getSource() == Scissor)
        {
            userChosen = 3;
            user = "Scissor";
            count = 1;
        }
        if (cpuChosen == 1)
        {
            cpu = "Rock";
        }
        if (cpuChosen == 2)
        {
            cpu = "Paper";
        }
        if (cpuChosen == 3)
        {
            cpu = "Scissor";
        }
        total++;
        if(e.getSource() == Reset)
        {
            user = "nothing";
            cpu = "nothing";
            total = 0;
            count = 0;
            won = 0;
            lost = 0;

        }
        repaint();
        }
    public void decide()
    {
        if(userChosen==1)
        {
                    if (cpuChosen==1)
                    {
                        result = 2;
                        draw++;
                    }
                    else if (cpuChosen==2)
                    {
                        result = 3;
                        lost++;}
                    else
                    {
                        result = 1;
                        won++;
                    }
            }
            
         else if(userChosen==2)
            {
                
                if (cpuChosen==1)
                {
                    result = 1;
                    won++;
                    
                }
                else if (cpuChosen==2)
                {
                    result = 2;
                    draw++;
                }
                else
                {
                    result = 3;
                    lost++;
                }
                
            
            }
            
          else if(userChosen==3)
            {
                
                    if (cpuChosen==1)
                    {
                        result = 3;
                        lost++;
                    }
                    else if (cpuChosen==2)
                    {
                        result = 1;
                        won++;
                    }
                    
                    else
                    {
                        result = 2;
                        draw++;
                    }
            }
            
        
}
    public void paint (Graphics g)
    {
        decide();
        g.fillRect(0, 0, 1280, 800);
        g.setColor(Color.white);
        g.drawString("alex a.k.a kazaf, all rights reserved", 300, 400);
        g.drawString(""+draw   , 230, 300);
        g.drawString(""+lost    , 60, 300);
        g.drawString(""+won   , 150, 300);
        g.drawString("Ya chosen " + user, 200, 140);
        g.drawString("CPU chosen " + cpu, 200, 160);
        g.drawString("--result--" + result, 100, 360);
        g.drawString("--userChosen--" + userChosen, 100, 380);
        g.drawString("--cpuChosen-" + cpuChosen, 100, 400);
        if (result == 1)
        {              
        g.drawString("Yo, Ya won!", 200, 200);
        }
        else if (result ==2)
        {
        g.drawString("it's drawn, Come on. try again", 200, 200);
        }
        else
        {
            g.drawString("Oh, sorry! ya lost!", 200, 200);
        }
    }
}


rps.html

CODE
<html>

    <head>
            <applet code="rps.class" height=800 width=600 >

            </applet>
            
    </head>






    <body>
            

    </body>


</html>

User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/7/09 10:32PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month