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
를 업데이트하고있어 알고있다.
미리 감사드립니다.
좋은 눈, 래리! 나는 그 줄을 제거하고 _ 밑줄에 추가했다. // winnings_ for 루프 문을 찾는다. 'if (! wins) {funds = funds - totalBet; }' 통계 확률에 따라 효과가있는 것 같습니다. 고맙습니다 –