2015-09-07 11:44:36 -06:00
|
|
|
// enums/RoShamBo1.java
|
2021-01-31 15:42:31 -07:00
|
|
|
// (c)2021 MindView LLC: see Copyright.txt
|
2015-11-15 15:51:35 -08:00
|
|
|
// We make no guarantees that this code is fit for any purpose.
|
2016-09-23 13:23:35 -06:00
|
|
|
// Visit http://OnJava8.com for more book information.
|
2016-01-25 18:05:55 -08:00
|
|
|
// Demonstration of multiple dispatching
|
2016-07-28 12:48:23 -06:00
|
|
|
// {java enums.RoShamBo1}
|
2015-06-15 17:47:35 -07:00
|
|
|
package enums;
|
|
|
|
import java.util.*;
|
|
|
|
|
|
|
|
public class RoShamBo1 {
|
|
|
|
static final int SIZE = 20;
|
2017-01-20 21:30:44 -08:00
|
|
|
private static Random rand = new Random(47);
|
2015-06-15 17:47:35 -07:00
|
|
|
public static Item newItem() {
|
|
|
|
switch(rand.nextInt(3)) {
|
|
|
|
default:
|
|
|
|
case 0: return new Scissors();
|
|
|
|
case 1: return new Paper();
|
|
|
|
case 2: return new Rock();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
public static void match(Item a, Item b) {
|
|
|
|
System.out.println(
|
|
|
|
a + " vs. " + b + ": " + a.compete(b));
|
|
|
|
}
|
|
|
|
public static void main(String[] args) {
|
|
|
|
for(int i = 0; i < SIZE; i++)
|
|
|
|
match(newItem(), newItem());
|
|
|
|
}
|
2015-09-07 11:44:36 -06:00
|
|
|
}
|
|
|
|
/* Output:
|
2017-05-03 12:00:00 -06:00
|
|
|
Rock vs. Rock: DRAW
|
|
|
|
Paper vs. Rock: WIN
|
|
|
|
Paper vs. Rock: WIN
|
|
|
|
Paper vs. Rock: WIN
|
|
|
|
Scissors vs. Paper: WIN
|
|
|
|
Scissors vs. Scissors: DRAW
|
2015-06-15 17:47:35 -07:00
|
|
|
Scissors vs. Paper: WIN
|
|
|
|
Rock vs. Paper: LOSE
|
2017-05-03 12:00:00 -06:00
|
|
|
Paper vs. Paper: DRAW
|
2015-06-15 17:47:35 -07:00
|
|
|
Rock vs. Paper: LOSE
|
|
|
|
Paper vs. Scissors: LOSE
|
2017-05-03 12:00:00 -06:00
|
|
|
Paper vs. Scissors: LOSE
|
|
|
|
Rock vs. Scissors: WIN
|
2015-06-15 17:47:35 -07:00
|
|
|
Rock vs. Paper: LOSE
|
2017-05-03 12:00:00 -06:00
|
|
|
Paper vs. Rock: WIN
|
2015-06-15 17:47:35 -07:00
|
|
|
Scissors vs. Paper: WIN
|
|
|
|
Paper vs. Scissors: LOSE
|
2017-05-03 12:00:00 -06:00
|
|
|
Paper vs. Scissors: LOSE
|
|
|
|
Paper vs. Scissors: LOSE
|
|
|
|
Paper vs. Scissors: LOSE
|
2015-09-07 11:44:36 -06:00
|
|
|
*/
|