2017-03-05 4 views
0

Ethan Brown의 'Learning JavaScript'라는 책의 도움으로 개발 한 게임의 결과를 이해하는 데 어려움이 있습니다. 나는 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(); 

: 여기

는 코드입니다. 예를 들어, 세 가지 결과 중 두 개가 인 기말 금액보다 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으로 설정하면 나중에 100을 얻으려고 두 펀치가 50이 추가됩니다.

자금이 funds = funds - totalBet 인 행을 제거하면 예상되는 150 개를 얻게됩니다.

주사위를 굴린 후에 해당 줄을 이동하고 아무것도 얻지 못한 경우에만 실행하십시오.

+0

좋은 눈, 래리! 나는 그 줄을 제거하고 _ 밑줄에 추가했다. // winnings_ for 루프 문을 찾는다. 'if (! wins) {funds = funds - totalBet; }' 통계 확률에 따라 효과가있는 것 같습니다. 고맙습니다 –