扑克游戏中的支票,下注或折叠功能问题(javascript)

时间:2016-07-07 11:59:00

标签: javascript

我是Javascript的新手,对我的一个函数(checkOrBet)有疑问。我正在尝试制作德州扑克游戏,而那些似乎无法正常工作的部分是我测试常规对的地方。当牌被发给牌组时,它似乎并没有意识到已经制造了一对牌。

function game() {
    function Playert(name, chips) {
        this.name = name;
        this.chips = chips;
        this.firstCard = [];
        this.secondCard = [];
        this.blind = {};
        // this.turnpos = 0; //1 for current turn 

        //will be computer's response when prompted during the flop
        this.checkOrBet = function checkOrBet() {
            var response;
            //Ace High
            if (this.firstCard.rank == "A") {
                    response = "call";
                    console.log("Ace High!");
            } else if (this.secondCard.rank == "A") {
                    response = "call";
                    console.log("Ace High!");
            }
            //Tests for a pocket pair
            if (this.firstCard.rank == this.secondCard.rank) {
                response = "call";
                console.log("Pocket pairs!");
            }
            //Test for a regular pair 
            if (this.firstCard.rank == communityCards.cards[0].rank || communityCards.cards[1].rank || communityCards.cards[2].rank) {
                response = "call";
                console.log("pairs");

             } else if (this.secondCard.rank == communityCards.cards[
                            0]) {
                 response = "call";
                 console.log("pairs");
             } 

         }
     }

     function Pot(chipsp) {
         this.chipsp = 0;
     }
     //clears text every time start button is pressed 

            function clearBox() {
                document.getElementById("computer").innerHTML = "";
                document.getElementById("flop").innerHTML = "";
                document.getElementById("hand1").innerHTML = "";
            }
            clearBox();
            //players start with chips, low blind, big blind. 
            var computer = new Playert("Computer", 200);
            var player = new Playert("Player1", 200);
            //Need a deck to deal the cards for the players. 
            var deck = new Stack();
            //player's hand
            var phand = new Stack();
            //computer's hand
            var chand = new Stack();
            //game cards 
            var gamehand = new Stack();
            //make pot 
            var pot1 = new Pot();
            //community cards variable
            var communityCards = new Stack();
            deck.makeDeck(1);
            deck.shuffle(1);
            //deal 2 cards to player hand 
            if (phand.cardCount < 2) {
                alert("Not enough cards in deck");
            } else {
                for (i = 0; i < 2; i++) {
                    phand.addCard(deck.deal());
                }
            }
            //grab player div by the ID 
            playerhand = document.getElementById("hand1");
            //create p element to show cards in player div 
            var pdoc = document.createElement("p");
            //create text node to append to p element 
            var ptext = document.createTextNode(phand.cards);
            //append text node to p element 
            pdoc.appendChild(ptext);
            //append p element with cards to the hand1 div 
            playerhand.appendChild(pdoc);
            //deal cards to computer's hand
            if (chand.cardCount < 2) {
                alert("Not enough cards in deck");
            } else {
                for (i = 0; i < 2; i++) {
                    chand.addCard(deck.deal());
                }
            }
            //tie computer's hand to firstCard/secondCard
            computer.firstCard = chand.cards[0];
            computer.secondCard = chand.cards[1];
            console.log(computer.firstCard.rank);
            console.log(computer.secondCard.rank);

            //grab computer div by id 
            d = document.getElementById("computer");
            //create p element to show cards in computer div 
            var cdoc = document.createElement("p");
            //create text node to append to p element 
            var ctext = document.createTextNode(chand.cards.toString());
            //append text node to p element 
            cdoc.appendChild(ctext);
            //append p element to computer div 
            d.appendChild(cdoc);
            //grab game cards div 
            g = document.getElementById("flop");
            //create p element for appending 
            var fdoc = document.createElement("p");
            //create text node to append to paragraph element
            //choose which player has the blind 
            function blinds() {
                var small = 5;
                var big = 10;
                var random = Math.floor(Math.random() * 2) + 1;
                if (random == 1) {
                    player.blind = small;
                    player.chips -= small;
                    computer.blind = big;
                    computer.chips -= big;
                    pot1.chipsp += 15;
                } else if (random == 2) {
                    player.blind = big;
                    player.chips -= big;
                    computer.blind = small;
                    computer.chips -= small;
                    pot1.chipsp += 15;
                }
            }
            blinds();
            //Show chips for player
            var chips = document.getElementById("hand1");
            var chipsp = document.createElement("p");
            var chipsn = document.createTextNode("You have " + player.chips +
                " chips");
            chipsp.appendChild(chipsn);
            chips.appendChild(chipsp);
            //Show chips for the computer 
            var cchips = document.getElementById("computer");
            var pchips = document.createElement("p");
            var chipsnc = document.createTextNode("You have " + computer.chips +
                " chips");
            pchips.appendChild(chipsnc);
            cchips.appendChild(pchips);
            //grab computer hand div for printing out chips 
            function grab(x) {
                    d = document.getElementById(x);
                    return d;
                }
                //grab computer div 
            var cblind = grab("computer");
            //create p element 
            var cpblind = document.createElement("p");
            //create text node 
            var ctblind = document.createTextNode("Computer's blind is" + " " +
                computer.blind);
            //append text to p 
            cpblind.appendChild(ctblind);
            //append p to computer div 
            cblind.appendChild(cpblind);
            //grab hand1 for showing player blind 
            var ghand1 = grab("hand1");
            //create element
            var ghand2 = document.createElement("p");
            //create text node 
            var ghand3 = document.createTextNode("Player's blind is" + " " + player
                .blind);
            //append text to para
            ghand2.appendChild(ghand3);
            //append p to div 
            ghand1.appendChild(ghand2);
            //if computer gets the blind of 5,call no matter what 
            if (computer.blind = 5) {
                computer.chips -= 5;
            }
            //if player is low blind, prompt for call, fold, or bet
            if (player.blind == 5) {
                var cfb = prompt("Would you like to call, fold, or bet?");
                switch (cfb) {
                    case "call":
                        //do something 
                        //draw 3 cards from the deck and append
                        for (i = 0; i < 3; i++) {
                            communityCards.addCard(deck.draw(1));
                        }
                        player.chips -= 5;
                        pot1.chipsp += 5;
                        chipsn.nodeValue = "You have " + player.chips + " chips";
                        player.turnpos = 1; //has acted
                        break;
                    case "fold":
                        //do something
                        computer.chips += pot1.chipsp;
                        break;
                    case "bet":
                        //do something
                        break;
                    default:
                        //do something 
                }
                //start flop round 
                //draw 3 cards from the deck / switch statement for check,fold, bet? 
                //prompt computer and player on what they want to do 
            } else if (player.chips = 10) {
                //draw 3 cards from the deck and append
                for (i = 0; i < 3; i++) {
                    communityCards.addCard(deck.draw(1));
                }
                player.chips -= 5;
                pot1.chipsp += 5;
            }
            //print flop cards to flop div 
            var flopc = document.getElementById("flop");
            var flopcr = document.createElement("p");
            var floptn = document.createTextNode(communityCards.cards.toString());
            flopcr.appendChild(floptn);
            flopc.appendChild(flopcr);
            //print pot to flop div 
            var potf = document.createElement("p");
            var pottn = document.createTextNode("The pot is " + pot1.chipsp);
            potf.appendChild(pottn);
            flopc.appendChild(potf);
            //if player has acted, prompt computer to check, bet or fold
           // if (player.turnpos == 1) {
                //computer evaluates hands
            //}

             //test function
            computer.checkOrBet();

        }

1 个答案:

答案 0 :(得分:0)

坚持下去:

//Test for a regular pair 
        if (this.firstCard.rank == communityCards.cards[0].rank || this.firstCard.rank == communityCards.cards[1].rank || this.firstCard.rank == communityCards.cards[2].rank) {
            response = "call";
            console.log("pairs");

         } else if (this.secondCard.rank == communityCards.cards[
                        0]) {
             response = "call";
             console.log("pairs");
         } 

第二张卡也一样。