-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTask 2
121 lines (101 loc) · 3.44 KB
/
Task 2
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
import random
# Function to print the board
def print_board(board):
print("\n")
for row in board:
print(" | ".join(row))
print("-" * 9)
print("\n")
# Function to check for a win
def check_winner(board, player):
for row in board:
if all(cell == player for cell in row):
return True
for col in range(3):
if all(board[row][col] == player for row in range(3)):
return True
if all(board[i][i] == player for i in range(3)) or all(board[i][2 - i] == player for i in range(3)):
return True
return False
# Function to check if the board is full
def is_full(board):
return all(board[row][col] != " " for row in range(3) for col in range(3))
# Function to get all empty cells
def get_empty_cells(board):
return [(r, c) for r in range(3) for c in range(3) if board[r][c] == " "]
# Function for player move
def player_move(board, player):
while True:
try:
row, col = map(int, input(f"Player {player}, enter row and column (0-2, space-separated): ").split())
if board[row][col] == " ":
board[row][col] = player
break
else:
print("Cell already taken, choose another.")
except (ValueError, IndexError):
print("Invalid input! Enter numbers between 0 and 2.")
# Function for AI move using Minimax Algorithm
def minimax(board, depth, is_maximizing):
if check_winner(board, "O"): # AI wins
return 1
if check_winner(board, "X"): # Player wins
return -1
if is_full(board): # Draw
return 0
if is_maximizing:
best_score = -float("inf")
for (r, c) in get_empty_cells(board):
board[r][c] = "O"
score = minimax(board, depth + 1, False)
board[r][c] = " "
best_score = max(best_score, score)
return best_score
else:
best_score = float("inf")
for (r, c) in get_empty_cells(board):
board[r][c] = "X"
score = minimax(board, depth + 1, True)
board[r][c] = " "
best_score = min(best_score, score)
return best_score
# Function for AI move
def ai_move(board):
best_score = -float("inf")
best_move = None
for (r, c) in get_empty_cells(board):
board[r][c] = "O"
score = minimax(board, 0, False)
board[r][c] = " "
if score > best_score:
best_score = score
best_move = (r, c)
if best_move:
board[best_move[0]][best_move[1]] = "O"
# Main game loop
def play_game():
board = [[" " for _ in range(3)] for _ in range(3)]
print("Welcome to Tic-Tac-Toe!")
mode = input("Choose mode: 1 for Player vs Player, 2 for Player vs AI: ")
print_board(board)
current_player = "X"
while True:
if mode == "1": # Player vs Player
player_move(board, current_player)
elif mode == "2": # Player vs AI
if current_player == "X":
player_move(board, "X")
else:
print("AI is making a move...")
ai_move(board)
print_board(board)
if check_winner(board, current_player):
print(f"Player {current_player} wins!")
break
if is_full(board):
print("It's a draw!")
break
current_player = "O" if current_player == "X" else "X"
# Start the game
if __name__ == "__main__":
play_game()