Skip to content
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

Homework-04 #5

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,21 @@ <h2>Scoreboard</h2>
</table>
</div>

<h2>Choose Wisely</h2>
<h2 id="machineChoice">Choose Wisely</h2>

<button id="rock">Rock</button>
<button id="rock">rock</button>

<button id="paper">Paper</button>
<button id="paper">paper</button>

<button id="scissors">Scissors</button>
<button id="scissors">scissors</button>

<div id="status"></div>
</div>

<script src="js/jquery-2.1.4.js"></script>
<script
src="https://code.jquery.com/jquery-3.4.1.min.js"
integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo="
crossorigin="anonymous"></script>
<script src="js/main.js"></script>
</body>
</html>
123 changes: 123 additions & 0 deletions js/main.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,125 @@
$(function () {

// Rock Paper Scissor routine input user choice, generate random computer response
// find out who won, update score and then present result in table on screen
// There seems to be a bias in the program toward the user -- cause not clear
// First define globale variables that are used to display results or simplify flow:
let userWins=0
let machineWins=0
let machineInput=NaN
let userInput=NaN
Comment on lines +9 to +10

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@AtleLohrmann The input corresponds to a string when it's assigned, so having either an empty string or null would be a better candidate for a default value. However, Great job on experimenting with other data types for default values.


// The evenhandler is where we are waiting for input
// Very much in contrast to "normal linear programming"
// Everything emenates from here

// If the user clicks "rock":
$('#rock').click(() => {
let userInput = $('#rock').text()

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@AtleLohrmann you can also grab the text from $(this) because this would refer to the object clicked.
Also, because we know which button was clicked, you can use an inline string with the respective button's value.

check out this approach

mainLoop(userInput)
})
// If the user clicks "paper":
$('#paper').click(() => {
let userInput = $('#paper').text()
mainLoop(userInput)
})
// If the user clicks "scissors":
$('#scissors').click(() => {
let userInput = $('#scissors').text()
mainLoop(userInput)
})

//The mainLoop is where the action is after we have received user input
function mainLoop(userInput){
// first get the input from the machine into "MachineInput"
getMachineInput()
// find out who wins using the function from previous home work
winner=findWinner(userInput,machineInput)
// Depending on who wins, update total scores
updateScores(winner)
// change score board
$('#humanScore').text(userWins)
$('#computerScore').text(machineWins)
// Extra info for the user for clarity and also debugging -- a little awkward but it works
if (winner===1) {gwin="User"} else {gwin="Bot"} //transfer winner to text that we can use
$('#status').html("Computer played " + '<u>'+machineInput+'</u>' + '.' + '\n'
+ "You played " + '<u>'+userInput+'</u>'+'.' + '</br>' + gwin + " Won" )

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@AtleLohrmann the UI does not account for tied games

check solution here

// All done - go back to evenhandler
}


// The function findwinner is only slightly modified from previous homework
// Output 0 for a tie
// Output 1 for user wins (playerA)
// Output 2 for machine wins (player B)
function findWinner(playerAChoice,playerBChoice) {
if (playerAChoice == playerBChoice) {
return 0
console.log("Result: Tie")
}
else if (playerAChoice == "rock"){
if (playerBChoice == "paper") {
return 2
console.log("Result: PlayerB wins")
}
else if (playerBChoice == "scissors"){
return 1
console.log("Result: PlayerA wins")
}
}
else if (playerAChoice == "scissors"){
if (playerBChoice == "rock"){
return 2
console.log("Result: PlayerB wins")
}
else if (playerBChoice == "paper"){
return 1
console.log("Result: PlayerA wins")
}
}
else if (playerAChoice == "paper"){
if (playerBChoice == "scissors"){
return 2
console.log("Result: PlayerB wins")
}
else if (playerBChoice == "rock"){
return 1
console.log("Result: PlayerA wins")
}
}
}

// let us find out what the machine choses
function getMachineInput() {
// generate a random integer [0:2]
let randomInt = getRandomInt(3)
if (randomInt===0) {
choice='rock'

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@AtleLohrmann this is a functionally scoped var declaration. just curious why you switched from es6 to es5. It still works, so no worries there.

} else if (randomInt===1) {
choice='paper'
} else if (randomInt===2) {
choice='scissors'
} else {
alert('something wrong with the getMachineInput routine [0:2]:' + randomInt)
}
Comment on lines +102 to +104

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@AtleLohrmann Great use case for error handling. However, this statement would never run because of the randomly generated number.

// Transfer choice to the global variable
machineInput=choice
return machineInput
}

// Mapping from random real number [0:1] to integer [0:(max-1)]
function getRandomInt(max) {
return Math.floor(Math.random() * Math.floor(max));
}

// Updates scores depending on who won
function updateScores(winner) {
if (winner===1){
userWins=userWins+1
} else if (winner===2) {
machineWins=machineWins+1
}
}


})