Another Java forum classic: rock, paper, scissors. Or chifoumi, as it is called in France.
An interesting solution involves an enum (which I called Choice below). The only "fancy" thing in the code is checkWinner which checks if the first value is equal to the second value + 1, modulo 3 (as paper is stronger than rock, scissors stronger than paper, and rock stronger than scissors, so there's a circular thing going on there). I haven't bothered checking the user's input, but that should really be done.
And that's pretty much it -- connoisseurs will appreciate the use of Scanner which I usually spit upon!
I highly recommend reading the Wikipedia entry for Rock, paper, scissors, it's quite entertaining... Did you know it's been used in a (US) federal court?!
An interesting solution involves an enum (which I called Choice below). The only "fancy" thing in the code is checkWinner which checks if the first value is equal to the second value + 1, modulo 3 (as paper is stronger than rock, scissors stronger than paper, and rock stronger than scissors, so there's a circular thing going on there). I haven't bothered checking the user's input, but that should really be done.
import java.util.Random;
import java.util.Scanner;
public class RockPaperScissors {
public enum Choice {
ROCK(0),
PAPER(1),
SCISSORS(2);
private final int value;
Choice(int value) {
this.value = value;
}
public int value() {
return value;
}
}
private int checkWinner(Choice choice1, Choice choice2) {
if (choice2 == choice1) {
return 0;
} else if (choice1.value() == ((choice2.value() + 1) %3)) {
return 1;
} else return 2;
}
private Choice getChoiceAtRandom() {
return Choice.values()[new Random().nextInt(3)];
}
private Choice readChoice() {
System.out.println("Rock, paper or scissors?");
Scanner scanner = new Scanner(System.in);
String choice = scanner.next();
return Choice.valueOf(choice.toUpperCase());
}
public void doGameLoop() {
int counter = 0;
while (counter < 3) {
Choice userChoice = readChoice();
Choice computerChoice = getChoiceAtRandom();
int winner = checkWinner(userChoice, computerChoice);
if (winner == 0) {
System.out.println("Both " + userChoice + ". It's a tie");
} else if (winner == 1) {
System.out.println("You win! You played " + userChoice
+ " and computer played " + computerChoice) ;
} else {
System.out.println("You lose. You played " + userChoice
+ " and computer played " + computerChoice) ;
}
counter++;
}
}
public static void main(String[] args) {
new RockPaperScissors().doGameLoop();
}
}
And that's pretty much it -- connoisseurs will appreciate the use of Scanner which I usually spit upon!
I highly recommend reading the Wikipedia entry for Rock, paper, scissors, it's quite entertaining... Did you know it's been used in a (US) federal court?!
0 Comments On This Entry
About...
This is my fake blog. 'Cause I also have a real blog with real stuff in it.
← January 2022 →
| S | M | T | W | T | F | S |
|---|---|---|---|---|---|---|
| 1 | ||||||
| 2 | 3 | 4 | 5 | 6 | 7 | 8 |
| 9 | 10 | 11 | 12 | 13 | 14 | 15 |
| 16 | 17 | 18 | 19 | 20 | 21 | 22 |
| 23 | 24 | 25 | 26 | 27 | 28 | 29 |
| 30 | 31 |
Tags
My Blog Links
Recent Entries
-
Rock, Paper, Scissorson Feb 12 2010 06:40 AM
-
My Latest GreaseMonkey Script for DICon Feb 11 2010 04:29 AM
-
2 Messages of Noteon Nov 24 2009 04:46 PM
-
Palindrome with a Stack in C++on Nov 06 2009 03:13 PM
-
Pathfinding: Walk on the Bridge (II)on Nov 06 2009 10:46 AM
Recent Comments
Search My Blog
3 user(s) viewing
3 Guests
0 member(s)
0 anonymous member(s)
0 member(s)
0 anonymous member(s)



Leave Comment








|