In this article I'd like to do an interview-style problem-solving and coding exercise. I haven't actually encountered this problem in an interview ...
For further actions, you may consider blocking this person and/or reporting abuse
Awesome, I've also built a similar game before when I was learning Angular.
My solution is different though, I get the computer input by subtracting the player input to 4.
x = 4 - playerInput
If the player inputs 1, the computer would input 3.
If the player inputs 2, the computer would input 2.
If the player inputs 3, the computer would input 1.
Live demo
Thanks for the comment @devkumi ! If the human player takes the first turn (and therefore can't win, no matter what), then that's all you need to do!
This version forces the computer to play first, which means the computer needs to know whether the human player has made a mistake or not. If the human player makes a mistake, then the remainder,
r = (count - 1) mod 4
is used to compute the right move for the computer. I think in your case it would be4 - (count mod 4)
because you're going up from 0 to 21.I love the idea of this game! :)
I have a GitHub repo, where I collect pieces of code that one can practice refactoring on. You inspired me to add this game to the repository.
Check it here: floppylab/clean-code