3 Replies - 13186 Views - Last Post: 07 December 2008 - 10:07 AM

#1 fashionnugget  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 163
  • Joined: 25-November 08

Rock paper scissors help. :[

Post icon  Posted 07 December 2008 - 08:30 AM

I just started learning JS today and I went through all the w3schools.com tutorials on it and now I'm trying to make a rock paper scissors game with it. (I know I should have used cases, but I want to get it to work first, then I'll change it.)

java script:
function rps(player); {
	computer=Math.round((Math.random()*2) + 1);
	var cchoice;
	if (computer==1) {
		cchoice = "rock";
	}
	if (computer==2) {
		cchoice = "paper";
	}
	if (computer==3) {
		cchoice = "scissors";
	}
	var pchoice;
	if (player==1) {
		pchoice = "rock";
	}
	if (player==2) {
		pchoice = "paper";
	}
	if (player==3) {
		pchoice = "scissors";
	}
	if (choice=="rock" && cchoice=="rock"); {
		alert("You have tied with the mighty computer.");
	}
	if (choice=="rock" && cchoice=="paper"); {
		alert("The computer has covered your rock, and therefore destroyed you.");
	}
	if (choice=="rock" && cchoice=="scissors"); {
		alert("You have destroyed the mighty computer's scissors!");
	}
	
	if (choice=="paper" && cchoice=="rock"); {
		alert("You destroyed the computer's rock with your paper.");
	}
	if (choice=="paper" && cchoice=="paper"); {
		alert("You have tied with the all mighty computer.");
	}
	if (choice=="paper" && cchoice=="scissors"); {
		alert("The computer has cut your paper into many pieces, you lose.");
	}
	
	if (choice=="scissors" && cchoice=="rock"); {
		alert("The computer has smashed your scissors with his rock.");
	}
	if (choice=="scissors" && cchoice=="paper"); {
		alert("You have destroyed the computer with your might paper cutter.");
	}
	if (choice=="scissors" && cchoice=="scissors"); {
		alert("You have tied with the mighty computer.");
	}
}


html segment:
			<script src="play.js"></script>
			Choose your weapon wisely!<br><br>
			<form>
			<input type="button" value="Rock" onclick="rps(1)">
			<input type="button" value="Paper" onclick="rps(2)">
			<input type="button" value="Scissors" onclick="rps(3)">
			</form> 


When I hit the buttons nothing happens, I can't figure out where I went wrong... Sorry if this is an awful attempt, I just started learning.

This post has been edited by fashionnugget: 07 December 2008 - 09:21 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Rock paper scissors help. :[

#2 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5931
  • View blog
  • Posts: 12,854
  • Joined: 16-October 07

Re: Rock paper scissors help. :[

Posted 07 December 2008 - 09:32 AM

You never defined the variable "choice". I hate case statements, so don't apologize for that. ;)

When you seen repetition, figure out if you can put it in a function. e.g.
function getChoiceName(choice) {
	if (choice==1) {
		return "rock";
	} else if (choice==2) {
		return "paper";
	} else if (choice==3) {
		return "scissors";
	} else {
		return "oops";
	}
}

function rps(player); {
	var computer=Math.round((Math.random()*2) + 1);
	var cchoice = getChoiceName(computer);
	var pchoice = getChoiceName(player);




Note the if then else tree on the function. So how it must always return an answer and never stops when it reaches the right one? Do that with your check and you'll be fine. I'd write a getWinner function that returns 0 for tie, or 1 or 2 for who wins, then write the alter text based on that.

Good luck.
Was This Post Helpful? 1
  • +
  • -

#3 BetaWar  Icon User is offline

  • #include "soul.h"
  • member icon

Reputation: 1199
  • View blog
  • Posts: 7,308
  • Joined: 07-September 06

Re: Rock paper scissors help. :[

Posted 07 December 2008 - 09:40 AM

The major issue you had was too many semi-colons (;). If you put then right after a function declaration or if statement it tells JS that the function/if statement does nothing and causes problems (as you saw)

Here is some code that is fixed:
<script>
onerror=handleErr;
var txt="";function handleErr(msg,url,l)
{
txt="There was an error on this page.\n\n";
txt+="Error: " + msg + "\n";
txt+="URL: " + url + "\n";
txt+="Line: " + l + "\n\n";
txt+="Click OK to continue.\n\n";
alert(txt);
return true;
}


function rps(player) {
	computer=Math.round((Math.random()*2) + 1);
	var cchoice;
	if (computer==1) {
		cchoice = "rock";
	}
	if (computer==2) {
		cchoice = "paper";
	}
	if (computer==3) {
		cchoice = "scissors";
	}
	var choice;
	if (player==1) {
		choice = "rock";
	}
	if (player==2) {
		choice = "paper";
	}
	if (player==3) {
		choice = "scissors";
	}
	if (choice=="rock" && cchoice=="rock") {
		alert("You have tied with the mighty computer.");
	}
	if (choice=="rock" && cchoice=="paper") {
		alert("The computer has covered your rock, and therefore destroyed you.");
	}
	if (choice=="rock" && cchoice=="scissors") {
		alert("You have destroyed the mighty computer's scissors!");
	}
	
	if (choice=="paper" && cchoice=="rock") {
		alert("You destroyed the computer's rock with your paper.");
	}
	if (choice=="paper" && cchoice=="paper") {
		alert("You have tied with the all mighty computer.");
	}
	if (choice=="paper" && cchoice=="scissors") {
		alert("The computer has cut your paper into many pieces, you lose.");
	}
	
	if (choice=="scissors" && cchoice=="rock") {
		alert("The computer has smashed your scissors with his rock.");
	}
	if (choice=="scissors" && cchoice=="paper") {
		alert("You have destroyed the computer with your might paper cutter.");
	}
	if (choice=="scissors" && cchoice=="scissors") {
		alert("You have tied with the mighty computer.");
	}
}
</script>

Choose your weapon wisely!<br><br>
			<input type="button" value="Rock" onclick="rps(1)">
			<input type="button" value="Paper" onclick="rps(2)">
			<input type="button" value="Scissors" onclick="rps(3)">


<edit>
I too dislike switch statements so I tend to leave them alone. Here is some code that is greatly reduced in size and still works:

<script>
function rps(player) {
	computer=Math.round((Math.random()*2) + 1);
	var choices = ["Rock", "Paper", "Scissors"];

	if(player == computer){
	  alert("You and the computer tied with "+choices[player-1]+".");
	}
	else if((computer == 1 && player == 3) || (computer == 2 && player == 1) || (computer == 3 && player == 2)){
	  alert("The computer beat you! The computer's "+choices[computer-1]+" beats your "+choices[player-1]+".");
	}
	else{
	  alert("You WIN! Your "+choices[player-1]+" beats the computer's "+choices[computer-1]+".");
	}
}
</script>

Choose your weapon wisely!<br><br>
			<input type="button" value="Rock" onclick="rps(1)">
			<input type="button" value="Paper" onclick="rps(2)">
			<input type="button" value="Scissors" onclick="rps(3)">


Enjoy :D
</edit>

HTH
Was This Post Helpful? 1
  • +
  • -

#4 fashionnugget  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 163
  • Joined: 25-November 08

Re: Rock paper scissors help. :[

Posted 07 December 2008 - 10:07 AM

Very much so! Thank you both. :}
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1