home *** CD-ROM | disk | FTP | other *** search
/ PC User 2001 August / APC_Aug2001_CD2.iso / features / devtools / files / lb202win.exe / LB202W.EXE / TTT.BAS < prev    next >
Encoding:
BASIC Source File  |  2001-05-31  |  7.1 KB  |  284 lines

  1.  
  2.     'Tic Tac Toe
  3.     'Requires Liberty BASIC v2.0 or better
  4.     'Copyright 1993,2001 Shoptalk Systems
  5.     'All Rights Reserved
  6.  
  7.     nomainwin
  8.  
  9.     loadbmp "title", "bmp\titlettt.bmp"
  10.     loadbmp "square", "bmp\squarttt.bmp"
  11.  
  12.     WindowWidth = 314
  13.     WindowHeight = 380
  14.     button #brd, " T ", [1], UL, 10, 60
  15.     button #brd, " I ", [2], UL, 110, 60
  16.     button #brd, " C ", [3], UL, 210, 60
  17.     button #brd, " T ", [4], UL, 10, 160
  18.     button #brd, " A ", [5], UL, 110, 160
  19.     button #brd, " C ", [6], UL, 210, 160
  20.     button #brd, " T ", [7], UL, 10, 260
  21.     button #brd, " O ", [8], UL, 110, 260
  22.     button #brd, " E ", [9], UL, 210, 260
  23.     button #brd, "Pass Move", [skipMove], UL, 210, 10
  24.     open "Tic Tac Toe Playing Board" for graphics_nsb as #brd
  25.     print #brd, "trapclose [quit]";
  26.     print #brd, "size 3 ; down";
  27.  
  28. [start]    'start game (or restart)
  29.  
  30.     gosub [drawBoard]
  31.     for x = 1 to 9 : board$(x) = " " : next x
  32.     gosub [modelBoard]
  33.     turn = 0
  34.     true = 1
  35.     false = 0
  36.     pass = false
  37.     moveCounter = 0
  38.  
  39. [playLoop]
  40.  
  41.  
  42.     input position$
  43.  
  44.   goto [playLoop]
  45.  
  46.  
  47. [move]
  48.  
  49.     ' X moves
  50.     if board$(val(position$)) <> " " then notice "Illegal Move!" : goto [playLoop]
  51.     moveCounter = moveCounter + 1
  52.     pass = false
  53.     board$(val(position$)) = "X"
  54.     gosub [drawX]
  55.     gosub [modelBoard]
  56.     gosub [checkForWinOrDraw]
  57.     position$ = ""
  58.     goto [oMoves]
  59.  
  60. [skipMove]
  61.  
  62.     pass = true
  63.  
  64. [oMoves]
  65.  
  66.     ' O moves
  67.     gosub [computeMove]
  68.     board$(val(position$)) = "O"
  69.     gosub [drawO]
  70.     gosub [modelBoard]
  71.     gosub [checkForWinOrDraw]
  72.  
  73.   goto [playLoop]
  74.  
  75.  
  76. [1]
  77.     position$ = "1" : goto [move]
  78. [2]
  79.     position$ = "2" : goto [move]
  80. [3]
  81.     position$ = "3" : goto [move]
  82. [4]
  83.     position$ = "4" : goto [move]
  84. [5]
  85.     position$ = "5" : goto [move]
  86. [6]
  87.     position$ = "6" : goto [move]
  88. [7]
  89.     position$ = "7" : goto [move]
  90. [8]
  91.     position$ = "8" : goto [move]
  92. [9]
  93.     position$ = "9" : goto [move]
  94.  
  95.  
  96. [drawBoard]
  97.  
  98.     print #brd, "cls ; fill lightgray ; drawbmp title 7 2";
  99.     for x = 4 to 204 step 100
  100.     for y = 54 to 254 step 100
  101.         print #brd, "drawbmp square "; x; " "; y ;
  102.     next y
  103.     next x
  104.     print #brd, "flush";
  105.   return
  106.  
  107. [drawX]
  108.  
  109.     gosub [xlatePosition]
  110.     print #brd, "size 6 ; color darkred";
  111.     print #brd, "line "; squareX-11; " "; squareY-16; " "; squareX+19; " "; squareY+24
  112.     print #brd, "line "; squareX+19; " "; squareY-16; " "; squareX-11; " "; squareY+24
  113.     print #brd, "color red";
  114.     print #brd, "line "; squareX-15; " "; squareY-20; " "; squareX+15; " "; squareY+20
  115.     print #brd, "line "; squareX+15; " "; squareY-20; " "; squareX-15; " "; squareY+20
  116.     print #brd, "flush";
  117.  
  118.   return
  119.  
  120.  
  121. [drawO]
  122.  
  123.     gosub [xlatePosition]
  124.     print #brd, "size 7 ; color darkblue";
  125.     print #brd, "place "; squareX+5; " "; squareY+5
  126.     print #brd, "circle 20 ; color blue";
  127.     print #brd, "place "; squareX; " "; squareY
  128.     print #brd, "circle 20 ; flush";
  129.  
  130.   return
  131.  
  132.  
  133. [xlatePosition]
  134.  
  135.     squareX = 55
  136.     if instr("258", position$) > 0 then squareX = 155
  137.     if instr("369", position$) > 0 then squareX = 255
  138.  
  139.     squareY = int((val(position$)+2)/3)*100+5
  140.  
  141.   return
  142.  
  143.  
  144. [modelBoard]
  145.  
  146.     row1$ = board$(1)+board$(2)+board$(3)
  147.     row2$ = board$(4)+board$(5)+board$(6)
  148.     row3$ = board$(7)+board$(8)+board$(9)
  149.  
  150.     col1$ = board$(1)+board$(4)+board$(7)
  151.     col2$ = board$(2)+board$(5)+board$(8)
  152.     col3$ = board$(3)+board$(6)+board$(9)
  153.  
  154.     diag1$ = board$(1)+board$(5)+board$(9)
  155.     diag2$ = board$(7)+board$(5)+board$(3)
  156.  
  157.   return
  158.  
  159.  
  160.  
  161. [computeMove]
  162.  
  163.  
  164.     'check for instant win!
  165.  
  166.     if row1$ = "OO " then position$ = "3"
  167.     if row1$ = " OO" then position$ = "1"
  168.     if row1$ = "O O" then position$ = "2"
  169.     if row2$ = "OO " then position$ = "6"
  170.     if row2$ = " OO" then position$ = "4"
  171.     if row2$ = "O O" then position$ = "5"
  172.     if row3$ = "OO " then position$ = "9"
  173.     if row3$ = " OO" then position$ = "7"
  174.     if row3$ = "O O" then position$ = "8"
  175.  
  176.     if col1$ = "OO " then position$ = "7"
  177.     if col1$ = " OO" then position$ = "1"
  178.     if col1$ = "O O" then position$ = "4"
  179.     if col2$ = "OO " then position$ = "8"
  180.     if col2$ = " OO" then position$ = "2"
  181.     if col2$ = "O O" then position$ = "5"
  182.     if col3$ = "OO " then position$ = "9"
  183.     if col3$ = " OO" then position$ = "3"
  184.     if col3$ = "O O" then position$ = "6"
  185.  
  186.     if diag1$ = "OO " then position$ = "9"
  187.     if diag1$ = " OO" then position$ = "1"
  188.     if diag1$ = "O O" then position$ = "5"
  189.     if diag2$ = "OO " then position$ = "3"
  190.     if diag2$ = " OO" then position$ = "7"
  191.     if diag2$ = "O O" then position$ = "5"
  192.  
  193.     'make the purely defensive moves
  194.  
  195.     if row1$ = "XX " then position$ = "3"
  196.     if row1$ = " XX" then position$ = "1"
  197.     if row1$ = "X X" then position$ = "2"
  198.     if row2$ = "XX " then position$ = "6"
  199.     if row2$ = " XX" then position$ = "4"
  200.     if row2$ = "X X" then position$ = "5"
  201.     if row3$ = "XX " then position$ = "9"
  202.     if row3$ = " XX" then position$ = "7"
  203.     if row3$ = "X X" then position$ = "8"
  204.  
  205.     if col1$ = "XX " then position$ = "7"
  206.     if col1$ = " XX" then position$ = "1"
  207.     if col1$ = "X X" then position$ = "4"
  208.     if col2$ = "XX " then position$ = "8"
  209.     if col2$ = " XX" then position$ = "2"
  210.     if col2$ = "X X" then position$ = "5"
  211.     if col3$ = "XX " then position$ = "9"
  212.     if col3$ = " XX" then position$ = "3"
  213.     if col3$ = "X X" then position$ = "6"
  214.  
  215.     if diag1$ = "XX " then position$ = "9"
  216.     if diag1$ = " XX" then position$ = "1"
  217.     if diag1$ = "X X" then position$ = "5"
  218.     if diag2$ = "XX " then position$ = "3"
  219.     if diag2$ = " XX" then position$ = "7"
  220.     if diag2$ = "X X" then position$ = "5"
  221.     if position$ <> "" then return
  222.  
  223.  
  224. [attack]
  225.  
  226.     ' now make the offensive moves
  227.  
  228.     moves$ = ""
  229.  
  230.     if instr(diag1$,"X") = 0 then
  231.         moves$ = moves$ + mid$("159", instr(diag1$, " "), 1)
  232.      else
  233.         if instr(diag2$,"X") = 0 then
  234.             moves$ = moves$ + mid$("357", instr(diag2$, " "), 1)
  235.           else
  236.             ' now make random moves
  237.             all$ = row1$ + row2$ + row3$
  238.             for i = 1 to 9
  239.                 if mid$(all$, i, 1) = " " then moves$ = moves$ + chr$(48+i)
  240.             next i
  241.             if moves$ = "" then position$ = "" : return
  242.         end if
  243.     end if
  244.     i = int(rnd(1)*len(moves$))+1
  245.     position$ = mid$(moves$, i, 1)
  246.     return
  247.  
  248. [checkForWinOrDraw]
  249.  
  250.     ' check for win
  251.     letter$ = "XXX"
  252.     for i = 1 to 2
  253.         if instr(row1$+","+row2$+","+row3$, letter$) then [win]
  254.         if instr(col1$+","+col2$+","+col3$, letter$) then [win]
  255.         if instr(diag1$+","+diag2$, letter$) then [win]
  256.         letter$ = "OOO"
  257.     next i
  258.  
  259.     'check to see if the board is full
  260.     if instr(row1$+row2$+row3$, " ") then return
  261.  
  262.     'the game is a draw
  263.  
  264.     notice "Draw!"
  265.     goto [askPlayAgain]
  266.  
  267.  
  268. [win]
  269.  
  270.     if letter$ = "OOO" then notice "I WIN!  HA HA HA!"
  271.     if letter$ = "XXX" then notice "You win!!! Bummer!"
  272.  
  273. [askPlayAgain]
  274.  
  275.     confirm "Play again?"; play$
  276.     if play$ = "yes" then [start]
  277.  
  278. [quit]
  279.  
  280.     close #brd
  281.     end
  282.  
  283.  
  284.