-
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?
Conversation
const resultMatrix = [ | ||
// CPU | ||
// rock, paper, scissors | ||
/*Player rock */[ 0, -1, 1], | ||
/* paper */[ 1, 0, -1], | ||
/* scissors */[-1, 1, 0] | ||
|
||
] |
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 with if/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.
const choice = { | ||
"rock": 0, | ||
"paper": 1, | ||
"scissors": 2 | ||
} |
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.
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 comment
The reason will be displayed to describe this comment to others. Learn more.
Just tossing out a different way to approach determining the winner. Based on what you said about your initial approach by looking at the result matrix, I thought what if we use a 2d array as a data structure to capture just that. Food for thought.
Also some changes here are also in the other PR.