JSFiddle - React, Tailwind, and code Playground
by skwjdgn
JavaScript
//m 이상 n 이하의 무작위 정수를 반환합니다.
function rand(m ,n){
return m + Math.floor((n - m + 1) * Math.random());
}
//크라운 앤 앵커 게임의 여섯 그림 중 하나에 해당하는 문자열을 무작위로 반환합니다.
function randFace(){
return ["crown", "anchor", "heart", "spade", "club", "diamond"]
[rand(0, 5)];
}
let game = {
doBet : function(){
return rand(1, funds);
},
remainingBet : function(totalBet){
if(totalBet === 7){
totalBet = funds;
bets.heart = totalBet;
}else{
let remaining = totalBet;
do{
let bet = rand(1, remaining);
let face = randFace();
bets[face] = bets[face] + bet;
remaining = remaining - bet;
}while(remaining > 0)
}
funds = funds - totalBet;
console.log("\t bets: " + Object.keys(bets).map(face => `${face}: ${bets[face]} pence`).join(', ') + ` (total: ${totalBet} pence)`);
return funds;
},
rollDice : function(){
const hand = [];
for(let roll = 0; roll < 3; roll++){
hand.push(randFace());
}
console.log(`\t hand: ${hand.join(', ')}`);
return hand;
},
makeMoney : function(hand){
let winnings = 0;
for(let die = 0; die < hand.length; die++){
let face = hand[die];
if(bets[face] > 0) winnings = winnings + bets[face];
}
funds = funds + winnings;
console.log(`\t winnings: ${winnings}`);
return funds;
}
}
function doGame(funds){
let round = 0;
while(funds > 0 && funds < 100){
round++;
console.log(`rond ${round}:`);
console.log(`\t starting funds : ${funds}p`);
//돈을 겁니다.
let totalBet = game.doBet();
//돈을 나눕니다.
funds = game.remainingBet();
//주사위를 굴립니다.
const hand = game.rollDice();
//딴 돈을 가져옵니다.
funds = game.makeMoney(hand);
console.log("=====================================");
}
console.log(`\t ending funds: ${funds}`);
}
let bets = { crown : 0, anchor: 0, heart: 0,...