One of the earlier programs I wrote in Python was Scissor, Paper, Stone. It’s an old game that most people learn as a child, the premise of which is;
- There are two (2) players.
- Each player must make a secret choice from 3 options (Scissor, Paper or Stone)
- The players reveal their choice at the same time
- Scissor beats paper. Paper beats Stone. Stone beats Scissor.
Turning this into a playable game;
- Player one (1) is the user (human)
- Player two (2) is the computer
- Player one will need an input option to choose Scissor, Paper or Stone
- Player two will need a random number generator to generate a number between 1 and 3, each number is referenced to one of the selections (e.g. 1 equals Scissor).
- Player one’s and Player two’s selections must then be compared and a winner announced.
I’m writing the game in a text editor and then running it in the web browsers JavaScript console.
First I tackled the random number generation for the computer, using JavaScripts Random() function:
// geektechstuff var numberGuess = Math.floor((Math.random()*3+1)); var computerChoice if (numberGuess == 1) { computerChoice = 'Paper'; } else if (numberGuess == 2){ computerChoice = 'Scissor'; } else if (numberGuess == 3){ computerChoice = 'Stone'; } else { computerChoice = 'Error!' } console.warn(computerChoice)
The I added in the ability for a user to enter a number for their choice:
// geektechstuff var numberGuess = Math.floor((Math.random()*3+1)); var computerChoice var userChoice = prompt("Please enter a number 1, 2 or 3", "1"); var userNamedChoice if (numberGuess == 1) { computerChoice = 'Paper'; } else if (numberGuess == 2){ computerChoice = 'Scissor'; } else if (numberGuess == 3){ computerChoice = 'Stone'; } else { computerChoice = 'Error!' } if (userChoice == 1) { userNamedChoice = 'Paper'; } else if (userChoice == 2){ userNamedChoice = 'Scissor'; } else if (userChoice == 3){ computerChoice = 'Stone'; } else { userNamedChoice = 'Error!' } console.warn('Computer chose:', computerChoice) console.warn('Player chose:', userNamedChoice)
Now the program needs some refining.
// geektechstuff // variables var numberGuess = Math.floor((Math.random()*3+1)); var computerChoice = ''; var userChoice = parseInt(prompt("Please enter a number 1 (Paper), 2 (Scissor) or 3 (Stone)", "1")); var userNamedChoice = ''; var resultOfGame = ''; if (numberGuess === 1) { computerChoice = 'Paper'; } else if (numberGuess === 2){ computerChoice = 'Scissor'; } else if (numberGuess === 3){ computerChoice = 'Stone'; } else { computerChoice = 'Error'; } if (userChoice === 1) { userNamedChoice = 'Paper'; } else if (userChoice === 2){ userNamedChoice = 'Scissor'; } else if (userChoice === 3){ userNamedChoice = 'Stone'; } else { userNamedChoice = 'Error'; } // Checks to see who has won if (numberGuess == userChoice){ resultOfGame = 'Draw '; } else if ((computerChoice = 'Paper')&&(userNamedChoice = 'Scissor')){ resultOfGame = 'Player Wins'; } else if ((computerChoice = 'Scissor')&&(userNamedChoice = 'Stone')) { resultOfGame = 'Player Wins'; } else if ((computerChoice = 'Stone')&&(userNamedChoice = 'Paper')) { resultOfGame = 'Player Wins'; } else { resultOfGame = 'Computer Wins'; } // declares results console.warn('Computer Chose:', computerChoice); console.warn('Player Chose:', userNamedChoice); console.warn(resultOfGame);
For more information of Scissor, Paper, Stone see: https://en.wikipedia.org/wiki/Rock-paper-scissors
One thought on “Scissor, Paper, Stone (JavaScript)”
Comments are closed.