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