so i decided to get serious in javascript, and thought i should do a rock paper and scissor game.
features:
lets the user input how many times they want to play
checks for invalid inputs
displays the final scores
the computer is just random
so here it is... of course in .html format. copy, paste and run in your browser
<html>
<head>
<title>rock , paper, scissors</title>
</head>
<body>
<script type="text/javascript">
var numOfgames = prompt("how many times would you like to fight?"
;
parseInt(numOfgames);
gameCount=0;//this would be used to determine the end value
var win=0;
var loss=0;
var draw=0;//this three variables will hold the final scores
while(gameCount<numOfgames)
{
var playerChoice = prompt("what weapon would you like to attack the computer with: paper, rock, or scissors?"
;
var choiceList = new Array("","rock", "paper", "scissors"
;//an array that holds strings the computer can randomly select
var computerChoice=choiceList[Math.ceil(Math.random()*3)];//a statement that makes the computer select randomly from the array
if (playerChoice == computerChoice){
alert(playerChoice + " Against " + computerChoice + " its a Draw!"
;
draw++;//increases the draw score line
}
else if(playerChoice == "rock"
{
if (computerChoice == "scissors"
{
alert("rock crushes scissors!! player WINS!"
;
win++;//increases the win score line
}else if (computerChoice == "paper"
{
alert("paper cover rock... computer WINS!" );
loss++;//increases the loss score line
}
}else if(playerChoice == "paper"
{
if (computerChoice == "rock"
{
alert("paper covers rock... player WINS!"
;
win++;
}else if(computerChoice == "scissors"
{
alert("scissors cuts papers! computer WINS!"
;
loss++;
}
}else if(playerChoice == "scissors"
{
if (computerChoice == "paper"
{
alert("scissors cuts paper! player WINS!"
;
win++;
}else if(computerChoice == "rock"
{
alert("rock crushes scissors!! computer wins!"
;
loss++;
}
}else{
alert("please choose a valid weapon to fight the computer with; from \"rock, paper, scissors\""
;
continue;//continue looping without affecting the program flow
}//end if
gameCount++;
alert(numOfgames-gameCount + " more fights left!"
;
//document.write("<pre>"+"wins:"+win +" loss:"+loss +" draw:"+draw +"</pre><br />"
;//uncomment this if you want to see your score after each fight
}
document.write("<pre>"+"wins:"+win +" loss:"+loss +" draw:"+draw +"</pre><br />"
;//displays the scores in a html format
</script>
</body>
</html>
thanks for even opening this thread

better programs ahead