5 Replies - 2798 Views - Last Post: 19 October 2011 - 02:33 PM Rate Topic: -----

#1 seans0007  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 48
  • Joined: 03-October 11

OOP ROCK PAPER SCISSORS GAME WITH TO LOOP UNTIL SOMEONE GETS 5 WINS

Posted 18 October 2011 - 11:43 PM

I had to write an object oriented program to create a rock paper scissors game until someone reaches 5 wins..I've got it to run and everything i just dont know where to incorporate the while loop to loop until some reaches 5. Also when i run the program if i put in rock first then scissors it says scissors wins over rock when clearly it doesnt but if i put it the other way around it works fine. It does this for all of them. And ive tried messing with it to clear that up but can't get anywhere i feel like it is something simple just cant see it.. thanks

player class
import java.util.Scanner;
import java.util.Random;


public class Player {
    /**
     * name - player's name
     * hand - player's hand
     */
    private String name, hand;
    /**
     * Stores theName to the attribute name
     * of the Player class.
     * 
     * @param theName the user input from the
     *                main interface.
     */
       public void setName(String theName) {
           name = theName;
       }

       /**
        * Sets the hand of the player. If player's name is
        * "computer", let the brilliant computer decide
        * a move for itself.
        */
       public void setHand() {
           Scanner in = new Scanner(System.in);
           //Player's name is not computer.
           if(!name.equals("Computer") && !name.equals("computer")) {
               do {
                   hand = in.nextLine();
                   //make the hand comparison a piece of cake.
                   hand = hand.toUpperCase();
               }
               //don't accept input other than R, P, or S.
               while (!hand.equals("R") &&
                      !hand.equals("P") &&
                      !hand.equals("S"));
               //assigns the long name for the shorthand move name.
               if(hand.equals("R")) hand = "Rock";
               if(hand.equals("P")) hand = "Paper";
               if(hand.equals("S")) hand = "Scissors";
           }
           //Player's name is computer. Select a move automatically
           else if(name.equals("Computer") || name.equals("computer")) {
               int aiMove;
               Random rand = new Random();
               //the computer is thinking.
               aiMove = rand.nextInt(3);
               //assigns the long name for the shorthand move name.
               if(aiMove == 1) hand = "Rock";
               else if(aiMove == 2) hand = "Paper";
               else hand = "Scissors";
           }
       }

       /**
        * Getter function for the Player's name.
        *
        * @return the Player's name
        */
       public String getName() {
           return name;
       }
       
       /**
        * Getter function for the Player's move.
        *
        * @return the Player's move.
        */
       public String getHand() {
           return hand;
       }
}



driver class
import static java.lang.System.out;
import java.util.Scanner;

/**
 * Game.java -
 *   The application interface of
 *   Rock-Paper-Scissors Game.
 *
 * @author GERONA, JOHN MICHAEL D.
 * @author BSIT - II / Block 201
 * @author Adamson University
 * @version 09/06/2009
 */
public class Game {
    /**
     * Declares an array variable named
     * "players" holding a reference of type Player.
     */
    private static Player[] players;
    /**
     * numberPlayers - variable for number of players
     * win - game state
     */
    private static int numberPlayers, win;
    /**
     * Declares a variable named "in" holding
     * a reference of type Scanner.
     */
    static Scanner in = new Scanner(System.in);

    /**
     * The rest of the functions/objects are
     * called here.
     *
     * @param args Java language requires this :)/>)
     */
     int Player1;
     int Player2;
    public static void main(String[] args)
    {

       out.println("ROCK-PAPER-SCISSORS GAME");
        out.println("NOTE: to play with the computer, enter Computer as the " +
                    "Player's name.");
        out.println("      You can set the computer as Player 1 and Player 2");
        out.println("NOTE: hand input will loop until user(s) " +
                    "input(s) valid hand values.");
        out.println("      Enter only the first letter of your move.");
        out.println("VALID HANDS: R-Rock, P-Paper, S-Scissors");

        numberPlayers = 2;
        //creates an array instance of Player class
        players = new Player[numberPlayers];
        //calls the function to instantiate properly an array class.
        setPlayers();
        out.println();
        setName(); //calls the prompt for player's name input.
        out.println();
        setHand(); //calls the prompt for player's hand input.
        out.println();
        //prints the game results.
        announceWinner(setWinner(players[0].getHand(), players[1].getHand()));

    }

    /**
     * Creates instances of the player class
     * In this case, players are two.
     */
    private static void setPlayers() {
        for(int i=0;i<players.length;i++) {
            players[i] = new Player();
        }
    }

    /**
     * Prompts the user to enter a name for each player,
     * for identification purposes.
     */
    private static void setName() {
        for(int i=0;i<players.length;i++) {
            out.print("Player "); out.print(i+1); out.print(" name: ");
            players[i].setName(in.nextLine());
        }
    }

    /**
     * Prompts the user to enter his/her
     * choice of move whether it is a R for
     * rock, P for paper, or S for scissors.
     * The user will not be required to input a
     * move if the player's name is "Computer".
     */
    private static void setHand() {
        for(int i=0;i<players.length;i++) {
            //if Player's name is "computer", don't prompt the user
            //to input computer's move.
            if(players[i].getName().equals("Computer") ||
               players[i].getName().equals("computer")) {
                 out.print(players[i].getName() + "'s hand: ");
                 players[i].setHand();
                 out.println(players[i].getHand());
            }
            //prompt the user to input player's move if
            //player's name is not "computer".
            else {
                 out.print(players[i].getName() + "'s hand: ");
                 players[i].setHand();
            }
        }
    }

    /**
     * Compares the hand of the two players to
     * determine the winner.
     *
     * @param oneHand The move of Player 1
     * @param twoHand The move of Player 2
     * @return 1 if Player 1 wins, 0 if tie
     *         -1 if Player 2 wins.
     */
    private static int setWinner(String oneHand, String twoHand) {
        //rock and scissors
        if(oneHand.equals("ROCK") && twoHand.equals("SCISSORS"))
            win = 1;
            //scissors and paper
        else if(oneHand.equals("SCISSORS") && twoHand.equals("PAPER"))
            win = 1;
        //paper and rock
        else if(oneHand.equals("PAPER") && twoHand.equals("ROCK"))
            win = 1;
        //tie
        else if(oneHand.equals(twoHand))
            win = 0;
        //Player 2 win
        else
            win = -1;
        return win;
    }

    /**
     * Prints the winner on the command prompt.
     * If tie, prints a message that tells that the
     * game is tied.
     *
     * @param win The state of the game, 1 if
     *            in favor of the Player 1, -1 if
     *            in favor of Player 2, 0 if
     *            tie.
     */
    private static void announceWinner(int win) {
        //Player 1 wins
        if(win==1) {
            out.println(players[0].getHand() + " wins over " + players[1].getHand());
            out.println(players[0].getName() + " wins.");

        }
        //Player 2 wins
        else if(win==-1) {
            out.println(players[1].getHand() + " wins over " + players[0].getHand());
            out.println(players[1].getName() + " wins.");

        }
        //For some reason, the game is tied. :)/>)
        else if(win==0) {
            out.println("It's a tie.");
        }

    }
}



Is This A Good Question/Topic? 0
  • +

Replies To: OOP ROCK PAPER SCISSORS GAME WITH TO LOOP UNTIL SOMEONE GETS 5 WINS

#2 Sheph  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 432
  • View blog
  • Posts: 1,020
  • Joined: 12-October 11

Re: OOP ROCK PAPER SCISSORS GAME WITH TO LOOP UNTIL SOMEONE GETS 5 WINS

Posted 19 October 2011 - 12:04 AM

You want to loop lines 57 through 62 in your driver class until there are 5 wins. I'm not sure about the incorrect winner problem. It probably has something to do with your setWinner() method.
Was This Post Helpful? 1
  • +
  • -

#3 seans0007  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 48
  • Joined: 03-October 11

Re: OOP ROCK PAPER SCISSORS GAME WITH TO LOOP UNTIL SOMEONE GETS 5 WINS

Posted 19 October 2011 - 12:17 AM

View PostSheph, on 19 October 2011 - 12:04 AM, said:

You want to loop lines 57 through 62 in your driver class until there are 5 wins. I'm not sure about the incorrect winner problem. It probably has something to do with your setWinner() method.


awesome that worked thanks and I had my rock paper scissors in my driver class in all caps for some reason so thats why that didnt work i knew it was something simple but thanks again!
Was This Post Helpful? 0
  • +
  • -

#4 Sheph  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 432
  • View blog
  • Posts: 1,020
  • Joined: 12-October 11

Re: OOP ROCK PAPER SCISSORS GAME WITH TO LOOP UNTIL SOMEONE GETS 5 WINS

Posted 19 October 2011 - 12:33 AM

Wow nice, I can't believe I didn't notice that myself. I was looking at your setWinner() method over and over assuming that they were all caps in your Player class. Grats on working code!
Was This Post Helpful? 0
  • +
  • -

#5 immeraufdemhund  Icon User is offline

  • D.I.C Regular

Reputation: 79
  • View blog
  • Posts: 495
  • Joined: 29-March 10

Re: OOP ROCK PAPER SCISSORS GAME WITH TO LOOP UNTIL SOMEONE GETS 5 WINS

Posted 19 October 2011 - 04:20 AM

you can simplify both your classes a bit. First by seperating Object code from your driver code.

consider this variation for your Player class

  public char computerMove()
  {
    int aiMove;
    Random rand = new Random();
    aiMove = rand.nextInt(3);
    char[] handChar = {'P','R','S'};
    return handChar[aiMove];
  }
  /*
   * doesn't matter if whose hand it is, set it and give it full name
   */
  public boolean setHand(char handChar)
  {
    boolean validMove;
    switch (handChar)
    {
      case 'P':
      case 'R':
      case 'S':
        validMove = true;
        this.hand = getFullName(handChar);
        break;
//if handChar wasn't one of those 3 then it is not a valid move. Try again
      default:
        validMove = false;
    }
    return validMove;
  }
  private String getFullName(char handChar)
  {
    String temp;
    switch(handChar)
    {
      case 'R':
        temp = "Rock";
        break;
      case 'P':
        temp = "Paper";
        break;
      default:
        temp = "Scissors";
        break;
    }
    return temp;
  }
  @Override
  public String toString()
  {
    return name + " threw " + hand;
  }



Then you can do something like this in your Game class
    Player players[] = new Player[3];
    
    for (int i=0; i<players.length; i++)
    {
      players[i] = new Player();
    }
    
    players[0].setName("Robert", true); //AI named Robert
    players[1].setName("Christopher",false); //Human named Christopher
    players[2].setName(true); //AI name Computer

    Scanner in = new Scanner(System.in);
    for (int i = 0; i<players.length; i++)
    {
      boolean playerThrew = true;
      do
      {
        char hand;
        if (players[i].isComputer())
        {
          hand = players[i].computerMove();
        }
        else
        {
          System.out.println(players[i].getName() + "'s hand:");
          hand = in.nextLine().toUpperCase().charAt(0);
        }

        playerThrew = players[i].setHand(hand);
      }
      while (playerThrew != true);
    }
    
    for (Player player:players)
      System.out.println(player.toString());



my sample out put:

Quote

Christopher's hand:
S
Robert threw Scissors
Christopher threw Scissors
Computer threw Rock
BUILD SUCCESSFUL (total time: 22 seconds)

(run again)
Christopher's hand:
a
Christopher's hand:
f
Christopher's hand:
e
Christopher's hand:
3
Christopher's hand:
p
Robert threw Scissor
Christopher threw Paper
Computer threw Rock

This post has been edited by immeraufdemhund: 19 October 2011 - 04:24 AM

Was This Post Helpful? 0
  • +
  • -

#6 Tylermon  Icon User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 25
  • Joined: 05-June 11

Re: OOP ROCK PAPER SCISSORS GAME WITH TO LOOP UNTIL SOMEONE GETS 5 WINS

Posted 19 October 2011 - 02:33 PM

I noticed a few times that you have some code that seems rather redundant, or unnecessary such as:
if(!name.equals("Computer") && !name.equals("computer")) {
31	               do {
32	                   hand = in.nextLine();
33	                   //make the hand comparison a piece of cake.
34	                   hand = hand.toUpperCase();
35	               }

instead you can use the .equalsIgnoreCase and thus use less code in the long run for having to check if something was uppercase or lowercase.
if(!name.equalsIgnoreCase("Computer")) {
31	               do {
32	                   hand = in.nextLine();
33	                   //next line of code would not be needed if you used a equalsIgnorCase when checking what hand equals.
34	                   hand = hand.toUpperCase();
35	               }


Was This Post Helpful? 1
  • +
  • -

Page 1 of 1