Design Pattern - Strategy
1. 클래스다이어그램
2. Source
1. 클래스다이어그램

- Hand.java
package com.dazzilove.strategy.sample1;
public class Hand {
public static final int HANDVALUE_GUU = 0; //주먹
public static final int HANDVALUE_CHO = 1; //가위
public static final int HANDVALUE_PAA = 2; //보
public static final Hand[] hand = {
new Hand(HANDVALUE_GUU)
, new Hand(HANDVALUE_GUU)
, new Hand(HANDVALUE_PAA)
};
private static final String[] name = {
"주먹", "가위", "보"
};
private int handValue;
private Hand(int handValue) {
this.handValue = handValue;
}
public static Hand getHand(int handValue) {
return hand[handValue];
}
public boolean isStrongerThan(Hand h) {
return fight(h) == 1;
}
public boolean isWeakerThan(Hand h) {
return fight(h) == -1;
}
/*
* 무승부이면 0
* this가 이기면 1
* h가 이기면 -1
*/
private int fight(Hand h) {
if(this == h) {
return 0;
} else if((this.handValue + 1) % 3 == h.handValue) {
return 1;
} else {
return -1;
}
}
public String toString() {
return name[handValue];
}
} - Strategy.java
package com.dazzilove.strategy.sample1;
public interface Strategy {
public abstract Hand nextHand();
public abstract void study(boolean win);
} - WinningStrategy.java
package com.dazzilove.strategy.sample1;
import java.util.Random;
public class WinningStrategy implements Strategy {
private Random random;
private boolean won = false;
private Hand prevHand;
public WinningStrategy(int seed) {
random = new Random(seed);
}
@Override
public Hand nextHand() {
if(!won) {
prevHand = Hand.getHand(random.nextInt(3));
}
return prevHand;
}
@Override
public void study(boolean win) {
won = win;
}
} - ProbStrategy.java
package com.dazzilove.strategy.sample1;
import java.util.Random;
public class ProbStrategy implements Strategy {
private Random random;
private int prevHandValue = 0;
private int currentHandValue = 0;
private int[][] history = { { 1, 1, 1 }, { 1, 1, 1 }, { 1, 1, 1 } };
public ProbStrategy(int seed) {
random = new Random(seed);
}
@Override
public Hand nextHand() {
int bet = random.nextInt(getSum(currentHandValue));
int handValue = 0;
if (bet < history[currentHandValue][0]) {
handValue = 0;
} else if (bet < history[currentHandValue][0]
+ history[currentHandValue][1]) {
handValue = 1;
} else {
handValue = 2;
}
prevHandValue = currentHandValue;
currentHandValue = handValue;
return Hand.getHand(handValue);
}
private int getSum(int hv) {
int sum = 0;
for (int i = 0; i < 3; i++) {
sum += history[hv][i];
}
return sum;
}
@Override
public void study(boolean win) {
if (win) {
history[prevHandValue][currentHandValue]++;
} else {
history[prevHandValue][(currentHandValue + 1) % 3]++;
history[prevHandValue][(currentHandValue + 2) % 3]++;
}
}
} - Player.java
package com.dazzilove.strategy.sample1;
public class Player {
private String name;
private Strategy strategy;
private int winCount;
private int loseCount;
private int gameCount;
public Player(String name, Strategy strategy) {
this.name = name;
this.strategy = strategy;
}
public String getName() {
return this.name;
}
public Hand nextHand() {
return strategy.nextHand();
}
public void win() {
strategy.study(true);
winCount++;
gameCount++;
}
public void lose() {
strategy.study(false);
loseCount++;
gameCount++;
}
public void even() {
gameCount++;
}
public String toString() {
return "[name : " + name + ", " + gameCount + " games, " + winCount + " win, " + loseCount + " lose]";
}
} - Main.java
package com.dazzilove.strategy.sample1;
public class Main {
public static void main (String[] args) {
if (args.length != 2) {
System.out.println("Usage: java Main randomSeed1 randomSeed2");
System.out.println("Example: java Main 314 15");
System.exit(0);
}
int seed1 = Integer.parseInt(args[0]);
int seed2 = Integer.parseInt(args[1]);
Player player1 = new Player("홍길동", new WinningStrategy(seed1));
Player player2 = new Player("임꺽정", new ProbStrategy(seed2));
for (int i = 0; i < 500; i++) {
Hand nextHand1 = player1.nextHand();
Hand nextHand2 = player2.nextHand();
System.out.println("==== " + i + "번째 가위바위보!");
System.out.println(player1.getName() + " : " + nextHand1 + ", " + player2.getName() + " : " + nextHand2);
if (nextHand1.isStrongerThan(nextHand2)) {
System.out.println("Winner : " + player1);
player1.win();
player2.lose();
} else if (nextHand2.isStrongerThan(nextHand1)) {
System.out.println("Winner : " + player2);
player1.lose();
player2.win();
} else {
System.out.println("Even....");
}
}
System.out.println("Total result : ");
System.out.println(" " + player1);
System.out.println(" " + player2);
}
}




덧글