簡體   English   中英

將紙牌圖像添加到紙牌游戲中,而不是使用數組添加文本

[英]Adding card images into a card game rather than text using arrays

我的程序不斷崩潰,這是一個21點游戲(我不確定是否有區別),程序詢問玩家他們想做什么,然后一旦他們選擇了搖桿,它就崩潰了,我檢查了控制台,沒有錯誤出現,並且我已經在repl中測試了javascript,但在我嘗試將其合並到html頁面時,它無法正常工作。

同樣,當它起作用時,它將僅顯示房屋牌或第二張玩家紙牌,而不是所有玩家紙牌。

最終,一旦這個方法生效,我想通過為用戶添加卡片圖像來改善它,但是我不知道如何開始。

<html>
<head>
<h1 align="center">21 Game</h1>
</head>
<body bgcolor="#458B00">

<div align='center'>
    <h3>House Cards</h3>
    <h5 id='housecurrenthand'></h5>
    <h5 id='househand'> </h5>

    <h3>Players Cards</h3>
    <h5 id='playershand1'> </h5>
    <h5 id='playershand2'> </h5>
    <h5 id='playershand3'> </h5>
    <h5 id='playershand4'> </h5>
    <h5 id='playershand5'> </h5>
    <h5 id='playershand6'> </h5>
    <h5 id='playershand7'> </h5>
    <h5 id='playershand8'> </h5>
    <h5 id='playershand9'> </h5>
    <h5 id='playershand10'> </h5>

    <h4 id='winner'> </h4>

    <button onclick="play()" float='bottom'>Play</button>

</div>
<script language='JavaScript'> 

function play() {
  playersQ = prompt('How many people are playing?');
  playershands = [];
  i = 0;
  count = 0;
  var player_cards = 0;

  function player() {
    var player_cards = Math.floor(Math.random() * 21) + 1;
    while (player_cards < 21) {
      Hit = prompt('Players cards are ' + player_cards + ' Would you like stick or twist?(S/T)')
      if (Hit == 'T') {
        player_cards = player_cards + Math.floor(Math.random() * 11) + 1;
      } else if (Hit == 'S') {
        break;
      } else if (player_cards > 21) {
        alert('Player is bust');
        break;
      }
    }
    return player_cards;
  }

  function house() {
    var house_cards = Math.floor(Math.random() * 21) + 1;
    while (house_cards < 18 && house_cards < 22) {
      house_cards = house_cards + Math.floor(Math.random() * 11);
    }
    return house_cards;
  }

  while (i < playersQ) {
    playershands.push(player());
    i = i + 1;
  }

var house_hand = house();



  while (count < playersQ) {
    count2 = count + 1
    document.getElementById('playershand' +i ).innerHTML = ('Player ' + count2 + ' has ' + playershands[count]);
  }

document.getElementById('househand').innerHTML = ('House has ' + house_hand);

count = 0 ;

while (count < playersQ){
    if (playershands[count] > house_hand && playershands[count] < 22) {
      document.getElementById('winner').innerHTML = ('Player wins');
      count = count + 1;
    } else if (house_hand > playershands[count] && house_hand < 22) {
      document.getElementById('winner').innerHTML = ('House wins');
      break;
    } else if (house_hand == playershands[count]) {
      document.getElementById('winner').innerHTML = ('draw');
      break;
    }
  }
}

</script>
</body>
</html>

要抓牌,您可以:

  • 畫在畫布上
  • 告訴卡片“ Ace of heart” A♥等於“ 1A.png”
  • 可能使用SVG生成卡

有一個不錯的開發者!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM