Below is my Rock, paper, scissors game as I’ve written it. I am no javascript expert, but I try. I “elevated” the logic behind what the computer chooses from most versions of the game detail. All of the logic works as written. However, it only works when in the developer console, and I step it along step by step. If I run it normally in the browser it just keeps asking for the user’s input. It doesn’t ever print out the user’s choice, computer’s choice nor the result of the game. This next sentence may explain my ignorance in javascript, but in order to use some of the logic, I wanted to use I set up an array at the beginning that I input info and reference throughout. To allow me to start the game over without resetting the browser and therefore losing my array data, I set it up in a loop. I’m sure there is a “simple” reason why this is happening. I’m open to any input or suggestions on the game. Thank you for your help.
<!DOCTYPE html>
<html lang="en-us">
<head>
<title>Rock, Paper, or Scissors</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!-- <link rel="stylesheet" media="screen and (min-width: 600px)" href="../css/main.css">
<link href="https://fonts.googleapis.com/css?family=Gloria+Hallelujah" rel="stylesheet">-->
</head>
<body>
<header>
</header>
<main>
<!-- <form>
<fieldset>
<input type="button" value="Rock">
<br>
<input type="button" value="Paper">
<br>
<input type="button" value="Scissors">
</fieldset>
</form>-->
<p id="demo"> </p>
<script>
//My array
var rPS = new Array(0, 0, 0);
do {
// User choice
var userChoice = prompt("Do you choose rock, paper or scissors?");
if (! userChoice) {
// User choice was undefined
document.write("<p>Player 1, you cheated! Refresh this screen and fight like a man.</p>");
} else {
// Display user choice
document.write("<p>Player 1:" + " " + userChoice + "</p>");
}
// Computer choice
// Ideas:
// 1) If there is no previous hand have computer play paper
// 2) If the other player wins play the opposite on the next hand.
// 3) If you win never play the same option twice in a row.
// https://www.psychologytoday.com/us/blog/the-blame-game/201504/the-surprising-psychology-rock-paper-scissors
// https://statmodeling.stat.columbia.edu/2007/05/21/how_to_win_at_r/
var computerChoice;
if (computerChoice == null) {
computerChoice = "paper";
} else if (rPS [2] === 1) {
if (rPS [0] === "paper"){
computerChoice = "scissors";
} else if (rPS[0] === "scissors") {
computerChoice = "rock";
} else (computerChoice = "paper")
} else if (rPS [2] === 2){
if (rPS [1] === "paper"){
computerChoice = Math.random();
if (computerChoice < 0.50){
computerChoice = "rock";
} else {
computerChoice = "scissors";
}
} else if (rPS [1] === "rock"){
computerChoice = Math.random();
if (computerChoice < 0.50){
computerChoice = "paper";
} else {
computerChoice = "scissors";
}
} else {
computerChoice = Math.random();
if (computerChoice < 0.50){
computerChoice = "rock";
} else {
computerChoice = "paper";
}
}
} else {
if (rPS [2] === 0){
if (rPS[1] === "paper") {
computerChoice = Math.random();
if (computerChoice < 0.50){
computerChoice = "rock";
} else if ( computerChoice <= 0.75){
computerChoice = "paper";
} else {
computerChoice = "scissors";
}
} else if (rPS[1] === "rock") {
computerChoice = Math.random();
if (computerChoice < 0.50){
computerChoice = "scissors";
} else if ( computerChoice <= 0.75){
computerChoice = "paper";
} else {
computerChoice = "rock";
}
} else {
computerChoice = Math.random();
if (computerChoice < 0.50){
computerChoice = "paper";
} else if ( computerChoice <= 0.75){
computerChoice = "rock";
} else {
computerChoice = "scissors";
}
}
}
}
//document.getElementById("demo").innerHTML = rPS;
// Display computer choice
document.write("<p>Computer:" + " " + computerChoice + "</p>");
// Compare user choice vs computer choice
var compare = function(choice1,choice2) {
if (choice1 === choice2) {
return "It's a tie!";
}
if (choice1 === "rock") {
if (choice2 === "scissors") {
// rock wins
return "You win!";
} else {
// paper wins
return "You lose! Try again.";
}
}
if (choice1 === "paper") {
if (choice2 === "rock") {
// paper wins
return "You win!";
} else {
// scissors wins
return "You lose! Try again.";
}
}
if (choice1 === "scissors") {
if (choice2 === "rock") {
// rock wins
return "You lose! Try again.";
} else {
// scissors wins
return "You win!";
}
}
};
// Run the compare function
var results = compare(userChoice,computerChoice);
//logging info to array at top
rPS[0] = userChoice;
rPS[1] = computerChoice;
//from comps point of view
if (results == "It's a tie!") {
rPS[2] = 0;
} else if (results == "You win!") {
//loss
rPS[2] = 1;
} else {
//win
rPS[2] = 2;
}
document.getElementById("demo").innerHTML = rPS;
// Display results
var final = "<br><hr><br>" + results;
document.write(final);
var play = "yes"; //prompt("Would you like to play Again?");
} while (play === "yes" || play === "Yes" || play === "YES");
</script>
</main>
</body>
</html>