Created
August 22, 2017 06:36
-
-
Save yoongi0428/776fa1832777ee81467542bbfc8b3b2e to your computer and use it in GitHub Desktop.
game javascript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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