forked from mrayanasim09/python-projects
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtick_cross.py
138 lines (110 loc) · 3.9 KB
/
tick_cross.py
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
135
136
137
138
# This code is made by MRayan Asim
import random
import time
def print_board(board):
for i in range(3):
print(" | ".join(board[i]))
if i < 2:
print("---------")
def check_winner(board):
# Check rows
for row in board:
if row[0] == row[1] == row[2] != " ":
return row[0]
# Check columns
for col in range(3):
if board[0][col] == board[1][col] == board[2][col] != " ":
return board[0][col]
# Check diagonals
if board[0][0] == board[1][1] == board[2][2] != " ":
return board[0][0]
if board[0][2] == board[1][1] == board[2][0] != " ":
return board[0][2]
return None
def get_empty_cells(board):
empty_cells = []
for i in range(3):
for j in range(3):
if board[i][j] == " ":
empty_cells.append((i, j))
return empty_cells
def make_human_move(board):
while True:
row = int(input("Enter the row (0-2): "))
col = int(input("Enter the column (0-2): "))
if row in range(3) and col in range(3) and board[row][col] == " ":
board[row][col] = "X"
break
else:
print("Invalid move. Try again.")
def make_computer_move(board, difficulty):
empty_cells = get_empty_cells(board)
if empty_cells:
if difficulty == "easy":
row, col = random.choice(empty_cells)
elif difficulty == "medium":
# Check for winning moves
for cell in empty_cells:
temp_board = [
row[:] for row in board
] # Create a temporary board for simulation
temp_board[cell[0]][cell[1]] = "O"
if check_winner(temp_board) == "O":
row, col = cell
break
else:
# Check for blocking moves
for cell in empty_cells:
temp_board = [
row[:] for row in board
] # Create a temporary board for simulation
temp_board[cell[0]][cell[1]] = "X"
if check_winner(temp_board) == "X":
row, col = cell
break
else:
row, col = random.choice(empty_cells)
else:
# Choose the best move using a more advanced algorithm (e.g., Minimax)
# Implementation of Minimax is beyond the scope of this example
row, col = random.choice(empty_cells)
time.sleep(1) # Add a delay of 1 second before computer's move
board[row][col] = "✓" # Use tick symbol instead of "O"
def play_again():
response = input("Do you want to play again? (yes/no): ")
return response.lower() == "yes"
def select_difficulty():
while True:
difficulty = input("Select difficulty level (easy/medium/hard): ")
if difficulty.lower() in ["easy", "medium", "hard"]:
return difficulty.lower()
else:
print("Invalid difficulty level. Try again.")
def play_game():
while True:
board = [[" ", " ", " "], [" ", " ", " "], [" ", " ", " "]]
current_player = "X"
winner = None
difficulty = select_difficulty()
while not winner and get_empty_cells(board):
print_board(board)
if current_player == "X":
make_human_move(board)
else:
make_computer_move(board, difficulty)
winner = check_winner(board)
if current_player == "X":
current_player = "O"
else:
current_player = "X"
print_board(board)
if winner:
if winner == "X":
print("You win!")
else:
print("Computer wins!")
else:
print("It's a tie!")
if not play_again():
break
play_game()