create a rock-paper-scissors program in Java

Asked 2 years ago, Updated 2 years ago, 30 views

John's hand prints rock, paper, scissors, rock... and Paul's hand prints randomly, so I did the following, but it prints differently from what John's hand expects.
(By the way, the winner of this rock-paper-scissors program is the winner.)

How (where and how to fix the OrderedComputerPlayer) results in the expected execution?Thank you for your cooperation.

/** Regular rock-paper-scissors computer player*/
public class OrderedComputerPlayer extensionsPlayer{
    protected Hand goo, choki, par;
    // a random number used to make a decision

    /** constructor, name of player*/
    public OrderedComputerPlayer(String name){
    super(name);
    // set one's hands
    goo=new Goo();
    choki=new Choki();
    par = new Par();
    
    }

    /** touch the num player of the match
     *  Override Player showHand
     */
    public Hand showHand(int match, int num) {
        
    switch(num%4){
    case1:return goo;
        case2—return par;
        case3—return choki; 
        case0:return goo; 
        } num++;return goo;
    }
}
/** Regular rock-paper-scissors computer player*/
public class NormalComputerPlayer extensionsPlayer{
    protected Hand goo, choki, par;
    protected java.util.Random random;// random number used to make a decision

    /** constructor, name of player*/
    publicNormalComputerPlayer(String name){
    super(name);
    // set one's hands
    goo=new Goo();
    choki=new Choki();
    par = new Par();
    random = new java.util.Random();
    }

    /** touch the num player of the match
     *  Override Player showHand
     */
    public Hand showHand(int match, int num) {
    int hand = random.nextInt(3);
    if(hand==0){
        return goo;
    } else if(hand==1){
        return choki;
    } else{
        return par;
    }
    }
}
/** Class for starting rock-paper-scissors*/
public class Janken {
    public static void main(String args[]){
    try{
        Judge judge=new Judge(); // Create an umpire

        // Create players and register with referees
        Player jhon = new OrderedComputerPlayer("Jhon");
        judge.registerPlayer(jhon);
        Player Paul = new NormalComputerPlayer("Paul");
        judge.registerPlayer(paul);

        // Rock-paper-scissors starts in the third round.
        judge.startJankens(3);
    }
    catch(JankenExceptione){
        System.err.println("Error:"+e.getMessage()));
    }
    }
}
/** Rock-paper-scissors player abstract class*/
public abstract class Player {
    protected String name;// Name of the player
    protected int matches; // matches (the winner wins first matches)
    protected int wins; // player wins
    protected int losses;// Number of player losses

    /** constructor, name of player*/
    public Player (String name) {
    this.name = name; // Name of the player
    wins=losts=0;// Initialize win/loss
    }

    /** Returns the name of the player*/
    public String getName() {
    return name;
    }

    /** a method of determining how many games to play
     */
    public void setMatches(int matches) {
    this.matches = matches;
    }

    /** 
     *   method of being informed that someone has won
     */
    public void youWon() {
    wins++;
    }

    /** 
     *   method of being informed of a defeat
     */
    public void youLost() {
    losts++;
    }


    /** touch the num player of the match
     *   It's an abstract method, so it overrides in subclasses.
     */
    public abstract Hand showHand (int match, int num);
}

execution results:

$java Janken
Match 1: Jan Ken Pon!
Match 1-1:
Jhon's hand is Goo
Paul's hand is Goo

Aikode Sho
Match 1-2:
Jhon's hand is Par
Paul's hand is Goo

Match 1: Jhonwon.

Match 2: Jan Ken Pon!
Match 2-1:
Jhon's hand is Goo
Paul's hand is Par

Match 2: Paul won.

Match 3: Jan Ken Pon!
Match 3-1:
Jhon's hand is Goo
Paul's hand is Choki

Match 3: Jhon won.

Match 4: Jan Ken Pon!
Match 4-1:
Jhon's hand is Goo
Paul's hand is Goo

Aikode Sho
Match 4-2:
Jhon's hand is Par
Paul's hand is Choki

Match 4: Paul won.

Match 5: Jan Ken Pon!
Match 5-1:
Jhon's hand is Goo
Paul's hand is Choki

Match 5: Jhonwon.

Jhon is a champion.
Jhon: 3 wins
Paul—2 wins

<<Continued>>
Thank you for pointing it out.
When I said switch(match %4), it looks like the following

$java Janken
Match 1: Jan Ken Pon!
Match 1-1:
Jhon's hand is Goo
Paul's hand is Goo

Aikode Sho
Match 1-2:
Jhon's hand is Goo
Paul's hand is Goo

Aikode Sho
Match 1-3:
Jhon's hand is Goo
Paul's hand is Goo

Aikode Sho
Match 1-4:
Jhon's hand is Goo
Paul's hand is Goo

Aikode Sho
Match 1-5:
Jhon's hand is Goo
Paul's hand is Choki

Match 1: Jhonwon.

Match 2: Jan Ken Pon!
Match 2-1:
Jhon's hand is Par
Paul's hand is Par

Aikode Sho
Match 2-2:
Jhon's hand is Par
Paul's hand is Goo

Match 2: Jhonwon.

Match 3: Jan Ken Pon!
Match 3-1:
Jhon's hand is Choki
Paul's hand is Par

Match 3: Jhon won.

Jhon is a champion.
Jhon: 3 wins
Paul: 0 wins

With this, John's hand changes every time the match changes, so I'm looking forward to it

$java Janken
Match 1: Jan Ken Pon!
Match 1-1:
Jhon's hand is Goo
Paul's hand is Goo

Aikode Sho
Match 1-2:
Jhon's hand is Par
Paul's hand is Goo

Match 1: Jhonwon.

Match 2: Jan Ken Pon!
Match 2-1:
Jhon's hand is Choki
Paul's hand is Par

Match 2: Jhonwon.

Match 3: Jan Ken Pon!
Match 3-1:
Jhon's hand is Goo
Paul's hand is Par

Match 3: Paul won.

Match 4: Jan Ken Pon!
Match 4-1:
Jhon's hand is Goo
Paul's hand is Par

Match 4: Paul won.

Match 5: Jan Ken Pon!
Match 5-1:
Jhon's hand is Par
Paul's hand is Goo

Match 5: John won.

Jhon is a champion.
Jhon: 3 wins
Paul—2 wins

does not look like .

My program is

publicHandshowHand(int match, int num){
        
    switch(num%4){
      case1:return goo;
        case2—return par;
        case3—return choki; 
        case0:return goo; 
        } num++;return goo;
    }

is indicated, and the last one is
return goo; I think it will probably be reset every time the winner or loser is decided, and it starts with goo, but if you don't have return goo;, you'll get an error.

Regardless of whether you win or lose, what should I do to make it rock, go, go, go, go, go, go, go, go, go, go, go, go, go, go, go, go, go, go, go, go, go.

java

2022-09-30 14:42

1 Answers

There seems to be code that is still omitted, so it includes speculation, but the num variable will be reset each time depending on the match game, so you should prepare another variable to manage "how many times through the game" and decide what to do.

In other words, change the showHand to a variable that represents "how many times now" instead of match or num.


2022-09-30 14:42

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.