13 Replies - 994 Views - Last Post: 23 June 2013 - 12:07 PM Rate Topic: -----

#1 musicalProgrammer  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 36
  • Joined: 13-November 12

Rock, Paper, Scissors game - flow of control

Posted 17 June 2013 - 04:01 PM

Hello everyone.

During summer break from college, I've been reviewing Java to make sure it stays fresh in my mind. I started at the beginning of my textbook and have been doing a lot of the programming projects to help my review.

Currently I'm at Chapter 4, which deals with conditionals and loops. The project I'm currently working on is a Rock, Paper, Scissors game.

The issue I'm having is in the while loop. The logic there goes like this: While the game is running (which happens if the user presses '1'), generate a random number between 3 and 5 ,inclusive, to represent rock, paper, and scissors; prompt user to press 3, 4, or 5, and store their input; IF the both the computer and the player pick rock (3), print a message accordingly; etc, etc. The problem is, the messages do not print. Process instead just jumps on down to the option to play again or quit.

This really has me scratching my head, because the way I see it right now, the logic of how flow of control should work here is good.

NOTE: This program is still in progress, thus some things are missing, like the rest of the 'if' statements and a way to keep track of stats. As well, I came up with a rough design before I started writing the code, and just cowboy coded from there as ideas came to mind and things evolved.

Tnanks for taking the time to look at this and help me, I really appreciate it

-Reuben

Here is the code:

//-----------------------------------------------------------------------------\\
// Reuben Turner                 6/17/2013		RockPaperScissors.java \\
//									       \\
// This program is a simple game of Rock, Paper, Scissors. The user can play   \\
// game as many times as he/she wants and check their win-loss record after    \\
// they stop gameplay. 							       \\
//-----------------------------------------------------------------------------\\
import java.util.Scanner;
import java.util.Random;

// note to self - figure out how to keep track of stats

public class RockPaperScissors 
{
	public static void main (String[] args)
	{
		//=====Instance data=====\\
		int computerChoice; // represents the computer's choice
		
		int userChoice; // holds user input 
		
		Scanner scan = new Scanner(System.in); // Scanner obj to get input from user
		
		Random generator = new Random(); // Random object to create random computer moves
		
		
		//=====Program begins=====\\
		System.out.println ("Welcome to the game of Rock, Paper, Scissors!");
		
		System.out.println ("To play the game, press 1. To read the rules of the game, press 2");
		
		userChoice = scan.nextInt(); // get input
		
		//=====Show rules=====\\
		while (userChoice == 2) // if user presses 2..
		{
			System.out.println ();
			
			System.out.println ("In this game, you pick either rock, paper, or scissors against the      computer, " + "\n" + "who also picks either rock, paper, or scissors. Rock beats scissors; scissors beats paper; " + "\n" + "paper beats rock. To play, press 1. To re-read the rules, press 2.");
			
			userChoice = scan.nextInt();
		}
		
		//=====While game is playing=====\\
		while (userChoice == 1) // if user presses 1 to play game...
		{
			computerChoice = generator.nextInt(5) + 3; // generate computer's choice
			
			System.out.println ("Please pick '3' for rock, '4' for paper, and '5' for scissors: ");	
			
			int userPick; // holds user's choice within the scope of the loop
			
			userPick = scan.nextInt(); // get user's choice
			
			//-----If both computer and player choose rock-----\\
			if (computerChoice == 3 && userPick == 3)
			{
				System.out.println ("Computer chose rock. You chose rock. Verdict: Tie!" + "\n");
			}
			
			//-----If computer chooses rock and player chooses paper-----\\
			if (computerChoice == 3 && userPick == 4)
			{
				System.out.println ("Computer chose rock. You chose paper. Verdict: You win this round!"                + "\n");
			}
			
			// Add the other if statements...
			
			//=====Continue or quit=====\\
			System.out.println ("To quit and see your scores, press 0. To play again, press 1."); 
			
			userChoice = scan.nextInt(); // get input
			
			if (userChoice == 0) // if user presses 0
			{	
				System.exit(0); // terminate program
			}	
		}
	}
}



P.S. Some code didn't paste in so neatly. In my IDE it's all very neat. Sorry.

Is This A Good Question/Topic? 0
  • +

Replies To: Rock, Paper, Scissors game - flow of control

#2 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7290
  • View blog
  • Posts: 12,091
  • Joined: 19-March 11

Re: Rock, Paper, Scissors game - flow of control

Posted 17 June 2013 - 04:44 PM

It's a bit of a mess, but here are some notes, as they come to me.

Given that this is the end of your loop:


			if (userChoice == 0) // if user presses 0
			{	
				System.exit(0); // terminate program
			}


And this is the start of it:

		while (userChoice == 1)


there's a bit of a hole here. Since you do no validation on that last input, if the user enters something other than 0 or 1, you get unexpected behavior: the program terminates abruptly.
You also get unexpected behavior if the enters 0, since you don't actually provide the scores you promise, but that's another issue.



Your game logic is far too laborious. You have to write a separate branch for each case - what happens if we want to play rock, paper, scissors, canteloupe, marmelade, duck, duck, goose? (as an exercise, work out how many cases that would be - you can count "duck" just once if you like)

Two things that you should notice here. One is that you only really have a few messages to print, and the only thing that changes is the name of the item played. You can therefore compose a few messages and assemble the strings from pieces. You don't need to have "Computer chose rock." and "Computer chose paper" and I'm bored already, so I'll stop.
You just need "Computer chose" and "You chose" and "rock", "paper", "scissors". Storing the names (rock, paper, scissors) in an array will help a lot.


More important, picking the winner doesn't need a bazillion if statements (have you worked out how many combinations of 7 you'd need?). You just need to do a little arithmetic to figure out who won, and then fill in "rock", "paper", or "scissors" as appropriate from the array.

I'll let you puzzle over what the arithmetic looks like. Look over the list of operators in your java textbook, and think about each of them and why they're there. One of them might be useful.

Oh, right, you had a question. Sorry, let me hit that for you:
The problem is, the messages do not print. Process instead just jumps on down to the option to play again or quit. 

if (computerChoice == 3 && userPick == 3)


From what I'm seeing, this should always happen if these two conditions are met. That is, IF computerChoice happens to be 3, and IF userPick happens to be 3, then this branch will fire.

So, two things you can do to debug here. One is, set the values something you want to test and see if the if fires. (it should)

Then, put in some print statements to tell you what values are actually appearing here. The result should guide you to your next action.
Was This Post Helpful? 2
  • +
  • -

#3 musicalProgrammer  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 36
  • Joined: 13-November 12

Re: Rock, Paper, Scissors game - flow of control

Posted 18 June 2013 - 05:38 AM

First of all, thanks for posting! I really appreciate the input.

2) Yeah, I haven't really worked out the data validation thing yet - exceptions are Chapter 10, and I'm approaching this as if I never learned them before; I'm trying to really challenge myself to make sure I really know my stuff. I guess I don't...good thing I'm reviewing then, huh?

3) As far as the game logic goes: would you say I need to use a switch for all the different cases?

4) Good point with using a println to see if the program is executing that bit of code; I forgot that's a thing :)/>

5) Sorry about the sloppiness in some of the lines - the code pasted in oddly. In my IDE it's all very clean.

Thanks again!

- Reuben
Was This Post Helpful? 0
  • +
  • -

#4 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7290
  • View blog
  • Posts: 12,091
  • Joined: 19-March 11

Re: Rock, Paper, Scissors game - flow of control

Posted 18 June 2013 - 05:49 AM

View PostmusicalProgrammer, on 18 June 2013 - 07:38 AM, said:

First of all, thanks for posting! I really appreciate the input.

2) Yeah, I haven't really worked out the data validation thing yet - exceptions are Chapter 10, and I'm approaching this as if I never learned them before; I'm trying to really challenge myself to make sure I really know my stuff. I guess I don't...good thing I'm reviewing then, huh?



Quote

3) As far as the game logic goes: would you say I need to use a switch for all the different cases?


No, in fact a switch wouldn't work here. You can only use a switch in Java to branch on equality of a single object to some member of a set of comparands. Think of it as like an "if" for ints. Or else, think of an if as like a mini switch:

boolean bool = true;
switch bool {
  case true:   //do one thing 
  case false:  // do another
  default:     // never do this, unless you fall through to it




What you can't do is
case (a==1 && b ==2):  // compiler fail


because the switch is not asking "which of these expressions is true", it's asking "which of these items matches the current value of the switch variable".

You could use nested switches:


switch computerPick {
  case ROCK:    // using a decalred constant instead of a magic number
     switch userPick {
        case ROCK:   reportTie(ROCK);  // you could use a method to declare that a tie has occurred
                     break;
        ....
  ...
}



but you're still enumerating all the cases. You can do that if you want, and perhaps you should, but in the end it's very wasteful of your time and energy as a programmer. So go ahead and do it with the ifs or the switches just to check off that box on your "to do" list, but then think about how you'd do it with the numbers: 3,4,5. We have a weird circular relationship 3 < 4 < 5 < 3
How do we resolve that? There is a way! See if you can work it out.
Was This Post Helpful? 2
  • +
  • -

#5 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1275
  • View blog
  • Posts: 2,837
  • Joined: 05-April 11

Re: Rock, Paper, Scissors game - flow of control

Posted 18 June 2013 - 08:27 AM

Aww on my screen jon.kiparsky is giving us a heart <3

Hmm this can't be right
			computerChoice = generator.nextInt(5) + 3; // generate computer's choice
			
			System.out.println ("Please pick '3' for rock, '4' for paper, and '5' for scissors: ");


The nextInt(5) method will return one of the values 0-4
You then add 3 to the generated number

Generated number = 0, choice = 0 + 3 (3)
Generated number = 4, choice = 4 + 3 (7)

7 is not one of the allowed choices

A nextInt(3) + 3 will generated a minimum of 3 and a max of 5
Was This Post Helpful? 0
  • +
  • -

#6 musicalProgrammer  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 36
  • Joined: 13-November 12

Re: Rock, Paper, Scissors game - flow of control

Posted 18 June 2013 - 03:16 PM

View Postjon.kiparsky, on 18 June 2013 - 05:49 AM, said:

View PostmusicalProgrammer, on 18 June 2013 - 07:38 AM, said:

First of all, thanks for posting! I really appreciate the input.

2) Yeah, I haven't really worked out the data validation thing yet - exceptions are Chapter 10, and I'm approaching this as if I never learned them before; I'm trying to really challenge myself to make sure I really know my stuff. I guess I don't...good thing I'm reviewing then, huh?



Quote

3) As far as the game logic goes: would you say I need to use a switch for all the different cases?


No, in fact a switch wouldn't work here. You can only use a switch in Java to branch on equality of a single object to some member of a set of comparands. Think of it as like an "if" for ints. Or else, think of an if as like a mini switch:

boolean bool = true;
switch bool {
  case true:   //do one thing 
  case false:  // do another
  default:     // never do this, unless you fall through to it




What you can't do is
case (a==1 && b ==2):  // compiler fail


because the switch is not asking "which of these expressions is true", it's asking "which of these items matches the current value of the switch variable".

You could use nested switches:


switch computerPick {
  case ROCK:    // using a decalred constant instead of a magic number
     switch userPick {
        case ROCK:   reportTie(ROCK);  // you could use a method to declare that a tie has occurred
                     break;
        ....
  ...
}



but you're still enumerating all the cases. You can do that if you want, and perhaps you should, but in the end it's very wasteful of your time and energy as a programmer. So go ahead and do it with the ifs or the switches just to check off that box on your "to do" list, but then think about how you'd do it with the numbers: 3,4,5. We have a weird circular relationship 3 < 4 < 5 < 3
How do we resolve that? There is a way! See if you can work it out.


Fascinating. I'll try to figure out the best way. I didn't even know that there was such a thing as a nested switch, and I just finished the advanced class! You make a good point about the numbers and their relationship; I'll try to figure that out as well. Thank you!

View PostCasiOo, on 18 June 2013 - 08:27 AM, said:

Aww on my screen jon.kiparsky is giving us a heart <3

Hmm this can't be right
			computerChoice = generator.nextInt(5) + 3; // generate computer's choice
			
			System.out.println ("Please pick '3' for rock, '4' for paper, and '5' for scissors: ");


The nextInt(5) method will return one of the values 0-4
You then add 3 to the generated number

Generated number = 0, choice = 0 + 3 (3)
Generated number = 4, choice = 4 + 3 (7)

7 is not one of the allowed choices

A nextInt(3) + 3 will generated a minimum of 3 and a max of 5


Thanks for joining the conversation :) As far as I understand it, the number generated is between 3 and 5. I've double checked it with my textbook...
Was This Post Helpful? 0
  • +
  • -

#7 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1275
  • View blog
  • Posts: 2,837
  • Joined: 05-April 11

Re: Rock, Paper, Scissors game - flow of control

Posted 18 June 2013 - 03:30 PM

You should trust me and not your textbook then
Run the following code and you'll see :)
import java.util.Random;


public class Test {

	public static void main(String[] args) {
		Random generator = new Random();
		
		for (int i=0; i<20; i++) {
			int computerChoice = generator.nextInt(5) + 3; // generate computer's choice
			System.out.println(computerChoice);
		}
	}
}


Was This Post Helpful? 1
  • +
  • -

#8 musicalProgrammer  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 36
  • Joined: 13-November 12

Re: Rock, Paper, Scissors game - flow of control

Posted 18 June 2013 - 03:58 PM

View PostCasiOo, on 18 June 2013 - 03:30 PM, said:

You should trust me and not your textbook then
Run the following code and you'll see :)/>
import java.util.Random;


public class Test {

	public static void main(String[] args) {
		Random generator = new Random();
		
		for (int i=0; i<20; i++) {
			int computerChoice = generator.nextInt(5) + 3; // generate computer's choice
			System.out.println(computerChoice);
		}
	}
}



Funky. You're right.
Was This Post Helpful? 0
  • +
  • -

#9 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7290
  • View blog
  • Posts: 12,091
  • Joined: 19-March 11

Re: Rock, Paper, Scissors game - flow of control

Posted 18 June 2013 - 07:21 PM

View PostCasiOo, on 18 June 2013 - 05:30 PM, said:

You should trust me and not your textbook then



This is true. CasiOo is pretty damned reliable.

View PostmusicalProgrammer, on 18 June 2013 - 05:16 PM, said:

Fascinating. I'll try to figure out the best way. I didn't even know that there was such a thing as a nested switch, and I just finished the advanced class! You make a good point about the numbers and their relationship; I'll try to figure that out as well. Thank you!



The idea of a nested switch, I have to say, gives me the hives. However, it'd be how you'd do this with switching.
Was This Post Helpful? 1
  • +
  • -

#10 musicalProgrammer  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 36
  • Joined: 13-November 12

Re: Rock, Paper, Scissors game - flow of control

Posted 19 June 2013 - 08:49 AM

Thanks guys! I really appreciate the help. I'll post back with an update or if I need more help.
Was This Post Helpful? 0
  • +
  • -

#11 musicalProgrammer  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 36
  • Joined: 13-November 12

Re: Rock, Paper, Scissors game - flow of control

Posted 20 June 2013 - 04:19 PM

Hey guys - I got it to work it's finished! Thanks for your help :)
Was This Post Helpful? 0
  • +
  • -

#12 MentalFloss  Icon User is offline

  • "ADDICTED"[2:5]
  • member icon

Reputation: 525
  • View blog
  • Posts: 1,397
  • Joined: 02-September 09

Re: Rock, Paper, Scissors game - flow of control

Posted 23 June 2013 - 01:15 AM

Since you have finished your project and this actually inspired me to try to tackle the problem, I decided to give it a shot. Now, I haven't been programming in java for very long but I certainly would like a code review. Also, I didn't start a new thread because this is also just rock paper scissors...albeit very different from yours.

// GAME: Rock Paper Scissors
// SPEC:
//	- Rock beats scissors.
//	- Scissors beats paper.
//	- Paper beats rock.
//	- Equal choices tie.

import java.util.*;

public class Client {
	
	Random generator = new Random();
	int playerWins = 0;
	int computerWins = 0;
	
	public static void main(String[] args)
	{
		Client c = new Client();
		Scanner input = new Scanner(System.in);
		boolean keepPlaying = true;
		
		int choice = 0;
		Move player = null;
		Move computer = null;
		
		do
		{
			System.out.println("CHOICES - (1)ROCK (2)PAPER (3) SCISSORS");
			System.out.print(">");
			
			choice = input.nextInt();
			player = getMove(choice);
			if(player == null)
			{
				System.out.println("Invalid choice. Please try again.");
				continue;
			}
			
			computer = getMove(c.getRandom());
			Outcome result = player.getResult(computer);
			
			System.out.println("Result: " + result.Message);
			c.updateScore(result);
			c.displayScore();
			
			System.out.println("Would you like to play again? (1)YES (2)NO");
			choice = input.nextInt();
			keepPlaying = getKeepPlaying(choice);
			
		} while(keepPlaying);
	}

	private int getRandom()
	{
		return generator.nextInt(3) + 1;
	}
	
	private static Move getMove(int choice)
	{
		switch(choice)
		{
			case 1:
				return new Rock();
			case 2:
				return new Paper();
			case 3:
				return new Scissors();
			default:
				return null;
		}
	}
	
	private void updateScore(Outcome result)
	{
		if(result.Winner == Participants.PLAYER)
		{
			playerWins++;
		}
		else if(result.Winner == Participants.COMPUTER)
		{
			computerWins++;
		}
		else
		{
			return;
		}
	}
	
	private void displayScore()
	{
		System.out.println("SCORE = COMPUTER: " + computerWins + " | PLAYER: " + playerWins);
	}
	
	private static boolean getKeepPlaying(int choice)
	{
		if(choice == 1)
			return true;
		else
			return false;
	}
}



public interface Move {
	Outcome getResult(Move move);
}



public class Outcome {
	public Participants Winner;
	public String Message;
}



public enum Participants {
	TIE,
	PLAYER,
	COMPUTER
}



public class Rock implements Move {

	public Outcome getResult(Move move) {
		Outcome result = new Outcome();
		
		// Rock loses to paper
		if(move.getClass() == Paper.class)
		{
			result.Winner = Participants.COMPUTER;
			result.Message = "Paper covers Rock.";
		}
		// Rock beats scissors
		else if(move.getClass() == Scissors.class)
		{
			result.Winner = Participants.PLAYER;
			result.Message = "Rock crushes Scissors.";
		}
		// Both picked Rock. It's a tie.
		else
		{
			result.Winner = Participants.TIE;
			result.Message = "Rock and Rock are friends.";
		}
		return result;
	}
}



public class Paper implements Move {

	public Outcome getResult(Move move) {
		Outcome result = new Outcome();
		
		// Paper beats to rock
		if(move.getClass() == Rock.class)
		{
			result.Winner = Participants.PLAYER;
			result.Message = "Paper covers Rock.";
		}
		// Paper loses to scissors
		else if(move.getClass() == Scissors.class)
		{
			result.Winner = Participants.COMPUTER;
			result.Message = "Scissors cut Paper.";
		}
		// Both picked Paper. It's a tie.
		else
		{
			result.Winner = Participants.TIE;
			result.Message = "Paper and Paper are friends.";
		}
		return result;
	}

}



public class Scissors implements Move {

	public Outcome getResult(Move move) {
		Outcome result = new Outcome();
		
		// Scissors beat Paper
		if(move.getClass() == Paper.class)
		{
			result.Winner = Participants.PLAYER;
			result.Message = "Scissors cut Paper.";
		}
		// Scissors loses to Rock
		else if(move.getClass() == Rock.class)
		{
			result.Winner = Participants.COMPUTER;
			result.Message = "Rock crushes Scissors.";
		}
		// Both picked Scissors. It's a tie.
		else
		{
			result.Winner = Participants.TIE;
			result.Message = "Scissors and Scissors are friends.";
		}
		return result;
	}

}



CHOICES - (1)ROCK (2)PAPER (3) SCISSORS
>5
Invalid choice. Please try again.
CHOICES - (1)ROCK (2)PAPER (3) SCISSORS
>1
Result: Rock and Rock are friends.
SCORE = COMPUTER: 0 | PLAYER: 0
Would you like to play again? (1)YES (2)NO
1
CHOICES - (1)ROCK (2)PAPER (3) SCISSORS
>2
Result: Paper covers Rock.
SCORE = COMPUTER: 0 | PLAYER: 1
Would you like to play again? (1)YES (2)NO
1
CHOICES - (1)ROCK (2)PAPER (3) SCISSORS
>3
Result: Rock crushes Scissors.
SCORE = COMPUTER: 1 | PLAYER: 1
Would you like to play again? (1)YES (2)NO
2



Again, any critique would be nice. Thanks for looking it over.
Was This Post Helpful? 0
  • +
  • -

#13 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1275
  • View blog
  • Posts: 2,837
  • Joined: 05-April 11

Re: Rock, Paper, Scissors game - flow of control

Posted 23 June 2013 - 05:58 AM

Your main should really just kick start your program, and not contain lots of code

You don't need to return null in your getMove method, if you validate the choice before :)
By validating, you can remove the ugly null check
public boolean validateChoice(int choice) {
	return choice >= 1 && choice <= 3;
}



Your updateScore has an else clause that does nothing. Just remove it
//Remove these lines
else
{
	return;
}



getKeepPlaying can be reduced to one line
private static boolean getKeepPlaying(int choice) {
	return choice == 1;
}



There is a keyword for checking which type the class is
Use the instanceof keyword :)
if(move instanceof Paper)



Never declare public variables! It might not matter right now in your program, but it is good practice
Furthermore, the variables in the Outcome class should be readonly
Implement a constructor which takes two parameters, and then never ever make it possible to modify the variables again!
Was This Post Helpful? 2
  • +
  • -

#14 MentalFloss  Icon User is offline

  • "ADDICTED"[2:5]
  • member icon

Reputation: 525
  • View blog
  • Posts: 1,397
  • Joined: 02-September 09

Re: Rock, Paper, Scissors game - flow of control

Posted 23 June 2013 - 12:07 PM

Thanks for your help. Yeah, I've been out of the game for a while and I am just learning java (coming from C#) so I've forgotten a lot of stuff.

It's definitely true though - use it or lose it.

EDIT: (I removed the last edit because it didn't contribute anything)

Here is updated code:

public class Client {
		
	public static void main(String[] args)
	{
		Engine engine = new Engine();
		engine.play();
	}
}



public class Engine {
	
	private Scanner input = new Scanner(System.in);
	private Random generator = new Random();
	private int playerWins = 0;
	private int computerWins = 0;
	
	public void play()
	{		
		boolean keepPlaying = true;
		int choice = 0;
		Move player = null;
		Move computer = null;
		
		do
		{
			System.out.println("CHOICES - (1)ROCK (2)PAPER (3) SCISSORS");
			System.out.print(">");
			try {
				choice = input.nextInt();
			} catch (InputMismatchException e) {
				System.out.println(e.getMessage());
				break;
			}
			player = getMove(choice);
			if(!validateMove(choice))
			{
				printFailMessage();
				continue;
			}
			
			computer = getMove(getRandom());
			Outcome result = player.getResult(computer);
			
			System.out.println("Result: " + result.getMessage());
			updateScore(result);
			displayScore();
			
			System.out.println("Would you like to play again? (1)YES (2)NO");
			try {
				choice = input.nextInt();
			} catch (InputMismatchException e) {
				break;
			}
			keepPlaying = getKeepPlaying(choice);
			
		} while(keepPlaying);
	}

	private void printFailMessage()
	{
		System.out.println("Invalid choice. Please try again.");
	}
	
	private int getRandom()
	{
		return generator.nextInt(3) + 1;
	}
	
	private Move getMove(int choice)
	{
		switch(choice)
		{
			case 1:
				return new Rock();
			case 2:
				return new Paper();
			case 3:
				return new Scissors();
			default:
				return null;
		}
	}
	
	private boolean validateMove(int choice)
	{
		return (choice >= 1 && choice <= 3);
	}
	
	private void updateScore(Outcome result)
	{
		if(result.getWinner() == Participants.PLAYER)
		{
			playerWins++;
		}
		else if(result.getWinner() == Participants.COMPUTER)
		{
			computerWins++;
		}
	}
	
	private void displayScore()
	{
		System.out.println("SCORE = COMPUTER: " + computerWins + " | PLAYER: " + playerWins);
	}
	
	private boolean getKeepPlaying(int choice)
	{
		return choice == 1;
	}
}



public class Rock implements Move {

	public Outcome getResult(Move move) {
		
		// Rock loses to paper
		if(move instanceof Paper)
		{
			return new Outcome(Participants.COMPUTER, "Paper covers Rock.");
		}
		// Rock beats scissors
		else if(move instanceof Scissors)
		{
			return new Outcome(Participants.PLAYER, "Rock crushes Scissors.");
		}
		// Both picked Rock. It's a tie.
		else
		{
			return new Outcome(Participants.TIE, "Rock and Rock are friends.");
		}
	}
}



public class Outcome {
	private Participants winner;
	private String message;
	
	public Outcome(Participants winner, String message)
	{
		this.winner = winner;
		this.message = message;
	}
	
	public Participants getWinner()
	{
		return winner;
	}
	
	public String getMessage()
	{
		return message;
	}
}



I didn't include Scissors or Paper because they are the same as Rock (in structure). I also didn't include the classes that didn't change. Thanks again for your help CasiOo. Any other suggestions? Thanks.

EDIT: I noticed that if I type a letter for the nextInt() on scanner, it throws an exception so I tried putting a try around it but I cannot get it to gracefully get back to working state because with "n" in the input scanner, it just continuously prints the choice message and then says Invalid Choice.

This post has been edited by MentalFloss: 23 June 2013 - 12:59 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1