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 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
|
# tictactoe_comp
# with a computer player - 2009-12-25 - j.m.reneau
# requires BACIC256 0.9.4g or better
# -1=empty 0=x, 1=0
fastgraphics
dim board(9)
board = {-1,-1,-1,-1,-1,-1,-1,-1,-1}
gosub drawboard
print "tictactoe"
dim playertype$(2)
for t = 0 to 1
do
print "who is player " + (t+1) + " (c-computer, h-human)";
input playertype$[t]
until playertype$[t] = "c" or playertype$[t] = "h"
next t
player = 1
otherplayer = 0
do
# swap player and otherplayer
t = player
player = otherplayer
otherplayer = t
# play the player
if playertype$[player] = "h" then gosub humanplayer
if playertype$[player] = "c" then gosub computerplayer
#
gosub drawboard
# see if there is a winner and if not loop for next player
gosub iswinner
until winner <> -1
# we must have a winner
print "The winner was ";
if winner = 0 then print "X"
if winner = 1 then print "O"
if winner = 2 then print "cat"
end
humanplayer:
print "player ";
if player = 0 then
print "X";
else
print "O";
end if
print " please click on cell."
clickclear
while clickb = 0
pause .1
endwhile
# what cell did they click on
i = int(clickx/100) + int(clicky/100)*3
# if it is not empty then wait again
if board[i] <> -1 then goto humanplayer
# set cell and display
board[i] = player
return
computerplayer:
# go through each square and if it is a win do it
# find the best offensive move I can make and save in mybest - we may do it later
mybest = -1
mybesttwofer = 0
twoferplayer = player
for i = 0 to 8
if board[i] = -1 then
board[i] = player
gosub iswinner
if winner = player then return
gosub counttwofer
if twofer > mybesttwofer then
mybesttwofer = twofer
mybest = i
endif
board[i] = -1
endif
next i
#
# go through each square and if it is a win for the other guy - block it
# find the best defensive move I can make and save in hisbest - we may do it later
hisbest = -1
hisbesttwofer = 0
twoferplayer = otherplayer
for i = 0 to 8
if board[i] = -1 then
board[i] = otherplayer
gosub iswinner
if winner = otherplayer then
board[i] = player
return
endif
gosub counttwofer
if twofer > hisbesttwofer then
hisbesttwofer = twofer
hisbest = i
endif
board[i] = -1
endif
next i
#
# if we can stop a 2 way 2fer stop it
if hisbesttwofer >= 2 then
board[hisbest] = player
return
endif
#
# if we can get a 2 way 2fer do it
if mybesttwofer >= 2 then
board[mybest] = player
return
endif
#
# if we can stop a 1 way 2fer stop it
if hisbesttwofer = 1 then
board[hisbest] = player
return
endif
#
# if we can get a 1 way 2fer do it
if mybesttwofer = 1 then
board[mybest] = player
return
endif
#
# last resort make a random move
# - loop until we randomly select an empty cell
do
i = int(rand * 9)
until board[i] = -1
board[i] = player
return
iswinner: #
# is there a winner = return winner - -1 if ther game continues
# winner = 0 for X, winner=1 for Y, winner=2 for cat
for winner = 0 to 1
# check columns
for t = 0 to 2
if board[0+t] = winner and board[3+t] = winner and board[6+t] = winner then return
next t
# check rows
for t = 0 to 2
if board[3*t] = winner and board[3*t+1] = winner and board[3*t+2] = winner then return
next t
# check diagonals
if board[0] = winner and board[4] = winner and board[8] = winner then return
if board[2] = winner and board[4] = winner and board[6] = winner then return
next winner
# check for empty square
winner = -1
for t = 0 to 8
if board[t]=-1 then return
next t
# must be a cat
winner = 2
return
counttwofer: #
# count the number of 2 in a row with a blank for twoferplayer
# return as twofers
# check columns
twofer = 0
for t = 0 to 2
if board[0+t] = twoferplayer and board[3+t] = twoferplayer and board[6+t] = -1 then twofer = twofer + 1
if board[0+t] = twoferplayer and board[3+t] = -1 and board[6+t] = twoferplayer then twofer = twofer + 1
if board[0+t] = -1 and board[3+t] = twoferplayer and board[6+t] = twoferplayer then twofer = twofer + 1
next t
# check rows
for t = 0 to 2
if board[3*t] = twoferplayer and board[3*t+1] = twoferplayer and board[3*t+2] = -1 then twofer = twofer + 1
if board[3*t] = twoferplayer and board[3*t+1] = -1 and board[3*t+2] = twoferplayer then twofer = twofer + 1
if board[3*t] = -1 and board[3*t+1] = twoferplayer and board[3*t+2] = twoferplayer then twofer = twofer + 1
next t
# check diagonals
if board[0] = twoferplayer and board[4] = twoferplayer and board[8] = -1 then twofer = twofer + 1
if board[0] = twoferplayer and board[4] = -1 and board[8] = twoferplayer then twofer = twofer + 1
if board[0] = -1 and board[4] = twoferplayer and board[8] = twoferplayer then twofer = twofer + 1
if board[2] = twoferplayer and board[4] = twoferplayer and board[6] = -1 then twofer = twofer + 1
if board[2] = twoferplayer and board[4] = -1 and board[6] = twoferplayer then twofer = twofer + 1
if board[2] = -1 and board[4] = twoferplayer and board[6] = twoferplayer then twofer = twofer + 1
return
drawboard: #
clg
color black
rect 0,95,300,10
rect 0,195,300,10
rect 95,0,10,300
rect 195,0,10,300
#
font "Tahoma",50,100
# draw X and O
for t = 0 to 8
if board[t]=0 then color red : text ((t % 3)*100)+25, (int(t/3)*100)+10, "X"
if board[t]=1 then color blue : text ((t % 3)*100)+25, (int(t/3)*100)+10, "O"
next t
refresh
return
|