-
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
Homework-04 #5
base: master
Are you sure you want to change the base?
Homework-04 #5
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 |
---|---|---|
@@ -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 | ||
|
||
// 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() | ||
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. @AtleLohrmann you can also grab the text from 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" ) | ||
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. @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' | ||
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. @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
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. @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 | ||
} | ||
} | ||
|
||
|
||
}) |
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.
@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.