javascript - 剪刀石头布游戏 (js) - 如何创建圆形函数

标签 javascript

问题

  • 在 playRound 函数中,我返回一个函数,一旦结果对象等于 5(即 5 轮),该函数就应该停止。然而,它只会玩一个游戏,然后一旦你尝试再次玩;它会重复记录平局。

这是我尝试过的

我只使用了我的convertMoves()函数,而不是在返回函数中同时使用了我的playRound()函数

预期结果

一旦我回答了提示,我希望得到的不是平局,而是有意义的不同结果。它不应该总是“平局”,“平局”,“平局”,而是“赢”,“平局”,“输”,“赢”,“平局”

    var moves, result;

    moves = {
        rock: 0, 
        paper: 1, 
        scissors: 2,
    };

    result = {
        win: 0,
        tie: 0,
        lose: 0
    };

    function convertMoves() {
        playerSelection = prompt('Please choose rock, paper, or scissors').toLowerCase();
        return moves[playerSelection];
    }

    function computerPlay() {
        var movesValues = Object.values(moves);
        var random = Math.floor(Math.random() * movesValues.length);
        return movesValues[random];
    } 

    function playRound(playerSelection, computerSelection) {
        var processResult = (3 + computerSelection - playerSelection) % 3;

        if (!processResult) {
            ++result.tie;
            console.log('tie');
        } else if (1 == processResult) {
            ++result.lose;
            console.log('You lost');
        } else {
            ++result.win;
            console.log('You won');
        }

        return function() {
            var rounds = 0;
            var resultValues = Object.values(result);

            for (var i = 0; i < resultValues.length; i++) {
                rounds += resultValues[i];
            }

            console.log(rounds);
            if (rounds !== 5) {
                convertMoves();
                playRound(playerSelection, computerSelection)();
            }

            return result;
        }
    }

    var computerSelection = computerPlay();
    var playerSelection = convertMoves();

    console.log(playRound(playerSelection, computerSelection)());

最佳答案

playerSelection 只是得到 undefined 因为它的值在闭包函数中设置后没有更新。通过这样做 playerSelection = ConvertMoves(); 它的工作现在被罚款了。希望这能解决您的问题。

var moves, result;

    moves = {
        rock: 0, 
        paper: 1, 
        scissors: 2,
    };

    result = {
        win: 0,
        tie: 0,
        lose: 0
    };

    function convertMoves() {
        playerSelection = prompt('Please choose rock, paper, or scissors').toLowerCase();
        return moves[playerSelection];
    }

    function computerPlay() {
        var movesValues = Object.values(moves);
        var random = Math.floor(Math.random() * movesValues.length);
        return movesValues[random];
    } 

    function playRound(playerSelection, computerSelection) {
    debugger
        var processResult = (3 + computerSelection - playerSelection) % 3;

        if (!processResult) {
            ++result.tie;
            console.log('tie');
        } else if (1 == processResult) {
            ++result.lose;
            console.log('You lost');
        } else {
            ++result.win;
            console.log('You won');
        }

        return function() {
            var rounds = 0;
            var resultValues = Object.values(result);

            for (var i = 0; i < resultValues.length; i++) {
                rounds += resultValues[i];
            }

            console.log(rounds);
            if (rounds !== 5) {
                playerSelection = convertMoves();
                playRound(playerSelection, computerSelection)();
            }

            return result;
        }
    }

    var computerSelection = computerPlay();
    var playerSelection = convertMoves();

    console.log(playRound(playerSelection, computerSelection)());

关于javascript - 剪刀石头布游戏 (js) - 如何创建圆形函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57635440/

相关文章:

javascript - 制作捕鼠器点击链接

Javascript toLocalDateString 在 windows 和 mac 上输出不同的格式

javascript - 如何在 ASP.net (.aspx) 中 5 分钟后显示按钮?

javascript 动态更改图像 src 的位置 - NO JQuery

javascript - 在 AngularJS 中绑定(bind)键盘事件

javascript - 在 Service Worker 中缓存 Amazon S3 文件以供离线使用

javascript - 关于计数的 Stop If 和 Else 语句

javascript - 使用对象

javascript - 如何将数据输入 JavaScript 并使用 JQuery 进行渲染?

javascript - 基于一个单选按钮的不需要的隐藏输入