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

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