-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtic_tac_toe.py
More file actions
82 lines (68 loc) · 2.67 KB
/
tic_tac_toe.py
File metadata and controls
82 lines (68 loc) · 2.67 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
import random
from colorama import init, Fore, Style
init(autoreset=True)
def display_board(board):
print()
def colored(cell):
if cell == 'X':
return Fore.RED + cell + Style.RESET_ALL
elif cell == '0':
return Fore.BLUE + cell + Style.RESET_ALL
else:
return Fore.YELLOW + cell + Style.RESET_ALL
print(' ' + colored(board[0]) + ' | ' + colored(board[1]) + ' | ' + colored(board[2]))
print(Fore.CYAN + '-----------' + Style.RESET_ALL)
print(' ' + colored(board[3]) + ' | ' + colored(board[4]) + ' | ' + colored(board[5]))
print(Fore.CYAN + '-----------' + Style.RESET_ALL)
print(' ' + colored(board[6]) + ' | ' + colored(board[7]) + ' | ' + colored(board[8]))
def player_choice():
symbol = ''
while symbol not in ['X', '0']:
symbol = input(Fore.GREEN + "Choose your symbol (X or O.)" + symbol + Style.RESET_ALL).upper()
if symbol == 'X':
return ('X', 'O')
else:
return ('O', 'X')
def player_move(board, symbol):
move = -1
while move not in range(1, 10) or not board[move - 1].isdigit ():
try:
move = int(input("Enter your move (1-9): "))
if move not in range(1, 10) or not board[move - 1].isdigit():
print("Invalid move. Please try again.")
except ValueError:
print("Please enter an integer between 1 and 9.")
board[move - 1] = symbol
def ai_move(board, ai_symbol, player_symbol):
for i in range(9):
if board[i].isdigit():
board_copy = board.copy()
board_copy[i] = ai_symbol
if check_win(board_copy, ai_symbol):
board[i] = ai_symbol
return
for i in range(9):
if board[i].isdigit():
board_copy = board.copy()
board[i] = player_symbol
if check_win(board_copy, player_symbol):
board[i] = ai_symbol
return
possible_moves = [i for i in range(9) if board[i].isdigit()]
move = random.choice(possible_moves)
board[move] = ai_symbol
def check_win(board, symbol):
win_conditions = [
(0, 1, 2), (3, 4, 5), (6, 7, 8), # Horizontal
(0, 3, 6), (1, 4, 7), (2, 5, 8), # Vertical
(0, 4, 8), (2, 4, 6) # Diagonal
]
for cond in win_conditions:
if board[cond[0]] == board[cond[1]] == board[cond[2]] == symbol:
return True
return False
def check_full(board):
return all(not spot.isdigit() for spot in board)
def tic_tac_toe():
print("Welcome to Tic-Tac-Toe!")
player_name = input(Fore.GREEN + "Enter your name: ")