home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / basic / interpre / liberty / windows / tictac.bas < prev    next >
BASIC Source File  |  1994-05-25  |  9KB  |  329 lines

  1.  
  2.     'Tic Tac Toe
  3.     'Requires Liberty BASIC v0.9c or better
  4.     'Copyright 1993, Shoptalk Systems
  5.     'All Rights Reserved
  6.  
  7.     ' set up the board
  8.     dim board$(9)
  9.  
  10.     print "Tic Tac Toe"
  11.     print "Copyright 1993, Shoptalk Systems"
  12.     print
  13.     print "Instructions:"
  14.     print
  15.     print "  In this game, you play against the"
  16.     print "  computer.  You are X, and the computer"
  17.     print "  is O.  The one to get 3 in a row wins."
  18.     print "  If no one gets three in a row, then"
  19.     print "  it is a draw."
  20.     print
  21.     print "- Press any key to play -" ' for Liberty BASIC 1.x, change to: print "Press Enter to play"
  22.     h$ = input$(1) ' for Liberty BASIC 1.x, change to: input h$
  23.  
  24.     yourName$ = "Human"
  25.     prompt "What is your name?"; yourName$
  26.  
  27.     WindowWidth = 330  ' for Liberty BASIC 1.x, change to: WindowWidth = 316
  28.     WindowHeight = 400
  29.     button #brd, " T ", [1], UL, 10, 60
  30.     button #brd, " I ", [2], UL, 110, 60
  31.     button #brd, " C ", [3], UL, 210, 60
  32.     button #brd, " T ", [4], UL, 10, 160
  33.     button #brd, " A ", [5], UL, 110, 160
  34.     button #brd, " C ", [6], UL, 210, 160
  35.     button #brd, " T ", [7], UL, 10, 260
  36.     button #brd, " O ", [8], UL, 110, 260
  37.     button #brd, " E ", [9], UL, 210, 260
  38.     button #brd, "Pass Move", [skipMove], UL, 10, 10
  39.     open "Tic Tac Toe Playing Board" for graphics as #brd ' for Liberty BASIC 1.x, change graphics to graphics_nsb
  40.  
  41. [start]    'start game (or restart)
  42.  
  43.     for x = 1 to 9 : board$(x) = " " : next x
  44.     gosub [modelBoard]
  45.     turn = 0
  46.     gosub [drawBoard]
  47.  
  48. [playLoop]
  49.  
  50.  
  51.     input position$
  52.  
  53.   goto [playLoop]
  54.  
  55.  
  56. [move]
  57.  
  58.     ' X moves
  59.     if board$(int(val(position$))) <> " " then notice "Illegal Move!" : goto [playLoop]
  60.     board$(val(position$)) = "X"
  61.     gosub [drawX]
  62.     gosub [modelBoard]
  63.     gosub [checkForWinOrDraw]
  64.  
  65. [skipMove]
  66.  
  67.     ' O moves
  68.  
  69.     gosub [computeMove]
  70.     board$(val(position$)) = "O"
  71.     gosub [drawO]
  72.     gosub [modelBoard]
  73.     gosub [checkForWinOrDraw]
  74.  
  75.   goto [playLoop]
  76.  
  77.  
  78. [1]
  79.     position$ = "1" : goto [move]
  80. [2]
  81.     position$ = "2" : goto [move]
  82. [3]
  83.     position$ = "3" : goto [move]
  84. [4]
  85.     position$ = "4" : goto [move]
  86. [5]
  87.     position$ = "5" : goto [move]
  88. [6]
  89.     position$ = "6" : goto [move]
  90. [7]
  91.     position$ = "7" : goto [move]
  92. [8]
  93.     position$ = "8" : goto [move]
  94. [9]
  95.     position$ = "9" : goto [move]
  96.  
  97. [end]
  98.  
  99.     end
  100.  
  101. [drawBoard]
  102.  
  103.     print #brd, "cls";
  104.     print #brd, "fill lightgray";
  105.     print #brd, "size 3 ; down";
  106.     for x = 0 to 2
  107.     for y = 0 to 2
  108.         print #brd, "color darkgray";
  109.         print #brd, "line "; 100*x+95; " "; 100*y+55; " "; 100*x+5; " "; 100*y+55
  110.         print #brd, "line "; 100*x+5; " "; 100*y+55; " "; 100*x+5; " "; 100*y+145
  111.         print #brd, "color white";
  112.         print #brd, "line "; 100*x+95; " "; 100*y+55; " "; 100*x+95; " "; 100*y+145
  113.         print #brd, "line "; 100*x+5; " "; 100*y+145; " "; 100*x+95; " "; 100*y+145
  114.     next y
  115.     next x
  116.     print #brd, "flush"
  117.  
  118.   return
  119.  
  120.  
  121. [drawX]
  122.  
  123.     gosub [xlatePosition]
  124.     print #brd, "size 6";
  125.     print #brd, "color darkred";
  126.     print #brd, "line "; squareX-10; " "; squareY-15; " "; squareX+20; " "; squareY+25
  127.     print #brd, "line "; squareX+20; " "; squareY-15; " "; squareX-10; " "; squareY+25
  128.     print #brd, "color red";
  129.     print #brd, "line "; squareX-15; " "; squareY-20; " "; squareX+15; " "; squareY+20
  130.     print #brd, "line "; squareX+15; " "; squareY-20; " "; squareX-15; " "; squareY+20
  131.     print #brd, "flush";
  132.  
  133.   return
  134.  
  135.  
  136. [drawO]
  137.  
  138.     gosub [xlatePosition]
  139.     print #brd, "size 7";
  140.     print #brd, "color darkblue";
  141.     print #brd, "place "; squareX+5; " "; squareY+5
  142.     print #brd, "circle 20";
  143.     print #brd, "color blue";
  144.     print #brd, "place "; squareX; " "; squareY
  145.     print #brd, "circle 20";
  146.     print #brd, "flush";
  147.  
  148.   return
  149.  
  150.  
  151. [xlatePosition]
  152.  
  153.     position = val(position$)
  154.  
  155.     if instr("147", position$) > 0 then squareX = 55
  156.     if instr("258", position$) > 0 then squareX = 155
  157.     if instr("369", position$) > 0 then squareX = 255
  158.  
  159.     squareY = int((val(position$)+2)/3)*100+5
  160.  
  161.   return
  162.  
  163.  
  164. [modelBoard]
  165.  
  166.     row1$ = board$(1)+board$(2)+board$(3)
  167.     row2$ = board$(4)+board$(5)+board$(6)
  168.     row3$ = board$(7)+board$(8)+board$(9)
  169.  
  170.     col1$ = board$(1)+board$(4)+board$(7)
  171.     col2$ = board$(2)+board$(5)+board$(8)
  172.     col3$ = board$(3)+board$(6)+board$(9)
  173.  
  174.     diag1$ = board$(1)+board$(5)+board$(9)
  175.     diag2$ = board$(7)+board$(5)+board$(3)
  176.  
  177.   return
  178.  
  179.  
  180.  
  181. [computeMove]
  182.  
  183.     'check for instant win!
  184.     print "" ;
  185.  
  186. [instantWin]
  187.  
  188.     if row1$ = "OO " then position$ = "3" : return
  189.     if row1$ = " OO" then position$ = "1" : return
  190.     if row1$ = "O O" then position$ = "2" : return
  191.     if row2$ = "OO " then position$ = "6" : return
  192.     if row2$ = " OO" then position$ = "4" : return
  193.     if row2$ = "O O" then position$ = "5" : return
  194.     if row3$ = "OO " then position$ = "9" : return
  195.     if row3$ = " OO" then position$ = "7" : return
  196.     if row3$ = "O O" then position$ = "8" : return
  197.  
  198.     if col1$ = "OO " then position$ = "7" : return
  199.     if col1$ = " OO" then position$ = "1" : return
  200.     if col1$ = "O O" then position$ = "4" : return
  201.     if col2$ = "OO " then position$ = "8" : return
  202.     if col2$ = " OO" then position$ = "2" : return
  203.     if col2$ = "O O" then position$ = "5" : return
  204.     if col3$ = "OO " then position$ = "9" : return
  205.     if col3$ = " OO" then position$ = "3" : return
  206.     if col3$ = "O O" then position$ = "6" : return
  207.  
  208.     if diag1$ = "OO " then position$ = "9" : return
  209.     if diag1$ = " OO" then position$ = "1" : return
  210.     if diag1$ = "O O" then position$ = "5" : return
  211.     if diag2$ = "OO " then position$ = "3" : return
  212.     if diag2$ = " OO" then position$ = "7" : return
  213.     if diag2$ = "O O" then position$ = "5" : return
  214.  
  215.     'make the purely defensive moves
  216.  
  217.     if row1$ = "XX " then position$ = "3" : return
  218.     if row1$ = " XX" then position$ = "1" : return
  219.     if row1$ = "X X" then position$ = "2" : return
  220.     if row2$ = "XX " then position$ = "6" : return
  221.     if row2$ = " XX" then position$ = "4" : return
  222.     if row2$ = "X X" then position$ = "5" : return
  223.     if row3$ = "XX " then position$ = "9" : return
  224.     if row3$ = " XX" then position$ = "7" : return
  225.     if row3$ = "X X" then position$ = "8" : return
  226.  
  227.     if col1$ = "XX " then position$ = "7" : return
  228.     if col1$ = " XX" then position$ = "1" : return
  229.     if col1$ = "X X" then position$ = "4" : return
  230.     if col2$ = "XX " then position$ = "8" : return
  231.     if col2$ = " XX" then position$ = "2" : return
  232.     if col2$ = "X X" then position$ = "5" : return
  233.     if col3$ = "XX " then position$ = "9" : return
  234.     if col3$ = " XX" then position$ = "3" : return
  235.     if col3$ = "X X" then position$ = "6" : return
  236.  
  237.     if diag1$ = "XX " then position$ = "9" : return
  238.     if diag1$ = " XX" then position$ = "1" : return
  239.     if diag1$ = "X X" then position$ = "5" : return
  240.     if diag2$ = "XX " then position$ = "3" : return
  241.     if diag2$ = " XX" then position$ = "7" : return
  242.     if diag2$ = "X X" then position$ = "5" : return
  243.  
  244.     ' now make the offensive moves
  245.  
  246.     moves$ = ""
  247.  
  248.     if instr(diag1$,"X") > 0 then [d2]
  249.     if board$(1) = " " then moves$ = moves$ + "1"
  250.     if board$(5) = " " then moves$ = moves$ + "5"
  251.     if board$(9) = " " then moves$ = moves$ + "9"
  252.     i = int(rnd(1)*len(moves$))+1
  253.     position$ = mid$(moves$, i, 1)
  254.     return
  255.  
  256.  
  257.  
  258. [d2]
  259.  
  260.     if instr(diag2$,"X") > 0 then [moreMoves]
  261.     if board$(7) = " " then moves$ = moves$ + "7"
  262.     if board$(5) = " " then moves$ = moves$ + "5"
  263.     if board$(3) = " " then moves$ = moves$ + "3"
  264.     i = int(rnd(1)*len(moves$))+1
  265.     position$ = mid$(moves$, i, 1)
  266.     return
  267.  
  268.  
  269. [moreMoves]
  270.  
  271.     ' now make random moves
  272.  
  273.     all$ = row1$ + row2$ + row3$
  274.     blanks$ = ""
  275.     for i = 1 to 9
  276.         if mid$(all$, i, 1) = " " then blanks$ = blanks$ + chr$(48+i)
  277.     next i
  278.     if blanks$ = "" then position$ = "" : return
  279.     i = int(rnd(1)*len(blank$))+1
  280.     position$ = mid$(blanks$, i, 1)
  281.     return
  282.  
  283. [error]
  284.  
  285.     notice "Error, no move computed"
  286.     stop
  287.  
  288.  
  289.     'check to see if the game has been won or drawn
  290.  
  291. [checkForWinOrDraw]
  292.  
  293.     ' check for win
  294.     i$ = ","
  295.     letter$ = "XXX"
  296.     for i = 1 to 2
  297.         if instr(row1$+i$+row2$+i$+row3$, letter$) > 0 then [win]
  298.         if instr(col1$+i$+col2$+i$+col3$, letter$) > 0 then [win]
  299.         if instr(diag1$+i$+diag2$, letter$) > 0 then [win]
  300.         letter$ = "OOO"
  301.     next i
  302.  
  303.     'check to see if the board is full
  304.     if instr(row1$+row2$+row3$, " ") > 0 then return
  305.  
  306.     'the game is a draw
  307.  
  308.     confirm "Draw!  Play again?"; play$
  309.     if play$ = "yes" then [start]
  310.     goto [quit]
  311.  
  312.  
  313. [win]
  314.  
  315.     if letter$ = "OOO" then notice "I WIN!  HA HA HA!"
  316.     if letter$ = "XXX" then notice "You win "; yourName$; "!!!, Bummer!"
  317.     confirm "Play again?"; play$
  318.     if letter$ = "OOO" and play$ = "no" then notice "Sore loser!"
  319.     if play$ = "yes" then [start]
  320.  
  321.  
  322. [quit]
  323.  
  324.     close #brd
  325.     cls
  326.     print "-GAME OVER-, press ALT-F4 to exit"
  327.     end
  328.  
  329.