Ch 10 Strategy Pattern Player interface strategy Strategy

  • Slides: 7
Download presentation
Ch. 10 Strategy Pattern Player <<interface>> -strategy Strategy +next. Hand +win +study +lose +even

Ch. 10 Strategy Pattern Player <<interface>> -strategy Strategy +next. Hand +win +study +lose +even Winning. Strategy 장덕성 Prob. Strategy +next. Hand +study 계명대학교 컴퓨터공학과 정보공학실험실

Ch. 10 Strategy Pattern ▌Hand Class public class Hand { public static final int

Ch. 10 Strategy Pattern ▌Hand Class 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 = { // 가위바위보의 손을 나타내는 3개의 인스턴스 new Hand(HANDVALUE_GUU), new Hand(HANDVALUE_CHO), new Hand(HANDVALUE_PAA), }; private static final String[] name = { // 가위바위보의 손의 문자열 표현 "주먹", "가위", "보", }; private int handvalue; // 가위바위보의 손의 값 private Hand(int handvalue) { this. handvalue = handvalue; } public static Hand get. Hand(int handvalue) { // 값으로부터 인스턴스를 얻는다. return hand[handvalue]; } public boolean is. Stronger. Than(Hand h) { // this가 h보다 강할 때true return fight(h) == 1; } public boolean is. Weaker. Than(Hand h) { // this가h보다 약할 때true return fight(h) == -1; } private int fight(Hand h) { // 무승부는 0, this가 이기면 1, h가 이기면-1 if (this == h) { return 0; } else if ((this. handvalue + 1) % 3 == h. handvalue) { return 1; } else { return -1; } } 장덕성 계명대학교 컴퓨터공학과 정보공학실험실

Ch. 10 Strategy Pattern ▌Strategy Interface public interface Strategy { public abstract Hand next.

Ch. 10 Strategy Pattern ▌Strategy Interface public interface Strategy { public abstract Hand next. Hand(); public abstract void study(boolean win); } ▌Winning. Strategy Class import java. util. Random; public class Winning. Strategy implements Strategy { private Random random; private boolean won = false; private Hand prev. Hand; public Winning. Strategy(int seed) { random = new Random(seed); } public Hand next. Hand() { if (!won) { prev. Hand = Hand. get. Hand(random. next. Int(3)); } return prev. Hand; } public void study(boolean win) { won = win; } } 장덕성 계명대학교 컴퓨터공학과 정보공학실험실

Ch. 10 Strategy Pattern ▌Prob. Strategy Class import java. util. Random; public class Prob.

Ch. 10 Strategy Pattern ▌Prob. Strategy Class import java. util. Random; public class Prob. Strategy implements Strategy { private Random random; private int prev. Hand. Value = 0; private int current. Hand. Value = 0; private int[][] history = { { 1, 1, 1, }, }; public Prob. Strategy(int seed) { random = new Random(seed); } public Hand next. Hand() { int bet = random. next. Int(get. Sum(current. Hand. Value)); int handvalue = 0; if (bet < history[current. Hand. Value][0]) { handvalue = 0; } else if (bet < history[current. Hand. Value][0] + history[current. Hand. Value][1]) { handvalue = 1; } else { handvalue = 2; } 장덕성 계명대학교 컴퓨터공학과 정보공학실험실

Ch. 10 Strategy Pattern ▌Prob. Strategy Class(계속) prev. Hand. Value = current. Hand. Value;

Ch. 10 Strategy Pattern ▌Prob. Strategy Class(계속) prev. Hand. Value = current. Hand. Value; current. Hand. Value = handvalue; return Hand. get. Hand(handvalue); } private int get. Sum(int hv) { int sum = 0; for (int i = 0; i < 3; i++) { sum += history[hv][i]; } return sum; } public void study(boolean win) { if (win) { history[prev. Hand. Value][current. Hand. Value]++; } else { history[prev. Hand. Value][(current. Hand. Value + 1) % 3]++; history[prev. Hand. Value][(current. Hand. Value + 2) % 3]++; } } } 장덕성 계명대학교 컴퓨터공학과 정보공학실험실

Ch. 10 Strategy Pattern ▌Player Class public class Player { …… public Player(String name,

Ch. 10 Strategy Pattern ▌Player Class public class Player { …… public Player(String name, Strategy strategy) { this. name = name; this. strategy = strategy; } public Hand next. Hand() { return strategy. next. Hand(); } public void win() { strategy. study(true); wincount++; gamecount++; } public void lose() { strategy. study(false); losecount++; gamecount++; } public void even() { gamecount++; } public String to. String() { return "[" + name + ": " + gamecount + “games, " + wincount + " win, " + losecount + " lose" + "]"; } } 장덕성 계명대학교 컴퓨터공학과 정보공학실험실

Ch. 10 Strategy Pattern ▌Main Class public class Main { public static void main(String[]

Ch. 10 Strategy Pattern ▌Main Class public class Main { public static void main(String[] args) { if (args. length != 2) { // 사용방법 설명 } int seed 1 = Integer. parse. Int(args[0]); int seed 2 = Integer. parse. Int(args[1]); Player player 1 = new Player("홍길동", new Winning. Strategy(seed 1)); Player player 2 = new Player("임꺽정", new Prob. Strategy(seed 2)); for (int i = 0; i < 10000; i++) { Hand next. Hand 1 = player 1. next. Hand(); Hand next. Hand 2 = player 2. next. Hand(); if (next. Hand 1. is. Stronger. Than(next. Hand 2)) { System. out. println("Winner: " + player 1); player 1. win(); player 2. lose(); } else if (next. Hand 2. is. Stronger. Than(next. Hand 1)) { System. out. println("Winner: " + player 2); player 1. lose(); player 2. win(); } else { System. out. println("Even. . . "); player 1. even(); player 2. even(); } } System. out. println("Total result: "); // 결과 출력 } } 장덕성 계명대학교 컴퓨터공학과 정보공학실험실