-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproject.py
More file actions
134 lines (115 loc) · 3.68 KB
/
Copy pathproject.py
File metadata and controls
134 lines (115 loc) · 3.68 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import random
def display_board(board):
print("Tic Tac Toe")
print("------------")
for i in range(0, 9, 3):
print(f"{board[i]} | {board[i+1]} | {board[i+2]}")
print("---------")
def player_starts():
return random.randint(1, 2)
def machine_move(board):
# machine move logic
if '5' in board:
board[4] = 'O'
else:
for i in range(1, 10):
if board[i - 1] not in ['X', 'O']:
board[i - 1] = 'O'
if check_winner(board) == 'Machine':
return
board[i - 1] = i
for i in range(1, 10):
if board[i - 1] not in ['X', 'O']:
board[i - 1] = 'X'
if check_winner(board) == 'User':
board[i - 1] = 'O'
return
board[i - 1] = i
side_pos = [2, 4, 6, 8]
for pos in side_pos:
if board[pos - 1] not in ['X', 'O']:
board[pos - 1] = 'O'
return
diagonal_pos = [1, 3, 7, 9]
for pos in diagonal_pos:
if board[pos - 1] not in ['X', 'O']:
board[pos - 1] = 'O'
return
while True:
random_spot = random.randint(0, 8)
if board[random_spot] not in ['X', 'O']:
board[random_spot] = 'O'
return
def user_move(board):
#user move.
while True:
try:
position = int(input("Enter a number (1-9): "))
if position < 1 or position > 9:
print("Please enter a value within 1-9.")
continue
if board[position - 1] in ['X', 'O']:
print("That position is already marked choose another.")
continue
board[position - 1] = 'X'
break
except ValueError:
print("Please enter a valid integer.")
def check_winner(board):
#check row
for i in range(0, 9, 3):
if board[i] == board[i+1] == board[i+2] == 'X':
return "User"
elif board[i] == board[i+1] == board[i+2] == 'O':
return "Machine"
#check column
for i in range(3):
if board[i] == board[i+3] == board[i+6] == 'X':
return "User"
elif board[i] == board[i+3] == board[i+6] == 'O':
return "Machine"
#check diagonal
if board[0] == board[4] == board[8] == 'X' or board[2] == board[4] == board[6] == 'X':
return "User"
elif board[0] == board[4] == board[8] == 'O' or board[2] == board[4] == board[6] == 'O':
return "Machine"
#if no winner found return none
return None
def main():
# display the board
board = ['1', '2', '3', '4', '5', '6', '7', '8', '9']
display_board(board)
# who starts first
player = player_starts()
if player == 1:
print("Machine starts first")
else:
print("User starts first")
# game loop
while True:
# check if there is a winner
winner = check_winner(board)
if winner:
print(f"{winner} wins!")
break
# machine turn
if player == 1:
machine_move(board)
display_board(board)
player = 2
# user turn
else:
user_move(board)
display_board(board)
player = 1
# check for draw
if all(cell in ['X','O'] for cell in board):
winner = check_winner(board)
if winner:
print(f"{winner} wins!")
break
else:
print("It is a draw")
break
if __name__ == "__main__":
main()