Created
October 3, 2018 01:14
-
-
Save yokolet/f8f3d81e7a4ec0e05bd8219e09b85eb5 to your computer and use it in GitHub Desktop.
Design Tic-Tac-Toe
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| """ | |
| Design a Tic-tac-toe game that is played between two players on a n x n grid. You may | |
| assume the following rules: | |
| - A move is guaranteed to be valid and is placed on an empty block. | |
| - Once a winning condition is reached, no more moves is allowed. | |
| - A player who succeeds in placing n of their marks in a horizontal, vertical, or | |
| diagonal row wins the game. | |
| Example: | |
| Given n = 3, assume that player 1 is "X" and player 2 is "O" in the board. | |
| toe = TicTacToe(3) | |
| print(toe.move(0, 0, 1)) # 0 | |
| |X| | | | |
| | | | | Player 1 makes a move at (0, 0). | |
| | | | | | |
| print(toe.move(0, 2, 2)) # 0 | |
| |X| |O| | |
| | | | | Player 2 makes a move at (0, 2). | |
| | | | | | |
| print(toe.move(2, 2, 1)) # 0 | |
| |X| |O| | |
| | | | | Player 1 makes a move at (2, 2). | |
| | | |X| | |
| print(toe.move(1, 1, 2)) # 0 | |
| |X| |O| | |
| | |O| | Player 2 makes a move at (1, 1). | |
| | | |X| | |
| print(toe.move(2, 0, 1)) # 0 | |
| |X| |O| | |
| | |O| | Player 1 makes a move at (2, 0). | |
| |X| |X| | |
| print(toe.move(1, 0, 2)) # 0 | |
| |X| |O| | |
| |O|O| | Player 2 makes a move at (1, 0). | |
| |X| |X| | |
| print(toe.move(2, 1, 1)) # 1 (player 1 wins) | |
| |X| |O| | |
| |O|O| | Player 1 makes a move at (2, 1). | |
| |X|X|X| | |
| """ | |
| class TicTacToe: | |
| def __init__(self, n): | |
| """ | |
| Initialize your data structure here. | |
| :type n: int | |
| """ | |
| self.n = n | |
| self.rows = [0] * n | |
| self.cols = [0] * n | |
| self.left_diag = 0 | |
| self.right_diag = 0 | |
| def move(self, row, col, player): | |
| """ | |
| Player {player} makes a move at ({row}, {col}). | |
| @param row The row of the board. | |
| @param col The column of the board. | |
| @param player The player, can be either 1 or 2. | |
| @return The current winning condition, can be either: | |
| 0: No one wins. | |
| 1: Player 1 wins. | |
| 2: Player 2 wins. | |
| :type row: int | |
| :type col: int | |
| :type player: int | |
| :rtype: int | |
| """ | |
| value = 1 | |
| if player == 2: | |
| value = -1 | |
| self.rows[row] += value | |
| self.cols[col] += value | |
| if row == col: | |
| self.left_diag += value | |
| if abs(self.left_diag) == self.n: | |
| return player | |
| if (self.n - row - 1) == col: | |
| self.right_diag += value | |
| if abs(self.right_diag) == self.n: | |
| return player | |
| if abs(self.rows[row]) == self.n or\ | |
| abs(self.cols[col]) == self.n: | |
| return player | |
| return 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment