Rock Paper Scissors(Text-based) help

  • (2 Pages)
  • +
  • 1
  • 2

18 Replies - 3602 Views - Last Post: 22 February 2009 - 08:39 PM Rate Topic: -----

#1 Ipodhero178  Icon User is offline

  • D.I.C Regular

Reputation: 5
  • View blog
  • Posts: 286
  • Joined: 22-February 09

Rock Paper Scissors(Text-based) help

Posted 22 February 2009 - 12:44 PM

Hi everyone, I am trying to make a text-based version of the game rock,paper,scissors. My problem is that the players(player one, which is the user and player two is the AI) are not statements and I need to know how to make them statements.

import java.util.Scanner;
import java.util.Random;

public class RPS
{
	public static void main(String args[])
	{
		Scanner scan= new Scanner(System.in);
		[b]//the players
		playerone; //player one(the human)
		playertwo; //player two(the AI)
[/b]
		System.out.println("Rock,Paper,Scissors!");
		System.out.println("1 = Rock");
		System.out.println("2 = Paper");
		System.out.println("3 = Scissors");
		System.out.println("Player choose ===>>  ");
		scan=input.nextInt();
		Random rand=new Random(123);
		int a= rand.nextInt(3)+1;
		//for rock first
		if(playerone==1 && playertwo==1)
		{
			tie=true; //for a tie
		}
		if(playerone==1 && playertwo==2)
		{
			lose=true; //for a loss
		}
		if(playerone==1 && playertwo==3)
		{
			win=true;//for a win
		}
		
		//for paper
		if(playerone==2 && playertwo==2)
		{
			tie=true;//paper tie
		}
		if(playerone==2 && playertwo==1)
		{
			win=true;//paper win
		}
		if(playerone==2 && playertwo==3)
		{
			loss=true;//paper loss
		}
		
		//Scissors
		if(playerone==3 && playertwo==3)
		{
			tie=true;//scissor tie
		}
		if(playerone==3 && playertwo==2)
		{
			win=true;//scissor win
		}
		if(playerone==3 && playertwo==1)
		{
			loss=true;//scissor loss
		}
		
			
	}
}


This post has been edited by Ipodhero178: 22 February 2009 - 12:47 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Rock Paper Scissors(Text-based) help

#2 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 521
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Re: Rock Paper Scissors(Text-based) help

Posted 22 February 2009 - 12:47 PM

Here you go...

int playerone = 0, playertwo = 0;


Now you can make those values equal to whatever you want, depending on the values of rock, paper, and scissors.

And instead of scan=input.nextInt();...use playerone = input.nextInt();. That will make playerone equal to whatever they put in.

This post has been edited by Locke: 22 February 2009 - 12:48 PM

Was This Post Helpful? 0
  • +
  • -

#3 Ipodhero178  Icon User is offline

  • D.I.C Regular

Reputation: 5
  • View blog
  • Posts: 286
  • Joined: 22-February 09

Re: Rock Paper Scissors(Text-based) help

Posted 22 February 2009 - 12:50 PM

View PostLocke, on 22 Feb, 2009 - 11:47 AM, said:

Here you go...

int playerone = 0, playertwo = 0;


Now you can make those values equal to whatever you want, depending on the values of rock, paper, and scissors.

And instead of scan=input.nextInt();...use playerone = input.nextInt();. That will make playerone equal to whatever they put in.


Thanks a lot. :) It's really been bugging me for days. I didn't see the other problem either.
Was This Post Helpful? 0
  • +
  • -

#4 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 521
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Re: Rock Paper Scissors(Text-based) help

Posted 22 February 2009 - 12:52 PM

Whoops.

You've also never declared win lose tie as variables either.

Add this line somewhere before your if statements...

boolean win = false, lose = false, tie = false;

This post has been edited by Locke: 22 February 2009 - 12:53 PM

Was This Post Helpful? 1
  • +
  • -

#5 Ipodhero178  Icon User is offline

  • D.I.C Regular

Reputation: 5
  • View blog
  • Posts: 286
  • Joined: 22-February 09

Re: Rock Paper Scissors(Text-based) help

Posted 22 February 2009 - 12:54 PM

Now I am getting compile errors with tie,win, and lose in the code. Can you help me with that too?

EDIT: I saw your message. Thanks again!

This post has been edited by Ipodhero178: 22 February 2009 - 12:55 PM

Was This Post Helpful? 0
  • +
  • -

#6 Ipodhero178  Icon User is offline

  • D.I.C Regular

Reputation: 5
  • View blog
  • Posts: 286
  • Joined: 22-February 09

Re: Rock Paper Scissors(Text-based) help

Posted 22 February 2009 - 01:05 PM

Now my code will not display the results of the game. It just says "process completed" when the user inputs their choice.


//the players
		int playerone=0; //player one(the human)
		int playertwo=0; //player two(the AI)
		System.out.println("Rock,Paper,Scissors!");
		System.out.println("1 = Rock");
		System.out.println("2 = Paper");
		System.out.println("3 = Scissors");
		System.out.print("Player choose ===>>  ");
		playerone=scan.nextInt();
		Random rand=new Random(123);
		int a= rand.nextInt(3)+1;
		//win, lose, and tie
		boolean win=false, lose=false, tie=false;
		//for rock first
		if(playerone==1 && playertwo==1)
		{
			tie=true; //for a tie
			System.out.println("rock and rock. You tied!");
		}
		if(playerone==1 && playertwo==2)
		{
			lose=true; //for a loss
			System.out.println("rock and scissors. You won!");
		}
		if(playerone==1 && playertwo==3)
		{
			win=true;//for a win
			System.out.println("rock and paper. You lost!");
		}
		
		//for paper
		if(playerone==2 && playertwo==2)
		{
			tie=true;//paper tie
			System.out.println("paper and paper. You tied!");
		}
		if(playerone==2 && playertwo==1)
		{
			win=true;//paper win
			System.out.println("paper and rock. You won!");
		}
		if(playerone==2 && playertwo==3)
		{
			lose=true;//paper loss
			System.out.println("paper and scissors. You lost!");
		}
		
		//Scissors
		if(playerone==3 && playertwo==3)
		{
			tie=true;//scissor tie
			System.out.println("scissors and scissors. You tied!");
		}
		if(playerone==3 && playertwo==2)
		{
			win=true;//scissor win
			System.out.println("scissors and paper. You won!");
		}
		if(playerone==3 && playertwo==1)
		{
			lose=true;//scissor loss
			System.out.println("scissors and rock. You lost!");
		}
		
			
	}
}

This post has been edited by Ipodhero178: 22 February 2009 - 01:11 PM

Was This Post Helpful? 0
  • +
  • -

#7 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 521
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Re: Rock Paper Scissors(Text-based) help

Posted 22 February 2009 - 01:11 PM

EDIT: I'm an idiot...

You never set the playertwo value to anything...

int a=rand.nextInt(3) + 1; Needs to be --> playertwo = rand.nextInt(3) + 1;

That'll work. ;)

This post has been edited by Locke: 22 February 2009 - 01:18 PM

Was This Post Helpful? 1
  • +
  • -

#8 Ipodhero178  Icon User is offline

  • D.I.C Regular

Reputation: 5
  • View blog
  • Posts: 286
  • Joined: 22-February 09

Re: Rock Paper Scissors(Text-based) help

Posted 22 February 2009 - 01:13 PM

View PostLocke, on 22 Feb, 2009 - 12:11 PM, said:

You have to print out if they won or lost somewhere. You never did that.

if (win)
    System.out.println("You won!");

else if (lose)
    System.out.println("You lost...");

else
    System.out.println("It's a tie.");


That should go at the end.

I thought the strings inside the if statements would perform that.

EDIT: It's ok man.
EDIT 2: I'm an idiot, lol. I see what you mean. You have been very helpful. Thanks a bunch!

This post has been edited by Ipodhero178: 22 February 2009 - 01:17 PM

Was This Post Helpful? 0
  • +
  • -

#9 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 521
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Re: Rock Paper Scissors(Text-based) help

Posted 22 February 2009 - 01:16 PM

OOPS.

Good god...I'm having trouble today...

Ok...it doesn't need input. It needs to be the random number.

This post has been edited by Locke: 22 February 2009 - 01:17 PM

Was This Post Helpful? 0
  • +
  • -

#10 Ipodhero178  Icon User is offline

  • D.I.C Regular

Reputation: 5
  • View blog
  • Posts: 286
  • Joined: 22-February 09

Re: Rock Paper Scissors(Text-based) help

Posted 22 February 2009 - 01:20 PM

Now I am getting logical errors. The AI keeps choosing paper all the time. What do I do?
Was This Post Helpful? 0
  • +
  • -

#11 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 521
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Re: Rock Paper Scissors(Text-based) help

Posted 22 February 2009 - 01:22 PM

You need to remove the seed from the constructor of your Random object. When you supply a seed, it will pick the same sequence of random numbers every execution of the program. For it to be truly random, no seed should be given.

Random rand = new Random(/*NO NUMBERS*/);

:)/>

This post has been edited by Locke: 22 February 2009 - 01:23 PM

Was This Post Helpful? 0
  • +
  • -

#12 Ipodhero178  Icon User is offline

  • D.I.C Regular

Reputation: 5
  • View blog
  • Posts: 286
  • Joined: 22-February 09

Re: Rock Paper Scissors(Text-based) help

Posted 22 February 2009 - 01:28 PM

I did. It worked. I did this with another program of mine(it was a guessing game, High and Low) and I got decimal points in the Random object, when I make it an int. How could that happen?
Was This Post Helpful? 0
  • +
  • -

#13 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 521
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Re: Rock Paper Scissors(Text-based) help

Posted 22 February 2009 - 01:32 PM

Do you mean that you're getting decimal places now? Or in the other program?
Was This Post Helpful? 0
  • +
  • -

#14 Ipodhero178  Icon User is offline

  • D.I.C Regular

Reputation: 5
  • View blog
  • Posts: 286
  • Joined: 22-February 09

Re: Rock Paper Scissors(Text-based) help

Posted 22 February 2009 - 01:35 PM

In the other program.
Was This Post Helpful? 0
  • +
  • -

#15 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 521
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Re: Rock Paper Scissors(Text-based) help

Posted 22 February 2009 - 01:42 PM

Check that you're not outputting one of your random integers...then a period, then another random integer. That can give the illusion of decimal places, but it's not really true.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2