想到了个童年小游戏,2个人4只手就能玩,简单用JavaScript实现一下

/**

 * 规则:双方各有左右2个数,初始值为1。每回合,可以将自身的一个数与对方的一个数相加,然后模10。

 * 如,第一回合你操作:你(1 1)机器人(1 1)--> 你(1 2)机器人(1 1)

 * 下回合机器人操作:你(1 2)机器人(1 1)--> 你(1 2)机器人(1 3)

 * 第三回合你操作:你(1 2)机器人(1 3)--> 你(1 5)机器人(1 3)

 * ……

 * 直到有一方的左右2个数都为0,则为胜者,游戏结束。

 */

  

const readline = require('readline');

  

const readl = readline.createInterface({

    input: process.stdin,

    output: process.stdout

});

  

class player {

    constructor(name, left, right) {

        this.name = name

        this.left = left

        this.right = right

        this.finalNumber = 0

        console.log("player:" + this.name + " ~ " + this.left + " " + this.right)

    }

    plusLeft(number) {

        this.left = (this.left + number) % 10

        console.log("player:" + this.name + " ~ " + this.left + " " + this.right)

    }

    plusRight(number) {

        this.right = (this.right + number) % 10

        console.log("player:" + this.name + " ~ " + this.left + " " + this.right)

    }

    isWinner() {

        if (this.left == this.finalNumber && this.right == this.finalNumber) {

            console.log("Winner is " + this.name + "!");

            readl.close();

            return true

        } else {

            return false

        }

    }

    humanThink(opponent) {

        return new Promise((resolve, reject) => {

            readl.question('L + L: 1, L + R: 2, R + L: 3, R + R: 4, 请输入:', (answer) => {

                if (answer == 1) {

                    this.plusLeft(opponent.left)

                    resolve(false)

                } else if (answer == 2) {

                    this.plusLeft(opponent.right)

                    resolve(false)

                } else if (answer == 3) {

                    this.plusRight(opponent.left)

                    resolve(false)

                } else if (answer == 4) {

                    this.plusRight(opponent.right)

                    resolve(false)

                } else {

                    console.log("输入错误,请重新输入!");

                    resolve(true)

                }

            });

        });

    }

    robotThink(opponent) {

        const plus = (a, b) => {

            return (a + b) % 10

        }

        const roll = () => {

            const now = new Date();

            const timestamp = now.getTime();

            console.log(timestamp);

            return timestamp % 2 == 0;

        }

        const ll = plus(this.left, opponent.left)

        const lr = plus(this.left, opponent.right)

        const rl = plus(this.right, opponent.left)

        const rr = plus(this.right, opponent.right)

        if (ll == this.finalNumber) {

            this.plusLeft(opponent.left)

        } else if (lr == this.finalNumber) {

            this.plusLeft(opponent.right)

        } else if (rl == this.finalNumber) {

            this.plusRight(opponent.left)

        } else if (rr == this.finalNumber) {

            this.plusRight(opponent.right)

        } else {

            if (this.left == this.finalNumber) {

                if (roll()) {

                    this.plusRight(opponent.left)

                } else {

                    this.plusRight(opponent.right)

                }

            } else if (this.right == this.finalNumber) {

                if (roll()) {

                    this.plusLeft(opponent.left)

                } else {

                    this.plusLeft(opponent.right)

                }

            } else {

                if (roll()) {

                    if (roll()) {

                        this.plusRight(opponent.left)

                    } else {

                        this.plusRight(opponent.right)

                    }

                } else {

                    if (roll()) {

                        this.plusLeft(opponent.left)

                    } else {

                        this.plusLeft(opponent.right)

                    }

                }

            }

        }

    }

}

  

const player1 = new player("You", 1, 1)

const player2 = new player("The robot", 1, 1)

  

let round = 1

  

const game = async () => {

    console.log("round:", round)

    if (round % 2 == 1) {

        let res = true

        while (res) {

            res = await player1.humanThink(player2)

        }

    } else {

        player2.robotThink(player1)

    }

    if (!player1.isWinner() && !player2.isWinner()) {

        round = round + 1

        game()

    }

}

  

game()
posted @ 2024-12-26 14:45  百事无糖可乐  阅读(46)  评论(0)    收藏  举报