TicTacToe

Simple "Tic Tac Toe" port from Liberty BASIC, in Ausria we call this "Deppen Schach (Chess for Idiots)".

code format="vbnet" 'Tic Tac Toe 'Requires Liberty BASIC v2.0 or better 'Copyright 1993,2001 Shoptalk Systems 'All Rights Reserved '   'Update for "Run BASIC" 'By: Stefan Pendl 'Date: 16.01.07

graphic #brd, 320, 400

[start] 'start game (or restart) gosub [drawBoard] for i = 1 to 9 board$(i) = " " next i   gosub [modelBoard] turn = 0 true = 1 false = 0 pass = false moveCounter = 0 play$ = ""

[check.key] cls render #brd if pass = false then print "Enter 'P' to let Computer start." input "Enter cell number: "; char$

select case char$ case "1", "2", "3", "4", "5", "6", "7", "8", "9" position$ = char$ goto [move] case "P", "p" position$ = "" goto [skipMove] case else print "Wrong input, try again!" end select goto [check.key]

[move] ' X moves if board$(val(position$)) <> " " then print "Illegal Move!" goto [check.key] end if

moveCounter = moveCounter + 1 pass = true board$(val(position$)) = "X" gosub [drawX] gosub [modelBoard] gosub [checkForWinOrDraw] position$ = "" goto [oMoves]

[skipMove] if pass = true then print "It's your turn!" goto [check.key] else pass = true end if

[oMoves] ' O moves gosub [computeMove] board$(val(position$)) = "O" gosub [drawO] gosub [modelBoard] gosub [checkForWinOrDraw] goto [check.key] end

[checkForWinOrDraw] ' check for win win = 0 letter$ = "XXX" for i = 1 to 2 if instr(row1$+","+row2$+","+row3$, letter$) or _ instr(col1$+","+col2$+","+col3$, letter$) or _ instr(diag1$+","+diag2$, letter$) then win = 1 i = 2 else letter$ = "OOO" end if   next i

#brd font("times_new_roman", 20, "bold") #brd place(10, 390) if letter$ = "OOO" and win = 1 then #brd color("green") #brd "\I WIN! HA HA HA!" call Smilie "win" else if letter$ = "XXX" and win = 1 then #brd color("red") #brd "\You win!!! Bummer!" call Smilie "loose" else 'check to see if the board is full if instr(row1$+row2$+row3$, " ") then return else 'the game is a draw #brd color(255, 135, 0) #brd "\Draw!" call Smilie "draw" end if       end if    end if

[askPlayAgain] cls render #brd play$ = "N" input "Play again (Y/[N])"; play$ if instr("Yy", play$) then [start]

[quit] end

[drawBoard] #brd cls("lightgray") #brd color("red") #brd font("times_new_roman", 20, "bold","italic") #brd place(10, 30) #brd "\TIC TAC TOE" #brd font("times_new_roman", 12, "bold") #brd place(180, 50) #brd "\for Run BASIC" #brd color("black") #brd size(3) for a = 0 to 2 for b = 0 to 2 x = a*100+4 y = b*100+54 #brd place(x, y)           #brd box(x + 100, y + 100) cell = int(x/100)+int(y*3/100) #brd place(x + 5, y + 20) #brd "\"; cell next b   next a    return

[drawX] gosub [xlatePosition] #brd size(6) #brd color("darkred") #brd line(squareX-11, squareY-16, squareX+19, squareY+24) #brd line(squareX+19, squareY-16, squareX-11, squareY+24) #brd color("red") #brd line(squareX-15, squareY-20, squareX+15, squareY+20) #brd line(squareX+15, squareY-20, squareX-15, squareY+20) return

[drawO] gosub [xlatePosition] #brd size(7) #brd color("darkblue") #brd place(squareX+5, squareY+5) #brd circle(20) #brd color("blue") #brd place(squareX, squareY) #brd circle(20) return

[xlatePosition] squareX = 55 if instr("258", position$) > 0 then squareX = 155 if instr("369", position$) > 0 then squareX = 255

squareY = int((val(position$)+2)/3)*100+5 return

[modelBoard] row1$ = board$(1)+board$(2)+board$(3) row2$ = board$(4)+board$(5)+board$(6) row3$ = board$(7)+board$(8)+board$(9)

col1$ = board$(1)+board$(4)+board$(7) col2$ = board$(2)+board$(5)+board$(8) col3$ = board$(3)+board$(6)+board$(9)

diag1$ = board$(1)+board$(5)+board$(9) diag2$ = board$(7)+board$(5)+board$(3) return

[computeMove] 'check for instant win!

if row1$ = "OO " then position$ = "3" if row1$ = " OO" then position$ = "1" if row1$ = "O O" then position$ = "2" if row2$ = "OO " then position$ = "6" if row2$ = " OO" then position$ = "4" if row2$ = "O O" then position$ = "5" if row3$ = "OO " then position$ = "9" if row3$ = " OO" then position$ = "7" if row3$ = "O O" then position$ = "8"

if col1$ = "OO " then position$ = "7" if col1$ = " OO" then position$ = "1" if col1$ = "O O" then position$ = "4" if col2$ = "OO " then position$ = "8" if col2$ = " OO" then position$ = "2" if col2$ = "O O" then position$ = "5" if col3$ = "OO " then position$ = "9" if col3$ = " OO" then position$ = "3" if col3$ = "O O" then position$ = "6"

if diag1$ = "OO " then position$ = "9" if diag1$ = " OO" then position$ = "1" if diag1$ = "O O" then position$ = "5" if diag2$ = "OO " then position$ = "3" if diag2$ = " OO" then position$ = "7" if diag2$ = "O O" then position$ = "5" if position$ <> "" then return

'make the purely defensive moves

if row1$ = "XX " then position$ = "3" if row1$ = " XX" then position$ = "1" if row1$ = "X X" then position$ = "2" if row2$ = "XX " then position$ = "6" if row2$ = " XX" then position$ = "4" if row2$ = "X X" then position$ = "5" if row3$ = "XX " then position$ = "9" if row3$ = " XX" then position$ = "7" if row3$ = "X X" then position$ = "8"

if col1$ = "XX " then position$ = "7" if col1$ = " XX" then position$ = "1" if col1$ = "X X" then position$ = "4" if col2$ = "XX " then position$ = "8" if col2$ = " XX" then position$ = "2" if col2$ = "X X" then position$ = "5" if col3$ = "XX " then position$ = "9" if col3$ = " XX" then position$ = "3" if col3$ = "X X" then position$ = "6"

if diag1$ = "XX " then position$ = "9" if diag1$ = " XX" then position$ = "1" if diag1$ = "X X" then position$ = "5" if diag2$ = "XX " then position$ = "3" if diag2$ = " XX" then position$ = "7" if diag2$ = "X X" then position$ = "5" if position$ <> "" then return

[attack] ' now make the offensive moves moves$ = ""

if instr(diag1$,"X") = 0 then moves$ = moves$ + mid$("159", instr(diag1$, " "), 1) else if instr(diag2$,"X") = 0 then moves$ = moves$ + mid$("753", instr(diag2$, " "), 1) else ' now make random moves all$ = row1$ + row2$ + row3$ for i = 1 to 9 if mid$(all$, i, 1) = " " then moves$ = moves$ + str$(i) next i           if moves$ = "" then position$ = "" return end if       end if    end if    i = int(rnd(1)*len(moves$))+1 position$ = mid$(moves$, i, 1) return

sub Smilie state$ StartX = 159 StartY = 199 #brd size(80) #brd color("yellow") #brd place(StartX, StartY) #brd circle(40) #brd size(8) #brd color("blue") select case state$ case "draw" #brd line(StartX-40, StartY-30, StartX-20, StartY-30) #brd line(StartX+20, StartY-30, StartX+40, StartY-30) case else #brd place(StartX-30, StartY-30) #brd circle(4) #brd place(StartX+30, StartY-30) #brd circle(4) end select #brd color("red") select case state$ case "loose" #brd line(StartX-40, StartY+40, StartX-30, StartY+30) #brd line(StartX-30, StartY+30, StartX+30, StartY+30) #brd line(StartX+30, StartY+30, StartX+40, StartY+40) case "draw" #brd line(StartX-40, StartY+30, StartX+40, StartY+30) case else #brd line(StartX-40, StartY+30, StartX-30, StartY+40) #brd line(StartX-30, StartY+40, StartX+30, StartY+40) #brd line(StartX+30, StartY+40, StartX+40, StartY+30) end select end sub code