블랙잭/script.js: 두 판 사이의 차이
< 블랙잭
백괴게임>Riemann 잔글 (I think the problem is resolved) |
백괴게임>Riemann 잔글편집 요약 없음 |
||
6번째 줄: | 6번째 줄: | ||
{{#vardefine:name|p21}} | {{#vardefine:name|p21}} | ||
{{#vardefine:creat|Riemann}} | {{#vardefine:creat|Riemann}} | ||
{{#vardefine:version|0. | {{#vardefine:version|0.79}} | ||
{{#vardefine:descript|블랙잭}} | {{#vardefine:descript|블랙잭}} | ||
{{#vardefine:local|true}} | {{#vardefine:local|true}} | ||
19번째 줄: | 19번째 줄: | ||
0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,0xD9,0xDA,0xDB,0xDD,0xDE]; | 0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,0xD9,0xDA,0xDB,0xDD,0xDE]; | ||
const pointData = {a1:1,a2:2,a3:3,a4:4,a5:5,a6:6,a7:7,a8:8,a9:9,aa:10,ab:10,ad:10,ae:10,b1:1,b2:2,b3:3,b4:4,b5:5,b6:6,b7:7,b8:8,b9:9,ba:10,bb:10,bd:10,be:10,c1:1,c2:2,c3:3,c4:4,c5:5,c6:6,c7:7,c8:8,c9:9,ca:10,cb:10,cd:10,ce:10,d1:1,d2:2,d3:3,d4:4,d5:5,d6:6,d7:7,d8:8,d9:9,da:10,db:10,dd:10,de:10}; | const pointData = {a1:1,a2:2,a3:3,a4:4,a5:5,a6:6,a7:7,a8:8,a9:9,aa:10,ab:10,ad:10,ae:10,b1:1,b2:2,b3:3,b4:4,b5:5,b6:6,b7:7,b8:8,b9:9,ba:10,bb:10,bd:10,be:10,c1:1,c2:2,c3:3,c4:4,c5:5,c6:6,c7:7,c8:8,c9:9,ca:10,cb:10,cd:10,ce:10,d1:1,d2:2,d3:3,d4:4,d5:5,d6:6,d7:7,d8:8,d9:9,da:10,db:10,dd:10,de:10}; | ||
var pcSet = []; | |||
function hexToChar(x) { | function hexToChar(x) { | ||
return String.fromCharCode(0xD83C) +String.fromCharCode(x + 0xDC00); | return String.fromCharCode(0xD83C) +String.fromCharCode(x + 0xDC00); | ||
25번째 줄: | 26번째 줄: | ||
return x.toString(16); | return x.toString(16); | ||
} | } | ||
var dealerD, guestD; | |||
$("#deal").click(function(){ | $("#deal").click(function(){ | ||
$("#console").text("새 판을 엽니다.") | $("#console").text("새 판을 엽니다.") | ||
$("#deal").css("display", "none"); | $("#deal").css("display", "none"); | ||
$("#dealerDeck").text(""); | $("#dealerDeck").text(""); | ||
$("#guestDeck").text(""); | $("#guestDeck").text(""); | ||
pcSet = pcLst; | pcSet = Object.create(pcLst); | ||
console.log(pcSet.length); | |||
deal(); | deal(); | ||
return 0; | return 0; | ||
}); | }); | ||
$("#hit").click(function(){ | $("#hit").click(function(){ | ||
hit(); | |||
hit( | |||
return 0; | return 0; | ||
}); | }); | ||
$("#stand").click(function(){ | $("#stand").click(function(){ | ||
stand( | stand(); | ||
return 0; | return 0; | ||
}); | }); | ||
67번째 줄: | 66번째 줄: | ||
} | } | ||
}; | }; | ||
function hit( | function hit() { | ||
$("#hit").css("display", "none"); | $("#hit").css("display", "none"); | ||
$("#stand").css("display", "none"); | $("#stand").css("display", "none"); | ||
guestD.push(cardPop()); | |||
var guestDeq = ""; | var guestDeq = ""; | ||
for (i = 0; i < | for (i = 0; i < guestD.length; i++) { | ||
guestDeq += hexToChar( | guestDeq += hexToChar(guestD[i]); | ||
} | } | ||
$("#guestDeck").text(guestDeq); | $("#guestDeck").text(guestDeq); | ||
var guestScore = []; | var guestScore = []; | ||
var score = 0; | var score = 0; | ||
for (i = 0; i < | for (i = 0; i < guestD.length; i++) { | ||
guestScore.push(pointData[hexToStr( | guestScore.push(pointData[hexToStr(guestD[i])]); | ||
score += guestScore[i]; | score += guestScore[i]; | ||
} | } | ||
85번째 줄: | 84번째 줄: | ||
if (score == 21 || (score == 11 && guestScore.includes(1))) { | if (score == 21 || (score == 11 && guestScore.includes(1))) { | ||
$("#console").text("21점이므로 자동으로 스탠드를 외쳤습니다.") | $("#console").text("21점이므로 자동으로 스탠드를 외쳤습니다.") | ||
stand( | stand(dealerD,guestD); | ||
} else if (score > 21) { | } else if (score > 21) { | ||
bust(); | bust(); | ||
94번째 줄: | 93번째 줄: | ||
return 0; | return 0; | ||
} | } | ||
function stand( | function stand() { | ||
$("#hit").css("display", "none"); | $("#hit").css("display", "none"); | ||
$("#stand").css("display", "none"); | $("#stand").css("display", "none"); | ||
var dScore = 0; | var dScore = 0; | ||
var dealerScore = [] | var dealerScore = [] | ||
for (i = 0; i < | for (i = 0; i < dealerD.length; i++) { | ||
dealerScore.push(pointData[hexToStr( | dealerScore.push(pointData[hexToStr(dealerD[i])]); | ||
dScore += dealerScore[i]; | dScore += dealerScore[i]; | ||
} | } | ||
while (dScore < 17) { | while (dScore < 17) { | ||
dealerD.push(cardPop()); | |||
$("#console").text("br/>딜러가 카드를 뽑았습니다.") | $("#console").text("br/>딜러가 카드를 뽑았습니다.") | ||
dealerScore = []; | dealerScore = []; | ||
dScore = 0; | dScore = 0; | ||
for (i = 0; i < | for (i = 0; i < dealerD.length; i++) { | ||
dealerScore.push(pointData[hexToStr( | dealerScore.push(pointData[hexToStr(dealerD[i])]); | ||
dScore += dealerScore[i]; | dScore += dealerScore[i]; | ||
dealerDeq += hexToChar( | dealerDeq += hexToChar(dealerD[i]); | ||
} | } | ||
} | } | ||
var dealerDeq = ""; | var dealerDeq = ""; | ||
for (i = 0; i < | for (i = 0; i < dealerD.length; i++) { | ||
dealerDeq += hexToChar( | dealerDeq += hexToChar(dealerD[i]); | ||
} | } | ||
$("#dealerS").text(dScore); | $("#dealerS").text(dScore); | ||
122번째 줄: | 121번째 줄: | ||
guestScore = []; | guestScore = []; | ||
score = 0; | score = 0; | ||
for (i = 0; i < | for (i = 0; i < guestD.length; i++) { | ||
guestScore.push(pointData[hexToStr( | guestScore.push(pointData[hexToStr(guestD[i])]); | ||
score += guestScore[i]; | score += guestScore[i]; | ||
} | } | ||
$("#guestS").text(score); | $("#guestS").text(score); | ||
if (score > 21) { | if (score > 21) { | ||
bust() | bust() | ||
133번째 줄: | 131번째 줄: | ||
win() | win() | ||
} else { | } else { | ||
compare( | compare(dealerD,guestD); | ||
} | } | ||
return 0; | return 0; | ||
} | } | ||
function compare( | function compare() { | ||
var dealerScore = []; | var dealerScore = []; | ||
var dScore = 0; | var dScore = 0; | ||
for (i = 0; i < | for (i = 0; i < dealerD.length; i++) { | ||
dealerScore.push(pointData[hexToStr( | dealerScore.push(pointData[hexToStr(dealerD[i])]); | ||
dScore += dealerScore[i]; | dScore += dealerScore[i]; | ||
} | } | ||
var guestScore = []; | var guestScore = []; | ||
var score = 0; | var score = 0; | ||
for (i = 0; i < | for (i = 0; i < guestD.length; i++) { | ||
guestScore.push(pointData[hexToStr( | guestScore.push(pointData[hexToStr(guestD[i])]); | ||
score += guestScore[i]; | score += guestScore[i]; | ||
} | } |
2018년 2월 6일 (화) 09:57 판
const pcLst = [ 0xA1,0xA2,0xA3,0xA4,0xA5,0xA6,0xA7,0xA8,0xA9,0xAA,0xAB,0xAD,0xAE, 0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xBB,0xBD,0xBE, 0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCD,0xCE, 0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,0xD9,0xDA,0xDB,0xDD,0xDE]; const pointData = {a1:1,a2:2,a3:3,a4:4,a5:5,a6:6,a7:7,a8:8,a9:9,aa:10,ab:10,ad:10,ae:10,b1:1,b2:2,b3:3,b4:4,b5:5,b6:6,b7:7,b8:8,b9:9,ba:10,bb:10,bd:10,be:10,c1:1,c2:2,c3:3,c4:4,c5:5,c6:6,c7:7,c8:8,c9:9,ca:10,cb:10,cd:10,ce:10,d1:1,d2:2,d3:3,d4:4,d5:5,d6:6,d7:7,d8:8,d9:9,da:10,db:10,dd:10,de:10}; var pcSet = []; function hexToChar(x) { return String.fromCharCode(0xD83C) +String.fromCharCode(x + 0xDC00); } function hexToStr(x) { return x.toString(16); } var dealerD, guestD; $("#deal").click(function(){ $("#console").text("새 판을 엽니다.") $("#deal").css("display", "none"); $("#dealerDeck").text(""); $("#guestDeck").text(""); pcSet = Object.create(pcLst); console.log(pcSet.length); deal(); return 0; }); $("#hit").click(function(){ hit(); return 0; }); $("#stand").click(function(){ stand(); return 0; }); function deal() { dealerD = []; guestD = []; dealerD.push(cardPop()); guestD.push(cardPop()); dealerD.push(cardPop()); guestD.push(cardPop()); $("#dealerDeck").text(hexToChar(dealerD[0]) + "\u{1F0A0}"); $("#dealerS").text("?"); $("#guestDeck").text(hexToChar(guestD[0]) + hexToChar(guestD[1])); var guestScore = []; var score = pointData[hexToStr(guestD[0])] + pointData[hexToStr(guestD[1])]; $("#guestS").text(score); if ((pointData[hexToStr(guestD[0])] == 1 && pointData[hexToStr(guestD[1])] == 10) || (pointData[hexToStr(guestD[0])] == 10 && pointData[hexToStr(guestD[1])] == 1)) { $("#guestS").text(21); jack(); } else { $("#hit").css("display", "inline-flex"); $("#stand").css("display", "inline-flex"); } }; function hit() { $("#hit").css("display", "none"); $("#stand").css("display", "none"); guestD.push(cardPop()); var guestDeq = ""; for (i = 0; i < guestD.length; i++) { guestDeq += hexToChar(guestD[i]); } $("#guestDeck").text(guestDeq); var guestScore = []; var score = 0; for (i = 0; i < guestD.length; i++) { guestScore.push(pointData[hexToStr(guestD[i])]); score += guestScore[i]; } $("#guestS").text(score); if (score == 21 || (score == 11 && guestScore.includes(1))) { $("#console").text("21점이므로 자동으로 스탠드를 외쳤습니다.") stand(dealerD,guestD); } else if (score > 21) { bust(); } else { $("#hit").css("display", "inline-flex"); $("#stand").css("display", "inline-flex"); } return 0; } function stand() { $("#hit").css("display", "none"); $("#stand").css("display", "none"); var dScore = 0; var dealerScore = [] for (i = 0; i < dealerD.length; i++) { dealerScore.push(pointData[hexToStr(dealerD[i])]); dScore += dealerScore[i]; } while (dScore < 17) { dealerD.push(cardPop()); $("#console").text("br/>딜러가 카드를 뽑았습니다.") dealerScore = []; dScore = 0; for (i = 0; i < dealerD.length; i++) { dealerScore.push(pointData[hexToStr(dealerD[i])]); dScore += dealerScore[i]; dealerDeq += hexToChar(dealerD[i]); } } var dealerDeq = ""; for (i = 0; i < dealerD.length; i++) { dealerDeq += hexToChar(dealerD[i]); } $("#dealerS").text(dScore); $("#dealerDeck").text(dealerDeq); guestScore = []; score = 0; for (i = 0; i < guestD.length; i++) { guestScore.push(pointData[hexToStr(guestD[i])]); score += guestScore[i]; } $("#guestS").text(score); if (score > 21) { bust() } else if (dScore > 21) { win() } else { compare(dealerD,guestD); } return 0; } function compare() { var dealerScore = []; var dScore = 0; for (i = 0; i < dealerD.length; i++) { dealerScore.push(pointData[hexToStr(dealerD[i])]); dScore += dealerScore[i]; } var guestScore = []; var score = 0; for (i = 0; i < guestD.length; i++) { guestScore.push(pointData[hexToStr(guestD[i])]); score += guestScore[i]; } if (dScore < 12 && dealerScore.includes(1)) { dScore += 10 } if (score < 12 && guestScore.includes(1)) { score += 10 } $("#dealerS").text(dScore); $("#guestS").text(score); if (dScore > score) { lose() } else if (dScore == score) { push() } else { win() } return 0; } function win() { $("#console").text("축하합니다! 이겼습니다.") $("#deal").css("display", "inline-flex"); return 0; } function lose() { $("#console").text("졌습니다...") $("#deal").css("display", "inline-flex"); return 0; } function push() { $("#console").text("비겼습니다.") $("#deal").css("display", "inline-flex"); return 0; } function bust() { $("#console").text("버스트! 유감입니다. 졌습니다...") $("#hit").css("display", "none"); $("#stand").css("display", "none"); $("#deal").css("display", "inline-flex"); return 0; } function jack() { $("#console").text("블랙잭 달성! 축하합니다. 이겼습니다!") $("#deal").css("display", "inline-flex"); return 0; } function cardPop() { var j, ret; j = Math.floor(Math.random() * pcSet.length); ret = pcSet[j]; pcSet.splice(j,1); return ret; }