| | |
HELP: OO rock paper scissors game
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
i'm thinking how to do this since last week.
i can make a rps game in java, BUT not in OO way. just the simple ones.
what i want to do is a rps game - the OO way.
i planning this program to have two options, play with player and play with computer.
what classes and methods that i need to code?
i'm thinking of..
1. player class
---1.a choice method
---1.b processChoice method
2. startGame class
---1.?????
??????????????????
these are all i can think of! i know there's a better logic in creating this kind of progrm. ugh, i just started studying oo programming this day. noob.
tell me what are the methods and classes appropriate for this rock paper scissors game.
i an't asking for codes, just the methods and classes..
please help. thanks.
i can make a rps game in java, BUT not in OO way. just the simple ones.
what i want to do is a rps game - the OO way.
i planning this program to have two options, play with player and play with computer.
what classes and methods that i need to code?
i'm thinking of..
1. player class
---1.a choice method
---1.b processChoice method
2. startGame class
---1.?????
??????????????????
these are all i can think of! i know there's a better logic in creating this kind of progrm. ugh, i just started studying oo programming this day. noob.
tell me what are the methods and classes appropriate for this rock paper scissors game.
i an't asking for codes, just the methods and classes..

please help. thanks.
Last edited by scias23; Aug 21st, 2009 at 1:57 pm.
a joke.
•
•
Join Date: Sep 2008
Posts: 1,580
Reputation:
Solved Threads: 200
RPS is an extremely simple game. . because of that, it isn't really necessary to do it OP style - what would your objects be? The Rock, Paper, and Scissors need not be Objects because they are better represented as Strings (in my opinion). I suppose you could make the Players themselves Objects and make takeTurn methods and getWinner methods. getWinner could return the name of the player who won or "tie" to keep the game going.
Out.
•
•
Join Date: May 2008
Posts: 122
Reputation:
Solved Threads: 5
Every player has a Hand which could be:
A rock, A paper, or A scissor
Every player could have:
A name, A hand, A number of Wins, A number of Losses, and a choice that will affect his hand/number wins/or number losses.
Every game could have:
A bunch of players, a way to know whos playing, a way to tell who won, and a way to tell everyone who won.
Player:
Enum choices;
String name;
Hand hand;
int wins, losses;
void makeChoice();
String getName();
Hand getHand();
int getWins();
int getLosses();
Game:
Player[] players;
void startGame();
Player getWinner(Players[] players);
String anncWinner(Player player);
main() //call everything
Should be that simple
Just dont use this bottom half as a guide.... Think about what each Object has to do and start implementing.
A rock, A paper, or A scissor
Every player could have:
A name, A hand, A number of Wins, A number of Losses, and a choice that will affect his hand/number wins/or number losses.
Every game could have:
A bunch of players, a way to know whos playing, a way to tell who won, and a way to tell everyone who won.
Player:
Enum choices;
String name;
Hand hand;
int wins, losses;
void makeChoice();
String getName();
Hand getHand();
int getWins();
int getLosses();
Game:
Player[] players;
void startGame();
Player getWinner(Players[] players);
String anncWinner(Player player);
main() //call everything
Should be that simple
Just dont use this bottom half as a guide.... Think about what each Object has to do and start implementing. Last edited by TheWhite; Aug 21st, 2009 at 6:41 pm.
how can i compare two instances of a class?
i have, Player class, it has getHand() method.
suppose i create two instances of player
then call the getHand method
then, how can i compare the value of one.getHand() and two.getHand() ?? is this possible??
i want the comparison to be done in another method or class. because i want all the processing to be done in other method or class, and i just want my main class to just do the calling.
thanks a lot!
i have, Player class, it has getHand() method.
suppose i create two instances of player
java Syntax (Toggle Plain Text)
Player one = new Player(); Player two = new Player();
then call the getHand method
java Syntax (Toggle Plain Text)
one.getHand(in.nextLine()); two.getHand(in.nextLine());
then, how can i compare the value of one.getHand() and two.getHand() ?? is this possible??
i want the comparison to be done in another method or class. because i want all the processing to be done in other method or class, and i just want my main class to just do the calling.
thanks a lot!
a joke.
•
•
Join Date: May 2008
Posts: 122
Reputation:
Solved Threads: 5
First, your function getHand() is a little misleading.
When you "get"Something(), it should always return the value of the Something, in that class.
Likewise, when you "set"Something() (what you should have used to set the hand for each player), sets the Something, in that class.
Using this to answer your question:
Your "Game" should keep track of all the "Players", correct? It should contain a function ( Game.getWinner() ) that will go through all the players hands ( player.getHand() ) and compare them and return a single winner.
So,
Each Game keeps track of each Player who can setHand(String Hand) and getHand().
Each Game has a getWinner() function which returns a single winner by comparing each players getHand().
Technically, to be officially "complete", there should be a function:
void setWinner(Player player)
Player getWinner()
and findWinner() which setsWinner() by searching each players getHand()
When you "get"Something(), it should always return the value of the Something, in that class.
Likewise, when you "set"Something() (what you should have used to set the hand for each player), sets the Something, in that class.
Using this to answer your question:
Your "Game" should keep track of all the "Players", correct? It should contain a function ( Game.getWinner() ) that will go through all the players hands ( player.getHand() ) and compare them and return a single winner.
So,
Each Game keeps track of each Player who can setHand(String Hand) and getHand().
Each Game has a getWinner() function which returns a single winner by comparing each players getHand().
Technically, to be officially "complete", there should be a function:
void setWinner(Player player)
Player getWinner()
and findWinner() which setsWinner() by searching each players getHand()
Last edited by TheWhite; Aug 30th, 2009 at 11:22 pm.
•
•
Join Date: May 2008
Posts: 122
Reputation:
Solved Threads: 5
What do you mean Player = one.setHand()? "one" is already a player right? and "one" already exists in the main game.... "Player =" ??
one.setHand(Hand hand); would set the hand.
one.getHand(); would get one's hand.
If you made the Hands an Enum, you can use a switch statement to compare each players hand:
This is to compare 2 players. Continue to do this to all the players until only 1 is left to call the winner. If there is a tie, well, you will have to figure out your own algorithm.
If you get stuck, you can probably have the function return an int: 0 says the first param is the winner, 1 says the second param is the winner, and -1 says its a tie between both players.
That you will have to figure out on your own // this is what some developers get payed to do (finding the most efficient/cost effective way at programming scripted intelligence)
one.setHand(Hand hand); would set the hand.
one.getHand(); would get one's hand.
If you made the Hands an Enum, you can use a switch statement to compare each players hand:
JAVA Syntax (Toggle Plain Text)
public Player getWinner(Player one, Player two) { switch ( one.getHand() ) { case ROCK: //check if two.getHand() beats ROCK, //if so, return two as the winner case PAPER: //check if two.getHand() beats PAPER, //if so, return two as the winner ...... default: //if none of the above apply, //return one as the winner } }
If you get stuck, you can probably have the function return an int: 0 says the first param is the winner, 1 says the second param is the winner, and -1 says its a tie between both players.
That you will have to figure out on your own // this is what some developers get payed to do (finding the most efficient/cost effective way at programming scripted intelligence)
Last edited by TheWhite; Aug 31st, 2009 at 2:33 am.
thanks for your help, hand comparison is working now.
now, i want to set how many rounds the player will play.
look at this..
my PROBLEM IS.. when i set the number of rounds, i get this output..
what happens is:
1. i enter number of rounds, say 3.
2. as i see from the output, out.print("\nPlayer 1 Name: "); oneName = getName(); out.print("Player 2 Name: "); twoName = getName(); seems to loop 3 TIMES ALSO.
3. i can set the player's name BUT note that only the twoName (the player two) is set, oneName can't.
what i WANT TO SEE ON THE OUTPUT IS:
so:
1. i enter number of rounds, say 3.
2. i enter the names of the players.
3. names appear properly on the hand input. (eg. Michael's Hand: rock)
i'm wondering why this part
seems to loop also three times. it's odd because this statements aren't inside the for loop.
help!
now, i want to set how many rounds the player will play.
look at this..
java Syntax (Toggle Plain Text)
private static void twoPlayer() { int count; out.print("How many rounds you wish to play? "); count = in.nextInt(); out.print("\nPlayer 1 Name: "); oneName = getName(); out.print("Player 2 Name: "); twoName = getName(); for(int i=1;i<=count;i++) { System.out.print(oneName + "'s Hand: "); oneHand = getHand(); System.out.print(twoName + "'s Hand: "); twoHand = getHand(); announceWinner(compareHand(oneHand, twoHand)); out.println(); } }
my PROBLEM IS.. when i set the number of rounds, i get this output..
ROCK-PAPER-SCISSORS GAME
NOTE: hand input will loop until user(s) input(s) valid hand values
VALID HANDS: rock, paper, scissors
Select your option below:
[1] Player vs Player
[2] Player vs Computer 1
How many rounds you wish to play? 3
Player 1 Name: Player 2 Name: Player 1 Name: Player 2 Name:
Michael
's Hand: rock
Michael's Hand: scissors
ROCK wins over SCISSORS
wins.
scissors
's Hand: rock
Michael's Hand: rock
It's a tie.
's Hand: paper
Michael's Hand: rock
PAPER wins over ROCK
wins.what happens is:
1. i enter number of rounds, say 3.
2. as i see from the output, out.print("\nPlayer 1 Name: "); oneName = getName(); out.print("Player 2 Name: "); twoName = getName(); seems to loop 3 TIMES ALSO.
3. i can set the player's name BUT note that only the twoName (the player two) is set, oneName can't.
what i WANT TO SEE ON THE OUTPUT IS:
ROCK-PAPER-SCISSORS GAME
NOTE: hand input will loop until user(s) input(s) valid hand values
VALID HANDS: rock, paper, scissors
Select your option below:
[1] Player vs Player
[2] Player vs Computer 1
How many rounds you wish to play? 3
Player 1 Name: Michael
Player 2 Name: John
Michael's Hand: rock
John's Hand: scissors
ROCK wins over SCISSORS
Michael wins.
Michael's Hand: rock
John's Hand: rock
It's a tie.
Michael's Hand: paper
John's Hand: rock
PAPER wins over ROCK
Michael wins.1. i enter number of rounds, say 3.
2. i enter the names of the players.
3. names appear properly on the hand input. (eg. Michael's Hand: rock)
i'm wondering why this part
java Syntax (Toggle Plain Text)
out.print("\nPlayer 1 Name: "); oneName = getName(); out.print("Player 2 Name: "); twoName = getName();
help!
a joke.
![]() |
Similar Threads
- Help with Rock Paper Scissors game (C++)
- Rock Paper Scissors: String Problem (C++)
- Rock Paper Scissors and how to break out (Python)
- While loop in Rock Paper Scissors (Python)
- Rock paper scissors using functions (C++)
- rock paper scissors game not working (C++)
- Rock Paper Scissors scoring help (C++)
Other Threads in the Java Forum
- Previous Thread: Continuing RMI Issues
- Next Thread: java project ideas for students
| Thread Tools | Search this Thread |
3d 6 @param affinetransform android api applet application arc array arrays automation binary bluetooth bold byte c++ chat class client code color compare component coordinates database detection doctype eclipse educational error file fractal froglogic game givemetehcodez graphics gui guitesting helpwithhomework html ide ideas image ingres input integer internet intersect j2me java java.xls javaexcel javaprojects jni jpanel jtextarea julia keytool keyword linux list loop map method methods mobile netbeans newbie nextline pong print problem producer program programming project projectideas read recursion recursive replaysolutions rim scanner screen sell server set size sms sort sql string swing terminal threads tree web websites windows






