PDA

View Full Version : Rock Paper Scissors



pinkkop
09-30-2006, 06:16 PM
sorry about the other post
can anyone help me out with this
thanks

i am trying to get it to play again and not print out the scores until the player is done playing



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

public class boobitty
{
public static void main (String[] args)
{
int Computer=0,Player=0,tie=0,compic,pscore;
String str="Y";
Random generate= new Random();
Scanner scan=new Scanner(System.in);
while (str=="Y");
{
compic=generate.nextInt(3)+1;
System.out.println ("Enter 1 for Rock, 2 for Paper, and 3 for Scissors");
pscore=scan.nextInt();
if (compic==pscore)
{
System.out.println("Tie Game");
tie++;
System.out.println ("Play Again? y/n");
str = scan.nextLine();
}
else
{
switch (pscore)
{
case 1:
{
if (compic==2)
{
System.out.println ("Paper beats Rock");
System.out.println ("Computer wins");
Computer++;
System.out.println ("Play Again? y/n");
str = scan.nextLine();
}
else
{
System.out.println ("Rock beats Scissors");
System.out.println ("Player wins!");
Player++;
System.out.println ("Play Again? y/n?");
str = scan.nextLine();
}
break;
}
case 2:
{
if (compic==1)
{
System.out.println ("Paper beats Rock");
System.out.println ("Player wins!");
Player++;
System.out.println ("Play Again? y/n?");
str = scan.nextLine();
}
else
{
System.out.println ("Scissors beat Paper");
System.out.println ("Computer wins!");
Computer++;
System.out.println ("Play Again? y/n?");
str = scan.nextLine();
}
break;
}
case 3:
{
if (compic==1)
{
System.out.println ("Rock beats Scissors");
System.out.println ("Computer wins");
Computer++;
System.out.println ("Play Again? y/n?");
str = scan.nextLine();
}
else
{
System.out.println ("Scissors beat Paper");
System.out.println ("Player wins");
Player++;
System.out.println ("Play Again? y/n");
str = scan.nextLine();
}
break;
}
default:
{
System.out.println("Enter 1 2 or 3");
System.out.println ("Would you like to play again? y/n");
str=scan.nextLine();
break;
}
}

}
}



if (str=="Y")
{
System.out.println ("Play Again? y/n");
str = scan.nextLine();
}
else
{
System.out.println ("Scores:");
System.out.println ("Ties= "+tie+" Wins= "+Player+" Losses= "+Computer);


}





}
}


this is what i get from it
Enter 1 for Rock, 2 for Paper, and 3 for Scissors
2
Scissors beat Paper
Computer wins!
Play Again? y/n?
Scores:
Ties= 0 Wins= 0 Losses= 1

Twey
09-30-2006, 08:29 PM
I don't like the way you've done this.
import java.util.Random;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.IOException;

class JankenPlayer {
int score = 0;
boolean isAI;

Random rnd = new Random();

public JankenPlayer(boolean ai) {
isAI = ai;
}

public int go() {
if(isAI) {
int a = rnd.nextInt(3);
System.out.println("Computer chose " + Janken.choices[a] + ".");
return a;
} else {
System.out.print("\nIt's your turn. [R]ock, [P]aper or [S]cissors? ");
BufferedReader stdin = new BufferedReader(
new InputStreamReader(
System.in
)
);

String line = "";
int choice = -1;

while(choice == -1) {
try {
line = stdin.readLine();
} catch(IOException e) {
System.out.println("Error: could not read from stdin.");
System.exit(2);
}
if(line.length() == 0) line = " ";
switch(line.charAt(0)) {
case 'r':
case 'R':
choice = 0;
break;
case 'p':
case 'P':
choice = 1;
break;
case 's':
case 'S':
choice = 2;
break;
default:
System.out.print("Invalid choice. Please choose one of [R/P/S]: ");
}
}
return choice;
}
}
}

class Janken {
int maxPoints = 3;
JankenPlayer human = new JankenPlayer(false),
computer = new JankenPlayer(true);

public static final int ROCK = 0, PAPER = 1, SCISSORS = 2;

public static final String[] choices = {"rock", "paper", "scissors"};

public void play(int towin) {
maxPoints = towin;
while(human.score < maxPoints && computer.score < maxPoints)
takeTurn();
if(human.score < computer.score)
System.out.println("\n\n\n*************************\n***** COMPUTER WINS *****\n*************************");
else
System.out.println("\n\n\n************************\n****** HUMAN WINS ******\n************************");

}

public void takeTurn() {
int s1 = human.go(), s2 = computer.go();
if(s1 == s2)
System.out.println("Draw. Computer " + computer.score + ", human " + human.score + ".");
else if(s1 == s2 - 1 || (s1 == Janken.SCISSORS && s2 == Janken.ROCK))
System.out.println("Computer scores. Computer " + (++computer.score) + ", human " + human.score + ".");
else if(s1 == s2 + 1 || (s1 == Janken.ROCK && s2 == Janken.SCISSORS))
System.out.println("Human scores. Computer " + computer.score + ", human " + (++human.score) + ".");
}

static void printUsage() {
System.out.println("Usage: java Janken [#]\n"
+ "\twhere # is the maximum\n"
+ "\tnumber of points. Defaults\n"
+ "\tto 2.");
System.exit(1);
}

public static void main(String[] args) {
int gameLen = 2;
if(args.length > 1) printUsage();
else if(args.length == 1)
try {
gameLen = Integer.parseInt(args[0]);
} catch(NumberFormatException e) {
printUsage();
}
Janken s = new Janken();
s.play(gameLen);
}
}

pinkkop
10-01-2006, 01:34 AM
Thank you