943,709 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 2578
  • Java RSS
You are currently viewing page 1 of this multi-page discussion thread
Aug 21st, 2009
1

HELP: OO rock paper scissors game

Expand Post »
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.
Similar Threads
Reputation Points: 11
Solved Threads: 0
Junior Poster in Training
scias23 is offline Offline
69 posts
since Jan 2009
Aug 21st, 2009
0

Re: HELP: OO rock paper scissors game

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.
Reputation Points: 874
Solved Threads: 352
Posting Maven
BestJewSinceJC is offline Offline
2,758 posts
since Sep 2008
Aug 21st, 2009
3

Re: HELP: OO rock paper scissors game

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.
Reputation Points: 72
Solved Threads: 6
Junior Poster
TheWhite is offline Offline
174 posts
since May 2008
Aug 22nd, 2009
0

Re: HELP: OO rock paper scissors game

thanks
Reputation Points: 11
Solved Threads: 0
Junior Poster in Training
scias23 is offline Offline
69 posts
since Jan 2009
Aug 30th, 2009
0

Re: HELP: OO rock paper scissors game

how can i compare two instances of a class?

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

suppose i create two instances of player
java Syntax (Toggle Plain Text)
  1. Player one = new Player();
  2. Player two = new Player();

then call the getHand method
java Syntax (Toggle Plain Text)
  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!
Reputation Points: 11
Solved Threads: 0
Junior Poster in Training
scias23 is offline Offline
69 posts
since Jan 2009
Aug 30th, 2009
0

Re: HELP: OO rock paper scissors game

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.
Reputation Points: 72
Solved Threads: 6
Junior Poster
TheWhite is offline Offline
174 posts
since May 2008
Aug 31st, 2009
0

Re: HELP: OO rock paper scissors game

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?
Reputation Points: 11
Solved Threads: 0
Junior Poster in Training
scias23 is offline Offline
69 posts
since Jan 2009
Aug 31st, 2009
1

Re: HELP: OO rock paper scissors game

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:
JAVA Syntax (Toggle Plain Text)
  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.
Reputation Points: 72
Solved Threads: 6
Junior Poster
TheWhite is offline Offline
174 posts
since May 2008
Aug 31st, 2009
0

Re: HELP: OO rock paper scissors game

thanks for your help, hand comparison is working now.

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

look at this..
java Syntax (Toggle Plain Text)
  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
java Syntax (Toggle Plain Text)
  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!
Reputation Points: 11
Solved Threads: 0
Junior Poster in Training
scias23 is offline Offline
69 posts
since Jan 2009
Aug 31st, 2009
0

Re: HELP: OO rock paper scissors game

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)

^__^
Reputation Points: 10
Solved Threads: 1
Newbie Poster
ellioth_13 is offline Offline
9 posts
since Aug 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Continuing RMI Issues
Next Thread in Java Forum Timeline: Tic Tac Toe game





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC