OnJava8-Examples/patterns/PaperScissorsRock.java

110 lines
2.2 KiB
Java
Raw Normal View History

2015-05-05 11:20:13 -07:00
//: patterns/PaperScissorsRock.java
2015-05-29 14:18:51 -07:00
// <20>2015 MindView LLC: see Copyright.txt
2015-05-05 11:20:13 -07:00
// Demonstration of multiple dispatching.
import java.util.*;
2015-05-27 23:30:19 -07:00
enum Outcome { WIN, LOSE, DRAW }
2015-05-05 11:20:13 -07:00
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"; }
}
class ItemFactory {
public static Item newItem() {
switch((int)(Math.random() * 3)) {
default:
case 0:
return new Scissors();
case 1:
return new Paper();
case 2:
return new Rock();
}
}
}
class Compete {
public static Outcome match(Item a, Item b) {
System.out.print(a + " <--> " + b + " : ");
return a.compete(b);
}
}
public class PaperScissorsRock {
public static void main(String args[]) {
2015-05-06 15:14:33 -07:00
ArrayList<Item> items = new ArrayList<>();
2015-05-05 11:20:13 -07:00
for(int i = 0; i < 40; i++)
items.add(ItemFactory.newItem());
for(int i = 0; i < items.size()/2; i++)
System.out.println(
Compete.match(
2015-05-06 15:14:33 -07:00
items.get(i), items.get(i*2)));
2015-05-05 11:20:13 -07:00
}
} ///:~