PDA

View Full Version : function problems

FunkyCow
Apr 1st, 2008, 04:24 PM
im making a simple dice game and i have this function and form
if you want to see the entire page i uploaded it Here (http://www.geocities.com/milkdafunkycow/formdice.html)

function roll(p1)
{

if (p1=99)
{
a=(Math.ceil(Math.random()*6));
b=(Math.ceil(Math.random()*6));
c=(Math.ceil(Math.random()*6));

//456
if (a==4 && b == 5 && c == 6)
p1=13;
else
if (a==4 && c == 5 && b == 6)
p1=13;
else
if (b==4 && a == 5 && c == 6)
p1=13;
else
if (b==4 && c == 5 && a == 6)
p1=13;
else
if (c==4 && a == 5 && b == 6)
p1=13;
else
if (c==4 && b == 5 && a == 6)
p1=13;
else
//123
if (a==1 && b == 2 && c == 3)
p1=0;
else
if (a==1 && c == 2 && b == 3)
p1=0;
else
if (b==1 && a == 2 && c == 3)
p1=0;
else
if (b==1 && c == 2 && a == 3)
p1=0;
else
if (c==1 && a == 2 && b == 3)
p1=0;
else
if (c==1 && b == 2 && a == 3)
p1=0;
else
//trips
if (a==b && b==c)
p1=(a+6);
else
//doubles
if (a==b)
p1=c;
else
if (a==c)
p1=b;
else
if (b==c)
p1=a;
else
p1=99;
}
//display
if (p1 >= 0 && p1 <= 13)
document.game.display1.value=("["+a+"] "+"["+b+"] "+"["+c+"] "+p1+"points!!!")
else
document.game.display1.value=("["+a+"] "+"["+b+"] "+"["+c+"] "+"No Points Roll Again");
return p1;
}

<form name="game">
<table width="100%" >
<tr>
<td><input type="text" name="display1" size="50"></td>
<td><input type="button" value="Player 1 ROLL!!!" onClick="roll(player1)"></td>
</tr>
<tr>
<td><input type="text" name="display2" size="50"></td>
<td><input type="button" value="Player 2 ROLL!!!" onClick="roll(player2)"></td>
</tr>
<tr>
<td></td><td><input type="reset" value="Clear"></td>
</tr>
</table>
</form>

My first problem is that i put the display inside the code but i wanted it to display to two different boxes. I fixed this problem by making a second function and changing the display numberand just wanted to know it there was a better way.

My second problem is that i wanted to make it so when you roll something that gives you points the button becomes useless i tired if(p1=99) but it didn't seem to do anything.

Any help would be appreciated and also any tips to make the script run more smoothly thanks

vwphillips
Apr 1st, 2008, 05:48 PM
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<TITLE> Dice Game </TITLE>
<script>
/*
Simple Dice Roller Game
Created by Thomas DiMaggio
Feel Free To Change
*/
var a;
var b;
var c;

function roll(but,field){
var frm=document.game;
var a=(Math.ceil(Math.random()*6));
var b=(Math.ceil(Math.random()*6));
var c=(Math.ceil(Math.random()*6));
var p1=101;
if (a==4 && b == 5 && c == 6) p1=13;
else if (a==4 && c == 5 && b == 6) p1=13;
else if (b==4 && a == 5 && c == 6) p1=13;
else if (b==4 && c == 5 && a == 6) p1=13;
else if (c==4 && a == 5 && b == 6) p1=13;
else if (c==4 && b == 5 && a == 6) p1=13;
else if (a==1 && b == 2 && c == 3) p1=0;
else if (a==1 && c == 2 && b == 3) p1=0;
else if (b==1 && a == 2 && c == 3) p1=0;
else if (b==1 && c == 2 && a == 3) p1=0;
else if (c==1 && a == 2 && b == 3) p1=0;
else if (c==1 && b == 2 && a == 3) p1=0;
else if (a==b && b==c) p1=(a+6);
else if (a==b) p1=c;
else if (a==c) p1=b;
else if (b==c) p1=a;
//display
if (p1 >= 0 && p1 <= 13){
frm[field].value=("["+a+"] "+"["+b+"] "+"["+c+"] "+p1+"points!!!");
but.setAttribute('disabled','disabled');
}
else frm[field].value=("["+a+"] "+"["+b+"] "+"["+c+"] "+"No Points Roll Again");
}

function Clear(){
var frm=document.game;
frm['display1'].value='';
frm['display2'].value='';
frm['but1'].removeAttribute('disabled');
frm['but2'].removeAttribute('disabled');
}
</script>

<BODY bgcolor="black" text="white">

<h1> <center>Dice Roller</center> <hr><hr> </h1>

<form name="game">
<table width="100%" >
<tr>
<td><input type="text" name="display1" size="50"></td>

<td><input type="button" name="but1" value="Player 1 ROLL!!!" onClick="roll(this,'display1')"></td>
</tr>
<tr>
<td><input type="text" name="display2" size="50"></td>
<td><input type="button" name="but2" value="Player 2 ROLL!!!" onClick="roll(this,'display2')"></td>
</tr>
<tr>
<td></td><td><input type="button" value="Clear" onclick="Clear();"></td>
</tr>

</table>
</form>
<br><br><br><br><br><br>
<table border="4" cellpadding="5" cellspacing="5" style="float: right; background-color: white; color: black;">
<tr>
<th align="center"> Results </th>
<th align="center"> Points </th>
</tr>

<tr>
<td align="center">1 , 2 , and 3</td>
<td align="center">Zero Points</td>
</tr>
<tr>
<td align="center">Any pair</td>
<td align="center">Third Number*</td>

</tr>
<tr>
<td align="center">Three of a kind</td>
<td align="center">Number Plus 6**</td>
</tr>
<tr>
<td align="center">4, 5, and 6</td>

<td align="center"> Thirteen Points</td>
</tr>
<tr>
<td colspan="2" align="right" style="font-size:12">
*[i.e. 1 ,1 ,6 = 6 points]<br>
**[i.e. 1 ,1 ,1 = 1+6(7 points)]
</td>
</tr>

</table>
</body>
</html>