Movement list - Rock Paper Scissors

  • (2 Pages)
  • +
  • 1
  • 2

17 Replies - 5249 Views - Last Post: 14 October 2011 - 04:51 AM

#1 Costasone  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 10-October 11

Movement list - Rock Paper Scissors

Posted 10 October 2011 - 06:52 AM

Hi,
I need help. I have to make a Game Rock, Paper, Scissors. Where I have a counters (all ready working). Movement history :
Player Paper - Computer Scissors V (victory sign)
Player Rock- DRAW - Computer Rock
V (victory sign)Player Paper - Computer Rock
Something like that.
with reset button I should also reset this table.

This is Code I am working on:
 
<html>
<head>
<script LANGUAGE="Javascript">


function Game(choice) {
with (document.game) 
{
comp = Math.round((Math.random() * 2) + 1); 
var choicename;

if (comp == 1) choicename = "rock";
if (comp == 2) choicename = "paper";
if (comp == 3) choicename = "scissors";
msg.value = 'The computer chose ' + choicename + ';  ';
var item = myNodeList[i];
for (var i = 0; i < myNodeList.length; ++i) {
   
}

switch(choice) { // calculates score
case 1 : // rock
         if (comp == 1) {  
		 draw.value++;  
		 msg.value += 'It is a draw.';  
		 nodeItem = nodeList.item("Computer rock - player rock");
		 break; }
         if (comp == 2) {  
		 loss.value++; 
		 msg.value += 'You lost!';     
		 nodeItem = nodeList.item("Computer paper - player rock");
		 break; }
         if (comp == 3) {  
		 win.value++; 
		 msg.value += 'You won!';      
		 nodeItem = nodeList.item("Computer scissors - player rock");
		 break; }
case 2 : // paper
         if (comp == 1) {  
		 win.value++;  
		 msg.value += 'You won!';   
		 nodeItem = nodeList.item("Computer rock - player paper");
		 break; }
         if (comp == 2) {  
		 draw.value++; 
		 msg.value += 'It is a draw.'; 
		nodeItem = nodeList.item("Computer paper - player paper");
		 break; }
         if (comp == 3) {  
		 loss.value++; 
		 msg.value += 'You lost!';   
		nodeItem = nodeList.item("Computer scissors - player paper");		 
		 break; }
case 3 : // scissors
         if (comp == 1) {  
		 loss.value++;  
		 msg.value += 'You lost!'; 
		 nodeItem = nodeList.item("Computer rock - player scissors");
		 break; }
         if (comp == 2) {  
		 win.value++; 
		 msg.value += 'You won!';    
		 nodeItem = nodeList.item("Computer paper - player scissors");
		 break; }
         if (comp == 3) {  
		 draw.value++; 
		 msg.value += 'It is a draw.';
		nodeItem = nodeList.item("Computer scissors - player scissors");		 
		 break; }
      }
	        msg.value += '  Go again!';
   
   }
   
}

</script>
</HEAD>

<BODY>

<center>
<form name=game>


<td align=center>User:</td><br>
<td align=center><a href="javascript:void(0);" onclick="Game(1);"><img src="rock.gif" width=40 height=23 border=0></a></td>
<td align=center><a href="javascript:void(0);" onclick="Game(2);"><img src="paper.gif" width=80 height=23 border=0></a></td>
<td align=center><a href="javascript:void(0);" onclick="Game(3);"><img src="scissors.gif" width=29 height=23 border=0></a></td>
</br>
<tr>
<td colspan=6 align=center><input type=text name=msg size=45></td>
</tr>
<tr>
<br>
<td colspan=20 align=center>
<input type=text name=win readonly value="0" size="2"> Wins  
<input type=text name=loss readonly value="0" size="2"> Losses   
<input type=text name=draw readonly value="0" size="2"> Draws

<br>
<input type="reset" >
</br>
</br>
</td>
</tr>

</form>
</center>


<p>
</head>


<body>
<table>

<tr>
<td>

</body>

</html>



Tried with node option... but just don't understand.

I just don't understand what DOM function I should use. And how to make right array that will read from different cases.

All day I was testing and nothing useful.

Is This A Good Question/Topic? 0
  • +

Replies To: Movement list - Rock Paper Scissors

#2 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3490
  • View blog
  • Posts: 10,058
  • Joined: 08-June 10

Re: Movement list - Rock Paper Scissors

Posted 10 October 2011 - 07:16 AM

there is some nasty code around blurring the vision.
- never ever use with() unless you have a very good reason to (and I can’t imagine what that should be). most of the time it will confuse you because DOM methods look like regular variables.
- validate your HTML!

besides that, why using
<a href="javascript:void()" onclick="Game(1);"><img … ></a>

when you just can use

<img … onclick="Game(1);">
?

a better randomizer
var comp = ["rock", "paper", "scissors"][Math.floor((Math.random() * 3))];

Math.round() has a smaller possibility at the borders. the numbers go as
0.000 - 0.499 => 0 (25%)
0.500 - 1.499 => 1 (50%)
1.500 - 1.999 => 2 (25%)

and I strongly recommend that you use functions to set the values. those nested if()s can be avoided by some simple templating techniques.

additionally, there are no elements named nodeList or myNodeList. the NodeList.item() method expects an index number to be given, but in JS you would normally use the bracket notation
document.getElementsByClassName("myclass")[2];
// instead of
document.getElementsByClassName("myclass")->item(2);

Was This Post Helpful? 1
  • +
  • -

#3 Costasone  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 10-October 11

Re: Movement list - Rock Paper Scissors

Posted 10 October 2011 - 11:25 PM

Thanks for reply.

<img … onclick="Game(1);">


This is really simpler version. I'll keep it in mind.

Like you said my code is too complicated. Here is simpler, but I can't get my counters working. what is wrong? Why it wont increment?

<html>

<script LANGUAGE="Javascript">
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 draw=0;
var loss=0;
var win=0;
	
	var choices = ["Rock", "Paper", "Scissors"];
		if(player == computer)
		{
		draw++;
		alert("You and the computer tied with "+choices[player-1]+".");
				     }
    else if((computer == 1 && player == 3) || (computer == 2 && player == 1) || (computer == 3 && player == 2))
	{
	loss++;
		
	   alert("The computer WON! The computer's "+choices[computer-1]+" beats your "+choices[player-1]+".");
  	}
    else {
		win++;	
     alert("You WIN! Your "+choices[player-1]+" beats the computer's "+choices[computer-1]+".");
			
		}
				
}
		
   </script>

<form >		
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)">
					
			<td colspan=20 align=center>
<input type=text name=win readonly value="0" size="2"> Wins  
<input type=text name=loss readonly value="0" size="2"> Losses   
<input type=text name=draw  readonly value="0" size="2"> Draws
<input type="reset" >

</form>
</td>

</script>

 </html>



And still don't understand how to make history of game. (me NOOB :) ) In theory I need to make a array that will hold a my list and after each turn it must bring it on the page. In all if statements It must send information to array about each movement.
I see it something like this:

var list [];

if(player == computer)
		{
		alert("Player "+choices[player-1]+"-TIE- Computer "+choices[computer-1]+"");
				     }
    else if((computer == 1 && player == 3) || (computer == 2 && player == 1) || (computer == 3 && player == 2))
	{
	alert("Player "+choices[player-1]+"- Computer "+choices[computer-1]+" W");
  	}
    else {
		win++;	
     alert("W Player "+choices[player-1]+"- Computer "+choices[computer-1]+"");
			
		}



How to make array collect information from if statements and output as list.

Trying to understand but something is missing. Its like why I should use this or why I cant use that. After many hours of testing nothing works at all :wacko: . Starting to think that my brain isn't meant for programming.

Thanks again for trying to help!
Was This Post Helpful? 0
  • +
  • -

#4 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3490
  • View blog
  • Posts: 10,058
  • Joined: 08-June 10

Re: Movement list - Rock Paper Scissors

Posted 11 October 2011 - 12:56 AM

there are some flaws.

your error handler won’t work (this way). every error handling function is passed one and only one parameter when it is called: the according event object (IE doesn’t pass anything but uses the global window.event object).
there is also no need to save the error text as global.

the second problem is scope. list is an array local to the function and will be deleted when the function finishes running, i.e. with each call to rps() the array is reset. there are several ways to save that data
- save it in a cookie
- save it in a browser session (requires a modern browser which supports the sessionStorage or localStorage objects)
- save it in an object*

those three possibilities have different levels of complexity.
saving in an object requires you to know about objects (gearing towards OOP)
cookies are supported in almost every browser, but they can be disabled
browser sessions only work in modern browsers

PS. the language attribute in <script> is deprecated, use type="text/javascript" instead

* - just making list a global variable doesn’t do it. because global variables are bad coding practice. a simple example:
// file1.js
var x = [];
function test(a)
{
    x.push(a);
}

// file2.js
function anothertest()
{
    x = 1;
}

// executing script
test(1);
anothertest();
test(2); // quits with an error

Was This Post Helpful? 1
  • +
  • -

#5 Costasone  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 10-October 11

Re: Movement list - Rock Paper Scissors

Posted 11 October 2011 - 03:28 AM

I started from scratch.

Right know I have this. I have added some questions in comments.

<html>
<head>
<script type="text/javascript">
 var tie=0; 
 var lost=0;
 var won=0;
 
  
  
  // Like you sead I shold try to use more functions. 
 function computerRandom(comp){ // Hear I am trying to make a function for random numbers that all other functions will read from hear.
//var comp = ["rock", "paper", "scissors"][Math.floor((Math.random() * 3))]; !!!>>> 
//>>> DOES this make random 0,1,2 or it dose 1-2-3 and does it automaticali assigns rock is 1, paper is 2, scissors is 3.
  var comp;
 comp = Math.round((Math.random() * 2) + 1); 
  }
  // this function I made if player pushes Rock button 
 function playerRock(){
//return computerRandom();  QUESTION: does return in this meaning READ from "function computerRandom" or (as the word says) RETURNS value from "function playerRock" to "function computerRandom".

  var comp;
   comp = Math.round((Math.random() * 2) + 1); // Random is hear because I coldnt make first function to work with other functions.
 if  (comp ==1){
	alert('It is a Tie.'); 
	tie.value++;
 }
 else if (comp ==2){
 alert('You lost');
 lost.value++;
 }
 else if (comp ==3){
 alert('You Won!');
 won.value++;
  }
 }
 // this function I made if player pushes paper button
 function playerPaper(){
//return computerRandom();
 var comp;
 comp = Math.round((Math.random() * 2) + 1); 
  if  (comp ==1){
	alert('You Won!');
won.value++;	
 }
 else if (comp ==2){
 alert('It is a Tie');
 tie.value++;
 }
 else if (comp ==3){
 alert('You lost!');
won.value++; 
 }
 }
 // this function I made if player pushes scissors button
 function playerScissors(){
//return computerRandom();
 
 var comp;
 comp = Math.round((Math.random() * 2) + 1); 
 if (comp ==1){
	alert('You lost'); 
	lost.value++;
 }
 else if (comp ==2){
 alert('You won');
 won.value++;
 }
 else if (comp ==3){
 alert('It is a Tie!');
  tie.value++;
  }
 }

</script>
</HEAD>
<body>
<form>
Choose your weapon wisely!<br><br>
            <input type="button" value="Rock" onclick="playerRock()">
            <input type="button" value="Paper" onclick="playerPaper()">
            <input type="button" value="Scissors" onclick="playerScissors()">
			
<input type=text name=win readonly value="0" size="2"> Wins  
<input type=text name=lost readonly value="0" size="2"> Losses   
<input type=text name=tie readonly value="0" size="2"> Draws
</form>
</body>

</html>


I have an idea that maybe I should use function in function.
Is that legal in JS?
I would do something like that:

 function main{
   
   var won;
   var tie;
   var lost;
  objekt.list // saves history of movement
   while (wantToPlayAgain=true){ // so if player wont stop playing the list wont stop updating. As you said that function after execution will delete all from memory.
		
		function random{
		//generate random number
			function game
			//using random number
			if rock
			//+counter+list
			if paper
			//+counter+list
			if scissors	
			//+counter+list			
					}
        
   }else (wantToPlayAgain=false)
   break;
       
 
 }
 



Should I continue with that?

Thank you again.
Was This Post Helpful? 0
  • +
  • -

#6 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3490
  • View blog
  • Posts: 10,058
  • Joined: 08-June 10

Re: Movement list - Rock Paper Scissors

Posted 11 October 2011 - 03:53 AM

  // Like you sead I shold try to use more functions. 
 function computerRandom(comp){ // Hear I am trying to make a function for random numbers that all other functions will read from hear.
  var comp;
 comp = Math.round((Math.random() * 2) + 1); 
  }

unfortunately this function does nothing useful. while you create a not-so-random number, it is lost at the end of the function. you would need to return the value you computed.
//var comp = ["rock", "paper", "scissors"][Math.floor((Math.random() * 3))]; !!!>>> 
//>>> DOES this make random 0,1,2 or it dose 1-2-3 and does it automaticali assigns rock is 1, paper is 2, scissors is 3.


this code populates comp with either the "rock", "paper" or "scissors" string. you could as well use only the generated numbers, be it 0-1-2 or 1-2-3.

//return computerRandom(); QUESTION: does return in this meaning READ from "function computerRandom" or (as the word says) RETURNS value from "function playerRock" to "function computerRandom".

it quits the function playerRock() with the return value from the function computerRandom() which is undefined. (there is no return value defined in computerRandom(), thus using the default)


comp = Math.round((Math.random() * 2) + 1); // Random is hear because I coldnt make first function to work with other functions.

see reasoning above.
Was This Post Helpful? 1
  • +
  • -

#7 Costasone  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 10-October 11

Re: Movement list - Rock Paper Scissors

Posted 11 October 2011 - 04:03 AM

But what about this logic? Will it work like that:
then It will run until player stops it manually.
and if it is in while loop the function doesn't stop.

 function main{
   var won;
   var tie;
   var lost;
  objekt.list // saves history of movement
   while (wantToPlayAgain=true){
		
		
		function random{
		//generate random number
			function game
			//using random number
			if rock
			//+counter+list
			if paper
			//+counter+list
			if scissors	
			//+counter+list			
					}
        
   }else (wantToPlayAgain=false)
   break;
       
 
 }
 


Thank you!
Was This Post Helpful? 0
  • +
  • -

#8 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3490
  • View blog
  • Posts: 10,058
  • Joined: 08-June 10

Re: Movement list - Rock Paper Scissors

Posted 11 October 2011 - 04:53 AM

there is no need for a loop, you can make it event driven (i.e. if you want to play, press the play button, otherwise the abort button)

additionally, do don’t define functions inside a loop (you can use them in a loop, though)

I’d rather go along

// still pseudo code

function randomGenerator() {
    // create a random number representing rock/paper/scissors
}

function play(userchoice) {
    var computerchoice = randomGenerator()
    // do some processing to be able to compare both values …
    // compare/get winner
    if (userchoice > computerchoice) // win
    if (userchoice = computerchoice) // draw
    if (userchoice < computerchoice) // loose
    // save results
    save()
    // display results
    display()
}

function abort() {
    // reset game, leave page
}
event.define(play)
event.define(abort)

Was This Post Helpful? 1
  • +
  • -

#9 Costasone  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 10-October 11

Re: Movement list - Rock Paper Scissors

Posted 12 October 2011 - 02:43 AM

Hello,

I was using your skeleton (THANKS). This is what I made for the moment.

Before I make it worse. Am I on the right direction.
Can I use Arrays like that? 1 Array list both player and computer takes value from it.
Why my simple alert(for the testing) won't work?
Am I using correctly function linking?
Are my IF statements correct?
How to use correct DOM button link?


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

		<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">

<head>
<script type="text/javascript">

function possibility(){

  var poss = ["rock", "paper", "scissors"];
}
  
  function randomGenerator() {
   
   var poss = possibility();
   var compRandom = poss[Math.floor(Math.random()*possibility.length)];
   
	// create a random number representing rock/paper/scissors
}

function play(userchoice) {
   
   var computerchoice = randomGenerator(compRandom);
    var userchoice = possibility();
	
	alert(computerchoice);
	
	
	// do some processing to be able to compare both values …
    // compare/get winner

//	if (userchoice('rock') && computerchoice('scissors') ||userchoice('paper') && computerchoice('rock') || userchoice('scissors') && computerchoice('paper') ) // win
	//alert ("You choose " + userchoice +"and computer choose"+computerchoice+"You Won!")
   // else if (userchoice == computerchoice) // draw
	//alert ("You choose " + userchoice +"and computer choose"+computerchoice+"It is a TIE")
    //else if (userchoice('paper') && computerchoice('scissors') || userchoice('scissors') && computerchoice('rock') || userchoice('rock') && computerchoice('paper') ) // loose
    //alert ("You choose " + userchoice +"and computer choose"+computerchoice+"You lost!")
	
	
	// save results
  //  save()
    // display results
  //  display()

  }

function abort() {
    // reset game, leave page
}
//event.define(play)
//event.define(abort)
 
</script>
</HEAD>
<body>
<form>		
			<input type="button" value="Rock" onclick="userchoice('rock')">
            <input type="button" value="Paper" onclick="userchoice('paper')">
            <input type="button" value="Scissors" onclick="userchoice('scissors')">
</form>	
</body>

</html>



Thank you!
Was This Post Helpful? 0
  • +
  • -

#10 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3490
  • View blog
  • Posts: 10,058
  • Joined: 08-June 10

Re: Movement list - Rock Paper Scissors

Posted 12 October 2011 - 03:17 AM

you’ve forgotten to return values from your possibility() and randomGenerator() functions.
Was This Post Helpful? 1
  • +
  • -

#11 Costasone  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 10-October 11

Re: Movement list - Rock Paper Scissors

Posted 12 October 2011 - 04:32 AM

Thanks ! Now I am stuck with If statements. What is missing or what did I do wrong?



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

		<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">

<head>
<script type="text/javascript">

function possibility(){

  var poss = ["rock", "paper", "scissors"];
  return poss;
  
  
}
  
  function randomGenerator() {
   
   var poss = possibility();
   var compRandom = poss[Math.floor(Math.random()*poss.length)];
   return compRandom;
	// create a random number representing rock/paper/scissors
}

function play(userchoice) {
  // var save = new object ();
   
   var computerchoice = randomGenerator();
    var userchoice = possibility();
		
	// do some processing to be able to compare both values …
    // compare/get winner

if ( (userchoice('rock') && computerchoice('scissors')) || (userchoice('paper') && computerchoice('rock')) || (userchoice('scissors') && computerchoice('paper')) )
{ // win
alert ("You choose " + userchoice +"and computer choose"+computerchoice+"You Won!")
}
else if (userchoice == computerchoice)
{ // draw
alert ("You choose " + userchoice +"and computer choose"+computerchoice+"It is a TIE")
}

else if ((userchoice('paper') && computerchoice('scissors')) || (userchoice('scissors') && computerchoice('rock')) || (userchoice('rock') && computerchoice('paper') )) 
{// loose
alert ("You choose " + userchoice +"and computer choose"+computerchoice+"You lost!")
	}
	
	// save results
  //  save()
    // display results
  //  display()

  }

function abort() {
    // reset game, leave page
}
//event.define(play)
//event.define(abort)


 
</script>
</head>
<form>		
	<input type="button" value="Rock" onclick="play('rock')">
        <input type="button" value="Paper" onclick="play('paper')">
       <input type="button" value="Scissors" onclick="play('scissors')">
</form>	
<body>
</body>

</html>



Was This Post Helpful? 0
  • +
  • -

#12 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3490
  • View blog
  • Posts: 10,058
  • Joined: 08-June 10

Re: Movement list - Rock Paper Scissors

Posted 12 October 2011 - 05:10 AM

learn to read the Error Console.

additionally, userchoice is an array while computerchoice is a string.

PS. you’re not using XHTML, so you can use a HTML doctype. and your HTML is invalid!

This post has been edited by Dormilich: 12 October 2011 - 05:13 AM

Was This Post Helpful? 1
  • +
  • -

#13 Costasone  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 10-October 11

Re: Movement list - Rock Paper Scissors

Posted 13 October 2011 - 12:32 AM

If I did It correctly. Now I changed my randomGeneretor to an Array. I did to an array because my If statement was using array elements. But is it right to do? Or in my case it is better to change Array to string?

Also am I using correctly DOM? I am calling out function play with value ('array'), but nothing.

Really thank you for helping me and trying to explain what I am doing wrong. :genius:

Javascript puzzle very slowly but with your help coming together. :stupid:


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>

<head>
<script type='text/javascript'>

function possibility(){

  var poss = ['rock', 'paper', 'scissors'];
  return poss;
  
  
}
  
  function randomGenerator() {
   
   var poss = possibility();
   var compRandom = poss[Math.floor(Math.random()*poss.length)];
   return compRandom;
	// create a random number representing rock/paper/scissors
}

function play(userchoice) {
  // var save = new object ();
   var convert = randomGenerator();
   var computerchoice = convert.split();
   	var userchoice = possibility();
	
	// do some processing to be able to compare both values …
    // compare/get winner

if ( (userchoice == ('rock') && computerchoice == ('scissors')) || (userchoice==('paper') && computerchoice==('rock')) || (userchoice==('scissors') && computerchoice==('paper') ))
{ // win
alert ('You choose ' + userchoice +'and computer choose'+computerchoice+'You Won!')
}
else if (userchoice == computerchoice)
{ // draw
alert ('You choose ' + userchoice +'and computer choose'+computerchoice+'It is a TIE')
}

else if ((userchoice==('paper') && computerchoice==('scissors')) || (userchoice==('scissors') && computerchoice==('rock')) || (userchoice==('rock') && computerchoice==('paper')) )
{// loose
alert ('You choose ' + userchoice +'and computer choose'+computerchoice+'You lost!')
	}
	
	// save results
  //  save()
    // display results
  //  display()

  }

function abort() {
    // reset game, leave pageF
}
//event.define(play)
//event.define(abort)


 
</script>
</head>

			<input type="button" value="Rock" onclick="play('rock')">
            <input type="button" value="Paper" onclick="play('paper')">
            <input type="button" value="Scissors" onclick="play('scissors')">
	



</html>



Was This Post Helpful? 0
  • +
  • -

#14 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3490
  • View blog
  • Posts: 10,058
  • Joined: 08-June 10

Re: Movement list - Rock Paper Scissors

Posted 13 October 2011 - 01:46 AM

randomGenerator() is OK so far.

but why are you redefining userchoice?

personally, I’d use numbers. easier to work with.

This post has been edited by Dormilich: 13 October 2011 - 01:48 AM

Was This Post Helpful? 0
  • +
  • -

#15 Costasone  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 10-October 11

Re: Movement list - Rock Paper Scissors

Posted 14 October 2011 - 01:34 AM

Hi,

With your help my compare statement works. Now I am working on Start-Stop program.
At the moment I have simple question OK or Cancel.

How is declaring a function to If, while, or other statements ... I mean that I want to call from function Abort in the If statement function play -
Do I have to always declare new VAR for this. Like : Var play = play(userchoice) and then like run play or is there other options for that kind of declaring.

Next thing I was thinking of putting into abort function instead of IF statement While Statement.
SO let's say I will have a Start-Stop-Restart buttons. While Start the play function executes after each time I press Rock Paper Scissors. Stop stops the execution.

Will this be better?


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>

<head>
<script type='text/javascript'>
window.onload=abort;

function possibility(){

  var poss = ['rock', 'paper', 'scissors'];
  return poss;
  
  }
  
  function randomGenerator() {
   
   var poss = possibility();
   var compRandom = poss[Math.floor(Math.random()*poss.length)];
   return compRandom;
	// create a random number representing rock/paper/scissors
}

function play(userchoice) {
  // var save = new object ();
   var convert = randomGenerator();
   var computerchoice = convert.split();
   			
		
	// do some processing to be able to compare both values …
    // compare/get winner

if ( (userchoice == ('rock') && computerchoice == ('scissors')) || (userchoice==('paper') && computerchoice==('rock')) || (userchoice==('scissors') && computerchoice==('paper') ))
{ // win
alert ('You choose ' + userchoice +' and computer choose '+computerchoice+'You Won!')
}
else if (userchoice == computerchoice)
{ // draw
alert ('You choose ' + userchoice +' and computer choose '+computerchoice+' It is a TIE')

}

else if ((userchoice==('paper') && computerchoice==('scissors')) || (userchoice==('scissors') && computerchoice==('rock')) || (userchoice==('rock') && computerchoice==('paper')) )
{// loose
alert (' You choose ' + userchoice +' and computer choose '+computerchoice+' You lost!')

	}
	
	// save results
  //  save()
    // display results
  //  display()

  }
    
function abort() {
    var r=confirm("Do you want to play Rock, Paper, Scissors");
	if (r==true)
  {
  event.play;
  }
else
  {
  alert("Good luck!");
  }
}
	
	
	
	// reset game, leave pageF

//event.define(play)
//event.define(abort)


 
</script>
</head>

			<input type="button" value="Rock" onclick="play('rock')">
            <input type="button" value="Paper" onclick="play('paper')">
            <input type="button" value="Scissors" onclick="play('scissors')">
			


</html>



Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2