HELP: OO rock paper scissors game

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jan 2009
Posts: 42
Reputation: scias23 is an unknown quantity at this point 
Solved Threads: 0
scias23's Avatar
scias23 scias23 is offline Offline
Light Poster

HELP: OO rock paper scissors game

 
1
  #1
Aug 21st, 2009
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.
Last edited by scias23; Aug 21st, 2009 at 1:57 pm.
a joke.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 1,580
Reputation: BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all 
Solved Threads: 200
BestJewSinceJC BestJewSinceJC is offline Offline
Posting Virtuoso

Re: HELP: OO rock paper scissors game

 
0
  #2
Aug 21st, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 122
Reputation: TheWhite is on a distinguished road 
Solved Threads: 5
TheWhite TheWhite is offline Offline
Junior Poster

Re: HELP: OO rock paper scissors game

 
3
  #3
Aug 21st, 2009
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.
Last edited by TheWhite; Aug 21st, 2009 at 6:41 pm.
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 42
Reputation: scias23 is an unknown quantity at this point 
Solved Threads: 0
scias23's Avatar
scias23 scias23 is offline Offline
Light Poster

Re: HELP: OO rock paper scissors game

 
0
  #4
Aug 22nd, 2009
thanks
a joke.
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 42
Reputation: scias23 is an unknown quantity at this point 
Solved Threads: 0
scias23's Avatar
scias23 scias23 is offline Offline
Light Poster

Re: HELP: OO rock paper scissors game

 
0
  #5
Aug 30th, 2009
how can i compare two instances of a class?

i have, Player class, it has getHand() method.

suppose i create two instances of player
  1. Player one = new Player();
  2. Player two = new Player();

then call the getHand method
  1. one.getHand(in.nextLine());
  2. 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.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 122
Reputation: TheWhite is on a distinguished road 
Solved Threads: 5
TheWhite TheWhite is offline Offline
Junior Poster

Re: HELP: OO rock paper scissors game

 
0
  #6
Aug 30th, 2009
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()
Last edited by TheWhite; Aug 30th, 2009 at 11:22 pm.
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 42
Reputation: scias23 is an unknown quantity at this point 
Solved Threads: 0
scias23's Avatar
scias23 scias23 is offline Offline
Light Poster

Re: HELP: OO rock paper scissors game

 
0
  #7
Aug 31st, 2009
but how can i compare each player's hand?

i call the Player = one.setHand() in the main method -- how can i compare two objects of setHand() in a function?
a joke.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 122
Reputation: TheWhite is on a distinguished road 
Solved Threads: 5
TheWhite TheWhite is offline Offline
Junior Poster

Re: HELP: OO rock paper scissors game

 
1
  #8
Aug 31st, 2009
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:
  1. public Player getWinner(Player one, Player two) {
  2. switch ( one.getHand() ) {
  3. case ROCK:
  4. //check if two.getHand() beats ROCK,
  5. //if so, return two as the winner
  6. case PAPER:
  7. //check if two.getHand() beats PAPER,
  8. //if so, return two as the winner
  9. ......
  10. default:
  11. //if none of the above apply,
  12. //return one as the winner
  13. }
  14. }
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)
Last edited by TheWhite; Aug 31st, 2009 at 2:33 am.
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 42
Reputation: scias23 is an unknown quantity at this point 
Solved Threads: 0
scias23's Avatar
scias23 scias23 is offline Offline
Light Poster

Re: HELP: OO rock paper scissors game

 
0
  #9
Aug 31st, 2009
thanks for your help, hand comparison is working now.

now, i want to set how many rounds the player will play.

look at this..
  1. private static void twoPlayer() {
  2. int count;
  3. out.print("How many rounds you wish to play? ");
  4. count = in.nextInt();
  5.  
  6. out.print("\nPlayer 1 Name: ");
  7. oneName = getName();
  8. out.print("Player 2 Name: ");
  9. twoName = getName();
  10.  
  11. for(int i=1;i<=count;i++) {
  12. System.out.print(oneName + "'s Hand: ");
  13. oneHand = getHand();
  14. System.out.print(twoName + "'s Hand: ");
  15. twoHand = getHand();
  16.  
  17. announceWinner(compareHand(oneHand, twoHand));
  18.  
  19. out.println();
  20. }
  21. }

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.
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
  1. out.print("\nPlayer 1 Name: ");
  2. oneName = getName();
  3. out.print("Player 2 Name: ");
  4. twoName = getName();
seems to loop also three times. it's odd because this statements aren't inside the for loop.

help!
a joke.
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 7
Reputation: ellioth_13 is an unknown quantity at this point 
Solved Threads: 0
ellioth_13's Avatar
ellioth_13 ellioth_13 is offline Offline
Newbie Poster

Re: HELP: OO rock paper scissors game

 
0
  #10
Aug 31st, 2009
i hav developed some similar application.

i use a 1player vs com method
i used buttons such as stone paper & scissor
after the player clicked 1 of those button the com will randomly generate its rsponse (e.g. stone, paper or scissor)

^__^
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC