2015-09-07 11:44:36 -06:00
|
|
|
// patterns/PaperScissorsRock.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.
|
2021-03-04 16:15:04 -07:00
|
|
|
// Demonstration of multiple dispatching.
|
2015-06-15 17:47:35 -07:00
|
|
|
import java.util.*;
|
2015-11-03 12:00:44 -08:00
|
|
|
import java.util.function.*;
|
|
|
|
import java.util.stream.*;
|
2021-03-05 02:28:04 -07:00
|
|
|
import enums.Outcome;
|
|
|
|
import static enums.Outcome.*;
|
|
|
|
import enums.Item;
|
|
|
|
import enums.Paper;
|
|
|
|
import enums.Scissors;
|
|
|
|
import enums.Rock;
|
2015-11-11 20:20:04 -08:00
|
|
|
import onjava.*;
|
|
|
|
import static onjava.Tuple.*;
|
2015-06-15 17:47:35 -07:00
|
|
|
|
|
|
|
class ItemFactory {
|
2015-11-03 12:00:44 -08:00
|
|
|
static List<Supplier<Item>> items =
|
|
|
|
Arrays.asList(
|
|
|
|
Scissors::new, Paper::new, Rock::new);
|
2016-11-21 12:37:57 -08:00
|
|
|
static final int SZ = items.size();
|
2017-01-09 14:26:12 -08:00
|
|
|
private static SplittableRandom rand =
|
|
|
|
new SplittableRandom(47);
|
2015-06-15 17:47:35 -07:00
|
|
|
public static Item newItem() {
|
2016-11-21 12:37:57 -08:00
|
|
|
return items.get(rand.nextInt(SZ)).get();
|
2015-11-03 12:00:44 -08:00
|
|
|
}
|
2015-11-11 20:20:04 -08:00
|
|
|
public static Tuple2<Item,Item> newPair() {
|
|
|
|
return tuple(newItem(), newItem());
|
2015-06-15 17:47:35 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class Compete {
|
2015-11-11 20:20:04 -08:00
|
|
|
public static Outcome match(Tuple2<Item,Item> p) {
|
2016-11-21 12:37:57 -08:00
|
|
|
System.out.print(p.a1 + " -> " + p.a2 + " : ");
|
|
|
|
return p.a1.compete(p.a2);
|
2015-06-15 17:47:35 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public class PaperScissorsRock {
|
2016-01-25 18:05:55 -08:00
|
|
|
public static void main(String[] args) {
|
2015-11-03 12:00:44 -08:00
|
|
|
Stream.generate(ItemFactory::newPair)
|
|
|
|
.limit(20)
|
|
|
|
.map(Compete::match)
|
|
|
|
.forEach(System.out::println);
|
2015-06-15 17:47:35 -07:00
|
|
|
}
|
2015-09-07 11:44:36 -06:00
|
|
|
}
|
|
|
|
/* Output:
|
2016-07-22 14:45:35 -06:00
|
|
|
Scissors -> Rock : LOSE
|
2015-12-15 11:47:04 -08:00
|
|
|
Scissors -> Paper : WIN
|
|
|
|
Rock -> Paper : LOSE
|
2016-07-22 14:45:35 -06:00
|
|
|
Rock -> Rock : DRAW
|
2015-12-15 11:47:04 -08:00
|
|
|
Rock -> Paper : LOSE
|
|
|
|
Paper -> Scissors : LOSE
|
|
|
|
Rock -> Paper : LOSE
|
2016-07-22 14:45:35 -06:00
|
|
|
Scissors -> Scissors : DRAW
|
|
|
|
Scissors -> Rock : LOSE
|
2015-12-15 11:47:04 -08:00
|
|
|
Scissors -> Paper : WIN
|
2016-07-22 14:45:35 -06:00
|
|
|
Scissors -> Rock : LOSE
|
2015-12-15 11:47:04 -08:00
|
|
|
Paper -> Scissors : LOSE
|
2016-07-22 14:45:35 -06:00
|
|
|
Rock -> Rock : DRAW
|
|
|
|
Scissors -> Scissors : DRAW
|
|
|
|
Paper -> Paper : DRAW
|
|
|
|
Scissors -> Paper : WIN
|
|
|
|
Scissors -> Rock : LOSE
|
|
|
|
Scissors -> Paper : WIN
|
|
|
|
Rock -> Paper : LOSE
|
|
|
|
Rock -> Scissors : WIN
|
2015-09-07 11:44:36 -06:00
|
|
|
*/
|