十、Strategy 策略模式

需求:使用不同的算法解决相同的问题

设计原理:

代码清单:

接口 Strategy

public interface Strategy {
    public abstract Hand nextHand();
    public abstract void study(boolean win);
}

WinningStrategy

public class WinningStrategy implements Strategy{
    private Random random;
    private boolean won  = false;
    private Hand prevHand;

    public WinningStrategy(){
        random = new Random();
    }

    @Override
    public Hand nextHand() {
        if(!won){
            prevHand = Hand.getHand(random.nextInt(3));
        }
        return prevHand;
    }

    @Override
    public void study(boolean win) {
        won = win;
    }
}

ProbStrategy

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(){
       random = new Random();
    }

    @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]++;
        }
    }
}

Hand:

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_CHO),
            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 isStrongThan(Hand h){
        return fight(h) == 1;
    }

    public boolean isWeakerThan(Hand h){
        return fight(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];
    }
}

Player

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 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++;
    }

    @Override
    public String toString() {
        return "玩家: {" +
                "姓名='" + name + '\'' +
                ", 赢=" + wincount +
                ", 输=" + losecount +
                ", 平局=" + (gamecount-wincount-losecount )+
                ", 总局数=" + gamecount +
                '}';
    }
}

测试类:

public class Main {
    public static void main(String[] args){

        Player player1 = new Player("zhangsan",new WinningStrategy());
        Player player2 = new Player("wangwu",new ProbStrategy());
        for(int i=0;i<10000;i++){
            Hand nextHand1 = player1.nextHand();
            Hand nextHand2 = player2.nextHand();
            if(nextHand1.isStrongThan(nextHand2)){
                System.out.println("赢家:"+player1);
                player1.win();
                player2.lose();
            }else if(nextHand2.isStrongThan(nextHand1)){
                System.out.println("赢家:"+player2);
                player1.lose();
                player2.win();
            }else {
                System.out.println("EvEN...");
                player1.even();
                player2.even();
            }
        }
        System.out.println("合计:");
        System.out.println(player1.toString());
        System.out.println(player2.toString());
    }
}

结果:

 

猜你喜欢

转载自www.cnblogs.com/baizhuang/p/10470548.html