Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/fsharp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 摇滚乐_Javascript - Fatal编程技术网

Javascript 摇滚乐

Javascript 摇滚乐,javascript,Javascript,我已经看过很多关于石头、布、剪刀的帖子,但我仍然找不到一个明确的解决办法。我是一个比较新的人,只是想完全理解我在做什么 我用了一些不同的方法来反复讨论这个问题。因此,我担心代码结构中存在不一致 我现在大部分时间都在更新记分板,并试图从DOM更新playerChoice。任何帮助都将不胜感激。谢谢大家 const player = { currentChoice: null, playerScore: 0 } const computer = { currentChoic

我已经看过很多关于石头、布、剪刀的帖子,但我仍然找不到一个明确的解决办法。我是一个比较新的人,只是想完全理解我在做什么

我用了一些不同的方法来反复讨论这个问题。因此,我担心代码结构中存在不一致

我现在大部分时间都在更新记分板,并试图从DOM更新playerChoice。任何帮助都将不胜感激。谢谢大家

const player = {
    currentChoice: null,
    playerScore: 0
}

const computer = {
    currentChoice: null,
    computerScore: 0
}

const choices = ["Lapis", "Papyrus", "Scalpellus"];
player.currentChoice = choices[0]

function computerChooses() {
    const randomIndex = Math.floor(Math.random() * choices.length);
    computer.currentChoice = choices[randomIndex]
}

//using dot-method to access the array

function compareChoices() {
    computerChooses();
    if (computer.currentChoice === player.currentChoice) {
        postResult("It's a tie! Both players chose " + player.currentChoice);

        //conditionals computer against player
    } else if (computer.currentChoice === choices[0]) {
        if (player.currentChoice === choices[1]) {
            postResult("The Player wins.");
            player.playerScore += 1
        } else {
            postResult("The Computer wins.");
            computer.computerScore += 1
        }
    } else if (computer.currentChoice === choices[1]) {
        if (player.currentChoice === choices[2]) {
            postResult("The Player Wins")
            player.playerScore += 1
        } else {
            postResult("The Computer wins.");
            computer.computerScore += 1
        }
    } else if (computer.currentChoice === choices[2]) {
        if (player.currentChoice === choices[0]) {
            postResult("The Player wins.");
            player.playerScore += 1
        } else {
            postResult("The Computer wins.")
            computer.computerScore += 1
        }
    }
}

function postResult(results) {
    const resultText = document.createElement('p');
    resultText.innerText = results;
    document.body.appendChild(resultText);
}

compareChoices();

function weaponSelect() {
    document.getElementById
}
document.getElementById('button').addEventListener('click', buttonSelect)
buttonSelect();

从你的例子中,我冒昧地做了一些改变,创建了一个函数来保存核心游戏,然后调用它玩10次并显示结果

这个想法是将游戏的使用与游戏的实现分开,因为游戏不应该关注玩家赢了多少次,游戏应该只关注游戏本身的运行,这是编程的最基本原则与DRY(不要重复你自己)

我从实现开始,使用包装所有游戏应该做的

  • 将保存3个变量
  • 将能够自动选择一个选项
  • 还是要一个吧
  • 通过2个选项,将决定胜利者
然后我开始使用它运行
10次
并计算每次的赢家。。。完全独立于游戏本身

我已经对代码进行了注释,如果有问题请告诉我

//游戏
类RpsGame{
构造函数(){
this.options=[“石头”、“布”、“剪刀”];
[this.ROCK,this.PAPER,this.剪刀]=this.options;
}
//将要求播放机手动输入
AskPayerSelection=()=>{
var s=null;
while(s==null){
s=提示(`你想玩什么?按1:${ROCK}2:${PAPER}3:${SCISSORS}`);
试试{
s=parseInt(s,10);//转换为整数
如果(!Number.isInteger | | s<1&&s>3)s=null;//必须有效
}
catch(err){s=null;}//如果无效,则将其设置为null,以便再次请求
}
返回此选项。选项[s-1];
}
//自动分配选项
getAutoPlayerSelection=()=>{
var s=Math.floor(Math.random()*2);//介于0和2之间
返回此选项。选项[s];
}
//计算玩家1和玩家2之间的赢家
calculateWinner=(播放器1、播放器2)=>{
//同样的
如果(player1===player2)返回'DRAW';
//p1有岩石
else if(player1==这个.ROCK){
如果(player2==this.PAPER)返回'player2';
如果(player2==this.SCISSORS)返回'player1';
}
//p1有纸
else if(player1==此.PAPER){
如果(player2===this.ROCK)返回'player1';
如果(player2==this.SCISSORS)返回'player2';
}
//p1有剪刀
否则{
如果(player2==this.PAPER)返回'player1';
如果(player2===this.ROCK)返回'player2';
}
}
}
// #############################################################################################
//让我们启动游戏引擎
var engine=new RpsGame();
变量选择={rounds:[],player1:0,player2:0};
//让我们玩10次

对于(var i=1;我你的具体问题是什么?我如何将分数更新到记分板。我应该使用DOM在单击时更新球员分数,我只是有点迷路。我这里的内容似乎不起作用,我确信我已经交叉了一些东西,代码可能不一致。所以我只是在寻找一个解决方案有点清楚。你不应该使用
+=1
,因为无论结果如何,它总是
1
,如果你反复玩,在循环中求和,而不是在一个只应该返回赢家的函数中…一个函数应该只做一件事,这样你就可以很容易地重复使用它。。。