2017-03-05 6 views
0

Ethan Brownの書籍「Learning JavaScript」の助けを借りて、私が開発したゲームの結果を理解することができません。 I 7は容易に最終的な結果を監視することがあることがtotalBet可変ハードコードされたJSゲームクラウンとアンカー:結果のエラー

//helper functions for randomizing 
function rand(x,y){ 
    return x + Math.floor((y-x+1)*Math.random()); 
} 

function getFace(){ return ['crown','heart','spade','club','diamond','anchor'][rand(0,5)]; } 

//the game 
function crownsAndAnchors(){ 
    console.log('Let\'s play Crowns and Anchors!'); 
    let rounds = 0; 
    let funds = 50; 
    while(funds > 1 && funds < 100){ 
    rounds ++; 
    console.log(`Round: ${rounds}`); 
    let totalBet = 7; 
    let bets = { crown: 0, heart: 0, spade:0, club:0, diamond:0, anchor:0 }; 
    if (totalBet == 7){ 
     totalBet = funds; 
     console.log('You pulled out 7p, your lucky number! Bet all on heart'); 
     bets.heart = totalBet; 
    }else{ 
     //distribute totalBet randomly 
    } 
    funds = funds - totalBet; 
    console.log('\tBets: ' + Object.keys(bets).map(face => `${face} ${bets[face]}p`).join(' || ') + ` (total: ${totalBet} pence)`); 
    const hand = []; 
    // roll the dice 
    console.log("_______________rolling the dice ____________") 
    for (let i = 0; i < 3; i++) { 
     hand.push(getFace()); 
    } 
    console.log(`\tHand: ${hand.join(', ')}`); 
    //check for winnings 
    let wins = 0; 
    for (let i = 0; i < hand.length; i++) { 
     let face = hand[i]; 
     if (bets[face] > 0) wins = wins + bets[face]; 
    } 
    funds = funds + wins; 
    console.log(`\tWinnings: ${wins}`); 
    } 
    console.log(`\nEnding Funds: ${funds}`); 
} 

crownsAndAnchors(); 

:ここ

コードです。たとえば、3つの死亡転帰のうち2つがであるべきである場合、heartである場合、正しいですか?私は(ノードv7.6.0)のコードを実行すると

しかし、これは私が返されていますものです:

Let's play Crowns and Anchors! 

Round: 1 
You pulled out 7p, your lucky number! Bet all on heart 
     Bets: crown: 0p || heart: 50p || spade: 0p || club: 0p || diamond: 0p || anchor: 0p (total: 50 pence) 
_______________rolling the dice ____________ 
     Hand: heart, heart, club 
     Winnings: 100 

Ending funds: 100 

私は何とか間違って、私はちょうどその理由を把握することはできませんfundsを更新しています知っています。

ありがとうございました!

答えて

0

行 funds = funds - totalBet; が資金を0に設定している場合、後で2つの50のウィンが追加されて100が与えられます。

funds = funds - totalBetの行を削除すると、期待している150が得られます。

ダイスロールの後にその行を移動し、何も勝っていない場合にのみ実行してください。

+0

グッドアイ、ラリー!私は先に進み、その行を削除して、// winnings_ forループのブロック文を確認します。 'if(!wins){funds = funds - totalBet; } ' 統計的確率に従って作業しているようですが、今すぐ!ありがとうございました –

関連する問題