在javascript游戏中打破循环

时间:2016-09-15 19:40:32

标签: javascript

我知道这个可能是一个非常简单的修复,但我没有看到它。我试图在Javascript中制作一个Acey Deucey游戏,但由于某种原因,游戏循环似乎被打破了。游戏的工作方式基本上就是这样,经销商抽两张牌,玩家下注是否经销商抽取的下一张牌是否在前两张牌之间。

请记住,我仍然是Javascript的新手,所以这就是为什么我在努力解决这个问题。

var readline = require('readline');

var rl = readline.createInterface({input: process.stdin, output: process.stdout});

var deck = [];
var dealerPot = 200;
var playerPot = 50;
var playerBet = 0;

function initializeDeck(d) {    
    for(i = 0; i < 52; i++) {
        d[i] = i;
    }
}

function shuffle(d) {
    var l = d.length - 1;
    var toSwap;
    var temp;
    for(i = l; i > 0; i--) {
        toSwap = Math.floor(Math.random()*i);
        temp = d[i];
        d[i] = d[toSwap];
        d[toSwap] = temp;
    }


/* sanity check     
     for(i = 0; i < 52; i++) {
        console.log(d[i]);
    }
*/
}

function between(x, min, max) {

    var temp;
    if(min > max) {
        min = temp;
        max = min;
        temp = max;
    }

    return x >= min && x <= max;
}

function setFace(x) {
    var f = x % 13;

    var faces = ["ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "jack", "queen", "king"];

    switch(f) {
        case 1:
            return faces[0];
        case 2:
            return faces[1];
        case 3:
            return faces[2];    
        case 4:
            return faces[3];
        case 5:
            return faces[4];
        case 6:
            return faces[5];
        case 7:
            return faces[6];
        case 8:
            return faces[7];
        case 9:
            return faces[8];
        case 10:
            return faces[9];
        case 11:
            return faces[10];
        case 12:
            return faces[11];
        case 0:
            return faces[12];

    }

}

function setSuit(x) {
    var suits = [" of hearts", " of diamonds",  " of spades", " of clubs"];
    var s = x / 13; 

    if(s <= 1) {
        return suits[0];
    }
    if(s > 1 && s <= 2) {
        return suits[1];
    }
    if(s > 2 && s <= 3) {
        return suits[2];
    }
    if(s > 3 && s <= 4) {
        return suits[3];
    }
}

function deal(x, y) {
    console.log("Dealer draws " + setFace(x) + setSuit(x) + " and " + setFace(y) + setSuit(y));
}

console.log("Welcome! Enter 1 to continue, 0 to quit");
rl.setPrompt('AceyDeucy> ');
rl.prompt();
rl.on('line', function(line) {
    if (line === "0") rl.close();
    initializeDeck(deck);
    shuffle(deck);
    var i = 0;
    var card1;
    var card2;
    var card3;

     while(playerPot != 0 || dealerPot != 0) {

        card1 = deck[i];
        card2 = deck[i + 1];
        card3 = deck[i + 2];

        console.log("Dealer Pot: " + dealerPot);
        console.log("Player Pot: " + playerPot);

        deal(card1,card2);

        rl.question("Place your bet ", function(playerBet){
            rl.prompt();
            console.log(playerBet);

            console.log("Dealer draws" + setFace(card3) + setSuit(card3));
            if(between(card3, card1, card2) == true) {
                console.log("you win!");
                playerPot += playerBet;
                dealerPot -= playerBet;
            }

            else {
                console.log("you lose!");
                playerPot -= playerBet;
                dealerPot += playerBet;
            }       
        });

        i++;
    }

    rl.question("Enter 1 to play again, 0 to quit", function(number) {
        rl.prompt();
    });

}).on('close',function(){
    console.log("Thanks!");
    process.exit(0);
});

0 个答案:

没有答案