Click to See Complete Forum and Search --> : Image array


taznia1999
11-23-2003, 11:04 AM
I'm doing a paper, rock scissors game. Everything works except the calculating who wins. Sometimes it says I won and sometimes it says the computer won and sometimes it says it's a draw when we are getting the same thing(ie: I get paper and the computer gets rock). Am I missing something so that it isn't seeing the PChoice and the CChoice as what they are?

<form>
<SCRIPT LANGUAGE="JavaScript">
var PChoice;
var CChoice;

var imageArray = new Array();
for (var i=0 ; i<3 ; i++)
imageArray[i] = new Image();
imageArray[0].src="hands/paper-l.gif";
imageArray[1].src="hands/rck-l.gif";
imageArray[2].src="hands/sciss-l.gif";

function playGame(Choice)
{

PChoice=Choice;
document.user_image.src = imageArray[PChoice-1].src;

animation = setInterval('SwitchImage();',50);
setTimeout('results(animation);',900);
clear_text()
}

function SwitchImage()
{
CChoice=(Math.round(Math.random()*2)+1);
document.comp_image.src = imageArray[CChoice-1].src;
}

function results(animation)
{
clearInterval(animation);
clearTimeout(animation);

if ((PChoice == 1 && CChoice == 2)
||(PChoice == 2 && CChoice ==3)
|| (PChoice == 3 && CChoice ==1))
{
msg.value = 'You won!';
win.value++;
}

else if (PChoice == CChoice)
{
msg.value='It is a draw!';
tie.value++;
}

else
{
msg.value='You lost!';
loss.value++;
}
}
function clear_text()
{
msg.value=' ';
}

function clear_score()
{
loss.value=0
win.value=0
tie.value=0
}

</script>
</form>

<h2>Paper, Rock and Scissors Game</h2>
<p>Click on either the rock, scissors or paper to make your selection.</p>
<table>
<tr align="center">
<td bgcolor="#ffccff"></a><img src="person.gif" width=100 height=75></a>
<a href="javascript:;" onClick=playGame(1);><img border=1 src="hands/paper-l.gif" alt="Paper" width=100 height=75></a>
<a href="javascript:;" onClick=playGame(2);><img border=1 src="hands/rck-l.gif" alt="Rock" width=100 height=75></a>
<a href="javascript:;" onClick=playGame(3);><img border=1 src="hands/sciss-l.gif" alt="Scissors" width=100 height=75></a>
</td>
</tr>
</table>
<table align="center">
<tr>
<td><input type=text name=msg size=45></td>
</tr>
</table>
<table align="center">
<tr>
<td bgcolor="#ffccff"><p><img src="person.gif" width=100 height=75 align="middle"></a>
Selected:<img NAME="user_image" src="hands/paper-l.gif" width=100 height=75 align="middle"></td>
<td bgcolor="#ffcccc"><img src="comp.gif" width=100 height=75 align="middle"></a>
Selected: <img NAME="comp_image" src="hands/rck-l.gif" width=100 height=75 align="middle"><br></p>
</td></tr>
</table>
<p>
<INPUT TYPE="text" SIZE=2 NAME="win" WIN.VALUE="0"> Wins
<INPUT TYPE="text" SIZE=2 NAME="loss" LOSS.VALUE="0"> Losses
<INPUT TYPE="text" SIZE=2 NAME="tie" TIE.VALUE="0"> Ties
</p>
<p> <input type=button value="Clear Score" onClick="clear_score()"></p>
</body>
</html>