Jump to content


JavaScript - Making Blackjack game in JavaScript


3 replies to this topic

#1 lee890910

    Young Padawan

  • Members
  • Pip
  • 21 posts

Posted 23 May 2006 - 05:07 PM

I am trying to create a Game of 21 which is same as a black jack, and I am trying to make it so that:

if a player clicks on the take card button, it generates a random number between 0 and 10 and is added to the player's score. if the player's score goes over 21, then player loses and shows an alert message saying player lost and also put a loss count as minus one.
if a player clicks on the done button, it generates a random number between 0 and 10 and it is added to the poopnent's score. numbers continue to be added as long as the opponent has less than 17.
if the opponent goves over 21, the opponent loses. once the opponent has more than 16, the scores of the player and the opponent are compared and if player wins, then show an alert message saying player won and vice versa. if player wins, then +1 to the stats, and if player loses, then -1 to the stats.

and I think I have all the code to execute this game, but it seems that I have a minor error or some little letter or symbol missing that I cannot find. Its a little frustrating since there is no debugger for javascript. Can someone debug my errors, and can you also tell me what program you use to do javascript coding and what program if there is any to debug your code. Thanks.
<html>

<head>
<meta http-equiv="Content-Type"
content="text/html; charset=iso-8859-1">
<meta name="GENERATOR" content="Microsoft FrontPage Express 2.0">
<title>Project_01</title>
</head>

<body bgcolor="#FFFFFF">
<script language="JavaScript">
<!--

function takeCard()
{

var stats = winsBox.value = 0;
var score = myBox.value = 0;
score = score + Math.round(Math.random() * 9) + 1;
if(score > 21)
{
resultBox.value = "You Lost.";
DoneButton.disabled = true;
CardButton.disabled = true;
stats = stats - 1;
}

}

function done()
{
var stats = winsBox.value = 0;
var score = myBox.value = 0;
var opscore = 0;
while(opscore > 16);
{
opscore = opscore + Math.round(Math.random() * 9) + 1;
}
if(opscore > 21)
{
resultBox.value = "Opponent Lost.";
stats = stats + 1;
}
else if(opscore <= 21 && score > opscore)
{
resultBox.value = "You Won.";
stats = stats + 1;
}

}

function playAgain()
{
var stats = winsBox.value = 0;
var score = myBox.value = 0;
score = 0;
opscore = 0;
resultBox.value = "";
DoneButton.disabled = false;
CardButton.disabled = false;

}

//-->
</script>

<p align="center"><font color="#FF0000" size="6">Game of 21</font></p>

<p align="center"><font size="4">Opponent<br>

<input type="text" size="20" name="opponentBox"></font></p>

<p align="center"><font size="4">You<br>
<input type="text" size="20" name="myBox"></font></p>

<p align="center">
<input type="button" name="CardButton"
value="Take a Card" onclick="if(score<21) {takeCard()};"> 
<input type="button" name="DoneButton" 
value="Done" onclick="done()"> 
<input type="button" name="AgainButton" 
value="Play Again"
onclick="playAgain()"></p>

<p align="center"><font size="4">Result <input type="text"
size="24" name="resultBox"></font></p>

<p align="center"><font size="4">Score <input type="text"
size="4" name="winsBox"></font></p>
</body>

</html>

Edited by lee890910, 25 May 2006 - 11:01 AM.


#2 lee890910

    Young Padawan

  • Members
  • Pip
  • 21 posts

Posted 25 May 2006 - 11:02 AM

please help me ;)

#3 Flex-

    Young Padawan

  • Members
  • Pip
  • 28 posts

Posted 25 May 2006 - 11:42 AM

Cant you just link up to a blackjack game from a server?

#4 Futingkiller

    Young Padawan

  • Members
  • Pip
  • 110 posts

Posted 26 May 2006 - 01:10 PM

well the best javascript debuger is Mozilla FireFox (that's what i use)(javascript console)
i am corecting you code now......
i'll edit this mesage with the erors that i found.
ok i finished (if you find bugs with it tell me so i can fix them
<html>

<head>
<meta http-equiv="Content-Type"
content="text/html; charset=iso-8859-1">
<meta name="GENERATOR" content="Microsoft FrontPage Express 2.0">
<title>Project_01</title>
</head>

<body bgcolor="#FFFFFF">
<script type="text/javascript">
<!--
var score=0;
var stats=0;
var opscore=0;
function takeCard()
{
score = score + Math.round(Math.random() * 9) + 1;
document.Formy.myBox.value=score;
if(score > 21)
{
document.Formy.resultBox.value = "You Lost.";
document.Formy.DoneButton.disabled = true;
document.Formy.CardButton.disabled = true;
stats = stats - 1;
document.Formy.winsBox.value=stats;
}

}

function done()
{
opscore = 0;
while(opscore < 16)
{
opscore = opscore + Math.round(Math.random() * 9) + 1;
}
document.Formy.opponentBox.value =opscore;
if(opscore > 21)
{
document.Formy.resultBox.value = "Opponent Lost.";
stats = stats + 1;
}
else if(score > opscore)
{
document.Formy.resultBox.value = "You Won.";
stats = stats + 1;
} else if(score < opscore)
{
document.Formy.resultBox.value = "You Lost.";
stats = stats - 1;
}else {
document.Formy.resultBox.value = "No1 wins";
}
document.Formy.winsBox.value=stats;
document.Formy.DoneButton.disabled = true;
document.Formy.CardButton.disabled = true;
}

function playAgain()
{
score = 0;
opscore = 0;
document.Formy.resultBox.value = "";
document.Formy.myBox.value = "";
document.Formy.opponentBox.value = "";
document.Formy.DoneButton.disabled = false;
document.Formy.CardButton.disabled = false;
document.Formy.winsBox.value=stats;
}

//-->
</script>
<form name='Formy'>
<p align="center"><font color="#FF0000" size="6">Game of 21</font></p>

<p align="center"><font size="4">Opponent<br>

<input type="text" size="20" name="opponentBox"></font></p>

<p align="center"><font size="4">You<br>
<input type="text" size="20" name="myBox"></font></p>

<p align="center">
<input type="button" name="CardButton" value="Take a Card" onclick="takeCard();">
<input type="button" name="DoneButton" value="Done" onclick="done()">
<input type="button" name="AgainButton" value="Play Again" onclick="playAgain()"></p>

<p align="center"><font size="4">Result <input type="text" size="24" name="resultBox"></font></p>

<p align="center"><font size="4">Score <input type="text" size="4" name="winsBox"></font></p>
</form>
</body>

</html>
now to explain the errors:
1) (not sure is it is obligatory)
for javascipt to use the textbox, it needs to have a form with a name.
the way to use the value of the textbox is by using this syntax : "document.FormName.TextBoxName.value"
2)
if you want to make a variable a global variable (so any of your functions can use it) it need's to be declared outside functions using "var".
it is sugested to set for all the variables an initial value.
3)
inside a function if you use "var VariableName" it creates an local variable for the curent function, so you can't see that variable or it's value from outside the function
4)
if you use the syntax : "Math.round(Math.random()*9)" it generates values from 0..9, but the accuracy of 0 and 9 is 50% less than the rest, because
[0..0,5) generates 0
[0,5..1,5) generates 1
......
[8,5..9) generates 9
i usualy use Math.floor(Math.random()*10), because it generates with the same accuracy values from 0..9 (in this case) because
[0..1) generates 0
[1..2) generates 1
.....
[9..10) generates 9
sorry for the late post, but i have't been home the past 2 days
if you don't understand something i've done don't be shy and ask me
-------- Enjoy :) ----------

Edited by Futingkiller, 26 May 2006 - 02:27 PM.






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users