7 Replies - 3609 Views - Last Post: 12 February 2012 - 05:59 PM Rate Topic: -----

#1 Dorito_125  Icon User is offline

  • D.I.C Head

Reputation: 8
  • View blog
  • Posts: 88
  • Joined: 13-December 08

Learning Methods with Rock, Paper, Scissors

Posted 11 February 2012 - 05:05 PM

I went ahead in my textbook and next we're learning about methods. Earlier on we saw an example of if statements in a rock paper scissors game.

I thought it would be neat to try and apply what I read about methods to the idea and this is what I came up with.

/**
* A simple Rock Paper Scissors game
*
* @author Andrew Hood
* @version 2/11/2012
*/
 
import java.util.*;
 
class RockPaperScissors
{
        public static int playerScore(int winner)
        {
                int xyz =0;
                if(winner == 1)
                {
                        xyz++;
                }
                else if(winner == 2) 
                {
                        xyz--;
                }
                return xyz;
        }
        public static void main(String[] args)
        {
                Scanner input = new Scanner(System.in);
                int playerChoice, compChoice, playAgain = 0, playerWins = 0;
                
                
                System.out.println("Rock Paper Scissors!\n");
                while(playAgain!=9) // Contine loop until sentinel
                {
                        System.out.println("[1]Rock, [2]Paper, [3]Scissors");
                        playerChoice = input.nextInt();
                        compChoice = (int) (3*Math.random()+1);
                        switch(playerChoice)
                        {
                                case 1: System.out.print("You chose Rock, ");
                                        if(compChoice == 1)
                                        { 
                                                System.out.println("Computer chose Rock.");
                                                System.out.println("Draw!"); 
                                        }
                                        if(compChoice == 2)
                                        {
                                                System.out.println("Computer chose Paper.");
                                                System.out.println("Computer wins!");
                                                playerWins += playerScore(2);
                                        }
                                        if(compChoice == 3)
                                        { 
                                                System.out.println("Computer chose Scissors.");
                                                System.out.println("Player wins!");
                                                playerWins += playerScore(1);
                                        }
                                        break;
                                        
                                case 2: System.out.print("You chose Paper, ");
                                        if(compChoice == 1)
                                        { 
                                                System.out.println("Computer chose Rock.");
                                                System.out.println("Player Wins!");
                                                playerWins += playerScore(1);
                                        }
                                        if(compChoice == 2)
                                        { 
                                                System.out.println("Computer chose Paper.");
                                                System.out.println("Draw!");
                                        }
                                        if(compChoice == 3)
                                        { 
                                                System.out.println("Computer chose Scissors.");
                                                System.out.println("Computer wins!");
                                                playerWins += playerScore(2);
                                        }
                                        break;
                                case 3: System.out.print("You chose Scissors, ");
                                        if(compChoice == 1)
                                        { 
                                                System.out.println("Computer chose Rock.");
                                                System.out.println("Computer Wins!");
                                                playerWins += playerScore(2);
                                        }
                                        if(compChoice == 2)
                                        { 
                                                System.out.println("Computer chose Paper.");
                                                System.out.println("Player wins!");
                                                playerWins += playerScore(1);
                                        }
                                        if(compChoice == 3)
                                        { 
                                                System.out.println("Computer chose Scissors.");
                                                System.out.println("Draw!");
                                        }
                                        break;
                                default: System.out.println("You must choose 1,2 or 3");
                                        break;
                        } // End switch
                        
                        System.out.println("Your score is: " + playerWins);
                        System.out.println("Would you like to play again? [1]Yes, [9]No");
                        playAgain = input.nextInt();
                } // End while loop
                
        } // End public static void main
} // End RockPaperScissors



I was thinking about making a two player version, where your choice would influence but not dictate what you got. That way even though the other person saw what you put. You still have a chance to win. Like computer vs computer where you both take sides.

Is This A Good Question/Topic? 0
  • +

Replies To: Learning Methods with Rock, Paper, Scissors

#2 Sheph  Icon User is offline

  • D.I.C Lover
  • member icon

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

Re: Learning Methods with Rock, Paper, Scissors

Posted 11 February 2012 - 06:16 PM

Are you saying you need help with the 2 player version? I'm not quite sure I understand what you mean by influence but not dictate? I'm not quite sure what your asking either?
Was This Post Helpful? 0
  • +
  • -

#3 Dorito_125  Icon User is offline

  • D.I.C Head

Reputation: 8
  • View blog
  • Posts: 88
  • Joined: 13-December 08

Re: Learning Methods with Rock, Paper, Scissors

Posted 11 February 2012 - 06:40 PM

View PostSheph, on 11 February 2012 - 06:16 PM, said:

Are you saying you need help with the 2 player version? I'm not quite sure I understand what you mean by influence but not dictate? I'm not quite sure what your asking either?


While actually just wanted to discuss and maybe get some ideas about the code I wrote. How would you improve it?

Truth is I'm not 100% sure about how I want to go about the two player version so I'm surprised not your confused about it. The thing is if your both sitting at the computer the second player is going to see your choice. My thoughts were if you chose lets say Rock you would have a 50% chance of getting Rock and 25% of each of the others. So even with 2nd player knowing your choice they would choose what they wanted and not just pick what would beat your move.

I don't know if that makes any more sense.

This post has been edited by Dorito_125: 11 February 2012 - 06:42 PM

Was This Post Helpful? 0
  • +
  • -

#4 Mylo  Icon User is offline

  • Knows all, except most.

Reputation: 265
  • View blog
  • Posts: 747
  • Joined: 11-October 11

Re: Learning Methods with Rock, Paper, Scissors

Posted 11 February 2012 - 07:33 PM

I don't think changing the players choice is a good idea, it stops the player from playing against patterns of the other. I would think that you create a timer of 1 (countdown from 3) seconds in which both players must select their move. They can use the numpad 1,2,3 keys and the z,x,c keys which are on opposite sides of the keyboard. The choice selected by the player shouldnt show on the screen until both players have selected.

Another thing you might do if you need to use the mouse, is prompt the other player to turn around while a choice is being made.

This post has been edited by Mylo: 11 February 2012 - 07:34 PM

Was This Post Helpful? 0
  • +
  • -

#5 Fuzzyness  Icon User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: Learning Methods with Rock, Paper, Scissors

Posted 11 February 2012 - 07:36 PM

I get what you are saying about the 2 player one, if you want to do it that way. I suggest you have 3 Array with length of 4 each.
String[] rock = {"Rock","Rock","Scissors","Paper"};
String[] scissor = {"Scissors","Scissors", ... };
String[] paper = {"Paper","Paper", ... };



You will prompt them with 3 options, Rock Paper of Scissors. Depending which one they choose, you will use that array and use the random.nextInt(4); Which will give you a random number and you will take the index of the array they chose. Will give them a 50% to get what they wanted but 25% for each of the other two.

Make sense?

This post has been edited by Fuzzyness: 11 February 2012 - 07:36 PM

Was This Post Helpful? 1
  • +
  • -

#6 Dorito_125  Icon User is offline

  • D.I.C Head

Reputation: 8
  • View blog
  • Posts: 88
  • Joined: 13-December 08

Re: Learning Methods with Rock, Paper, Scissors

Posted 11 February 2012 - 08:04 PM

View PostMylo, on 11 February 2012 - 07:33 PM, said:

I don't think changing the players choice is a good idea, it stops the player from playing against patterns of the other. I would think that you create a timer of 1 (countdown from 3) seconds in which both players must select their move. They can use the numpad 1,2,3 keys and the z,x,c keys which are on opposite sides of the keyboard. The choice selected by the player shouldnt show on the screen until both players have selected.

Another thing you might do if you need to use the mouse, is prompt the other player to turn around while a choice is being made.


MM, that's a good idea. We haven't learned how to get keydown events yet but I think that would be a good way to do. Could make it so both one of "1,2,3" && "z,x,c" must be depressed before it will continue or the match will expire at three seconds and say to try again or something of that nature.

I'll need to find how to do the timer and get keydown events.

View PostFuzzyness, on 11 February 2012 - 07:36 PM, said:

I get what you are saying about the 2 player one, if you want to do it that way. I suggest you have 3 Array with length of 4 each.
String[] rock = {"Rock","Rock","Scissors","Paper"};
String[] scissor = {"Scissors","Scissors", ... };
String[] paper = {"Paper","Paper", ... };



You will prompt them with 3 options, Rock Paper of Scissors. Depending which one they choose, you will use that array and use the random.nextInt(4); Which will give you a random number and you will take the index of the array they chose. Will give them a 50% to get what they wanted but 25% for each of the other two.

Make sense?


Yeah that's exactly what I was thinking.
Was This Post Helpful? 0
  • +
  • -

#7 Sheph  Icon User is offline

  • D.I.C Lover
  • member icon

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

Re: Learning Methods with Rock, Paper, Scissors

Posted 11 February 2012 - 08:14 PM

But they would go the 50% chance to win, 25% chance to lose every time if they saw what you put in first. I think the code you wrote is very good for someone learning from your textbook.

If you are looking for a challenge, instead of worrying about just player vs player, why not try to make a RockPaperScissors class that can accept 2 parameters in its constructor, that tell it where it gets its choices from (the player or the computer)
public RockPaperScissors(boolean player1Human, boolean player2Human) {
   // ...
}
if player 1 is human, ask for input from the console on player 1's turn. If they are a computer, generate a random value.

Then you can have a meta game with your friends by betting on which computer will win! :D

Edit: Hmmmm, it appears from your code that you haven't brushed up on Object Oriented Programming yet. So if that's confusing then just ignore my suggestion. (unless you want to try to learn about classes and objects on your own)

This post has been edited by Sheph: 11 February 2012 - 08:17 PM

Was This Post Helpful? 0
  • +
  • -

#8 Dorito_125  Icon User is offline

  • D.I.C Head

Reputation: 8
  • View blog
  • Posts: 88
  • Joined: 13-December 08

Re: Learning Methods with Rock, Paper, Scissors

Posted 12 February 2012 - 05:59 PM

View PostSheph, on 11 February 2012 - 08:14 PM, said:

But they would go the 50% chance to win, 25% chance to lose every time if they saw what you put in first. I think the code you wrote is very good for someone learning from your textbook.

If you are looking for a challenge, instead of worrying about just player vs player, why not try to make a RockPaperScissors class that can accept 2 parameters in its constructor, that tell it where it gets its choices from (the player or the computer)
public RockPaperScissors(boolean player1Human, boolean player2Human) {
   // ...
}
if player 1 is human, ask for input from the console on player 1's turn. If they are a computer, generate a random value.

Then you can have a meta game with your friends by betting on which computer will win! :D

Edit: Hmmmm, it appears from your code that you haven't brushed up on Object Oriented Programming yet. So if that's confusing then just ignore my suggestion. (unless you want to try to learn about classes and objects on your own)


Okay seeing as how I want to foremost learn and practice methods. This is what I came up with.

What do you think?

/**
* A simple Rock Paper Scissors game
*
* @author Andrew Hood
* @version 2/11/2012
*/
 
import java.util.*;
 
class RockPaperScissors
{
        public static int Shoot(int howManyPlayers)
        {
                Scanner input = new Scanner(System.in);
                int player1Choice = 0, player2Choice = 0, winner;
                if(howManyPlayers == 1)
                {
                        System.out.println("Player 1, [1]Rock, [2]Paper, [3]Scissors");
                        player1Choice = input.nextInt();
                        player2Choice = (int) (3*Math.random()+1);
                }
                if(howManyPlayers == 2)
                {
                        System.out.println("Player 1, [1]Rock, [2]Paper, [3]Scissors");
                        player1Choice = input.nextInt();
                        System.out.println("Player 2, [1]Rock, [2]Paper, [3]Scissors");
                        player2Choice = input.nextInt();
                }
                winner = WhoBeatsWho(player1Choice, player2Choice);
                return winner;
        } // End Shoot
        
        public static int WhoBeatsWho(int player1Choice, int player2Choice)
        {
                int winner = 0;
                switch(player1Choice)
                {
                        case 1: System.out.print("Player 1 chose Rock, ");
                                if(player2Choice == 1)
                                { 
                                        System.out.println("Player 2 chose Rock.");
                                        System.out.println("Draw!"); 
                                }
                                if(player2Choice == 2)
                                {
                                        System.out.println("Player 2 chose Paper.");
                                        System.out.println("Player 2 wins!");
                                        winner = 2;
                                }
                                if(player2Choice == 3)
                                { 
                                        System.out.println("Player 2 chose Scissors.");
                                        System.out.println("Player 1 wins!");
                                        winner = 1;
                                }
                                break;
                                
                        case 2: System.out.print("Player 1 chose Paper, ");
                                if(player2Choice == 1)
                                { 
                                        System.out.println("Player 2 chose Rock.");
                                        System.out.println("Player 1 Wins!");
                                        winner = 1;
                                }
                                if(player2Choice == 2)
                                { 
                                        System.out.println("Player 2 chose Paper.");
                                        System.out.println("Draw!");
                                }
                                if(player2Choice == 3)
                                { 
                                        System.out.println("Player 2 chose Scissors.");
                                        System.out.println("Player 2 wins!");
                                        winner = 2;
                                }
                                break;
                        case 3: System.out.print("Player 1 chose Scissors, ");
                                if(player2Choice == 1)
                                { 
                                        System.out.println("Player 2 chose Rock.");
                                        System.out.println("Player 2 Wins!");
                                        winner = 2;
                                }
                                if(player2Choice == 2)
                                { 
                                        System.out.println("Player 2 chose Paper.");
                                        System.out.println("Player 1 wins!");
                                        winner = 1;
                                }
                                if(player2Choice == 3)
                                { 
                                        System.out.println("Player 2 chose Scissors.");
                                        System.out.println("Draw!");
                                }
                                break;
                } // End switch
                return winner;
        }
        
        public static void main(String[] args)
        {
                Scanner input = new Scanner(System.in);
                int howManyPlayers, winner, playAgain = 0;
 
                System.out.println("Rock Paper Scissors!\n");
                System.out.println("[1] Player, [2] Player");
                howManyPlayers = input.nextInt();
                
                while(playAgain != 9)
                {
                    winner = Shoot(howManyPlayers);
                    System.out.println("Would you like to play again? [1]Yes [9]No");
                    playAgain = input.nextInt();
                }
                
        } // End public static void main
} // End RockPaperScissors


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1