loading...

re: Coding Interview: The Matchstick Game VIEW POST

FULL DISCUSSION
 

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 be 4 - (count mod 4) because you're going up from 0 to 21.

code of conduct - report abuse