home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / lb091.zip / TTT.BAS < prev    next >
BASIC Source File  |  1993-11-18  |  11KB  |  412 lines

  1.  
  2.     'Tic Tac Toe
  3.     'Requires Liberty BASIC v1.0 or better
  4.     'Copyright 1993, Shoptalk Systems
  5.     'All Rights Reserved
  6.  
  7.     ' set up the board
  8.     dim board$(9)
  9.     dim razz$(9)
  10.  
  11.     nomainwin
  12.  
  13.     loadbmp "title", "titlettt.bmp"
  14.     loadbmp "square", "squarttt.bmp"
  15.  
  16.     WindowWidth = 314
  17.     WindowHeight = 400
  18.     button #brd, " T ", [1], UL, 10, 60
  19.     button #brd, " I ", [2], UL, 110, 60
  20.     button #brd, " C ", [3], UL, 210, 60
  21.     button #brd, " T ", [4], UL, 10, 160
  22.     button #brd, " A ", [5], UL, 110, 160
  23.     button #brd, " C ", [6], UL, 210, 160
  24.     button #brd, " T ", [7], UL, 10, 260
  25.     button #brd, " O ", [8], UL, 110, 260
  26.     button #brd, " E ", [9], UL, 210, 260
  27.     button #brd, "Pass Move", [skipMove], UL, 210, 10
  28.     textbox #brd.comment, 10, 27, 170, 25
  29.     menu #brd, "&Help", "&Instructions", [instruct], "&About Tic Tac Toe", [about]
  30.     open "Tic Tac Toe Playing Board" for graphics_nsb as #brd
  31.     print #brd, "trapclose [quit]";
  32.     print #brd, "size 3 ; down";
  33.     gosub [drawBoard]
  34.     yourName$ = "Human"
  35.     prompt "What is your name?"; yourName$
  36.  
  37.     razz$(0) = "Boooh! Hisss!"
  38.     razz$(1) = "Oh! I'm Scared!"
  39.     razz$(2) = "No Dur!"
  40.     razz$(3) = "Why that move?"
  41.     razz$(4) = "Lost Cause."
  42.     razz$(5) = "No Hope."
  43.     razz$(6) = "Change Careers."
  44.     razz$(7) = "Major Bummer!"
  45.     razz$(8) = "Boring."
  46.     razz$(9) = "I'm insulted."
  47.  
  48.  
  49. [start]    'start game (or restart)
  50.  
  51.     for x = 1 to 9 : board$(x) = " " : next x
  52.     gosub [modelBoard]
  53.     turn = 0
  54.     true = 1
  55.     false = 0
  56.     pass = false
  57.     comment$ = "Your Turn, " + yourName$
  58.     lastComment$ = ""
  59.     moveCounter = 0
  60.     gosub [comment]
  61.  
  62. [playLoop]
  63.  
  64.  
  65.     input position$
  66.  
  67.   goto [playLoop]
  68.  
  69.  
  70. [move]
  71.  
  72.     ' X moves
  73.     if board$(val(position$)) <> " " then notice "Illegal Move!" : goto [playLoop]
  74.     moveCounter = moveCounter + 1
  75.     pass = false
  76.     board$(val(position$)) = "X"
  77.     gosub [drawX]
  78.     gosub [modelBoard]
  79.     gosub [checkForWinOrDraw]
  80.     goto [oMoves]
  81.  
  82. [skipMove]
  83.  
  84.     pass = true
  85.  
  86. [oMoves]
  87.  
  88.     ' O moves
  89.     gosub [computeMove]
  90.     board$(val(position$)) = "O"
  91.     gosub [drawO]
  92.     gosub [modelBoard]
  93.     gosub [checkForWinOrDraw]
  94.  
  95.   goto [playLoop]
  96.  
  97.  
  98. [1]
  99.     position$ = "1" : goto [move]
  100. [2]
  101.     position$ = "2" : goto [move]
  102. [3]
  103.     position$ = "3" : goto [move]
  104. [4]
  105.     position$ = "4" : goto [move]
  106. [5]
  107.     position$ = "5" : goto [move]
  108. [6]
  109.     position$ = "6" : goto [move]
  110. [7]
  111.     position$ = "7" : goto [move]
  112. [8]
  113.     position$ = "8" : goto [move]
  114. [9]
  115.     position$ = "9" : goto [move]
  116.  
  117. [end]
  118.  
  119.     end
  120.  
  121. [drawBoard]
  122.  
  123.     print #brd, "fill lightgray";
  124.     print #brd, "drawbmp title 7 2";
  125.     for x = 4 to 204 step 100
  126.     for y = 54 to 254 step 100
  127.         print #brd, "drawbmp square "; x; " "; y ;
  128.     next y
  129.     next x
  130.     print #brd, "flush";
  131.  
  132.  
  133. [comment]
  134.  
  135.     if comment$ = "" or comment$ = lastComment$ then return
  136.     print #brd.comment, comment$
  137.  
  138.   return
  139.  
  140.  
  141.  
  142. [drawX]
  143.  
  144.     gosub [xlatePosition]
  145.     print #brd, "size 6";
  146.     print #brd, "color darkred";
  147.     print #brd, "line "; squareX-11; " "; squareY-16; " "; squareX+19; " "; squareY+24
  148.     print #brd, "line "; squareX+19; " "; squareY-16; " "; squareX-11; " "; squareY+24
  149.     print #brd, "color red";
  150.     print #brd, "line "; squareX-15; " "; squareY-20; " "; squareX+15; " "; squareY+20
  151.     print #brd, "line "; squareX+15; " "; squareY-20; " "; squareX-15; " "; squareY+20
  152.     print #brd, "flush";
  153.  
  154.   return
  155.  
  156.  
  157. [drawO]
  158.  
  159.     gosub [xlatePosition]
  160.     print #brd, "size 7";
  161.     print #brd, "color darkblue";
  162.     print #brd, "place "; squareX+5; " "; squareY+5
  163.     print #brd, "circle 20";
  164.     print #brd, "color blue";
  165.     print #brd, "place "; squareX; " "; squareY
  166.     print #brd, "circle 20";
  167.     print #brd, "flush";
  168.  
  169.   return
  170.  
  171.  
  172. [xlatePosition]
  173.  
  174.     position = val(position$)
  175.  
  176.     squareX = 55
  177.     if instr("258", position$) > 0 then squareX = 155
  178.     if instr("369", position$) > 0 then squareX = 255
  179.  
  180.     squareY = int((val(position$)+2)/3)*100+5
  181.  
  182.   return
  183.  
  184.  
  185. [modelBoard]
  186.  
  187.     row1$ = board$(1)+board$(2)+board$(3)
  188.     row2$ = board$(4)+board$(5)+board$(6)
  189.     row3$ = board$(7)+board$(8)+board$(9)
  190.  
  191.     col1$ = board$(1)+board$(4)+board$(7)
  192.     col2$ = board$(2)+board$(5)+board$(8)
  193.     col3$ = board$(3)+board$(6)+board$(9)
  194.  
  195.     diag1$ = board$(1)+board$(5)+board$(9)
  196.     diag2$ = board$(7)+board$(5)+board$(3)
  197.  
  198.   return
  199.  
  200.  
  201.  
  202. [computeMove]
  203.  
  204.  
  205.     ' create some sort of intimidating comment
  206.  
  207.     newComment$ = razz$(int(rnd(1)*10))
  208.     if comment$ = newComment$ then [computeMove] ' try again
  209.     comment$ = newComment$
  210.     if pass = true then comment$ = "You pass."
  211.     gosub [comment]
  212.  
  213.  
  214.     'check for instant win!
  215.  
  216. [instantWin]
  217.  
  218.     if moveCounter < 3 then [defend]
  219.  
  220.     if row1$ = "OO " then position$ = "3" : return
  221.     if row1$ = " OO" then position$ = "1" : return
  222.     if row1$ = "O O" then position$ = "2" : return
  223.     if row2$ = "OO " then position$ = "6" : return
  224.     if row2$ = " OO" then position$ = "4" : return
  225.     if row2$ = "O O" then position$ = "5" : return
  226.     if row3$ = "OO " then position$ = "9" : return
  227.     if row3$ = " OO" then position$ = "7" : return
  228.     if row3$ = "O O" then position$ = "8" : return
  229.  
  230.     if col1$ = "OO " then position$ = "7" : return
  231.     if col1$ = " OO" then position$ = "1" : return
  232.     if col1$ = "O O" then position$ = "4" : return
  233.     if col2$ = "OO " then position$ = "8" : return
  234.     if col2$ = " OO" then position$ = "2" : return
  235.     if col2$ = "O O" then position$ = "5" : return
  236.     if col3$ = "OO " then position$ = "9" : return
  237.     if col3$ = " OO" then position$ = "3" : return
  238.     if col3$ = "O O" then position$ = "6" : return
  239.  
  240.     if diag1$ = "OO " then position$ = "9" : return
  241.     if diag1$ = " OO" then position$ = "1" : return
  242.     if diag1$ = "O O" then position$ = "5" : return
  243.     if diag2$ = "OO " then position$ = "3" : return
  244.     if diag2$ = " OO" then position$ = "7" : return
  245.     if diag2$ = "O O" then position$ = "5" : return
  246.  
  247. [defend]
  248.  
  249.     'make the purely defensive moves
  250.  
  251.     if moveCounter = 1 then [attack]
  252.  
  253.     if row1$ = "XX " then position$ = "3" : return
  254.     if row1$ = " XX" then position$ = "1" : return
  255.     if row1$ = "X X" then position$ = "2" : return
  256.     if row2$ = "XX " then position$ = "6" : return
  257.     if row2$ = " XX" then position$ = "4" : return
  258.     if row2$ = "X X" then position$ = "5" : return
  259.     if row3$ = "XX " then position$ = "9" : return
  260.     if row3$ = " XX" then position$ = "7" : return
  261.     if row3$ = "X X" then position$ = "8" : return
  262.  
  263.     if col1$ = "XX " then position$ = "7" : return
  264.     if col1$ = " XX" then position$ = "1" : return
  265.     if col1$ = "X X" then position$ = "4" : return
  266.     if col2$ = "XX " then position$ = "8" : return
  267.     if col2$ = " XX" then position$ = "2" : return
  268.     if col2$ = "X X" then position$ = "5" : return
  269.     if col3$ = "XX " then position$ = "9" : return
  270.     if col3$ = " XX" then position$ = "3" : return
  271.     if col3$ = "X X" then position$ = "6" : return
  272.  
  273.     if diag1$ = "XX " then position$ = "9" : return
  274.     if diag1$ = " XX" then position$ = "1" : return
  275.     if diag1$ = "X X" then position$ = "5" : return
  276.     if diag2$ = "XX " then position$ = "3" : return
  277.     if diag2$ = " XX" then position$ = "7" : return
  278.     if diag2$ = "X X" then position$ = "5" : return
  279.  
  280.  
  281. [attack]
  282.  
  283.     ' now make the offensive moves
  284.  
  285.     moves$ = ""
  286.  
  287.     if instr(diag1$,"X") > 0 then [d2]
  288.     if board$(1) = " " then moves$ = moves$ + "1"
  289.     if board$(5) = " " then moves$ = moves$ + "5"
  290.     if board$(9) = " " then moves$ = moves$ + "9"
  291.     i = int(rnd(1)*len(moves$))+1
  292.     position$ = mid$(moves$, i, 1)
  293.     return
  294.  
  295.  
  296.  
  297. [d2]
  298.  
  299.     if instr(diag2$,"X") > 0 then [moreMoves]
  300.     if board$(7) = " " then moves$ = moves$ + "7"
  301.     if board$(5) = " " then moves$ = moves$ + "5"
  302.     if board$(3) = " " then moves$ = moves$ + "3"
  303.     i = int(rnd(1)*len(moves$))+1
  304.     position$ = mid$(moves$, i, 1)
  305.     return
  306.  
  307.  
  308. [moreMoves]
  309.  
  310.     ' now make random moves
  311.  
  312.     all$ = row1$ + row2$ + row3$
  313.     blanks$ = ""
  314.     for i = 1 to 9
  315.         if mid$(all$, i, 1) = " " then blanks$ = blanks$ + chr$(48+i)
  316.     next i
  317.     if blanks$ = "" then position$ = "" : return
  318.     i = int(rnd(1)*len(blank$))+1
  319.     position$ = mid$(blanks$, i, 1)
  320.     return
  321.  
  322. [error]
  323.  
  324.     notice "Error, no move computed"
  325.     stop
  326.  
  327.  
  328.     'check to see if the game has been won or drawn
  329.  
  330. [checkForWinOrDraw]
  331.  
  332.     ' check for win
  333.     i$ = ","
  334.     letter$ = "XXX"
  335.     for i = 1 to 2
  336.         if instr(row1$+i$+row2$+i$+row3$, letter$) > 0 then [win]
  337.         if instr(col1$+i$+col2$+i$+col3$, letter$) > 0 then [win]
  338.         if instr(diag1$+i$+diag2$, letter$) > 0 then [win]
  339.         letter$ = "OOO"
  340.     next i
  341.  
  342.     'check to see if the board is full
  343.     if instr(row1$+row2$+row3$, " ") > 0 then return
  344.  
  345.     'the game is a draw
  346.  
  347.     confirm "Draw!  Play again?"; play$
  348.     if play$ = "yes" then print #brd, "cls "; : gosub [drawBoard] : goto [start]
  349.     goto [quit]
  350.  
  351.  
  352. [win]
  353.  
  354.     if letter$ = "OOO" then notice "I WIN!  HA HA HA!"
  355.     if letter$ = "XXX" then notice "You win "; yourName$; "!!!, Bummer!"
  356.     confirm "Play again?"; play$
  357.     if letter$ = "OOO" and play$ = "no" then notice "Sore loser!"
  358.     if play$ = "yes" then print #brd, "cls "; : gosub [drawBoard] : goto [start]
  359.  
  360.  
  361. [quit]
  362.  
  363.     if instructIsOpen = 1 then close #instruct
  364.     close #brd
  365.  
  366.     end
  367.  
  368.  
  369. [instruct]
  370.  
  371.     WindowHeight = 250
  372.     UpperLeftX = 300
  373.     UpperLeftY = 50
  374.  
  375.     button #instruct, "OK", [closeInstruct], LR, 5, 5
  376.     open "Tic Tac Toe Instructions" for graphics_nsb as #instruct
  377.  
  378.     print #instruct, "trapclose [closeInstruct]";
  379.  
  380.     text$ = "\ "
  381.     text$ = text$ + "\Tic Tac Toe"
  382.     text$ = text$ + "\Copyright 1993, Shoptalk Systems"
  383.     text$ = text$ + "\ "
  384.     text$ = text$ + "\Instructions:"
  385.     text$ = text$ + "\ "
  386.     text$ = text$ + "\  In this game, you play against the"
  387.     text$ = text$ + "\  computer.  You are X, and the computer"
  388.     text$ = text$ + "\  is O.  The one to get 3 in a row wins."
  389.     text$ = text$ + "\  If no one gets three in a row, then"
  390.     text$ = text$ + "\  it is a draw."
  391.  
  392.     print #instruct, text$
  393.     print #instruct, "flush";
  394.  
  395.     instructIsOpen = 1
  396.  
  397.     goto [playLoop]
  398.  
  399. [closeInstruct]
  400.  
  401.     close #instruct
  402.     instructIsOpen = 0
  403.  
  404.     goto [playLoop]
  405.  
  406.  
  407. [about]
  408.  
  409.     notice "About Tic Tac Toe"+chr$(13)+"Tic Tac Toe, A Liberty BASIC program"
  410.  
  411.     goto [playLoop]
  412.