Skip to content

Instantly share code, notes, and snippets.

@yoongi0428
Created August 22, 2017 06:36
Show Gist options
  • Save yoongi0428/776fa1832777ee81467542bbfc8b3b2e to your computer and use it in GitHub Desktop.
Save yoongi0428/776fa1832777ee81467542bbfc8b3b2e to your computer and use it in GitHub Desktop.
game javascript
var bj_main;
//Card
function Card(type, number){
this.type = "";
this.number = -1;
this.value = -1;
this.SetInfo(type, number);
}
Card.prototype.SetInfo = function(type, number){
this.type = type;
this.number = number;
if(number > 10) this.value = 10; // J,Q,K ๋Š” 10
else if(number === 1) this.value = 11; // A๋Š” ์ผ๋‹จ 11๋กœ ํ•œ๋‹ค. (๊ตณ์ด 11๋กœ ์•ˆ๋‘˜ ์ด์œ ๊ฐ€ ์žˆ์„๊นŒ)
else this.value = number;
}
//์นด๋“œ๋ฑ ๋ฆฌ์ŠคํŠธ ์…”ํ”Œ์„ ์œ„ํ•œ ๋ฉ”์†Œ๋“œ
Array.prototype.shuffle = function(){for(var j, x, i = this.length; i; j = Math.floor(Math.random() * i), x = this[--i], this[i] = this[j], this[j] = x);};
Array.prototype.sum = function (){for(var sum=0, i=0; i < this.length; sum += this[i++]); return sum;};
// MAIN
function BlackJack(){
// ๊ฒŒ์ž„์— ํ•„์š”ํ•œ ๋ณ€์ˆ˜๋“ค
var type = ['hearts','diamonds', 'spades', 'clubs'],
deck = [],
playerCard = [],
dealerCard = [],
cardIdx=0,
INIT_MONEY = 1000000,
L_BOUND = 16,
U_BOUND = 17,
isFirst = true,
isPlaying = false,
isOver = false,
isStand = false,
isDoubled = false;
p = new Player();
d = new Dealer();
// PLAYER
function Player(){
this.money=INIT_MONEY;
this.bet=0;
this.total=0;
this.ace=0;
}
Player.prototype.SetBet = function(b){
if(this.money >= b)
this.bet = b;
//else ๋ˆ๋ถ€์กฑ์—๋Ÿฌ (์–ด๋–ค์‹์œผ๋กœ ์ฒ˜๋ฆฌ ํ•  ๊ฒƒ์ธ๊ฐ€) 1. ์—๋Ÿฌํ•จ์ˆ˜ -> ๋ˆ๋ถ€์กฑ์—๋Ÿฌ 2. ๋ˆ๋ถ€์กฑ ํŒ์—… 3. ๊ธฐํƒ€ ....
};
Player.prototype.Total = function(){
this.total = this.GetTotal();
};
Player.prototype.GetTotal = function(){
var total = 0;
for(var i=0; i<this.cards.length(); i++){
total = cards[i].value;
}
while(total > 21 && this.ace > 0) {
total -= 10;
this.ace -= 1;
}
return total;
};
// DEALER
function Dealer(){
this.total=0;
this.ace=0;
}
Dealer.prototype.Total = function(){
this.total = this.GetTotal();
};
Dealer.prototype.GetTotal = function(){
var total = 0;
for(var i=0; i<this.cards.length(); i++){
total = cards[i].value;
}
while(total > 21 && this.ace > 0) {
total -= 10;
this.ace -= 1;
}
return total;
};
// GAME INIT
function Init() {
initDeck();
}
function InitDeck() {
newdeck = []
for(var i=0; i<4; i++){
for(var j=1; j<=13; j++){
newdeck.push(new Card(type[i], j));
}
}
newdeck.shuffle();
deck = newdeck;
}
function DistributeCards(){
addCard(p);
addCard(d);
addCard(p);
addCard(d);
p.Total();
d.Total();
}
// GAME PLAY
function isBlackjack(){
pt = p.total;
dt = d.total;
if(pt === 21 && dt === 21) return 1; // ๋ฌด์Šน๋ถ€
else if(pt === 21) return 2; // ํ”Œ๋ ˆ์ด์–ด win
else if(dt === 21) return 3; // ๋”œ๋Ÿฌ win
else return 0; // ๋ธ”๋ž™์žญ ์—†์Œ
}
function checkBlackjack() {
var bj = isBlackjack();
switch (bj) {
case 0:
// ๋ธ”๋ž™์žญ ์—†์Œ
break;
case 1:
Result("draw");
break;
case 2:
Result("player-bj");
break;
case 3:
Result("dealer-bj");
break;
}
}
function DealerTurn(){
dt = d.Total();
if(dt > 21){
Result("dealer-burst");
}
else if(dt === 21){
Result("dealer-bj");
}
else if(dt <= L_BOUND){
DealerTurn();
}
else {
End();
}
}
function changeMoney(mul){
var increment = p.bet * mul;
if(p.money + increment < 0){
// ๋ˆ๋ถ€์กฑ (์—๋Ÿฌ ์ฒ˜๋ฆฌ ํ•จ์ˆ˜ ํ•„์š”)
}
else{
p.money += increment;
}
}
function deal(){
isPlaying = true;
if(!isFirst){
playerCard = [];
dealerCard = [];
cardIdx=0;
}
var bet; // bet๊ธˆ์•ก ๊ฐ€์ ธ์™€์•ผํ•จ
p.SetBet(bet);
changeMoney(-1);
InitDeck();
DistributeCards();
checkBlackjack();
isFirst=true;
}
function hit(){
addCard(p);
p.Total();
if(p.total > 21){
Result("player-burst");
}
}
function stand(){
isStand = true;
DealerTurn();
}
function double(){
if(p.money)
isDoubled = true;
hit();
stand();
}
function addCard(player){
newcard = deck[cardIdx++];
player.cards.push();
player.Total();
}
// ๋ชจ๋‘๊ฐ€ 21์ด ๋„˜์ง€ ์•Š์•˜๋‹ค. ํ•œ ๊ฒŒ์ž„ ์ข…๋ฃŒ.
function End(){
pt = p.total;
dt = d.total;
if(pt > dt){
Result("player-win");
}
else if(pt < dt){
Result("dealer-win");
}
else{
Result("draw");
}
}
// ๊ฒŒ์ž„ ์™„์ „ํžˆ ์ข…๋ฃŒ
function gameover(){
}
// ๊ฒฐ๊ณผ ์ฒ˜๋ฆฌ
function Result(msg){
switch (msg){
case "player-win": break;
case "player-bj": break;
case "player-burst": break;
case "dealer-win": break;
case "dealer-bj": break;
case "dealer-burst": break;
case "draw": break;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment