JSFiddle - React, Tailwind, and code Playground
by skwjdgn
HTML
<script src='//rawgit.com/eu81273/jsfiddle-console/master/console.js'></script>
JavaScript
/*
//1.랜덤으로 얼마를 걸지 결정
//2.랜덤으로 어디에 걸지 결정
//3.랜덤으로 어디에 얼마를 걸지 결정
//4.만약에 내가 걸 돈이 7이면 하트에다가 올인
//5.돈이 0 이 되거나 100이 넘으면 게임끝
----------------------------------------------
//input
//founds = 50;
//"crown", "anchor", "heart", "spade", "club", "diamond"
--------------------------------------------------------------
//output
// 몇번째 게임인지
// 얼마를 걸지
// 어디에 얼마를 걸지
// 얼마가 남았는지
// 게임이 종료되고 남은돈
//---------------------------------------------------------------
이 예제를 통해 얻는것
다양한 제어문들을 체험해본다.
내가아닌 다른사람의 코딩스타일을 배워본다.
*/
//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(funds){
return rand(1, funds);
},
remainingBet : function(totalBet){
if(totalBet === 7){
totalBet = funds;
bets.heart = totalBet;
}
if(totalBet !== 7){
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...