0% found this document useful (0 votes)
13 views5 pages

Minmax Alphabeta

This document describes the implementation of minimax and alpha-beta pruning algorithms for an AI player in Tic-Tac-Toe. It includes functions for making moves with minimax and alpha-beta search, evaluating board positions, checking for a winner, and restarting the game. The AI will evaluate all possible moves and return the position and score that maximizes its chance of winning based on the search algorithms.

Uploaded by

rafeak rafeak
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views5 pages

Minmax Alphabeta

This document describes the implementation of minimax and alpha-beta pruning algorithms for an AI player in Tic-Tac-Toe. It includes functions for making moves with minimax and alpha-beta search, evaluating board positions, checking for a winner, and restarting the game. The AI will evaluate all possible moves and return the position and score that maximizes its chance of winning based on the search algorithms.

Uploaded by

rafeak rafeak
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

MinMax & AlpaBeta

from tkinter import *

com='o'
player='x'
player_score={com:1,player:-1}

borad = [
['', '', ''],
['', '', ''],
['', '', ''],
]

def click_fun(x,y):
w = change(player, (x, y))
check_winer(w)

#(sc, pos) = minMax_move(borad)


(sc, pos) = alphabeta_move(borad)
if sc:
index = sc.index(max(sc))

print('########################')
print(sc)
print('#######################')

w = change(com, (pos[index][0], pos[index][1]))


check_winer(w)

def change(player, pos):


x, y = pos
if borad[x][y]['text'] == '':
borad[x][y]['text'] = player
return score_fun()

def score_fun():
for i in range(3):
if borad[i][0]['text'] == borad[i][1]['text'] ==
borad[i][2]['text'] and borad[i][0]['text'] != '':
pl = borad[i][0]['text']
return player_score[pl]

for i in range(3):
if borad[0][i]['text'] == borad[1][i]['text'] ==
borad[2][i]['text'] and borad[2][i]['text'] != '':
pl = borad[0][i]['text']
return player_score[pl]

if borad[0][0]['text'] == borad[1][1]['text'] ==
borad[2][2]['text'] and borad[2][2]['text'] != '':
pl = borad[0][0]['text']
return player_score[pl]

if borad[2][0]['text'] == borad[1][1]['text'] ==
borad[0][2]['text'] and borad[0][2]['text'] != '':
pl = borad[0][2]['text']
return player_score[pl]

is_tie = sum(1 for y in range(3) for x in range(3) if


borad[y][x]['text'] == '')
if is_tie == 0:
return 0

def check_winer(w):
if w is not None:
if w == 1:
label.config(text=("Computer won"))
result_style('red')
elif w == -1:
label.config(text=("YOU won"))
result_style("green")
else:
label.config(text=("Tie!"))
result_style("#ff7301")

def minMax_move(borad):

scores = []
pos = []
for x in range(3):
for y in range(3):
if borad[x][y]['text'] == '':
borad[x][y]['text'] = com
sc = minimaxi(borad, False)
scores.append(sc)
pos.append((x, y))
borad[x][y]['text'] = ''

return (scores, pos)

def minimaxi(borad, ismax=True):


score = score_fun()

if score != None:
return score

scores = []

for x in range(3):
for y in range(3):
if borad[x][y]['text'] == '':

if ismax:
borad[x][y]['text'] = com
else:
borad[x][y]['text'] = player

sc = minimaxi(borad,not ismax)

scores.append(sc)
borad[x][y]['text'] = ''

return (max(scores) if ismax else min(scores))

def alphabeta_move(borad):

scores = []
pos = []

alpha = -10
beta = 10

for x in range(3):
for y in range(3):
if borad[x][y]['text'] == '':

borad[x][y]['text'] = com
sc = alphabeta(borad, False, alpha, beta)

if alpha < sc:


alpha = sc

scores.append(sc)
pos.append((x, y))
borad[x][y]['text'] = ''

return (scores, pos)

def alphabeta(borad, ismax, alpha, beta):


score = score_fun()

if score != None:
return score

if ismax:
best = -10
for x in range(3):
for y in range(3):
if borad[x][y]['text'] == '':

borad[x][y]['text'] = com
sc = alphabeta(borad, not ismax, alpha, beta)

borad[x][y]['text'] = ''
if sc > best:
best = sc

if best > alpha:


alpha = best

if alpha >= beta:


return best

return best

else:
best = 10
for x in range(3):
for y in range(3):
if borad[x][y]['text'] == '':
borad[x][y]['text'] = player
sc = alphabeta(borad, not ismax, alpha, beta)
borad[x][y]['text'] = ''

if sc < best:
best = sc

if best < beta:


beta = best

if beta <= alpha:


return best

return best

def result_style(col):
for x in range(3):
for y in range(3):
borad[x][y].config(bg=col)

def restart():
label.config(text=('player : ' + player +' com : ' + com))
for x in range(3):
for y in range(3):
borad[x][y].config(text="", bg="#F0F0F0")

window = Tk()
window.title("Tic-Tac-Toe Game")

label = Label(text=('player : ' + player +' com : ' + com),


font=('consolas', 20))
label.pack(side="top")

restart_btn = Button(text="restart", font=('consolas', 20),


command=restart)
restart_btn.pack(side="top")

btns_frame = Frame(window)
btns_frame.pack()
for row in range(3):
for col in range(3):
borad[row][col] = Button(btns_frame, text="",
font=('consolas', 50), width=4, height=1,
command=lambda row=row, col=col:
click_fun(row, col))
borad[row][col].grid(row=row, column=col)

window.mainloop()

You might also like