// Adobe Black Jack should run in both Illustrator and Photoshop CS and above.
// Feel free to make changes or add features, just please add your name to mine in the credits
//
// To install this script, drop it in your Scripts folder located in somewhere like <APP>\Presets\Scripts\
// Note that you'll have to restart Photoshop or Illustrator before this will appear in your Scripts menu.
//
// This version of blackjack is slightly different from what is played in Vegas:
// 1) Dealers start with only one card so they can not get a blackjack until all the players make their plays. 
// 2) Since there is no bettting, only a tally of wins/losses, there's no need for doubling down or splitting.
//
// Enjoy!

var dealerTotal = 0
var playerTotal = 0
var playerScore = 0
var pAceMod = 0
var dAceMod = 0
var playerScore = 0
function drawCard(player){
	xcard = Math.floor(Math.random()*10)+2
	cardName = xcard
	if(xcard==11){
	cardName = "Ace"	
	switch(player){
		case "pOne":
			pAceMod = pAceMod+1
			break;
		case "pTwo":
			dAceMod = dAceMod+1
			break;
		}	
	}
}



function playerHit(){
	if(playerTotal<22){
	playerHit = confirm("Score:"+playerScore+"\n\nDealer is showing "+dealerTotal+". You have "+playerTotal+".\n\n Would you like another card?");
		if(playerHit==1){
			drawCard("pOne");
			playerTotal=playerTotal+xcard;
			alert("You get a "+cardName+" for a total of "+playerTotal+".");
			reHit(1);
		}
		else{
			alert("Staying with "+playerTotal+".");
			reHit(2);			
		}
		
	}
	if(playerTotal>=22){
		if(pAceMod > 0){
			playerTotal = playerTotal-10
			alert("Using your Ace as a 1 you have "+playerTotal+".");
			pAceMod = pAceMod-1
			reHit(1)
		}
		else{
			conclusion()
		}
	}
}

function xdealerHit(){
	if (dealerTotal<22){
		if (dealerTotal>16){
			alert ("Dealer is staying with "+dealerTotal+".");
			conclusion();
		}
		else{
			drawCard("pTwo");
			dealerTotal=dealerTotal+xcard;
			alert("Dealer draws a "+cardName+" for a total of "+dealerTotal+".");
			reHit(2);
		}
	}
	else{
		if(dAceMod>0){
			dealerTotal=dealerTotal-10;
			alert("Dealer is using his Ace as a 1 to get "+dealerTotal+".");
			dAceMod=dAceMod-1
			reHit(2);
		}
		else{
			conclusion()
		}
	}

}

function reHit(xplayer){
	if (xplayer ==1){
		playerHit();
	}
	if (xplayer ==2){
		xdealerHit();
	}
}

function conclusion(){
	
	if (dealerTotal>21){
		playerWin()
		
	}
	else if (playerTotal>21){
		playerLose()
		
	}
	else if (playerTotal>dealerTotal&&playerTotal<=21){
		playerWin()
		
	}	
	else if (dealerTotal>playerTotal&&dealerTotal<=21){
		playerLose()
		
	}
	else if (dealerTotal==playerTotal){
		playerDraw()
		
	}

}

function playerDraw(){
alert("Both players have "+dealerTotal+". Draw.");
}

function playerLose(){
alert("Dealer has "+dealerTotal+". You have "+playerTotal+". You Lose.");
playerScore = playerScore-1
}

function playerWin(){
alert("Dealer has "+dealerTotal+". You have "+playerTotal+". You Win!");
playerScore = playerScore +1
}

function playAgain(){
xRestart = confirm("Score:"+playerScore+"\n\nPlay Again?");
	if (xRestart == 1){;
		dealerTotal = 0
		playerTotal = 0
		pAceMod=0
		dAceMod=0
		xcard = 0
		mainLoop()
	}
}		

function mainLoop(){

drawCard("pOne");
playerTotal = playerTotal+xcard;
alert ("Your first card is a "+cardName+".");
drawCard("pTwo");
dealerTotal = dealerTotal+xcard;
alert ("The Dealer is showing a "+cardName+".");
drawCard("pOne");
playerTotal = playerTotal+xcard;
alert ("Your second card is a "+cardName+" for a total of "+playerTotal+".")
playerHit()
playAgain()
}

alert("Adobe BlackJack\n\nBy Blaine Christian\nblaine@npoly.com");
mainLoop();
