forked from keshavsingh3197/Python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame.py
More file actions
68 lines (54 loc) · 1.69 KB
/
game.py
File metadata and controls
68 lines (54 loc) · 1.69 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
65
66
67
68
board = [["0", "1", "2"],
["3", "4", "5"],
["6", "7", "8"]]
def print_board():
for row in board:
print(row)
print()
def check_game(mark):
# Check rows
for i in range(3):
if board[i][0] == mark and board[i][1] == mark and board[i][2] == mark:
return True
# Check columns
for i in range(3):
if board[0][i] == mark and board[1][i] == mark and board[2][i] == mark:
return True
# Check diagonals
if board[0][0] == mark and board[1][1] == mark and board[2][2] == mark:
return True
if board[0][2] == mark and board[1][1] == mark and board[2][0] == mark:
return True
return False
def play_tic():
current_player = "1"
mark = "X"
moves = 0
while moves < 9:
print_board()
print("Player " + current_player + "'s Move.")
print("Enter the cell number (0-8): ")
cell = int(input())
row = cell // 3
col = cell % 3
if board[row][col] != "X" and board[row][col] != "O":
board[row][col] = mark
moves += 1
if check_game(mark):
print_board()
print("Player " + current_player + " Wins!!!!!")
break
# Switch players
if current_player == "1":
current_player = "2"
mark = "O"
else:
current_player = "1"
mark = "X"
else:
print("This cell is already occupied, Try another one!")
continue
if moves == 9:
print_board()
print("No one has won the game, it's a Draw!!!!!!!!")
play_tic()