Tic-tac-toe: Difference between revisions

m
Line 4,741:
It uses minimax with alpha-beta pruning. Therefore, the computer never loses.
 
[https://easylang.onlinedev/apps/tictactoe.html Run it]
 
<syntaxhighlight lang="text">
# Tic tac toe
#
len f[] 9
state = 0
Line 4,801 ⟶ 4,803:
res = 0
for i = 1 step 3 to 7
call sum3 i 1 res
.
for i = 1 to 3
call sum3 i 3 res
.
call sum3 1 4 res
call sum3 3 2 res
cnt = 1
for i = 1 to 9
Line 4,821 ⟶ 4,823:
.
proc minmax player alpha beta . rval rmov .
call rate rval done
if done = 1
if player = 1
Line 4,833 ⟶ 4,835:
if f[mov] = 0
f[mov] = player
call minmax (5 - player) (-beta) (-rval) val h
val = -val
f[mov] = 0
Line 4,860 ⟶ 4,862:
.
proc computer . .
call minmax 4 -11 11 val mov
f[mov] = 4
call draw mov
call rate val done
state = 0
if done = 1
call show_result val
.
.
Line 4,873 ⟶ 4,875:
if f[mov] = 0
f[mov] = 1
call draw mov
state = 1
timer 0.5
Line 4,879 ⟶ 4,881:
.
on timer
call rate val done
if done = 1
call show_result val
else
call computer
.
.
Line 4,889 ⟶ 4,891:
if state = 0
if mouse_x > 6 and mouse_x < 90 and mouse_y > 16
call human
.
elif state >= 2
state -= 2
call init
.
.
call init
</syntaxhighlight>
 
1,973

edits