JavaScript:我的switch语句始终默认为第一种情况

时间:2018-03-30 15:04:42

标签: javascript switch-statement code.org

我正在制作一个二十一点游戏,有助于在javascript中计算卡片。其中一个功能是更新计数。为此,我设置了一个switch语句,如果该卡是2到6,则将计数增加1,如果该卡是jack,king,queen或ace,则将其递增1。我已多次查看我的代码,无法弄清楚出了什么问题。无论变量dcac等于什么,它总是默认为switch语句中的第一种情况。我甚至尝试设置ac = "three",它仍然默认为第一种情况。有人可以弄清楚出了什么问题吗?谢谢!

只是更新计数功能:

//Update the count
function updateCount(){
  var dc;
  var pc;
  var dealerCount = 0;
  var playerCount = 0;
  
  for(var d = 0; d < dealerCards.length; d += 1){
    
    dc = dealerCards[d].name; //String
    console.log("DC: " + dc);
    switch(dc){
      case "two":
        dealerCount += 1;
        console.log("d2");
        break;
      case "three":
        dealerCount += 1;
        console.log("d3");
        break;
      case "four":
        dealerCount += 1;
        console.log("d4");
        break;
      case "five":
        dealerCount += 1;
        console.log("d5");
        break;
      case "six":
        dealerCount += 1;
        console.log("d6");
        break;
      case "jack":
        dealerCount -= 1;
        console.log("dj");
        break;
      case "queen":
        dealerCount -= 1;
        console.log("dq");
        break;
      case "king":
        dealerCount -= 1;
        console.log("dk");
        break;
      case "ace":
        dealerCount -= 1;
        console.log("da");
        break;
      default:
        dealerCount += 0;
        
    }
  }
  
  for(var p = 0; p < playerCards.length; p += 1){
    pc = playerCards[p].name; //String
    console.log("PC: " + pc);
    switch(pc){
      case "two":
        playerCount += 1;
        console.log("p2");
        break;
      case "three":
        playerCount += 1;
        console.log("p3");
        break;
      case "four":
        playerCount += 1;
        console.log("p4");
        break;
      case "five":
        playerCount += 1;
        console.log("p5");
        break;
      case "six":
        playerCount += 1;
        console.log("p6");
        break;
      case "jack":
        playerCount -= 1;
        console.log("pj");
        break;
      case "queen":
        playerCount -= 1;
        console.log("pq");
        break;
      case "king":
        playerCount -= 1;
        console.log("pk");
        break;
      case "ace":
        playerCount -= 1;
        console.log("pa");
        break;
      default:
        playerCount += 0;
        
    }
  }
  //count = Count + Count
  //setText("countLbl", "Count: " + (playerCount + dealerCount));
}
var dealerCards = [];

控制台输出:

  

DC = ace

     

     

DC = 9

     

D2

     

PC = 3

     

P2

     

PC = 3

     

P2

我的整个程序(注意:它不会在堆栈溢出中运行,因为它是在code.org的应用实验室中编写的,我被强制用于我的课程)

/* TODO:
 * 1) Make play again button
 * 2) Make reset button
 * 3) Make count work
 * 4) Make hide/show count button
 * 5) Make first dealer card hidden
 * 6) Check for blackjack on first deal
 * 7) Make Ace = 1 if total is greater than 21
 * 8) Make deck re-shuffle upon reset or cards run out
*/







//var Count = 0;
var playerCards = [];
var dealerCards = new Array();
var deckArr = new Array();



//Stores the properties each card can have
var cardProp = {
  suit: ['heart', 'spade', 'club', 'diamond'],
  value: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 11, 11],
  name: ['ace', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten', 'jack', 'queen', 'king']
};

/*--------------------------------------------------------------------------------------------*/
console.log(playerCards.length);
populateDeck();
//console.log(deckArr.length);
//printDeck();

//deckArr = shuffleDeck(deckArr).slice(0);
printDeck();
console.log("----------------------------");

//STAND BUTTON
onEvent("standBtn", "click", function(){
  //Check player for blackjack
  if(getPlayerTotal() > 21){
    showElement("winLoseLbl");
    setText("winLoseLbl", "BUST! \n Dealer Wins!");
  } else {
    //Deal until dealer total is greater than 17
    //then check for win
    while(getDealerTotal() < 17){
      dealerHit();
    }
    
    //Check for dealer or player win
    if(getDealerTotal() < getPlayerTotal() || getDealerTotal() > 21){
      showElement("winLoseLbl");
      setText("winLoseLbl", "Player Wins!!");
    } else {
      showElement("winLoseLbl");
      setText("winLoseLbl", "Dealer Wins!!");
    }
  }
});



//START BUTTON
onEvent("startBtn", "click", function(){
  hideElement("startBtn");
  dealPlayerCards();
  dealDealerCards();
  updateCount();
  //TEMP Hide cards used for hit button
  hideElement("dealerCard3");
  hideElement("dealerCard4");
  hideElement("playerCard3");
  hideElement("playerCard4");
  
});

//DEAL BUTTON
//deal 2 cards to the player and 2 to the dealer
onEvent("dealBtn", "click", function(){
  //Deal player cards
  for(var c = 0; c < 2; c += 1){

    console.log("Deck Arr Length: " + deckArr.length);
    playerCards[c] = drawCard();
    console.log("Deck Arr Length: " + deckArr.length);
    displayCard(playerCards[c], "playerCard" + (c + 1));
    console.log(playerCards[c]);
    

  }
});


//HIT BUTTON

onEvent("hitBtn", "click", function(){
  //Draws a card then displays it in the first hidden card
  //slot if not already displayed, otherwise displays
  //in the second hidden card slot
  if(getProperty("playerCard3","hidden") === true){
    playerCards[2] = drawCard();
    showElement("playerCard3");
    displayCard(playerCards[2], "playerCard" + 3);
  } else if(getProperty("playerCard3", "hidden") === false && getProperty("playerCard4", "hidden") === true){
    playerCards[3] = drawCard();
    showElement("playerCard4");
    displayCard(playerCards[3], "playerCard" + 4);
  }
  updateCount();

  
});




/*--------------------------------------------------------------------------------------------*/

function dealerHit(){
  if(getProperty("dealerCard3","hidden") === true){
    dealerCards[2] = drawCard();
    showElement("dealerCard3");
    displayCard(dealerCards[2], "dealerCard" + 3);
  } else if(getProperty("dealerCard3", "hidden") === false && getProperty("dealerCard4", "hidden") === true){
    dealerCards[3] = drawCard();
    showElement("dealerCard4");
    displayCard(dealerCards[3], "dealerCard" + 4);
  }
}


function getDealerTotal(){
  var total = 0;
  for(var x = 0; x < dealerCards.length; x += 1){
    total += dealerCards[x].value;
  }
  return total;
}



function getPlayerTotal(){
  var total = 0;
  for(var x = 0; x < playerCards.length; x += 1){
    total += playerCards[x].value;
  }
  console.log(total);
  return total;
}



function dealPlayerCards(){
    for(var c = 0; c < 2; c += 1){

      //console.log("Deck Arr Length: " + deckArr.length);
      playerCards[c] = drawCard();
      //console.log("Deck Arr Length: " + deckArr.length);
      displayCard(playerCards[c], "playerCard" + (c + 1));
     // console.log(playerCards[c]);


  }
}


function dealDealerCards(){
    for(var c = 0; c < 2; c += 1){

      //console.log("Deck Arr Length: " + deckArr.length);
      dealerCards[c] = drawCard();
      //console.log("Deck Arr Length: " + deckArr.length);
      displayCard(dealerCards[c], "dealerCard" + (c + 1));
      //console.log(dealerCards[c]);
     

  }
}


//Checks the deltCards array to see if a card was delt




//Draws a random card from the deck
function drawCard(){
  var randomNum = randomNumber(0, deckArr.length - 1);
  var returnCard;
  //console.log("Random Num " + randomNum);
  
  returnCard = deckArr[randomNum];
  deckArr.splice(randomNum, 1);
  //printDeck();
  return returnCard;
}


//Displays the input card in the inputted position
function displayCard(card, position){
  if(card !== undefined){
    getKeyValue(card.name + "_" + card.suit, function(image){
      setProperty(position, "image", image);
    });
  } else {
    console.log("ERROR: NAME undefined");
    write("UNDEFINED");
  }
}



//Prints the deck for debugging
function printDeck(){
  for(var x = 0; x < deckArr.length; x += 1){
    console.log("#: " + x + " S: " + deckArr[x].suit + " V: " + deckArr[x].value + " N: " + deckArr[x].name);
  }
}


//Sets each index of deckArr to
//a new object with the PROPERTIES: Value, Name, Suit, Image
function populateDeck(){

  var count = 0;
  
  for(var s = 0; s < cardProp.suit.length; s += 1)
    for(var v = 0; v < cardProp.value.length; v += 1){
      //Create new object 
      //ready to store properties in
      deckArr[count] = {};
      
      //Set Properties to object created above
      deckArr[count].value = cardProp.value[v];
      deckArr[count].name = cardProp.name[v];
      deckArr[count].suit = cardProp.suit[s];

      count += 1;
    }
}




function shuffleDeck(deck){
  var newDeck = new Array(deck.length); 
  var randomNum = randomNumber(0, deck.length - 1);
  
  
  for(var c = deck.length - 1; c >= 0; c -= 1){
    while(newDeck[randomNum] !== undefined){ //need to fix
      randomNum = randomNumber((0, deck.length - 1));
    }
    newDeck[randomNum] = deck[c];
    randomNum = randomNumber(0, deck.length - 1);
  }
  return newDeck;
  
}


//Checks for a win in the player or the dealer
function checkWin(){
  for(var x = 0; x < playerCards.length; x += 1){
    
  }
}

//Update the count
function updateCount(){
  var dc;
  var pc;
  var dealerCount = 0;
  var playerCount = 0;
  
  for(var d = 0; d < dealerCards.length; d += 1){
    
    dc = dealerCards[d].name;
    console.log("DC: " + dc);
    switch(dc.valueOf()){
      case "two":
        dealerCount += 1;
        console.log("d2");
        break;
      case "three":
        dealerCount += 1;
        console.log("d3");
        break;
      case "four":
        dealerCount += 1;
        console.log("d4");
        break;
      case "five":
        dealerCount += 1;
        console.log("d5");
        break;
      case "six":
        dealerCount += 1;
        console.log("d6");
        break;
      case "jack":
        dealerCount -= 1;
        console.log("dj");
        break;
      case "queen":
        dealerCount -= 1;
        console.log("dq");
        break;
      case "king":
        dealerCount -= 1;
        console.log("dk");
        break;
      case "ace":
        dealerCount -= 1;
        console.log("da");
        break;
      default:
      dealerCount += 0;
        
    }
  }
  
  for(var p = 0; p < playerCards.length; p += 1){
    pc = "three";
    console.log("PC: " + pc);
    switch(pc.valueOf){
      case "two":
        playerCount += 1;
        console.log("p2");
        break;
      case "three":
        playerCount += 1;
        console.log("p3");
        break;
      case "four":
        playerCount += 1;
        console.log("p4");
        break;
      case "five":
        playerCount += 1;
        console.log("p5");
        break;
      case "six":
        playerCount += 1;
        console.log("p6");
        break;
      case "jack":
        playerCount -= 1;
        console.log("pj");
        break;
      case "queen":
        playerCount -= 1;
        console.log("pq");
        break;
      case "king":
        playerCount -= 1;
        console.log("pk");
        break;
      case "ace":
        playerCount -= 1;
        console.log("pa");
        break;
        default:
        playerCount += 0;
        
    }
  }
  //count = Count + Count
  setText("countLbl", "Count: " + (playerCount + dealerCount));
}

1 个答案:

答案 0 :(得分:-1)

dealerCards=[{name:'ace'},{name:'two'}]
playerCards=[{name:'three'},{name:'three'}]
updateCount();

//Update the count
function updateCount(){
  var dc;
  var pc;
  var dealerCount = 0;
  var playerCount = 0;
  
  for(var d = 0; d < dealerCards.length; d += 1){
    
    dc = dealerCards[d].name; //String
    console.log("DC: " + dc);
    switch(dc.valueOf()){
      case "two":
        dealerCount += 1;
        console.log("d2");
        break;
      case "three":
        dealerCount += 1;
        console.log("d3");
        break;
      case "four":
        dealerCount += 1;
        console.log("d4");
        break;
      case "five":
        dealerCount += 1;
        console.log("d5");
        break;
      case "six":
        dealerCount += 1;
        console.log("d6");
        break;
      case "jack":
        dealerCount -= 1;
        console.log("dj");
        break;
      case "queen":
        dealerCount -= 1;
        console.log("dq");
        break;
      case "king":
        dealerCount -= 1;
        console.log("dk");
        break;
      case "ace":
        dealerCount -= 1;
        console.log("da");
        break;
      default:
        dealerCount += 0;
        
    }
  }
  
  for(var p = 0; p < playerCards.length; p += 1){
    pc = playerCards[p].name; //String
    console.log("PC: " + pc);
    switch(pc.valueOf()){
      case "two":
        playerCount += 1;
        console.log("p2");
        break;
      case "three":
        playerCount += 1;
        console.log("p3");
        break;
      case "four":
        playerCount += 1;
        console.log("p4");
        break;
      case "five":
        playerCount += 1;
        console.log("p5");
        break;
      case "six":
        playerCount += 1;
        console.log("p6");
        break;
      case "jack":
        playerCount -= 1;
        console.log("pj");
        break;
      case "queen":
        playerCount -= 1;
        console.log("pq");
        break;
      case "king":
        playerCount -= 1;
        console.log("pk");
        break;
      case "ace":
        playerCount -= 1;
        console.log("pa");
        break;
      default:
        playerCount += 0;
        
    }
  }
  //count = Count + Count
 // setText("countLbl", "Count: " + (playerCount + dealerCount));
}