home

Contents


Related applications



© The scientific sentence. 2007

  Blackjack     Javscriptt OOP     Events     Windows     Contact us  

Javascript


Javascript


Blackjack code source


	<HTML>
	<HEAD>
	<TITLE>BlackJack</TITLE>
	<SCRIPT LANGUAGE="JavaScript">
	
	balance = 500;
	bet = 20;
	shuffletimes = 1000;

	function makeArray(size) {
	this.length = size; 
	}

	function init() {
	gameOver = true;
	empty = new Image();
	empty.src = "empty.gif"; 
	//empty card: green

	/* Construct a set of 13 cards from 1 to 10 with 
	the Jack, the Queen and the King */
	cardImage = new makeArray(13);
	for (var i=0; i<14; i++) {
	cardImage[i] = new Image();
	//cardImage[i].src = i+".gif " ; 
	}

	cardImage[0].src = "empty.gif " ; 
	cardImage[1].src = "14sf.gif " ; // l'As
	cardImage[2].src = "02sf.gif " ;
	cardImage[3].src = "03sf.gif ";
	cardImage[4].src = "04sf.gif ";
	cardImage[5].src = "05sf.gif ";
	cardImage[6].src = "06sf.gif ";
	cardImage[7].src = "07sf.gif ";
	cardImage[8].src = "08sf.gif ";
	cardImage[9].src = "09sf.gif ";
	cardImage[10].src = "10sf.gif " ;
	cardImage[11].src ="11sf.gif "; // Jack
	cardImage[12].src ="12sf.gif "; // Queen
	cardImage[13].src ="13sf.gif "; // King

	//Define a deck (set) of 13  black pick cards.

	deck = new makeArray(13);
	for (i=0; i<14; i++) {
	deck[i] = i;
	}
	}
	
	//Shuffling cards: -----------------------
	function shuffle() {
	cardsInDeck = 13;
	// Shuffle by swapping (shuffletimes) times.
	for (i=0; i<shuffletimes; i++) {
	var firstCard  = parseInt(Math.random()*13);
	var secondCard = parseInt(Math.random()*13);

	/* The parseInt() function parses a string and 
	returns an integer.
	The random() method returns a random number 
	between 0 and 1.
	* : the limit is 1 x 13 = 13 */

	//Shffling once is a "swap".
	var temp          = deck[firstCard];
	deck [firstCard]  = deck[secondCard];
	deck [secondCard] = temp;
	}
	emptyCards();
	document.boxes.result.value = '';
	}

	
	// To empty  cards, all are green
	function emptyCards() {
	for (i=0; i<5; i++) {
	document.images["p"+i].src = empty.src;
	document.images["c"+i].src = empty.src;
	}
	}

	// Player object: -----------------------
	function Player() { 
	// Player's properties
	this.noCards = 0; //Initialize
	//noCards is the number of played cards.
	this.card = new makeArray(5);
	for (var i=0; i<5; i++) {
	this.card[i] = 0; //Initialize
	}
	// Related methods: P for gold mouth and c 
	pour corto ( computer or bank)
	this.hit = hit;
	this.checkBlackJack = checkBlackJack;
	this.openCard = openCard;
	this.getCount = getCount;
	}


	//We will define these function:s
	function hit(plyrCode) {
	this.card[this.noCards] = deck[cardsInDeck];
	var imageName = plyrCode+(this.noCards);
	document.images[imageName].src = 
	cardImage[(this.card[this.noCards])].src;
	this.noCards++;
	cardsInDeck--; 
	// The balance for gold mouth
	if (plyrCode =="p"){
	balance -= bet;
	sold ();
	}
	}


	function playerBlackJacks() {
	document.boxes.result.value = " 
	the balckJack. gold mouth wins! "
	gameOver=true;
	}

	function computerBlackJacks() {
	c.openCard();
	document.boxes.result.value = " the balckJack. 
	corto wins: "
	gameOver=true;
	}

	//The first function to trigger: -----------------------
	function dealcards() {
	shuffle();
	p = new Player();
	c = new Player();
	p.hit("p");
	c.hit("c");
	p.hit("p");
	c.hit("c");

	if (p.checkBlackJack()) {
	playerBlackJacks();
	}
	else if (c.checkBlackJack()) {
	computerBlackJacks();
	}
	else
	{gameOver=false;}
	}


	// First card for the computer: -----------------------
	function openCard() {
	document.images["c0"].src = 
	cardImage[(this.card[0])].src;
	}


	//Check first for two cards:
	function checkBlackJack() {
	if (this.noCards==2) {
	var first  = this.card[0];
	var second = this.card[1];
	if (first > 10) {
	first = 10;
	}
	if (second > 10) {
	second = 10;
	}
	if (((first==1) && (second==10))||((first==10) 
	&& (second==1))) {
	return true;
	}
	else {
	return false;
	}
	}
	}


	//Get the count: -----------------------
	function getCount() {
	var total=0;
	for (var i=0; i<this.noCards; i++) {
	var cardValue = this.card[i];
	if (cardValue > 10) {
	cardValue=10;
	}
	total += cardValue;
	}
	return total;
	}


	// Doubble: -----------------------
	function getDouble(){
	playerHits();
	playerHits();
	}

	// Hit : one more card:
	function playerHits() {
	if (!gameOver) {
	p.hit("p");
	var playerTotal = p.getCount();
	if (playerTotal > 21) {
	playerBusts();
	}
	else if (p.noCards == 5) {
	playerWinOverFive();
	}
	}
	}

	
	//Stop  the game and  show the results: -----------------------
	function playerStays() {
	if (!gameOver) {
	c.openCard();
	while ((c.getCount()<16)&&(c.noCards<5)) {
	c.hit("c");
	}
	// Retrieve the player and computer counts
	var playerValue   = p.getCount();
	var computerValue = c.getCount();
	document.boxes.result.value = (" gold mouth count : " + 
	playerValue);
	document.boxes.result.value = (" corto count : " + 
	computerValue);

	// Check if it busts:
	if (computerValue > 21) {
	computerBusts();
	}
	else {

	// Check to see whish value  is higher:
	if (playerValue > computerValue) {
	playerWins(playerValue,computerValue);
	}
	else if (playerValue < computerValue) {
	computerWins(playerValue,computerValue);
	}
	else {
	aDraw(playerValue);
	}
	}
	}

	}  
	
	// Results to display: -----------------------
	
	function playerBusts() {
	document.boxes.result.value = (" gold mouth busts. 
	the game is over!");
	gameOver=true;
	}

	function computerBusts() {
	document.boxes.result.value = (" corto busts. 
	the game is over! ");
	gameOver=true;
	}

	function playerWinOverFive() {
	document.boxes.result.value = (" gold mouth: 5 cards, 
	total < 21. She wins!");
	gameOver=true; 
	}

	function playerWins (a,b) {
	document.boxes.result.value = (" gold mouth wins: score " + a + 
	" versus corto\'s score: "+b);
	gameOver=true;
	}

	function computerWins (a,b) {

	document.boxes.result.value = (" corto wins: score " + b + " 
	versus gold\'s score: "+a);
	gameOver=true;
	}

	function aDraw(a) {
	document.boxes.result.value =  (" A tie game !");
	gameOver=true;
	}

	function sold(){
	document.boxes.balance.value = balance;
	if (balance <= 0){
	gameOver=true;
	}
	}   


	</SCRIPT>
	</HEAD>

	<BODY ONLOAD="init();">
	<CENTER>
	<H1>Blackjack</H1>
	<br />
	<FORM name = "boxes">

	<table bgcolor= "#E6E6FA" width = "200"><TR>
	<TD><input align = "center"  style = "font-size:20"; TYPE="txt" SIZE="15" 
	NAME="bet" VALUE="20" color = "green"></td> 
	<td><input align = "center"  style = "font-size:20"; TYPE="txt" SIZE="15" 
	NAME="balance" VALUE="500" color = "red" 
	onfocus= "this.style.color='blue'; this.style.background = '#FFE4E1'";></TD>
	<td><br><input style = "color: red" type="RESET" /></td>
	</TR></table>

	<TABLE>		
	<TR><TD> 
	<img src = "goldMouth.jpg">
	</TD>
	<TD><IMG SRC="empty.gif" NAME="p0"></TD>
	<TD><IMG SRC="empty.gif" NAME="p1" ></TD>
	<!-- width="68" height="106"></TD> -->
	<TD><IMG SRC="empty.gif" NAME="p2"></TD>
	<TD><IMG SRC="empty.gif" NAME="p3"></TD>
	<TD><IMG SRC="empty.gif" NAME="p4"></TD>
	</TR>

	<TR><TD>  </TD></TR><TR>

	<TR>
	<TD>
	<img src="Corto.gif">
	</TD>

	<TD><IMG SRC="empty.gif" NAME="c0"></TD>
	<TD><IMG SRC="empty.gif" NAME="c1"></TD>
	<TD><IMG SRC="empty.gif" NAME="c2"></TD>
	<TD><IMG SRC="empty.gif" NAME="c3"></TD>
	<TD><IMG SRC="empty.gif" NAME="c4"></TD>
	</TR>

	</table>

	<br />

	<TABLE border = "0"> <tr> <td> <table><tr><td 
	width = "135">  </td></tr></table> </td><td>  
	<table border = "1"><tr><td>
	<INPUT TYPE="button" NAME="deal" VALUE="play" ONCLICK="dealcards();" 
	style="color: #0000FF"></td><td>
	<INPUT TYPE="button" NAME="hit" VALUE="card" ONCLICK="playerHits();" 
	style="color: #FF00FF"></td><td>
	<INPUT TYPE="button" NAME="stay" VALUE="stop" ONCLICK="playerStays();" 
	style="color: #0000FF"></td><td>
	<INPUT TYPE="button" NAME="init" VALUE="new game" ONCLICK="shuffle();" 
	style="color: #0000FF"></td><td>
	<INPUT TYPE="button" NAME="double" VALUE="double " ONCLICK="getDouble();" 
	style="color: #0000FF">
	</td></tr></table>
	</td></tr></TABLE>

	<br />
	<table><tr><td>
	<input align = "center" style="background-color:black ; color:lime; 
	font-size:20"; TYPE="txt" 
	SIZE="51" NAME="result" VALUE="" color = "red">
	</td></tr></table>
	
	lt;table><tr><td align = "right"><a href = 
	"games/blackjack_source.html">source code</a>
	</td></tr></table>
	
	</FORM>

	</CENTER>
	</BODY>

	</HTML>



Advertisement