Skip to content

Commit be619b6

Browse files
author
Partho Biswas
committed
348. Design Tic-Tac-Toe
1 parent eee1ca5 commit be619b6

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@ I have solved quite a number of problems from several topics. See the below tabl
181181
|68| **[66. Plus One](https://tinyurl.com/yd67rugq)** | [Python](https://tinyurl.com/wu6rdaw/66_Plus_One.py), [Swift](https://tinyurl.com/wuja3c4/66_Plus_One.swift) | | Easy | |
182182
|69| **[957. Prison Cells After N Days](https://tinyurl.com/yag46zkm)** | [Python](https://tinyurl.com/wu6rdaw/957_Prison_Cells_After_N_Days.py), [Swift](https://tinyurl.com/wuja3c4/957_Prison_Cells_After_N_Days.swift) | [Art 1](https://tinyurl.com/y7cbf32e), [Art 2](https://tinyurl.com/yd2y66dn), [Art 3](https://tinyurl.com/yczdwnwu), [Art 4](https://tinyurl.com/y8j9b593), [Art 5](https://tinyurl.com/y6u2u42m) | Medium | |
183183
|70| [1232. Check If It Is a Straight Line](https://tinyurl.com/y932lhyb) | [Python](https://tinyurl.com/wu6rdaw/1232_Check_If_It_Is_a_Straight_Line.py), [Swift](https://tinyurl.com/wuja3c4/1232_Check_If_It_Is_a_Straight_Line.swift) | | Easy | |
184+
|71| **[348. Design Tic-Tac-Toe](https://tinyurl.com/ybtrbuso)** | [Python](https://tinyurl.com/wu6rdaw/348_Design_Tic-Tac-Toe.py), [Swift](https://tinyurl.com/wuja3c4/348_Design_Tic-Tac-Toe.swift) | [Vid 1](https://tinyurl.com/y745qzme), [Art 1](https://tinyurl.com/ybvjnmuo) | Medium | A fucking tricky question |
184185

185186

186187
</p>
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
class TicTacToe(object):
2+
3+
def __init__(self, n):
4+
"""
5+
Initialize your data structure here.
6+
:type n: int
7+
"""
8+
self.rows, self.cols, self.diagonal, self.antiDiagonal, self.n = [0]*n, [0]*n, 0, 0, n
9+
10+
def move(self, row, col, player):
11+
"""
12+
Player {player} makes a move at ({row}, {col}).
13+
@param row The row of the board.
14+
@param col The column of the board.
15+
@param player The player, can be either 1 or 2.
16+
@return The current winning condition, can be either:
17+
0: No one wins.
18+
1: Player 1 wins.
19+
2: Player 2 wins.
20+
:type row: int
21+
:type col: int
22+
:type player: int
23+
:rtype: int
24+
"""
25+
offset = (player * 2) - 3 # To identify players moves uniquely. Lets say for players 1&2 -> "offset = player * 2 - 3" will results -1,1 respectively.
26+
self.rows[row] += offset
27+
self.cols[col] += offset
28+
if row == col:
29+
self.diagonal += offset
30+
if row + col == self.n - 1:
31+
self.antiDiagonal += offset
32+
if self.n in [self.rows[row], self.cols[col], self.diagonal, self.antiDiagonal]:
33+
return 2
34+
if -self.n in [self.rows[row], self.cols[col], self.diagonal, self.antiDiagonal]:
35+
return 1
36+
return 0
37+
38+
39+
40+
# Your TicTacToe object will be instantiated and called as such:
41+
# obj = TicTacToe(n)
42+
# param_1 = obj.move(row,col,player)

0 commit comments

Comments
 (0)