2016-07-07 46 views
-1

我對Javascript很陌生,並且對我的某個函數(checkOrBet)有疑問。我正在嘗試做一個德州撲克遊戲,看起來沒有工作的部分是我測試一對普通撲克的地方。它似乎沒有認識到一對已經被製造出來,當這些牌被交給甲板時。撲克遊戲中的檢查,下注或摺疊功能問題(javascript)

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(); 

     } 
+0

檢查,下注,棄牌或雙人牌是否存在問題? – Gaetan

+0

這是與常規對的測試。我試圖讓它在檢測到一對時登錄控制檯,並且每次運行程序時,即使翻牌上沒有配對,它總是會說「配對」。 –

+0

comunityCards在哪裏創建? – Gaetan

回答

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"); 
     } 

同爲第二張牌。

+0

中有一張卡似乎已經是真的非常感謝!你讓我今天一整天都感覺很好!!這是我在stackoverflow上的第一個問題。 –

+0

很高興它可以幫助你。下次嘗試更精確,不要給出源代碼的必要部分。 – Gaetan

+0

非常感謝。我不確定要粘貼多少代碼。你發佈的部分是否足以理解它? –