Forums

This topic is locked

java code

Posted 16 Jul 2008 18:06:47
1
has voted
16 Jul 2008 18:06:47 gerald omaghomi posted:
i have a java book out of which i copy codes, but whenever i copy the code and load in the browser.iget diffrent error messages..iwill attach two ffiles for you to see...it can be frustrating..please help


1)

<html>
<head>
<title>Draw Poker</title>
<script LANGUAGE="JavaScript1.1">
var score = 100;
var dealt = false;
var hand = new Array(6);
var held = new Array(6);
var deck = new Array(53);
function DealDraw() {
if (dealt == true) Draw();
else Deal();
}
function Deal() {
// fill the deck (in order, for now)
for (i=1; i<14; i++) {
deck[i] = new Card(i,"c";
deck[i+13] = new Card(i,"h";
deck[i+26] = new Card(i,"s";
deck[i+39] = new Card(i,"d";
}
// shuffle the deck
var n = Math.floor(400 * Math.random() + 500);
for (i=1; i<n; i++) {
card1 = Math.floor(52*Math.random() + 1);
card2 = Math.floor(52*Math.random() + 1);
temp = deck[card2];
deck[card2] = deck[card1];
deck[card1] = temp;
}
// Deal and Display cards
for (i=1; i<6; i++) {
hand[i] = deck[i];
document.images[i].src = hand[i].fname();
document.images[i+5].src = "hold.gif";
held[i] = false;
}
dealt = true;
score = score - 1; //deduct one for bet amount
document.form1.total.value = score;
document.images[11].src="draw.gif";
Addscore();
}
//Hold or discard a card
function Hold(num) {
if (!dealt) return;
if (!held[num]) {
held[num]=true;
document.images[5+num].src="hold2.gif";
}
else {
held[num]=false;
document.images[5+num].src="hold.gif";
}
}
//Draw new cards
function Draw() {
var curcard = 6;
for (i=1; i<6; i++) {
if (!held[i]) {
hand[i] = deck[curcard++];
document.images[i].src = hand[i].fname();
}
}
dealt = false;
document.images[11].src="deal.gif";
score += Addscore();
document.form1.total.value = score;
}
// Make a filename for an image, given Card object
function fname() {
return this.num + this.suit + ".gif";
}
// Constructor for Card objects
function Card(num,suit) {
this.num = num;
this.suit = suit;
this.fname = fname;
}
// Numeric sort function
function Numsort(a,b) { return a - b; }
// Calculate Score
function Addscore() {
var straight = false;
var flush = false;
var pairs = 0;
var three = false;
var tally = new Array(14);
// sorted array for convenience
var nums = new Array(5);
for (i=0; i<5; i++) {
nums[i] = hand[i+1].num;
}
nums.sort(Numsort);
// flush
if (hand[1].suit == hand[2].suit &&
hand[2].suit == hand[3].suit &&
hand[3].suit == hand[4].suit &&
hand[4].suit == hand[5].suit) flush = true;
// straight (Ace low)
if (nums[0] == nums[1] - 1 &&
nums[1] == nums[2] - 1 &&
nums[2] == nums[3] - 1 &&
nums[3] == nums[4] - 1) straight = true;
// straight (Ace high)
if (nums[0] == 1 && nums[1] == 10 && nums[2] == 11
&& nums[3] == 12 && nums[4] == 13)
straight = true;
// royal flush, straight flush, straight, flush
if (straight && flush && nums[4] == 13 && nums[0] == 1) {
document.form1.message.value="Royal Flush";
return 100;
}
if (straight && flush) {
document.form1.message.value="Straight Flush";
return 50;
}
if (straight) {
document.form1.message.value="Straight";
return 4;
}
if (flush) {
document.form1.message.value="Flush";
return 5;
}
// tally array is a count for each card value
for (i=1; i<14; i++) {
tally[i] = 0;
}
for (i=0; i<5; i++) {
tally[nums[i]] += 1;
}

for (i=1; i<14; i++) {
if (tally[i] == 4) {
document.form1.message.value = "Four of a Kind";
return 25;
}
if (tally[i] == 3) three = true;
if (tally[i] == 2) pairs += 1;
}
if (three && pairs == 1) {
document.form1.message.value="Full House";
return 10;
}
if (pairs == 2) {
document.form1.message.value="Two Pair";
return 2;
}
if (three) {
document.form1.message.value="Three of a Kind";
return 3;
}
if (pairs == 1) {
if (tally[1] == 2 || tally[11]==2
|| tally[12] == 2 || tally[13]==2) {
document.form1.message.value="Jacks or Better";
return 1;
}
}
document.form1.message.value="No Score";
return 0;
}
</script>
</head>
<body>
<img src="title.gif" width=381 height=81>
<hr>
<form NAME="form1">
<table>
<tr>
<td> <img border=0 src="blank.gif" height=136 width=106>
<td> <img border=0 src="blank.gif" height=136 width=106>
<td> <img border=0 src="blank.gif" height=136 width=106>
<td> <img border=0 src="blank.gif" height=136 width=106>
<td> <img border=0 src="blank.gif" height=136 width=106>
<td> </td>
</tr>
<tr>
<td> <a href="#" onClick="Hold(1);">
<img border=0 src="hold.gif" height=50 width=106></a>
<td> <a href="#" onClick="Hold(2);">
<img border=0 src="hold.gif" height=50 width=106></a>
<td> <a href="#" onClick="Hold(3);">
<img border=0 src="hold.gif" height=50 width=106></a>
<td> <a href="#" onClick="Hold(4);">
<img border=0 src="hold.gif" height=50 width=106></a>
<td> <a href="#" onClick="Hold(5);">
<img border=0 src="hold.gif" height=50 width=106></a>
</tr>
<tr>
<td> <B>Total<BR>Score:</B>
<input TYPE="TEXT" SIZE=6 NAME="total" VALUE="100"></td>
<td colspan=2> <B>Current <BR>Hand:</B>
<input TYPE="TEXT" SIZE=20 NAME="message"
VALUE="Press DEAL to begin.">
<td>
<td> <a href="#" onClick="DealDraw();">
<img border=0 src="deal.gif" height=50 width=106></a>
</tr>
</table>
</form>
</body>
</html>

-
--------------------------------------------
2)


<!-- TWO STEPS TO INSTALL EVALUATION RADIO BUTTONS:


1. Copy the coding into the HEAD of your HTML document
2. Add the last code into the BODY of your HTML document -->

<!-- STEP ONE: Paste this code into the HEAD of your HTML document -->

<HEAD>

<!-- This script and many more are available free online at -->
<!-- The JavaScript Source!! javascript.internet.com -->
<!-- Original: Rick Johnson ( ) -->
<!-- Web Site: 208.255.209.164/myPage/ -->
<script>
<!-- Begin
var color=""; var number=""; var letter="";
// radio buttons are either/or elements
// MIE evaluates onClick of an element. NN does not
function getRadios(what){
// get the color value
j=what.color.length; //alert(j)
for (i=0; i<j; i++){
if(what.color[i].checked) var color = what.color[i].value
}
// get number value
j=what.number.length; //alert(j)
for (i=0; i<j; i++){
if(what.number[i].checked) var number = what.number[i].value
}
//get letter value
j=what.letter.length; //alert(j)
for (i=0; i<j; i++){
if(what.letter[i].checked) var letter = what.letter[i].value
}
// show selections
what.buttons.value = (color+', '+number+', '+letter)
}
// End -->
</script>
</HEAD>

<!-- STEP TWO: Copy this code into the BODY of your HTML document -->

<BODY>

<!-- This script and many more are available free online at -->
<!-- The JavaScript Source!! javascript.internet.com -->
<!-- Original: Rick Johnson ( ) -->
<!-- Web Site: 208.255.209.164/myPage/ -->
<form name="myForm">
<div onClick='getRadios(document.myForm)' /* clicking division */>
<b>My Color:</b>
<input type=radio name="color" value="red" checked>Red
<input type=radio name="color" value="orange">Orange
<input type=radio name="color" value="yellow">Yellow
<input type=radio name="color" value="green">Green
<input type=radio name="color" value="blue">Blue
<input type=radio name="color" value="indigo">Indigo
<input type=radio name="color" value="violet">Violet
<br><br>
<b>My Number:</b>
<input type=radio name="number" value="One">1
<input type=radio name="number" value="Two">2
<input type=radio name="number" value="Thirty three">33
<input type=radio name="number" value="Fourty four">44
<input type=radio name="number" value="Five Hundred ">500
<br><br>
<b>My Letter:</b>
<input type=radio name="letter" value="a">A
<input type=radio name="letter" value="b">B
<input type=radio name="letter" value="c">C
<input type=radio name="letter" value="d">D
<input type=radio name="letter" value="e">E
</div /* end clicking */>
<br>
<b>My Button Values:</b>
<input type=text name="buttons" size=30><br><br>


</form>

<p><center>
<font face="arial, helvetica" size"-2">Free JavaScripts provided<br>
by <a href="javascriptsource.com">The JavaScript Source</a></font>
</center><p>

<!-- Script Size: 3.00 KB -->

Reply to this topic