-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDesign Tic Tac Toe.py
More file actions
40 lines (37 loc) · 1.23 KB
/
Design Tic Tac Toe.py
File metadata and controls
40 lines (37 loc) · 1.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
class TicTacToe:
def __init__(self, n: int):
self.n=n
self.row=defaultdict(lambda:0)
self.col=defaultdict(lambda:0)
self.primary=defaultdict(lambda:0) #primary diagonal
self.secondary=defaultdict(lambda:0) #secondary diagonal
def move(self, r: int, c: int, player: int) -> int:
n=self.n
row=self.row
col=self.col
primary=self.primary
secondary=self.secondary
if(player==1):
#move = +1 for player 1
row[r]+=1
col[c]+=1
d=r-c
s=r+c
primary[d]+=1 #primary diagonal
secondary[s]+=1 #secondarys diagonal
if(row[r]==n or col[c]==n or primary[d]==n or secondary[s]==n):
return 1
else:
#move = -1 for player 2
row[r]-=1
col[c]-=1
d=r-c
s=r+c
primary[d]-=1 #primary diagonal
secondary[s]-=1 #secondarys diagonal
if(row[r]==-n or col[c]==-n or primary[d]==-n or secondary[s]==-n):
return 2
return 0
# Your TicTacToe object will be instantiated and called as such:
# obj = TicTacToe(n)
# param_1 = obj.move(row,col,player)