Kyle is working a coding a two-player Tic Tac Toe game. She is done building the UI of the game, players are able to make their moves and only the one important thing that is left is to write a routine to check if any player won or is the game is a tie. You can help her by writing this routine to check if the game is a tie or which player actually one the game.
Program
Write a program to find out if the game is a tie or if the player 1 (X) or player 2 (O) won the game.
Assumption
- Only one player can win the game i.e. either X or O and game should not allow any further moves.
- A player can win because of 1 or more winning combinations.
Requirements
- A tic tac toe board has 9 cells (moves) that are arranged in a 3x3 grid. The cells are numbered from 1 to 9 as shown on the above board. The input data is provided in the order of cells 1 to 9.
- If a player has played in a cell, that particular cell will contain X or O.
- If a cell has not been played, it will contain ~(tilde).
- If more than one player wins, print INVALID.
- If none wins, print TIE.
- If X wins, print X.
- If O wins, print O.
- Any of the players X or O can go first.
- Print the out result in the upper case.
- Make sure the cell value is either X or O or ~(tilde).
Input
- The moves of players (cell values) on the board as a comma-separated list as input.
Output
- Print whether the moves are invalid, tie or which player won.
Test cases
No | Input () (Moves data) | Output () (Result) | Explanation |
---|---|---|---|
1 | X,X,X,O,O,O,X,X,X | INVALID | X has played more moves that O. |
2 | X,X,X,,O,O,X,,X | INVALID | Cells 5 and 8 don't have any values. |
3 | X,X,X,O,O,X,O,O,X | X | X won in the first row or third column. |
4 | O,O,X,X,O,X,X,~,O | O | O won, diagonal. |
5 | X,X,X,O,O,O,X,O,X | INVALID | Both X (1st row) and O (2nd row) won. |
6 | X,X,O,O,X,X,X,O,O | TIE | Nobody won. |
Instructions
- Accept the move data a single argument input via the command line argument.
- Write the logic to compute the result (INVALID, TIE, X or O).
- Print the result.