Tic-tac-toe: Difference between revisions
Content added Content deleted
m (→{{header|RapidQ}}: Board(9) and WInPos(8, ...) are not necessary.) |
|||
Line 4,458: | Line 4,458: | ||
# |
# |
||
func init . . |
func init . . |
||
linewidth 2 |
linewidth 2 |
||
clear |
clear |
||
color 666 |
color 666 |
||
move 34 |
move 34 96 |
||
line 34 |
line 34 20 |
||
move 62 |
move 62 96 |
||
line 62 |
line 62 20 |
||
move 10 |
move 10 72 |
||
line 86 |
line 86 72 |
||
move 10 |
move 10 44 |
||
line 86 |
line 86 44 |
||
linewidth 2.5 |
linewidth 2.5 |
||
for i = 1 to 9 |
for i = 1 to 9 |
||
f[i] = 0 |
f[i] = 0 |
||
. |
. |
||
if state = 1 |
if state = 1 |
||
timer 0.2 |
timer 0.2 |
||
. |
. |
||
. |
. |
||
func draw ind . . |
func draw ind . . |
||
c = (ind - 1) mod 3 |
c = (ind - 1) mod 3 |
||
r = (ind - 1) div 3 |
r = (ind - 1) div 3 |
||
x = c * 28 + 20 |
x = c * 28 + 20 |
||
y = r * 28 + |
y = r * 28 + 30 |
||
if f[ind] = 4 |
if f[ind] = 4 |
||
color 900 |
color 900 |
||
move x - 7 y - 7 |
move x - 7 y - 7 |
||
line x + 7 y + 7 |
line x + 7 y + 7 |
||
move x + 7 y - 7 |
move x + 7 y - 7 |
||
line x - 7 y + 7 |
line x - 7 y + 7 |
||
elif f[ind] = 1 |
elif f[ind] = 1 |
||
color 009 |
color 009 |
||
move x y |
move x y |
||
circle 10 |
circle 10 |
||
color -2 |
color -2 |
||
circle 7.5 |
circle 7.5 |
||
. |
. |
||
. |
. |
||
func sum3 a d . st . |
func sum3 a d . st . |
||
for i = 1 to 3 |
for i = 1 to 3 |
||
s += f[a] |
s += f[a] |
||
a += d |
a += d |
||
. |
. |
||
if s = 3 |
if s = 3 |
||
st = -1 |
st = -1 |
||
elif s = 12 |
elif s = 12 |
||
st = 1 |
st = 1 |
||
. |
. |
||
. |
. |
||
func rate . res done . |
func rate . res done . |
||
res = 0 |
res = 0 |
||
for i = 1 step 3 to 7 |
for i = 1 step 3 to 7 |
||
call sum3 i 1 res |
call sum3 i 1 res |
||
. |
. |
||
for i = 1 to 3 |
for i = 1 to 3 |
||
call sum3 i 3 res |
call sum3 i 3 res |
||
. |
. |
||
call sum3 1 4 res |
call sum3 1 4 res |
||
call sum3 3 2 res |
call sum3 3 2 res |
||
cnt = 1 |
cnt = 1 |
||
for i = 1 to 9 |
for i = 1 to 9 |
||
if f[i] = 0 |
if f[i] = 0 |
||
cnt += 1 |
cnt += 1 |
||
. |
. |
||
. |
. |
||
res *= cnt |
res *= cnt |
||
done = 1 |
done = 1 |
||
if res = 0 and cnt > 1 |
if res = 0 and cnt > 1 |
||
done = 0 |
done = 0 |
||
. |
. |
||
. |
. |
||
func minmax player alpha beta . rval rmov . |
func minmax player alpha beta . rval rmov . |
||
call rate rval done |
call rate rval done |
||
if done = 1 |
if done = 1 |
||
if player = 1 |
if player = 1 |
||
rval = -rval |
rval = -rval |
||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
mov = start |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
. |
. |
||
⚫ | |||
⚫ | |||
rval = alpha |
|||
⚫ | |||
⚫ | |||
⚫ | |||
. |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
. |
|||
⚫ | |||
⚫ | |||
until mov = start or rval >= beta |
|||
⚫ | |||
⚫ | |||
. |
. |
||
func show_result val . . |
func show_result val . . |
||
color 555 |
color 555 |
||
move 16 |
move 16 4 |
||
if val < 0 |
if val < 0 |
||
# this never happens |
# this never happens |
||
text "You won" |
text "You won" |
||
elif val > 0 |
elif val > 0 |
||
text "You lost" |
text "You lost" |
||
else |
else |
||
text "Tie" |
text "Tie" |
||
. |
. |
||
state += 2 |
state += 2 |
||
. |
. |
||
func computer . . |
func computer . . |
||
call minmax 4 -11 11 val mov |
call minmax 4 -11 11 val mov |
||
f[mov] = 4 |
f[mov] = 4 |
||
call draw mov |
call draw mov |
||
call rate val done |
call rate val done |
||
state = 0 |
state = 0 |
||
if done = 1 |
if done = 1 |
||
call show_result val |
call show_result val |
||
. |
. |
||
. |
. |
||
func human . . |
func human . . |
||
mov = floor ((mouse_x - 6) / 28) + 3 * floor (mouse_y / 28) + 1 |
mov = floor ((mouse_x - 6) / 28) + 3 * floor ((mouse_y - 16) / 28) + 1 |
||
if f[mov] = 0 |
if f[mov] = 0 |
||
f[mov] = 1 |
f[mov] = 1 |
||
call draw mov |
call draw mov |
||
state = 1 |
state = 1 |
||
timer 0.5 |
timer 0.5 |
||
. |
. |
||
. |
. |
||
on timer |
on timer |
||
call rate val done |
call rate val done |
||
if done = 1 |
if done = 1 |
||
call show_result val |
call show_result val |
||
else |
else |
||
call computer |
call computer |
||
. |
. |
||
. |
. |
||
on mouse_down |
on mouse_down |
||
if state = 0 |
if state = 0 |
||
if mouse_x > 6 and mouse_x < 90 and mouse_y |
if mouse_x > 6 and mouse_x < 90 and mouse_y > 16 |
||
call human |
call human |
||
. |
. |
||
elif state >= 2 |
elif state >= 2 |
||
state -= 2 |
state -= 2 |
||
call init |
call init |
||
. |
. |
||
. |
. |
||
call init |
call init |