Cats, Rabbits, and Snails Board Game
Learn this problemProblem statement
Simulate a finite ordered sequence of moves in a two-player animal board game. Rows and columns are zero-based. Red moves first, then turns alternate after each valid move.
The initial board is:
- Red cats at
[0,0]and[0,6], red rabbits at[1,2]and[1,4], and the red snail at[0,3]. - Blue cats at
[6,0]and[6,6], blue rabbits at[5,2]and[5,4], and the blue snail at[6,3].
Each move is the string "fromRow fromColumn toRow toColumn". Apply these rules:
- A cat moves from one to three cells orthogonally.
- A rabbit moves from one to two cells diagonally.
- A snail moves exactly one cell in any of the eight directions and cannot capture.
- A friendly piece blocks a cat or rabbit's path. When a cat or rabbit first encounters an enemy along its requested path, it captures that piece and stops there, even if the requested destination is farther away.
- A move is invalid if it has the wrong player's piece, violates movement or blocking rules, or leaves the moving player's snail threatened by a clear enemy cat or rabbit path. An invalid move changes no state and does not end the turn.
After a valid move, if the next player's snail is threatened and that player has no valid move that makes its snail safe, the moving player wins. Append "RED_WINS" or "BLUE_WINS" for that move. Otherwise append "OK". Append "INVALID" for an invalid move. After a win, append "GAME_OVER" for every remaining requested move. Return one result for every input move in order.
Function
playAnimalGame(moves: String[]) → String[]Examples
Example 1
moves = ["0 0 3 0","6 0 4 0","3 0 6 0","6 6 5 6"]return = ["OK","OK","OK","OK"]The red cat advances three cells. The blue cat then advances to row 4. Red requests row 6, but its cat encounters and captures the blue cat at row 4, so it stops there.
Example 2
moves = ["1 2 2 3","5 4 6 5","2 3 4 5","5 2 3 0","6 3 5 3"]return = ["OK","OK","OK","INVALID","OK"]Red's rabbit on row 4 threatens the blue snail diagonally. Moving the other blue rabbit does not remove that threat, so the fourth request is invalid and Blue keeps the turn. Moving the blue snail to [5,3] is safe.
Constraints
1 <= moves.length <= 200.- Every move contains exactly four integers between
0and6. - The board always starts in the fixed arrangement described above.
- Threat and escape checks use the same movement, blocking, capture, and snail-safety rules as requested moves.