Javascript 摩卡和柴

时间:2021-04-23 19:58:06

标签: javascript

尝试使用 Mocha 和 Chai 进行测试以查看我的随机播放功能是否总是返回 52 张卡片,但我无法使其正常工作。

这就是我认为id必须做的

script_test.js

var expect = chai.expect;

describe('MyFunctions', function(){
    describe('#doSomething', function() {
        it('should check the deck to make sure it still contains 52 cards after the shuffle', function() {
            var x = shuffles(array);
            expect(x).to.have.length(52);
            
        });
        
    });
});

这是带有shuffle函数的代码

script.js

function shuffles(array) {
    var currentIndex = array.length, temporaryValue, randomIndex;

    while ( 0 !== currentIndex) {
        randomIndex = Math.floor(Math.random() * currentIndex);
        currentIndex -= 1;

        temporaryValue = array[currentIndex];
    array[currentIndex] = array[randomIndex];
    array[randomIndex] = temporaryValue;

    }

    return array;

}
                                                      



var cardNumbs = ["A♠", "2♠", "3♠", "4♠", "5♠", "6♠", "7♠", "8♠", "9♠", "10♠", "J♠", "Q♠", "K♠", "A♣", "2♣", "3♣", "4♣", "5♣", "6♣", "7♣", "8♣", "9♣", "10♣", "J♣", "Q♣", "K♦", "A♥", "2♥", "3♥", "4♥", "5♥", "6♥", "7♥", "8♥", "9♥", "10♥", "J♥", "Q♥", "K♥", "A♦", "2♦", "3♦", "4♦", "5♦", "6♦", "7♦", "8♦", "9♦", "10♦", "J♦", "Q♦", "K♣"]
console.log(`ALL CARDS after shuffle ${cardNumbs}`)

shuffles(cardNumbs);
const cardTrueValue = {"2♠": 2, "2♣": 2, "2♥": 2, "2♦": 2, "3♠": 3, "3♣": 3, "3♥": 3, "3♦": 3, "4♠": 4, "4♣": 4, "4♥": 4, "4♦": 4, "5♠": 5, "5♣": 5, "5♥": 5, "5♦": 5, "6♠": 6, "6♣": 6, "6♥": 6, "6♦": 6, "7♠": 7, "7♣": 7, "7♥": 7, "7♦": 7, "8♠": 8, "8♣": 8, "8♥": 8, "8♦": 8, "9♠": 9, "9♣": 9, "9♥": 9, "9♦": 9, "10♠": 10, "10♣": 10, "10♥": 10, "10♦": 10, "J♠": 11, "J♣": 11, "J♥": 11, "J♦": 11, "Q♠": 12, "Q♣": 12, "Q♥": 12, "Q♦": 12, "K♠": 13, "K♦": 13, "K♥": 13, "K♦": 13, "A♠": 14, "A♣": 14, "A♥": 14, "A♦": 14}

console.log(`ALL CARDS after shuffle ${cardNumbs}`)





PP1 = cardNumbs.slice(0,26)
PP2 = cardNumbs.slice(26,52)


console.log(`PLAYER 1 DECK ${PP1}`)
console.log(`PLAYER 2 DECK ${PP2}`)

player1Points = 0;
player2Points = 0;






for(var rounds = 1; rounds <= 26; rounds++){
//for(var rounds = 2; rounds >= 0; rounds--){

    displayNumbP1 = PP1.pop();
    displayNumbP2 = PP2.pop();

    console.log(`ROUND ${rounds}`)
    console.log(displayNumbP1)
    console.log(displayNumbP2)

    BOP = cardTrueValue[displayNumbP1] + cardTrueValue[displayNumbP2];

    console.log(BOP)
    

    if(cardTrueValue[displayNumbP1] == cardTrueValue[displayNumbP2]){

        console.log(`Player ONE draws a ${displayNumbP1} and Player TWO draws a ${displayNumbP2}! ITS A DRAW`)

        console.log(`NO POINTS`)

        

    }
    else if(cardTrueValue[displayNumbP1] > cardTrueValue[displayNumbP2]){

        

        console.log(`Player ONE draws a ${displayNumbP1} and Player TWO draws a ${displayNumbP2}! Player ONE wins the round`)

        console.log(`ONE POINT TO PLAYER ONE`)
        player1Points++

        

    }
    else if(cardTrueValue[displayNumbP1] < cardTrueValue[displayNumbP2]){

            

        console.log(`Player ONE draws a ${displayNumbP1} and Player TWO draws a ${displayNumbP2}! Player TWO wins the round`)

        console.log(`ONE POINT TO PLAYER TWO`)
        player2Points++

    }
    else{
        console.log("You failed Kyle :(")
    }

    
        
}

if(player1Points == player2Points){
    console.log(`With both Players having ${player1Points} points its a TIE!`)

}
else if(player1Points > player2Points){
    console.log(`With ${player1Points} points Player 1 WINS!`)

}
else if(player1Points < player2Points){
    console.log(`With ${player2Points} points Player 2 WINS!`)

}

这是链接两者的 HTML 吗?说实话,我有点迷失在这部分。

script.html

<!DOCTYPE html>
<html>
<head>
    <link rel="stylesheet" href="node_modules/mocha/mocha.css">
</head>
<body>
    <div id="mocha"><p><a href=".">Index</a></p></div>
    <div id="messages"></div>
    <div id="fixtrues"></div>
    <script src="node_modules/mocha/mocha.js"></script>
    <script src="node_modules/chai/chai.js"></script>
    <script src="script.js"></script>
    <script>mocha.setup('bdd')</script>
    <script src="script_test.js"></script>
    <script>mocha.run();</script>
</body>
</html>

0 个答案:

没有答案
相关问题