-
Notifications
You must be signed in to change notification settings - Fork 10
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
hw 4, hopefully meets all requirements & bonuses #7
Open
sad3xoxo
wants to merge
3
commits into
jsr20190826:master
Choose a base branch
from
sad3xoxo:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
---|---|---|
|
@@ -55,3 +55,7 @@ button:active { | |
#status { | ||
margin-top: 20px; | ||
} | ||
|
||
.restartButton { | ||
display: none; | ||
} |
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
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,2 +1,232 @@ | ||
//this line prevents my editor from marking $ as undefined | ||
/* global $ */ | ||
|
||
$(function () { | ||
// select an option at random for the both | ||
function chooseBotWeapon () { | ||
const weapons = ['rock', 'paper', 'scissors'] | ||
const n = Math.floor(Math.random() * 3) | ||
const botWeapon = weapons[n] | ||
return botWeapon | ||
} | ||
|
||
// determine who wins the current hand | ||
function whoWins (playerChoice, winnerList) { | ||
// select the bot weapon | ||
const botChoice = chooseBotWeapon() | ||
|
||
// update the page text with current turn stats | ||
$('#playerChoice').text(`${playerChoice}`) | ||
$('#botChoice').text(`${botChoice}`) | ||
// if the same weapons were chosen, update page to show that it was a tie. | ||
// also return nobody to the winnerList since no one won the turn | ||
if (playerChoice === botChoice) { | ||
$('#showWinner').text('It was a tie!') | ||
return 'nobody' | ||
} | ||
|
||
// logic to determine who won the current hand | ||
// return the winner | ||
switch (botChoice) { | ||
case 'rock': | ||
if (playerChoice === 'scissors') { | ||
return 'bot' | ||
} else if (playerChoice === 'paper') { | ||
return 'player' | ||
} | ||
break | ||
|
||
case 'paper': | ||
if (playerChoice === 'rock') { | ||
return 'bot' | ||
} else if (playerChoice === 'scissors') { | ||
return 'player' | ||
} | ||
break | ||
|
||
case 'scissors': | ||
if (playerChoice === 'paper') { | ||
return 'bot' | ||
} else if (playerChoice === 'rock') { | ||
return 'player' | ||
} | ||
break | ||
default: | ||
console.log('Both players must choose an item!') | ||
return 'nobody' | ||
} | ||
} | ||
|
||
// update scoreboard page elements | ||
function updateScore (winner) { | ||
console.log(`running score count. adding points for ${winner}`) | ||
// clear any bonus info from Scoreboard | ||
$('#bonus').text('') | ||
// get details from scoreboard | ||
let currentHumanScore = parseInt($('#humanScore').text()) | ||
let currentBotScore = parseInt($('#computerScore').text()) | ||
// update the scoreboard and text with winner info | ||
if (winner === 'player') { | ||
currentHumanScore++ | ||
$('#showWinner').text('You win! :D') | ||
$('#humanScore').text(currentHumanScore) | ||
} else if (winner === 'bot') { | ||
currentBotScore++ | ||
$('#showWinner').text('You lose :(') | ||
$('#computerScore').text(currentBotScore) | ||
} | ||
} | ||
|
||
// calculate bonus points for this turn | ||
function calculateBonusPoints (winnerList) { | ||
const n = winnerList.length - 1 | ||
// grab the last 3 winners from the winnerList | ||
const one = winnerList[n] | ||
const two = winnerList[n - 1] | ||
const three = winnerList[n - 2] | ||
|
||
// player who wins the last three rounds gets a two point bonus | ||
if (one === two && two === three) { | ||
if (one === 'nobody') { | ||
console.log('no bonus for tie rounds.') | ||
} else { | ||
// add two bonus points and update scoreboard | ||
updateScore(winnerList[n]) | ||
updateScore(winnerList[n]) | ||
Comment on lines
+94
to
+95
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. @sad3xoxo could have also passed a 1 or 2 depending on the conditional statement and whether to add bonus points. it still works! |
||
$('#bonus').text(`Bonus: 2 pts for ${one}!`) | ||
} | ||
} else { | ||
console.log('no bonus detected.') | ||
} | ||
} | ||
|
||
// determine if there's a winner for the round | ||
function calculateRoundWinner () { | ||
//get score from page | ||
const humanScore = parseInt($('#humanScore').text()) | ||
const botScore = parseInt($('#computerScore').text()) | ||
|
||
if (humanScore >= 10) { | ||
// show a message that says Human wins the round! | ||
// add a point for the round | ||
alert('Congrats human, you\'ve won the round!') | ||
updateRoundScore('player') | ||
} else if (botScore >= 10) { | ||
// show a message that says Bot wins the round!\ | ||
// add a point for the round | ||
alert('Aww, you\'ve lost the round. :(') | ||
updateRoundScore('bot') | ||
} else { | ||
// no one has won this round | ||
// just in case | ||
console.log('Round 1: in progress') | ||
} | ||
} | ||
|
||
function updateRoundScore (winner) { | ||
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 is messy, but I just wanted to get this assignment handed in. | ||
// apologies for the poor planning here. :) | ||
|
||
// if round 1 has not been posted to the board already, | ||
// this must be round 1. update page with the winner | ||
if ($('#round1').text() === '') { | ||
const pageUpdate = `<p id="round1">Round 1: | ||
<b id="winner1">${winner}</b>\n</p>` | ||
$('#roundTracking').append(pageUpdate) | ||
clearScore() | ||
} else if ($('#round2').text() === '') { | ||
// if round 2 has not been posted to the board already, | ||
// this must be round 2. update page with the winner | ||
const pageUpdate = `<p id="round2">Round 2: | ||
<b id="winner2">${winner}</b></p>` | ||
$('#roundTracking').append(pageUpdate) | ||
// if the same person won twice, declare a winner and prompt | ||
// for a new game | ||
if ($('#winner1').text() === $('#winner2').text()) { | ||
const announceGameWin = `${winner} wins the Game!` | ||
alert(announceGameWin) | ||
promptNewGame() | ||
} | ||
clearScore() | ||
} else { | ||
// if round 3 has not been posted to the board already, | ||
// this must be round 3. | ||
const pageUpdate = `<p id="round3">Round 3: | ||
<b id="winner3">${winner}</b></p>` | ||
$('#roundTracking').append(pageUpdate) | ||
// there will definitely be a game winner this time, | ||
// so we'll determine and announce winner, then prompt for new game | ||
if ($('#winner1').text() === $('#winner3').text() || $('#winner2').text() === | ||
$('#winner3').text()) { | ||
const announceGameWin = `${winner} wins the Game!` | ||
alert(announceGameWin) | ||
promptNewGame() | ||
} | ||
// if you end up here that means the winner was determined | ||
// in round 2, so prompt for a new game | ||
promptNewGame() | ||
} | ||
} | ||
|
||
function clearScore () { | ||
// clear the scoreboard, obviouslyy | ||
$('#humanScore').text('0') | ||
$('#computerScore').text('0') | ||
$('#showWinner').text('') | ||
// clear any bonus info from Scoreboard | ||
$('#bonus').text('') | ||
} | ||
|
||
function promptNewGame () { | ||
// change the buttons around | ||
// the new button will ask if the user wants to start a new game | ||
// hence the name | ||
const hideButton = { display: 'none' } | ||
const revealButton = { display: 'inline' } | ||
$('.playButtons').css(hideButton) | ||
console.log('hide game buttons') | ||
$('.restartButton').css(revealButton) | ||
console.log('reveal the restart button') | ||
} | ||
|
||
const winnerList = [] | ||
|
||
function playerTurn (weapon, winnerList) { | ||
//all te stuff that happens when the user takes their turn | ||
const winner = whoWins(weapon) | ||
updateScore(winner) | ||
console.log('score updated') | ||
console.log(winnerList) | ||
return winner | ||
} | ||
|
||
// determine who wins the current round | ||
$('#rock').click(() => { | ||
winnerList.push(playerTurn('rock', winnerList)) | ||
// this is all being calculated here 'cause I didn't take | ||
// the time to plan this properly :) | ||
// ideally it would be lumped into the playerTurn() function | ||
calculateBonusPoints(winnerList) | ||
calculateRoundWinner() | ||
}) | ||
$('#paper').click(() => { | ||
winnerList.push(playerTurn('paper', winnerList)) | ||
calculateBonusPoints(winnerList) | ||
calculateRoundWinner() | ||
}) | ||
$('#scissors').click(() => { | ||
winnerList.push(playerTurn('scissors', winnerList)) | ||
calculateBonusPoints(winnerList) | ||
calculateRoundWinner() | ||
}) | ||
$('#restart').click(() => { | ||
// when this button is visible the user can click it to restart the game | ||
clearScore() | ||
const reveal = { display: 'inline' } | ||
$('.playButtons').css(reveal) | ||
const hide = { display: 'none' } | ||
$('.restartButton').css(hide) | ||
// clear round tracking | ||
$('#roundTracking').text('') | ||
}) | ||
}) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
@sad3xoxo interesting take on getting 3 consecutive wins. another approach could be to increment a consecutive win variable for human and computer and it resets to zero when the other player scores and does not reach 3 consecutive wins. it saves memory because the
winnerList
never reset and it stores items even after a final winner. In any case, great job adding the additional features to the game!!! 👍