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.

quuba commented: OOP-very good idea +1

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.

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.

commented: Helpful suggestions. +24

thanks

how can i compare two instances of a class?

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

suppose i create two instances of player

Player one = new Player();
Player two = new Player();

then call the getHand method

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!

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()

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?

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:

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
     }
}

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)

thanks for your help, hand comparison is working now.

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

look at this..

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

[B]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.[/B]

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

[B]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.[/B]

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

out.print("\nPlayer 1 Name: ");
        oneName = getName();
        out.print("Player 2 Name: ");
        twoName = getName();

seems to loop also three times. it's odd because this statements aren't inside the for loop.

help!

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)

^__^

Let me just show you a little quirk I have with your code, and let me note that the way you are doing it can work, but there's a better way.... it will make it easier for you, and me, and anyone else looking/trying to do something with your code.

Creating functions like this is unconventional in OO languages........ what if 10 people or 100 people for that matter, feel like playing your game? Are you going to create 10-100 functions to manage each situation depending on the # of players? What you did here, isn't really practical OO. You are scripting, in 1 limited function.... Your process depends on a constant (the # of players), which it shouldn't be.


I'm not getting what this next line does.... Let me guess, getName() System.out.printsln(a name)..... Each Player should have a name right? why is getName() outside of Player? This line should be System.out.println(one.getName()) for example..........

out.print("\nPlayer 1 Name: ");
        oneName = getName();
        out.print("Player 2 Name: ");
        twoName = getName();

Same here... getHand() should be in Player.... how are you figuring out which hand you want to get from which player here? makes no sense.... and your making it harder than it should be -- i could only imagine how you are figuring this out ;)
Also, why are you resaving oneName and oneHand in your game? It should already be in your Player if your player is a real Object. You should access it from there.

for(int i=1;i<=count;i++) {
            System.out.print(oneName + "'s Hand: ");
            oneHand = getHand();
            System.out.print(twoName + "'s Hand: ");
            twoHand = getHand();
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.