-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtictac.py
More file actions
64 lines (52 loc) · 1.61 KB
/
tictac.py
File metadata and controls
64 lines (52 loc) · 1.61 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#wap to play tic tac toe
board = [
[" "," "," "],
[" "," "," "],
[" "," "," "]
]
currentPlayer = 'X'
def displayBoard():
for i in board:
print(end="|")
for j in i:
print(j, end="|")
print()
def checkDraw():
for i in board:
if " " in i:
return False
return True
def checkWin(player):
displayBoard()
for i in range(3):
if board[i][0] == board[i][1] == board[i][2] == player:
print(f"{player} wins")
return True
for i in range(3):
if board[0][i] == board[1][i] == board[2][i] == player:
print(f"{player} wins")
return True
if board[0][0] ==board[i][1] == board[i][2] == player:
print(f"{player} wins")
return True
for i in range(3):
if board[0][i] == board[1][i] == board[2][i] == player:
print(f"{player} wins")
return True
if board[0][0] == board[1][1] == board[2][2] == player or board[0][2] == board[1][1] == board[2][0] == player:
print(f"{player} wins")
return True
return False
while(not checkWin(currentPlayer)):
if (checkDraw()):
print("Game over!!! It's draw")
break
currentPlayer = 'O' if currentPlayer == 'X' else 'X'
print(f"Current player:- {currentPlayer}")
row = int(input("Select the row: "))
col = int(input("Select the col: "))
while(board[row][col] != " "):
print("Invalid input Try again!!!")
row = int(input("Select the row: "))
col = int(input("Select the col: "))
board[row][col] = currentPlayer