Tic Tac Toe
ZoeG 17 May 2007 06:34
This isn't done yet, but it has some functionality. I had a bit of trouble with the language, but I worked around it so it may look pretty lame in spots.
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
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 " "
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
return false
end function
function moveToBlock
return false
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 NOT moveToWin then
if NOT moveToBlock then
makeRandomMove
end if
end if
end sub
function isGameOver
for i = 1 to 3
for j = 1 to 3
if CHR(theGrid(i,j)) = " " then
return 0
end if
next j
next i
return 1
end function
rem Start Execution Here
showTitle
print
cls
showTitle
drawGrid(1)
do
do
choice = inkey
loop until choice <> ""
if choice = "q" then end 'Player wants to exit
if choice = "Q" then end
if choice = "X" or choice = "x" then
if playerMove = 0 then
drawGrid(0)
else
locate 35, 1
'print "COmputer move"
computerMove
drawGrid(0)
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
'Need to detect if game is done
if isGameOver = 1 then
locate 36,1
print "GAME OVER -- sorry re-play is not implemented"
end if
loop