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

Alternative control flow #3

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

badsketch
Copy link

@badsketch badsketch commented Sep 10, 2022

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.

Comment on lines +20 to +27
const resultMatrix = [
// CPU
// rock, paper, scissors
/*Player rock */[ 0, -1, 1],
/* paper */[ 1, 0, -1],
/* scissors */[-1, 1, 0]

]
Copy link
Author

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.

Comment on lines +31 to 35
const choice = {
"rock": 0,
"paper": 1,
"scissors": 2
}
Copy link
Author

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.

Copy link
Author

Choose a reason for hiding this comment

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant