Tic Tac Toe version 1.0
screen 18, 32
width 80,60
color 0, rgb(255,255,255)
cls
const vk_up = 72
const vk_right = 77
const vk_down = 80
const vk_left = 75
dim shared theRow = 1
dim shared theCol = 1
dim shared theWinner as string
enum theDirection
dUP = 1
dDOWN
dRIGHT
dLEFT
END enum
dim shared theGrid(3,3) as Integer
for i = 1 TO 3
for j = 1 to 3
theGrid(i, j) = ASC(" ")
NEXT j
NEXT i
type dieTP
value as integer
kept as integer
end type
dim choice as string
sub showTitle
print
print " TIC-TAC-TOE"
print
print " Version 1.0"
print
print "Use arrow keys to navigate, press X to move"
print " Q to exit"
print
end sub
sub drawGrid(clearIt as integer )
'Draw board
const offset = 10
const length = 4
const halfLength = 2
'Draw the Grid
for i = offset to (offset+(length*3))
locate (offset+length), i
print "-"
locate i, (offset+length)
print "|"
locate (offset+length*2), i
print "-"
locate i, (offset+length*2)
print "|"
next i
locate (offset+length),(offset+length)
print "+"
locate (offset+length),(offset+length*2)
print "+"
locate (offset+length*2),(offset+length)
print "+"
locate (offset+length*2),(offset+length*2)
print "+"
'Draw Selection
for i = 1 to 3
for j = 1 to 3
locate (offset + halfLength + (i-1) * (length)),(offset + halfLength + (j-1) * (length))
if theRow = i and theCol = j then
color , rgb(200,200,200)
if CHR(theGrid(i,j)) = " " then
print "?"
else
print CHR(theGrid(i,j))
end if
color , rgb(255,255,255)
else
print CHR(theGrid(i,j))
end if
next j
next i
locate 35, 1
if clearIt then
PRINT " "
locate 36, 1
PRINT " "
PRINT " "
end if
end sub
sub clearBoard
for i = 1 to 3
for j = 1 to 3
theGrid(i,j) = ASC(" ")
next j
next i
drawGrid(1)
end sub
sub isGameOver
theWinner = " "
'Check for horizontal wins
if theGrid(1,1) = theGrid(1,2) and theGrid(1,2) = theGrid(1,3) then
if CHR(theGrid(1,1)) <> " " then
'print "Row 1"
theWinner = CHR(theGrid(1,1))
return
end if
end if
if theGrid(2,1) = theGrid(2,2) and theGrid(2,2) = theGrid(2,3) then
if CHR(theGrid(2,2)) <> " " then
'print "Row 2"
theWinner = CHR(theGrid(2,2))
return
end if
end if
if theGrid(3,1) = theGrid(3,2) and theGrid(3,2) = theGrid(3,3) then
if CHR(theGrid(3,1)) <> " " then
'print "Row 3"
theWinner = CHR(theGrid(3,1))
return
end if
end if
if theGrid(1,1) = theGrid(2,1) and theGrid(2,1) = theGrid(3,1) then
if CHR(theGrid(1,1)) <> " " then
'print "Col 1"
theWinner = CHR(theGrid(1,1))
return
end if
end if
if theGrid(1,2) = theGrid(2,2) and theGrid(2,2) = theGrid(3,2) then
if CHR(theGrid(2,2)) <> " " then
'print "Col 2"
theWinner = CHR(theGrid(2,2))
return
end if
end if
if theGrid(1,3) = theGrid(2,3) and theGrid(2,3) = theGrid(3,3) then
if CHR(theGrid(3,3)) <> " " then
'print "Col 3"
theWinner = CHR(theGrid(3,3))
return
end if
end if
if theGrid(1,1) = theGrid(2,2) and theGrid(2,2) = theGrid(3,3) then
if CHR(theGrid(3,3)) <> " " then
'print "Diag R"
theWinner = CHR(theGrid(3,3))
return
end if
end if
if theGrid(1,3) = theGrid(2,2) and theGrid(2,2) = theGrid(3,1) then
if CHR(theGrid(2,2)) <> " " then
'print "Diag L"
theWinner = CHR(theGrid(2,2))
return
end if
end if
dim isOver as integer
isOver = 1
'cats game
for i = 1 to 3
for j = 1 to 3
if CHR(theGrid(i,j)) = " " then
isOver = 0
end if
next j
next i
if isOver = 1 then
theWinner = "C"
end if
end sub
function playerMove()
if CHR(theGrid(theRow,theCol)) = " " then
theGrid(theRow,theCol) = ASC("X")
return 1
else
locate 35, 1
PRINT "Invalid Move"
return 0
end if
'drawGrid( 0 )
end function
sub moveCursor( direction as integer )
select case direction
case dUP
'print "UP direction"
if theRow > 1 then
theRow = theRow - 1
end if
case dDOWN
'print "DOWN direction"
if theRow < 3 then
theRow = theRow + 1
end if
case dLEFT
'print "LEFT direction"
if theCol > 1 then
theCol = theCol - 1
end if
case dRIGHT
'print "RIGHT direction"
if theCol < 3 then
theCol = theCol + 1
end if
end select
end sub
function moveToWin
'Maybe not the best way, but its easy
for i = 1 to 3
for j = 1 to 3
if CHR(theGrid(i, j)) = " " then
'Just try it out to see if it wins
theGrid(i,j) = ASC("O")
isGameOver
if theWinner = "O" then
return 1
end if
'Put it back
theGrid(i,j) = ASC(" ")
end if
next j
next i
return 0
end function
function moveToBlock
'Maybe not the best way, but its easy
for i = 1 to 3
for j = 1 to 3
if CHR(theGrid(i, j)) = " " then
'Just try it out to see if it wins
theGrid(i,j) = ASC("X")
isGameOver
if theWinner = "X" then
theGrid(i,j) = ASC("O")
return 1
end if
'Put it back
theGrid(i,j) = ASC(" ")
end if
next j
next i
return 0
end function
sub makeRandomMove
'print "not done yet"
for i = 1 to 3
for j = 1 to 3
if CHR(theGrid(i, j)) = " " then
theGrid(i,j) = ASC("O")
return
end if
next j
next i
end sub
sub computerMove
if moveToWin = 0 then
if moveToBlock = 0 then
makeRandomMove
end if
end if
end sub
rem Start Execution Here
showTitle
print
cls
showTitle
drawGrid(1)
dim shouldComputerMove as integer
do
do
choice = inkey
loop until choice <> ""
if choice = "q" then end 'Player wants to exit
if choice = "Q" then end
shouldComputerMove = 0
if choice = "X" or choice = "x" then
if playerMove = 0 then
drawGrid(0)
else
'locate 35, 1
'print "Computer move"
'computerMove
'drawGrid(0)
shouldComputerMove = 1
end if
elseif MULTIKEY(vk_up) then
moveCursor( dUP )
drawGrid(1)
elseif MULTIKEY(vk_down) then
moveCursor( dDOWN )
drawGrid(1)
elseif MULTIKEY(vk_right) then
moveCursor( dRIGHT )
drawGrid(1)
elseif MULTIKEY(vk_left) then
moveCursor( dLEFT )
drawGrid(1)
endif
drawGrid(1)
'Need to detect if game is done
isGameOver 'Check for player win
'print "Checking who won"
if theWinner <> " " then
locate 36,1
if theWinner = "C" then
print "GAME OVER - CATS GAME"
elseif theWinner = "O" then
print "GAME OVER - I WIN"
else
print "GAME OVER - YOU WIN"
end if
print "Press a Key to continue ..."
do
choice = inkey
loop until choice <> ""
clearBoard
'print " -- sorry re-play is not implemented"
else
if shouldComputerMove = 1 then
computerMove
drawGrid(0)
isGameOver
shouldComputerMove = 0
if theWinner <> " " then
locate 36,1
if theWinner = "C" then
print "GAME OVER - CATS GAME"
elseif theWinner = "O" then
print "GAME OVER - I WIN"
else
print "GAME OVER - YOU WIN"
end if
print "Press a Key to continue ..."
do
choice = inkey
loop until choice <> ""
clearBoard
'print " -- sorry re-play is not implemented"
end if
end if
end if
loop




