-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor Rock Paper Scissors game logic and improve code structure
- Loading branch information
1 parent
b726c7c
commit 2896320
Showing
1 changed file
with
75 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,54 +1,87 @@ | ||
|
||
|
||
function getComputerChoice() { | ||
const choices = ['rock', 'paper', 'scissors']; | ||
const randomChoice = Math.floor(Math.random() * 3); | ||
return choices[randomChoice]; | ||
const choices = ['rock', 'paper', 'scissors']; | ||
const randomChoice = Math.floor(Math.random() * 3); | ||
return choices[randomChoice]; | ||
} | ||
|
||
function getHumanChoice() { | ||
const choices = ['rock', 'paper', 'scissors']; | ||
let humanChoice = prompt('Choose rock, paper, or scissors:').toLowerCase(); | ||
while (!choices.includes(humanChoice)) { | ||
humanChoice = prompt('Invalid choice. Choose rock, paper, or scissors:').toLowerCase(); | ||
} | ||
return humanChoice; | ||
const choices = ['rock', 'paper', 'scissors']; | ||
let humanChoice = prompt('Choose rock, paper, or scissors:').toLowerCase(); | ||
while (!choices.includes(humanChoice)) { | ||
humanChoice = prompt('Invalid choice. Choose rock, paper, or scissors:').toLowerCase(); | ||
} | ||
return humanChoice; | ||
} | ||
|
||
function playRound(humanChoice, computerChoice) { | ||
if (humanChoice === computerChoice){ | ||
console.log(`You both chose ${humanChoice}, it's a tie!`) // Using template literal | ||
}else { // human != computer choice | ||
let winner = '' | ||
switch(humanChoice){ | ||
case 'rock': | ||
if (computerChoice === 'scissors'){ | ||
winner = 'human' | ||
console.log('You win! Rock beats scissors!') | ||
}else{ // computer chose 'paper' | ||
winner = 'computer' | ||
console.log('You lose! Paper beats rock!') | ||
} | ||
break; | ||
case 'paper': | ||
if (computerChoice === 'rock'){ | ||
winner = 'human' | ||
console.log('You win! Paper beats rock!') | ||
}else{ // computer chose 'scissors' | ||
winner = 'computer' | ||
console.log('You lose! Scissors beat paper!') | ||
} | ||
break; | ||
case 'scissors': | ||
if (computerChoice === 'paper'){ | ||
winner = 'human' | ||
|
||
console.log('You win! Scissors beat paper!') | ||
}else{ // computer chose 'rock' | ||
winner = 'computer' | ||
console.log('You lose! Rock beats paper!') | ||
} | ||
break; | ||
} | ||
return winner; | ||
} | ||
} | ||
|
||
let humanScore = 0; | ||
let computerScore = 0; | ||
|
||
function playRound(humanChoice, computerChoice){ | ||
if (humanChoice === computerChoice){ | ||
console.log(`You both chose ${humanChoice}, it's a tie!`) // Using template literal | ||
}else{ // human != computer choice | ||
switch(humanChoice){ | ||
case 'rock': | ||
if (computerChoice === 'scissors'){ | ||
console.log('You win! Rock beats scissors!') | ||
}else{ // computer chose 'paper' | ||
console.log('You lose! Paper beats rock!') | ||
} | ||
break; | ||
case 'paper': | ||
if (computerChoice === 'rock'){ | ||
console.log('You win! Paper beats rock!') | ||
}else{ // computer chose 'scissors' | ||
console.log('You lose! Scissors beat paper!') | ||
} | ||
break; | ||
case 'scissors': | ||
if (computerChoice === 'paper'){ | ||
console.log('You win! Scissors beat paper!') | ||
}else{ // computer chose 'rock' | ||
console.log('You lose! Rock beats paper!') | ||
} | ||
break; | ||
} | ||
function playGame() { | ||
let humanScore = 0; | ||
let computerScore = 0; | ||
|
||
let rounds = 5; | ||
while (rounds > 0 && rounds <= 5){ | ||
let humanSelection = getHumanChoice(); | ||
let computerSelection = getComputerChoice(); | ||
let winner = playRound(humanSelection, computerSelection); | ||
if (winner === 'human'){ | ||
humanScore++; | ||
}else if (winner === 'computer'){ | ||
computerScore++; | ||
} | ||
} | ||
if (humanScore != computerScore) { | ||
(humanScore > computerScore) ? | ||
console.log(`You win! Your score: ${humanScore} Computer score: ${computerScore}`) : | ||
console.log(`You lose! Your score: ${humanScore} Computer score: ${computerScore}`); | ||
} | ||
} | ||
|
||
const humanSelection = getHumanChoice(); | ||
const computerSelection = getComputerChoice(); | ||
|
||
playRound(humanSelection,computerSelection); | ||
|
||
|
||
|
||
|
||
// const humanSelection = getHumanChoice(); | ||
// const computerSelection = getComputerChoice(); | ||
|
||
// playRound(humanSelection,computerSelection); |