-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Alternative control flow #3
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,127 +15,86 @@ function getCpuChoice () { | |
return cpuChoice; | ||
} | ||
|
||
const rockBtn = document.getElementById('rock'); | ||
rockBtn.addEventListener('click', rockChoice); | ||
function rockChoice () { | ||
console.log('The function rockChoice() is running....'); | ||
cpuButtonHighlightRemoval(cpuRock); | ||
cpuButtonHighlightRemoval(cpuPaper); | ||
cpuButtonHighlightRemoval(cpuScissors); | ||
let winnerOfRound = ''; | ||
let cpuChoice = getCpuChoice(); | ||
let playerChoice = 'rock'; | ||
if (playerChoice === cpuChoice) { | ||
winnerOfRound = 'Draw, there is no winner'; | ||
console.log(winnerOfRound); | ||
cpuButtonHighlight(cpuRock); | ||
playerCPUDraw(playerScore, cpuScore); | ||
return changeBannerText(winnerOfRound); | ||
} | ||
else if (playerChoice == 'rock' && cpuChoice === 'paper') { | ||
winnerOfRound = 'You lose! Paper beats rock!'; | ||
console.log(winnerOfRound); | ||
cpuButtonHighlight(cpuPaper); | ||
cpuIncrementScore(cpuScore, cpuScoreElement); | ||
return changeBannerText(winnerOfRound); | ||
} | ||
else if (playerChoice == 'rock' && cpuChoice === 'scissors') { | ||
winnerOfRound = 'You win! Rock beats scissors!'; | ||
console.log(winnerOfRound); | ||
cpuButtonHighlight(cpuScissors); | ||
playerIncrementScore(playerScore, playerScoreElement); | ||
return changeBannerText(winnerOfRound); | ||
|
||
// 0 == tie, 1 == victory, -1 == loss | ||
const resultMatrix = [ | ||
// CPU | ||
// rock, paper, scissors | ||
/*Player rock */[ 0, -1, 1], | ||
/* paper */[ 1, 0, -1], | ||
/* scissors */[-1, 1, 0] | ||
|
||
] | ||
|
||
function determineResult(player, cpu) { | ||
// rock = 0, paper = 1, scissors = 2 | ||
const choice = { | ||
"rock": 0, | ||
"paper": 1, | ||
"scissors": 2 | ||
} | ||
Comment on lines
+31
to
35
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this choice to integer mapping I'm using isn't so great. There's a type of data structure called an ENUM in other languages including TypeScript that would have been a good fit of mapping. They're generally a good strategy when you want to represent something using an integer, which is generally more robust than using strings, which can be prone to error. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
return winnerOfRound; | ||
const playerVal= choice[player] | ||
const cpuVal = choice[cpu] | ||
return resultMatrix[playerVal][cpuVal] | ||
} | ||
|
||
const paperBtn = document.getElementById('paper'); | ||
paperBtn.addEventListener('click', playRoundPaperChoice); | ||
function playRoundPaperChoice () { | ||
console.log('The function paperChoice() is running....'); | ||
cpuButtonHighlightRemoval(cpuRock); | ||
cpuButtonHighlightRemoval(cpuPaper); | ||
cpuButtonHighlightRemoval(cpuScissors); | ||
let winnerOfRound = ''; | ||
function playRound(event) { | ||
// remove existing and get new choice | ||
cpuButtonHighlightRemoval(); | ||
let cpuChoice = getCpuChoice(); | ||
let playerChoice = 'paper'; | ||
if (playerChoice === cpuChoice) { | ||
winnerOfRound = 'Draw, there is no winner'; | ||
console.log(winnerOfRound); | ||
cpuButtonHighlight(cpuPaper); | ||
playerCPUDraw(playerScore, cpuScore); | ||
return changeBannerText(winnerOfRound); | ||
} | ||
else if (playerChoice == 'paper' && cpuChoice === 'rock'){ | ||
winnerOfRound = 'You win! Paper beats rock!'; | ||
console.log(winnerOfRound); | ||
cpuButtonHighlight(cpuRock); | ||
playerIncrementScore(playerScore, playerScoreElement); | ||
return changeBannerText(winnerOfRound); | ||
} | ||
else if (playerChoice == 'paper' && cpuChoice === 'scissors') { | ||
winnerOfRound = 'You lose! Scissors beats paper!'; | ||
console.log(winnerOfRound); | ||
cpuButtonHighlight(cpuScissors); | ||
cpuIncrementScore(cpuScore, cpuScoreElement); | ||
return changeBannerText(winnerOfRound); | ||
cpuButtonHighlight(cpuChoice); | ||
|
||
let playerChoice = event.currentTarget.value; | ||
|
||
// get result and print banner | ||
const result = determineResult(playerChoice, cpuChoice); | ||
if (result === 0) { | ||
bannerText = 'Draw, there is no winner'; | ||
} else if (result === 1) { | ||
bannerText = `You win! ${playerChoice} beats ${cpuChoice}`; | ||
playerIncrementScore(); | ||
} else { | ||
bannerText = `You lose! ${cpuChoice} beats ${playerChoice}`; | ||
cpuIncrementScore(); | ||
} | ||
return winnerOfRound; | ||
changeBannerText(bannerText); | ||
} | ||
|
||
const rockBtn = document.getElementById('rock'); | ||
rockBtn.addEventListener('click', playRound); | ||
|
||
const paperBtn = document.getElementById('paper'); | ||
paperBtn.addEventListener('click', playRound); | ||
|
||
const scissorsBtn = document.getElementById('scissors'); | ||
scissorsBtn.addEventListener('click', playRoundScissorsChoice); | ||
function playRoundScissorsChoice() { | ||
console.log('The function paperChoice() is running....'); | ||
cpuButtonHighlightRemoval(cpuRock); | ||
cpuButtonHighlightRemoval(cpuPaper); | ||
cpuButtonHighlightRemoval(cpuScissors); | ||
let winnerOfRound = ''; | ||
let cpuChoice = getCpuChoice(); | ||
let playerChoice = 'scissors'; | ||
if (playerChoice === cpuChoice) { | ||
winnerOfRound = 'Draw, there is no winner'; | ||
console.log(winnerOfRound); | ||
cpuButtonHighlight(cpuScissors); | ||
playerCPUDraw(playerScore, cpuScore); | ||
return changeBannerText(winnerOfRound); | ||
} | ||
else if (playerChoice == 'scissors' && cpuChoice === 'rock') { | ||
winnerOfRound = 'You lose! Rock beats scissors!'; | ||
console.log(winnerOfRound); | ||
cpuButtonHighlight(cpuRock); | ||
cpuIncrementScore(cpuScore, cpuScoreElement); | ||
return changeBannerText(winnerOfRound); | ||
} | ||
else if (playerChoice == 'scissors' && cpuChoice === 'paper') { | ||
winnerOfRound = 'You win! Scissors beats paper!'; | ||
console.log(winnerOfRound); | ||
cpuButtonHighlight(cpuPaper); | ||
playerIncrementScore(playerScore, playerScoreElement); | ||
return changeBannerText(winnerOfRound); | ||
} | ||
return winnerOfRound; | ||
} | ||
scissorsBtn.addEventListener('click', playRound); | ||
|
||
const cpuScissors = document.querySelector('#cpu-scissors'); | ||
const cpuPaper = document.querySelector('#cpu-paper'); | ||
const cpuRock = document.querySelector('#cpu-rock'); | ||
function cpuButtonHighlight(cpuBtn) { | ||
function cpuButtonHighlight(cpuChoice) { | ||
console.log('The function cpuButtonHighlight() is now running....'); | ||
let cpuSelection = cpuBtn; | ||
cpuSelection.classList.add('cpu-selection'); | ||
if (cpuChoice === "rock") { | ||
cpuRock.classList.add('cpu-selection'); | ||
} else if (cpuChoice === "paper") { | ||
cpuPaper.classList.add('cpu-selection'); | ||
} else { | ||
cpuScissors.classList.add('cpu-selection'); | ||
} | ||
} | ||
function cpuButtonHighlightRemoval(cpuBtn){ | ||
function cpuButtonHighlightRemoval(cpuChoice){ | ||
console.log('The function cpuButtonHighLightRemoval() is now running'); | ||
let cpuSelection = cpuBtn; | ||
cpuSelection.classList.remove('cpu-selection'); | ||
cpuRock.classList.remove('cpu-selection'); | ||
cpuPaper.classList.remove('cpu-selection'); | ||
cpuScissors.classList.remove('cpu-selection'); | ||
} | ||
|
||
const announcementBanner = document.querySelector('.announcement'); | ||
announcementBanner.addEventListener('click', restartGameCycle); | ||
function restartGameCycle () { | ||
location.reload(); | ||
} | ||
|
||
function changeBannerText(winnerOfRound) { | ||
console.log('The function changeBannerText() is now running....'); | ||
if (playerScore < 5 && cpuScore < 5) { | ||
|
@@ -154,20 +113,16 @@ var playerScore = 0; | |
var cpuScore = 0; | ||
const playerScoreElement = document.querySelector('#player-score'); | ||
const cpuScoreElement = document.querySelector('#cpu-score'); | ||
function playerIncrementScore (score, scoreElement) { | ||
function playerIncrementScore() { | ||
console.log('The function playerIncrementScore() is now running....'); | ||
playerScore++; | ||
playerScoreElement.innerHTML = `Score: ${playerScore}`; | ||
} | ||
function cpuIncrementScore (score, scoreElement) { | ||
function cpuIncrementScore() { | ||
console.log('The function cpuIncrementScore() is now running....'); | ||
cpuScore++; | ||
cpuScoreElement.innerHTML = `Score: ${cpuScore}`; | ||
} | ||
function playerCPUDraw(playerScore, cpuScore) { | ||
cpuScore += 0; | ||
playerScore += 0; | ||
} | ||
// Legacy Code base | ||
// const pbtns = document.querySelectorAll('.pbtn'); | ||
// pbtns.forEach(btns => btns.addEventListener('click', playerButtonSelection)); | ||
|
@@ -254,4 +209,4 @@ function playerCPUDraw(playerScore, cpuScore) { | |
// } | ||
// } | ||
|
||
// Caelan Miller - 2022 | ||
// Caelan Miller - 2022 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pros:
We have a central place to modify results if we were to extend this. What if we put a spin on this game and reverse the outcomes? So paper > scissors > rock > paper. We could change the values here instead of modifying all the
if/else
. Also what if we wanted to add a new choice, say 'sword', that beats scissors and paper. We could easily change the values here, but having a separate handler withif/else
would result in exponentially more branches to write out.Cons:
Pretty difficult to read. Converting the button click to it's value and then a numerical choice for the indices (lines 30-34 below) has a lot of indirection that can be confusing.