策略模式实战 - 猜拳游戏-测试类

时间:2024-12-10 15:36:40
/**
 * Java小卷带你轻松高效学编程,一对一辅导加q1372569394
 */
package com.juan.java.designpattern.strategy;

import ...

/**
 *
 * @author Java小卷
 * @date 2024-12-04 14:46
 * @since 1.0
 */
public class Main {

    public static void main(String[] args) {
        Player player1 = new Player("糖宝", new WinningStrategy());
        Player player2 = new Player("小卷", new SmartStrategy());
        // 比赛10轮
        for (int i = 0; i < 10; i++) {
            Hand hand1 = player1.nextHand();
            Hand hand2 = player2.nextHand();
            if (hand1.isStrongThan(hand2)) {
                System.out.println("胜者: " + player1);
                player1.win(hand2);
                player2.lose(hand1);
            } else if (hand2.isStrongThan(hand1)) {
                System.out.println("胜者: " + player2);
                player2.win(hand1);
                player1.lose(hand2);
            } else {
                System.out.println("打平...");
                player1.even(hand2);
                player2.even(hand1);
            }
        }

        System.out.println("最终结果:");
        System.out.println(player1);
        System.out.println(player2);
    }

}

程序输出:

在这里插入图片描述