Toggle line numbers
   1 /** 
   2  * StrategyPattern Example
   3  */
   4 
   5 import java.util.Random;
   6 
   7 class Hand {
   8     public static final int JUMUG = 0;
   9     public static final int GAWI = 1;
  10     public static final int BO = 2;
  11     public static final Hand[] hand = {
  12         new Hand(JUMUG),
  13         new Hand(GAWI),
  14         new Hand(BO),
  15     };
  16     private static final String[] name = {
  17         "Jumug", "Gawi", "Bo"
  18     };
  19     private int handvalue;
  20     private Hand(int handvalue) {
  21         this.handvalue = handvalue;
  22     }
  23     public static Hand getHand(int handvalue) { // SingletonPattern
  24         return hand[handvalue];
  25     }
  26     public boolean isStrongerThan(Hand h) {
  27         return fight(h) == 1;
  28     }
  29     public boolean isWeakerThan(Hand h) {
  30         return fight(h) == -1;
  31     }
  32     private int fight(Hand h) {
  33         if(this == h) {
  34             return 0;
  35         } else if((this.handvalue+1)%3 == h.handvalue) {
  36             return 1;
  37         } else {
  38             return -1;
  39         }
  40     }
  41     public String toString() {
  42         return name[handvalue];
  43     }
  44 }
  45 
  46 interface Strategy {
  47     public abstract Hand nextHand();
  48     public abstract void study(boolean win);
  49 }
  50 
  51 class WinningStrategy implements Strategy {
  52     private Random random;
  53     private boolean won = false;
  54     private Hand prevHand;
  55     public WinningStrategy(int seed) {
  56         random = new Random(seed);
  57     }
  58     public Hand nextHand() {
  59         if(!won) {
  60             prevHand = Hand.getHand(random.nextInt(3));
  61         }
  62         return prevHand;
  63     }
  64     public void study(boolean win) {
  65         won = win;
  66     }
  67 }
  68 
  69 class ProbStrategy implements Strategy {
  70     private Random random;
  71     private int prevHandValue = 0;
  72     private int currentHandValue = 0;
  73     private int[][] history = {
  74         {1, 1, 1, },
  75         {1, 1, 1, },
  76         {1, 1, 1, },
  77     };
  78     public ProbStrategy(int seed) {
  79         random = new Random(seed);
  80     }
  81     public Hand nextHand() {
  82         int bet = random.nextInt(getSum(currentHandValue));
  83         int handvalue = 0;
  84         if(bet < history[currentHandValue][0]) {
  85             handvalue = 0;
  86         } else if(bet < history[currentHandValue][0]
  87                         + history[currentHandValue][1]) {
  88             handvalue = 1;
  89         } else {
  90             handvalue = 2;
  91         }
  92         prevHandValue = currentHandValue;
  93         currentHandValue = handvalue;
  94         return Hand.getHand(handvalue);
  95     }
  96     private int getSum(int hv) {
  97         int sum = 0;
  98         for(int i=0; i < 3; i++) {
  99             sum += history[hv][i];
 100         }
 101         return sum;
 102     }
 103     public void study(boolean win) {
 104         if(win) {
 105             history[prevHandValue][currentHandValue]++;
 106         } else {
 107             history[prevHandValue][(currentHandValue+1)%3]++;
 108             history[prevHandValue][(currentHandValue+2)%3]++;
 109         }
 110     }
 111 }
 112 
 113 class Player {
 114     private String name;
 115     private Strategy strategy;
 116     private int wincount;
 117     private int losecount;
 118     private int gamecount;
 119     public Player(String name, Strategy strategy) {
 120         this.name = name;
 121         this.strategy = strategy;
 122     }
 123     public Hand nextHand() {
 124         return strategy.nextHand();
 125     }
 126     public void win() {
 127         strategy.study(true);
 128         wincount++;
 129         gamecount++;
 130     }
 131     public void lose() {
 132         strategy.study(false);
 133         losecount++;
 134         gamecount++;
 135     }
 136     public void even() {
 137         gamecount++;
 138     }
 139     public String toString() {
 140         return "["+name+":"+gamecount+" games, "+wincount+" win, "
 141             +losecount+" lose"+"]";
 142     }
 143 }
 144 
 145 public class StrategyHand {
 146     public static void main(String[] args) {
 147         int seed1 = 314;
 148         int seed2 = 15;
 149         Player player1 = new Player("Hong", new WinningStrategy(seed1));
 150         Player player2 = new Player("Kim", new ProbStrategy(seed2));
 151         for(int i=0; i < 10000; i++) {
 152             Hand nextHand1 = player1.nextHand();
 153             Hand nextHand2 = player2.nextHand();
 154             if(nextHand1.isStrongerThan(nextHand2)) {
 155                 System.out.println("Winner: "+player1);
 156                 player1.win();
 157                 player2.lose();
 158             } else if(nextHand2.isStrongerThan(nextHand1)) {
 159                 System.out.println("Winner: "+player2);
 160                 player1.lose();
 161                 player2.win();
 162             } else {
 163                 System.out.println("Even...");
 164                 player1.even();
 165                 player2.even();
 166             }
 167         }
 168         System.out.println("Total result:");
 169         System.out.println(""+player1);
 170         System.out.println(""+player2);
 171     }
 172 }
web biohackers.net