Skip to content

Commit

Permalink
Refactor Rock Paper Scissors game logic and improve code structure
Browse files Browse the repository at this point in the history
  • Loading branch information
gitclonehama committed Jun 26, 2024
1 parent b726c7c commit 2896320
Showing 1 changed file with 75 additions and 42 deletions.
117 changes: 75 additions & 42 deletions script.js
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);

0 comments on commit 2896320

Please sign in to comment.