Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: Simple game problem

  1. #1
    Member
    Join Date
    Jan 2011
    Posts
    88
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Simple game problem

    I've been working on a Rock Paper Scissors game. It works by the player typing in either "Rock" "Paper" or "Scissors" then the computer will generate at random "Rock" "Paper" or "Scissors"

    The problem im having is that nothing seems to win against anything besides "Rock" which seems to win against everything (besides rock which it "ties" to)

    here is my code:

    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
     
     
    public class Rock_Paper_Scissors {
     
    	private int stage = 0;
    	static BufferedReader stdin = new BufferedReader
        (new InputStreamReader(System.in));
     
    	private int random(int range) {
    		return (int) ((java.lang.Math.random() * range) + 1);
    	}
     
    	public String computerChoice() {
    		int choice = random(3);
    		if (choice == 1) {
    			 return "Rock";
    		} else if (choice == 2) {
    			return "Paper";
    		} else if (choice == 3) {
    			return "Scissors";
    		}
    		return null;
    	}
     
    	public static void main(String[] args) throws IOException {
    		Rock_Paper_Scissors rps = new Rock_Paper_Scissors();
    		while (true) {
    			if (rps.stage == 0) {
    				System.out.println("Type 'Rock' 'Paper' or 'Scissors'.");
    				rps.stage++;
    				rps.handleChoice(stdin.readLine());
    			}
    		}
    	}
     
    	public void handleChoice(String choice) {
    		computerChoice();
    		if (choice.equalsIgnoreCase("rock") || choice.equalsIgnoreCase("paper")
    				|| choice.equalsIgnoreCase("Scissors")) {
    			if (!choice.equalsIgnoreCase(computerChoice()) || !computerChoice().equalsIgnoreCase(choice)) {
    				if (choice.equalsIgnoreCase("rock") && computerChoice().equals("Scissors")) {
    					System.out.println(choice + " beats " + computerChoice() + ", You win!");
    				} else if (choice.equalsIgnoreCase("rock") && computerChoice().equals("Paper")) {
    					System.out.println(computerChoice() + " beats " + choice + ", you lose :(");
    				} else if (choice.equalsIgnoreCase("paper") && computerChoice().equals("Rock")) {
    					System.out.println(choice + " beats " + computerChoice() + ", You win!");
    				} else if (choice.equalsIgnoreCase("paper") && computerChoice().equals("Scissors")) {
    					System.out.println(computerChoice() + " beats " + choice + ", you lose :(");
    				} else if (choice.equalsIgnoreCase("scissors") && computerChoice().equals("Paper")) {
    					System.out.println(choice + " beats " + computerChoice() + ", You win!");
    				} else {
    					System.out.println(computerChoice());
    				}
    			} else {
    				System.out.println("Its a tie!");
    			}
    		} else {
    			System.out.println("Unknown choice " + choice);
    		}
    		stage = 0;
    	}
    }


  2. #2
    Junior Member
    Join Date
    Nov 2011
    Location
    Budapest, Hungary
    Posts
    10
    My Mood
    Mellow
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Simple game problem

    Works fine for me. Aren't you missing scissors vs. rock though?

    I think your program would be simpler if you created lowercase versions of the two things to compare and then just compared those strings. Then you could avoid typing equalsIgnoreCase millions of times. Then it would be a lot easier to spot bugs.

    Type 'Rock' 'Paper' or 'Scissors'.
    rock
    rock beats Scissors, You win!
    Type 'Rock' 'Paper' or 'Scissors'.
    rock
    Its a tie!
    Type 'Rock' 'Paper' or 'Scissors'.
    rock
    Paper beats rock, you lose
    Type 'Rock' 'Paper' or 'Scissors'.
    paper
    Paper
    Type 'Rock' 'Paper' or 'Scissors'.

    Unknown choice
    Type 'Rock' 'Paper' or 'Scissors'.
    paper
    Its a tie!
    Type 'Rock' 'Paper' or 'Scissors'.
    paper
    paper beats Rock, You win!
    Type 'Rock' 'Paper' or 'Scissors'.
    scissors
    Scissors
    Type 'Rock' 'Paper' or 'Scissors'.
    scissors
    Paper
    Type 'Rock' 'Paper' or 'Scissors'.
    scissors
    scissors beats Paper, You win!
    Type 'Rock' 'Paper' or 'Scissors'.
    Cave of Programming -- programming info and help, exercises, tutorials and other stuff.

  3. #3
    Member
    Join Date
    Jan 2011
    Posts
    88
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Simple game problem

    this is what im talking about right here little things like this

    rock
    Rock beats rock, you lose :(
    Type 'Rock' 'Paper' or 'Scissors'.

    what i chose was "rock" what the computer chose was "Rock" but somehow "Rock" ended up beating "rock"? I clearly told it that if the players choice is the same as the computers choice then the result should end up being a tie.

    if (!choice.equalsIgnoreCase(computerChoice()) || !computerChoice().equalsIgnoreCase(choice)) {
    ...
    } else {
    System.out.println("Its a tie!");

  4. #4
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Simple game problem

    It looks like you have one or more logical errors in your if statements. I hate to be blunt but I am not going to go through all the if statements one at a time to find the problems. One thing you can do is to remove the user and AI choices and hard code their values to make sure the correct branch is executed. If it does move onto the next combination. If not fix it. Time to get cracking.
    Improving the world one idiot at a time!

  5. #5
    Member
    Join Date
    Jan 2011
    Posts
    88
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Simple game problem

    alright ill give that a shot

  6. #6
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Simple game problem

    OK it looks like in each of your if statements you call the computerChoice method and each time it generates a new/different choice instead of using the same one for all the if statements.
    Improving the world one idiot at a time!

  7. #7
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Simple game problem

    Another issue, half the time you use equalsIgnoreCase and half the time you use equals.
    Improving the world one idiot at a time!

  8. The Following User Says Thank You to Junky For This Useful Post:

    frozen java (November 30th, 2011)

  9. #8
    Member
    Join Date
    Jan 2011
    Posts
    88
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Simple game problem

    I made it assign a value for each "Rock" "Paper" and "Scissors" works great now thanks for the help

    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
     
     
    public class Rock_Paper_Scissors {
     
    	private int stage = 0;
    	static BufferedReader stdin = new BufferedReader
        (new InputStreamReader(System.in));
     
    	private int random(int range) {
    		return (int) ((java.lang.Math.random() * range) + 1);
    	}
     
    	public int getPlayerValue(String choice) {
    		if (choice.equalsIgnoreCase("rock")) {
    			return 1;
    		} else if (choice.equalsIgnoreCase("paper")) {
    			return 2;
    		} else if (choice.equalsIgnoreCase("scissors")) {
    			return 3;
    		}
    		return 0;
    	}
     
    	public static void main(String[] args) throws IOException {
    		Rock_Paper_Scissors rps = new Rock_Paper_Scissors();
    		while (true) {
    			if (rps.stage == 0) {
    				System.out.println("Type 'Rock' 'Paper' or 'Scissors'.");
    				rps.stage++;
    				rps.handleChoice(stdin.readLine());
    			}
    		}
    	}
     
    	public void handleChoice(String choice) {
    		int compValue = random(3);
    		if (compValue != getPlayerValue(choice)) {
    			if (compValue == 1 && getPlayerValue(choice) == 3) { // Rock beating Scissors (computer)
    				System.out.println("Rock beats Scissors, you lose :(");
    			}
    			if (compValue == 2 && getPlayerValue(choice) == 1) { // Paper beating Rock (computer)
    				System.out.println("Paper beats Rock, you lose :(");
    			}
    			if (compValue == 3 && getPlayerValue(choice) == 2) { // Scissors beating Paper (computer)
    				System.out.println("Scissors beats Paper, you lose :(");
    			}
     
    			if (compValue == 1 && getPlayerValue(choice) == 2) { // Paper beating rock (player)
    				System.out.println("Paper beats Rock, you win!");
    			}
    			if (compValue == 2 && getPlayerValue(choice) == 3) { // Scissors beating paper (player)
    				System.out.println("Scissors beats paper, you win!");
    			}
    			if (compValue == 3 && getPlayerValue(choice) == 1) { // Rock beating Scissors (player)
    				System.out.println("Rock beats Scissors, you win!");
    			}
    		} else {
    			System.out.println("You tied!");
    		}
    		stage = 0;
    	}
    }

    Thanks Again!

Similar Threads

  1. Programming AI for simple game?
    By YouGoLast in forum Java Theory & Questions
    Replies: 3
    Last Post: September 27th, 2024, 02:33 AM
  2. Simple Checkers Game in J2ME
    By chris2307 in forum Java ME (Mobile Edition)
    Replies: 0
    Last Post: February 26th, 2011, 07:40 AM
  3. simple game with swing and awt problem
    By Pulse_Irl in forum AWT / Java Swing
    Replies: 2
    Last Post: October 12th, 2010, 02:04 PM
  4. Simple game in Java
    By velop in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 27th, 2010, 05:04 AM
  5. Problem while programming a simple game of Car moving on a road
    By rojroj in forum Java Theory & Questions
    Replies: 3
    Last Post: April 2nd, 2009, 10:24 AM