OnJava8-Examples/patterns/PaperScissorsRock.java

140 lines
2.8 KiB
Java
Raw Normal View History

2015-09-07 11:44:36 -06:00
// patterns/PaperScissorsRock.java
2015-06-15 17:47:35 -07:00
// Demonstration of multiple dispatching.
import java.util.*;
2015-11-03 12:00:44 -08:00
import java.util.function.*;
import java.util.stream.*;
2015-06-15 17:47:35 -07:00
enum Outcome { WIN, LOSE, DRAW }
interface Item {
Outcome compete(Item it);
Outcome eval(Paper p);
Outcome eval(Scissors s);
Outcome eval(Rock r);
}
class Paper implements Item {
@Override
public Outcome compete(Item it) {
return it.eval(this);
}
@Override
public Outcome eval(Paper p) {
return Outcome.DRAW;
}
@Override
public Outcome eval(Scissors s) {
return Outcome.WIN;
}
@Override
public Outcome eval(Rock r) {
return Outcome.LOSE;
}
@Override
public String toString() { return "Paper"; }
}
class Scissors implements Item {
@Override
public Outcome compete(Item it) {
return it.eval(this);
}
@Override
public Outcome eval(Paper p) {
return Outcome.LOSE;
}
@Override
public Outcome eval(Scissors s) {
return Outcome.DRAW;
}
@Override
public Outcome eval(Rock r) {
return Outcome.WIN;
}
@Override
public String toString() { return "Scissors"; }
}
class Rock implements Item {
@Override
public Outcome compete(Item it) {
return it.eval(this);
}
@Override
public Outcome eval(Paper p) {
return Outcome.WIN;
}
@Override
public Outcome eval(Scissors s) {
return Outcome.LOSE;
}
@Override
public Outcome eval(Rock r) {
return Outcome.DRAW;
}
@Override
public String toString() { return "Rock"; }
}
2015-11-03 12:00:44 -08:00
class ItemPair {
Item first;
Item second;
ItemPair(Item f, Item s) {
first = f;
second = s;
}
}
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);
final static int sz = items.size();
private static Random rand = new Random(47);
2015-06-15 17:47:35 -07:00
public static Item newItem() {
2015-11-03 12:00:44 -08:00
return items.get(rand.nextInt(sz)).get();
}
public static ItemPair newPair() {
return new ItemPair(newItem(), newItem());
2015-06-15 17:47:35 -07:00
}
}
class Compete {
2015-11-03 12:00:44 -08:00
public static Outcome match(ItemPair p) {
System.out.print(
p.first + " <--> " + p.second + " : ");
return p.first.compete(p.second);
2015-06-15 17:47:35 -07:00
}
}
public class PaperScissorsRock {
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:
2015-11-03 12:00:44 -08:00
Rock <--> Rock : DRAW
Paper <--> Rock : WIN
2015-06-15 17:47:35 -07:00
Paper <--> Rock : WIN
Paper <--> Rock : WIN
2015-11-03 12:00:44 -08:00
Scissors <--> Paper : WIN
2015-06-15 17:47:35 -07:00
Scissors <--> Scissors : DRAW
Scissors <--> Paper : WIN
2015-11-03 12:00:44 -08:00
Rock <--> Paper : LOSE
2015-06-15 17:47:35 -07:00
Paper <--> Paper : DRAW
Rock <--> Paper : LOSE
2015-11-03 12:00:44 -08:00
Paper <--> Scissors : LOSE
Paper <--> Scissors : LOSE
Rock <--> Scissors : WIN
Rock <--> Paper : LOSE
2015-06-15 17:47:35 -07:00
Paper <--> Rock : WIN
2015-11-03 12:00:44 -08:00
Scissors <--> Paper : WIN
Paper <--> Scissors : LOSE
Paper <--> Scissors : LOSE
Paper <--> Scissors : LOSE
Paper <--> Scissors : LOSE
2015-09-07 11:44:36 -06:00
*/