-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTic Tac Toe.py
134 lines (120 loc) · 4.13 KB
/
Tic Tac Toe.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
#Tic-Tac-toe
import random
# The game board
board = [' ' for _ in range(9)]
# To insert a letter (X or O) at a given position on the board
def insert_letter(letter, pos):
if pos < 0 or pos > 8:
print("Invalid position!")
return
board[pos] = letter
# To check if the space is free
def space_is_free(pos):
return board[pos] == ' '
# To print the board
def print_board(board):
print(' ' + board[0] + ' | ' + board[1] + ' | ' + board[2])
print('-----------')
print(' ' + board[3] + ' | ' + board[4] + ' | ' + board[5])
print('-----------')
print(' ' + board[6] + ' | ' + board[7] + ' | ' + board[8])
# To check if the board is full
def is_board_full(board):
return board.count(' ') == 0
# To check for a win
def is_winner(bo, le):
return ((bo[0] == le and bo[1] == le and bo[2] == le) or
(bo[3] == le and bo[4] == le and bo[5] == le) or
(bo[6] == le and bo[7] == le and bo[8] == le) or
(bo[0] == le and bo[3] == le and bo[6] == le) or
(bo[1] == le and bo[4] == le and bo[7] == le) or
(bo[2] == le and bo[5] == le and bo[8] == le) or
(bo[0] == le and bo[4] == le and bo[8] == le) or
(bo[2] == le and bo[4] == le and bo[6] == le))
# Minimax algorithm with Alpha-Beta Pruning
def minimax(board, depth, is_maximizing, alpha, beta):
if is_winner(board, 'X'):
return -10
elif is_winner(board, 'O'):
return 10
elif is_board_full(board):
return 0
if is_maximizing:
best_score = -1000
for i in range(9):
if space_is_free(i):
insert_letter('O', i)
score = minimax(board, depth + 1, False, alpha, beta)
insert_letter(' ', i)
best_score = max(score, best_score)
alpha = max(alpha, best_score)
if beta <= alpha:
break
return best_score
else:
best_score = 1000
for i in range(9):
if space_is_free(i):
insert_letter('X', i)
score = minimax(board, depth + 1, True, alpha, beta)
insert_letter(' ', i)
best_score = min(score, best_score)
beta = min(beta, best_score)
if beta <= alpha:
break
return best_score
# AI agent's move
def ai_move():
best_score = -1000
best_move = 0
for i in range(9):
if space_is_free(i):
insert_letter('O', i)
score = minimax(board, 0, False, -1000, 1000)
insert_letter(' ', i)
if score > best_score:
best_score = score
best_move = i
insert_letter('O', best_move)
print("\n AI's turn: \n")
return
# Human player's move
def human_move():
run = True
while run:
move = input("\n Please select a position to place an 'X' (1-9): ")
try:
move = int(move)
if move > 0 and move < 10:
if space_is_free(move - 1):
run = False
insert_letter('X', move - 1) # Indent this line
else:
print('Sorry, this space is occupied!')
else:
print('Please type a number within the range!')
except:
print('Please type a number!')
# Main game loop
def play_game():
print_board(board)
while not(is_board_full(board)):
if not(is_winner(board, 'O')):
human_move()
print_board(board)
if is_winner(board, 'X'):
print("\n You win!! \n")
break
if is_board_full(board):
break
ai_move()
print_board(board)
if is_winner(board, 'O'):
print("\n AI wins!! \n")
break
else:
break
if not(is_winner(board, 'O')) and not(is_winner(board, 'X')) and is_board_full(board):
print('It\'s a tie!')
# Start the game
play_game()