模拟掷骰子游戏的程序(需要帮助在此代码上添加银行)

时间:2014-04-30 01:31:31

标签: javascript

//掷骰子游戏是两个玩家X和Y玩的骰子游戏。 首先X扔掉这对骰子。    如果骰子的总和是7或11,则X赢得游戏。    如果总和为2,3或12,则Y获胜。    否则,总和被指定为"点,"与另一个折腾相匹配。 因此,如果两名球员都没有赢得第一次投球,那么骰子将被反复投掷直到 无论是点还是7点都会出现。    如果7先出现,Y赢。    否则,当点出现时X会获胜 //下面写的代码是我写的代码,它有垃圾游戏功能的代码,但我被困在我必须创建一个银行的部分和下注金额,能够下注金钱,停止时的金额完成投注。                       //用于测试游戏状态的变量                  var WON = 0,LOST = 1,CONTINUE_ROLLING = 2;

             // other variables used in program`enter code here`
             var firstRoll = true,            // true if first roll
                 sumOfDice = 0,               // sum of the dice
                 myPoint = 0, // point if no win/loss on first roll
                 gameStatus = CONTINUE_ROLLING; // game not over yet

             // process one roll of the dice
             function play()
             {
                if ( firstRoll ) {        // first roll of the dice
                   sumOfDice = rollDice();        

                   switch ( sumOfDice ) {
                      case 7: case 11:         // win on first roll
                         gameStatus = WON;
                         // clear point field
                         document.craps.point.value = ""; 
                         break;
                      case 2: case 3: case 12: // lose on first roll
                         gameStatus = LOST;
                         // clear point field
                         document.craps.point.value = ""; 
                         break;
                      default:                   // remember point
                         gameStatus = CONTINUE_ROLLING;
                         myPoint = sumOfDice;
                         document.craps.point.value = myPoint; 
                         firstRoll = false;
                   }
                }
                else {
                   sumOfDice = rollDice();

                   if ( sumOfDice == myPoint ) // win by making point
                      gameStatus = WON;
                   else
                      if ( sumOfDice == 7 )    // lose by rolling 7
                         gameStatus = LOST;
                }

                if ( gameStatus == CONTINUE_ROLLING )
                   window.status = "Roll again";
                else {
                   if ( gameStatus == WON )
                      window.status = "Player wins. " +
                         "Click Roll Dice to play again.";
                   else 
                      window.status = "Player loses. " + 
                         "Click Roll Dice to play again.";

                   firstRoll = true;
                }
             }

             // roll the dice
             function rollDice()
             {
                var die1, die2, workSum;   

                die1 = Math.floor( 1 + Math.random() * 6 );
                die2 = Math.floor( 1 + Math.random() * 6 );
                workSum = die1 + die2;

                document.craps.firstDie.value = die1;
                document.craps.secondDie.value = die2;
                document.craps.sum.value = workSum;

                return workSum;
             }
             </script>

       </head>
       <body>
          <form name = "craps" action = ""> // assigning rule to it 
             <table border = "1">
             <caption>Craps</caption>
             <tr><td>Die 1</td>
                 <td><input name = "firstDie" type = "text" />
                 </td></tr>
             <tr><td>Die 2</td>
                 <td><input name = "secondDie" type = "text" />
                 </td></tr>
             <tr><td>Sum</td>
                 <td><input name = "sum" type = "text" />
                 </td></tr>
             <tr><td>Point</td>
                 <td><input name = "point" type = "text" />
                 </td></tr>
             <tr><td><input type = "button" value = "Roll Dice" 
                 onclick = "play()" /></td></tr>
             </table>
          </form>        
// So at the end i need to add bank which reduces or add the bank according to the game rule.

0 个答案:

没有答案