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