Author Topic: Tic-Tac-Toe  (Read 6057 times)

0 Members and 1 Guest are viewing this topic.

This topic contains a post which is marked as Best Answer. Press here if you would like to see it.

Offline SierraKen

  • Forum Resident
  • Posts: 1454
    • View Profile
Tic-Tac-Toe
« on: August 13, 2019, 10:10:38 pm »
I know this game has been remade countless times on QBasic and other languages, but personally, I've wanted to make this game ever since I was a kid in the 80's. So, I worked on it for most of the day today and was able to finish it. :) It even uses graphics and not just all text. The computer knows how to block, and win, and also choose a random space when it can't do either. Plus I'm excited to say that it uses the mouse to choose your space. :)) Check it out!

(Note: a better looking and sounding version is after this post on the same page.)

Code: QB64: [Select]
  1. 'I've wanted to make this game for decades and finally am able to!
  2. 'This game was made on August 13, 2019 by Ken G.
  3. 'This is Freeware.
  4.  
  5. DIM a(10), b(10)
  6. _TITLE "Tic-Tac-Toe     by Ken G."
  7. SCREEN _NEWIMAGE(600, 480, 32)
  8. PRINT "                           TIC - TAC - TOE"
  9. PRINT "                             by   Ken G."
  10. PRINT "     Play against the computer in this classic game of Tic-Tac-Toe."
  11. PRINT "                    Whoever gets 3 in a row wins."
  12. PRINT "                  Choose a sqace by using your mouse."
  13. PRINT "                             You go first."
  14. PRINT "                        Press Space Bar to begin."
  15. st:
  16. sta$ = INKEY$
  17. IF sta$ = "" THEN GOTO st:
  18. IF sta$ = " " THEN GOTO start:
  19. GOTO st:
  20. start:
  21. t = 0
  22. turn = 0
  23. 'Draw Grid
  24. 'Vertical Lines
  25. LINE (220, 100)-(240, 410), _RGB32(255, 255, 255), BF
  26. LINE (360, 100)-(380, 410), _RGB32(255, 255, 255), BF
  27. 'Horizontal Lines
  28. LINE (90, 185)-(510, 205), _RGB32(255, 255, 255), BF
  29. LINE (90, 305)-(510, 325), _RGB32(255, 255, 255), BF
  30.  
  31. Go:
  32. mouseWheel = 0
  33.     mouseX = _MOUSEX
  34.     mouseY = _MOUSEY
  35.     mouseLeftButton = _MOUSEBUTTON(1)
  36.     mouseRightButton = _MOUSEBUTTON(2)
  37.     mouseMiddleButton = _MOUSEBUTTON(3)
  38.     mouseWheel = mouseWheel + _MOUSEWHEEL
  39.  
  40. IF mouseLeftButton = -1 THEN
  41.     IF mouseX > 88 AND mouseX < 218 AND mouseY > 93 AND mouseY < 182 AND b(1) = 0 AND a(1) = 0 AND t = 0 THEN GOSUB space1:
  42.     IF mouseX > 241 AND mouseX < 357 AND mouseY > 93 AND mouseY < 182 AND b(2) = 0 AND a(2) = 0 AND t = 0 THEN GOSUB space2:
  43.     IF mouseX > 381 AND mouseX < 509 AND mouseY > 93 AND mouseY < 182 AND b(3) = 0 AND a(3) = 0 AND t = 0 THEN GOSUB space3:
  44.     IF mouseX > 88 AND mouseX < 218 AND mouseY > 205 AND mouseY < 302 AND b(4) = 0 AND a(4) = 0 AND t = 0 THEN GOSUB space4:
  45.     IF mouseX > 241 AND mouseX < 357 AND mouseY > 205 AND mouseY < 302 AND b(5) = 0 AND a(5) = 0 AND t = 0 THEN GOSUB space5:
  46.     IF mouseX > 381 AND mouseX < 509 AND mouseY > 205 AND mouseY < 302 AND b(6) = 0 AND a(6) = 0 AND t = 0 THEN GOSUB space6:
  47.     IF mouseX > 88 AND mouseX < 218 AND mouseY > 326 AND mouseY < 410 AND b(7) = 0 AND a(7) = 0 AND t = 0 THEN GOSUB space7:
  48.     IF mouseX > 241 AND mouseX < 357 AND mouseY > 326 AND mouseY < 410 AND b(8) = 0 AND a(8) = 0 AND t = 0 THEN GOSUB space8:
  49.     IF mouseX > 381 AND mouseX < 509 AND mouseY > 326 AND mouseY < 410 AND b(9) = 0 AND a(9) = 0 AND t = 0 THEN GOSUB space9:
  50.  
  51. IF mouseLeftButton = -1 AND ending = 1 THEN GOTO start:
  52. IF mouseRightButton = -1 AND ending = 1 THEN END
  53.  
  54. IF t = 1 THEN GOSUB computer:
  55.  
  56. GOTO Go:
  57.  
  58. checkwin:
  59. 'Check to see if you won.
  60. IF a(1) = 1 AND a(2) = 1 AND a(3) = 1 THEN GOTO won:
  61. IF a(4) = 1 AND a(5) = 1 AND a(6) = 1 THEN GOTO won:
  62. IF a(7) = 1 AND a(8) = 1 AND a(9) = 1 THEN GOTO won
  63. IF a(1) = 1 AND a(4) = 1 AND a(7) = 1 THEN GOTO won:
  64. IF a(2) = 1 AND a(5) = 1 AND a(8) = 1 THEN GOTO won:
  65. IF a(3) = 1 AND a(6) = 1 AND a(9) = 1 THEN GOTO won:
  66. IF a(1) = 1 AND a(5) = 1 AND a(9) = 1 THEN GOTO won:
  67. IF a(3) = 1 AND a(5) = 1 AND a(7) = 1 THEN GOTO won:
  68. turn = turn + 1
  69. IF turn = 9 THEN GOTO catsgame:
  70. GOTO Go:
  71. won:
  72. FOR snd = 300 TO 900 STEP 50
  73.     SOUND snd, .5
  74. NEXT snd
  75. FOR tt = 1 TO 9
  76.     a(tt) = 0
  77.     b(tt) = 0
  78. NEXT tt
  79. t = 0
  80. LOCATE 2, 27: PRINT "Y O U   W O N ! ! !"
  81. LOCATE 4, 27: PRINT "Press the Space Bar to play again."
  82. LOCATE 6, 27: PRINT "Or any other key to end."
  83. again:
  84. ag$ = INKEY$
  85. IF ag$ = "" THEN GOTO again:
  86. IF ag$ <> " " THEN END
  87. IF ag$ = " " THEN GOTO start:
  88. GOTO again:
  89.  
  90. computer:
  91.  
  92. 'Check to win.
  93. 'Last space gone.
  94. IF b(1) = 1 AND b(2) = 1 AND a(3) = 0 AND b(3) = 0 THEN GOTO compspace3:
  95. IF b(4) = 1 AND b(5) = 1 AND a(6) = 0 AND b(6) = 0 THEN GOTO compspace6:
  96. IF b(7) = 1 AND b(8) = 1 AND a(9) = 0 AND b(9) = 0 THEN GOTO compspace9:
  97. IF b(1) = 1 AND b(4) = 1 AND a(7) = 0 AND b(7) = 0 THEN GOTO compspace7:
  98. IF b(2) = 1 AND b(5) = 1 AND a(8) = 0 AND b(8) = 0 THEN GOTO compspace8:
  99. IF b(3) = 1 AND b(6) = 1 AND a(9) = 0 AND b(9) = 0 THEN GOTO compspace9:
  100. IF b(1) = 1 AND b(5) = 1 AND a(9) = 0 AND b(9) = 0 THEN GOTO compspace9:
  101. IF b(3) = 1 AND b(5) = 1 AND a(7) = 0 AND b(7) = 0 THEN GOTO compspace7:
  102. 'First space gone.
  103. IF b(2) = 1 AND b(3) = 1 AND a(1) = 0 AND b(1) = 0 THEN GOTO compspace1:
  104. IF b(5) = 1 AND b(6) = 1 AND a(4) = 0 AND b(4) = 0 THEN GOTO compspace4:
  105. IF b(8) = 1 AND b(9) = 1 AND a(7) = 0 AND b(7) = 0 THEN GOTO compspace7:
  106. IF b(4) = 1 AND b(7) = 1 AND a(1) = 0 AND b(1) = 0 THEN GOTO compspace1:
  107. IF b(5) = 1 AND b(8) = 1 AND a(2) = 0 AND b(2) = 0 THEN GOTO compspace2:
  108. IF b(6) = 1 AND b(9) = 1 AND a(3) = 0 AND b(3) = 0 THEN GOTO compspace3:
  109. IF b(5) = 1 AND b(9) = 1 AND a(1) = 0 AND b(1) = 0 THEN GOTO compspace1:
  110. IF b(7) = 1 AND b(5) = 1 AND a(3) = 0 AND b(3) = 0 THEN GOTO compspace3:
  111. 'Middle space gone.
  112. IF b(1) = 1 AND b(3) = 1 AND a(2) = 0 AND b(2) = 0 THEN GOTO compspace2:
  113. IF b(4) = 1 AND b(6) = 1 AND a(5) = 0 AND b(5) = 0 THEN GOTO compspace5:
  114. IF b(7) = 1 AND b(9) = 1 AND a(8) = 0 AND b(8) = 0 THEN GOTO compspace8:
  115. IF b(1) = 1 AND b(7) = 1 AND a(4) = 0 AND b(4) = 0 THEN GOTO compspace4:
  116. IF b(2) = 1 AND b(8) = 1 AND a(5) = 0 AND b(5) = 0 THEN GOTO compspace5:
  117. IF b(3) = 1 AND b(9) = 1 AND a(6) = 0 AND b(6) = 0 THEN GOTO compspace6:
  118. IF b(1) = 1 AND b(9) = 1 AND a(5) = 0 AND b(5) = 0 THEN GOTO compspace5:
  119. IF b(3) = 1 AND b(7) = 1 AND a(5) = 0 AND b(5) = 0 THEN GOTO compspace5:
  120.  
  121. 'Check to block.
  122. 'Last space gone.
  123. IF a(1) = 1 AND a(2) = 1 AND a(3) = 0 AND b(3) = 0 THEN GOTO compspace3:
  124. IF a(4) = 1 AND a(5) = 1 AND a(6) = 0 AND b(6) = 0 THEN GOTO compspace6:
  125. IF a(7) = 1 AND a(8) = 1 AND a(9) = 0 AND b(9) = 0 THEN GOTO compspace9:
  126. IF a(1) = 1 AND a(4) = 1 AND a(7) = 0 AND b(7) = 0 THEN GOTO compspace7:
  127. IF a(2) = 1 AND a(5) = 1 AND a(8) = 0 AND b(8) = 0 THEN GOTO compspace8:
  128. IF a(3) = 1 AND a(6) = 1 AND a(9) = 0 AND b(9) = 0 THEN GOTO compspace9:
  129. IF a(1) = 1 AND a(5) = 1 AND a(9) = 0 AND b(9) = 0 THEN GOTO compspace9:
  130. IF a(3) = 1 AND a(5) = 1 AND a(7) = 0 AND b(7) = 0 THEN GOTO compspace7:
  131. 'First space gone.
  132. IF a(2) = 1 AND a(3) = 1 AND a(1) = 0 AND b(1) = 0 THEN GOTO compspace1:
  133. IF a(5) = 1 AND a(6) = 1 AND a(4) = 0 AND b(4) = 0 THEN GOTO compspace4:
  134. IF a(8) = 1 AND a(9) = 1 AND a(7) = 0 AND b(7) = 0 THEN GOTO compspace7:
  135. IF a(4) = 1 AND a(7) = 1 AND a(1) = 0 AND b(1) = 0 THEN GOTO compspace1:
  136. IF a(5) = 1 AND a(8) = 1 AND a(2) = 0 AND b(2) = 0 THEN GOTO compspace2:
  137. IF a(6) = 1 AND a(9) = 1 AND a(3) = 0 AND b(3) = 0 THEN GOTO compspace3:
  138. IF a(5) = 1 AND a(9) = 1 AND a(1) = 0 AND b(1) = 0 THEN GOTO compspace1:
  139. IF a(7) = 1 AND a(5) = 1 AND a(3) = 0 AND b(3) = 0 THEN GOTO compspace3:
  140. 'Middle space gone.
  141. IF a(1) = 1 AND a(3) = 1 AND a(2) = 0 AND b(2) = 0 THEN GOTO compspace2:
  142. IF a(4) = 1 AND a(6) = 1 AND a(5) = 0 AND b(5) = 0 THEN GOTO compspace5:
  143. IF a(7) = 1 AND a(9) = 1 AND a(8) = 0 AND b(8) = 0 THEN GOTO compspace8:
  144. IF a(1) = 1 AND a(7) = 1 AND a(4) = 0 AND b(4) = 0 THEN GOTO compspace4:
  145. IF a(2) = 1 AND a(8) = 1 AND a(5) = 0 AND b(5) = 0 THEN GOTO compspace5:
  146. IF a(3) = 1 AND a(9) = 1 AND a(6) = 0 AND b(6) = 0 THEN GOTO compspace6:
  147. IF a(1) = 1 AND a(9) = 1 AND a(5) = 0 AND b(5) = 0 THEN GOTO compspace5:
  148. IF a(3) = 1 AND a(7) = 1 AND a(5) = 0 AND b(5) = 0 THEN GOTO compspace5:
  149.  
  150. 'Computer decides a random space.
  151. computerchoice:
  152. comp = INT(RND * 9) + 1
  153. IF b(comp) = 1 THEN GOTO computerchoice:
  154. IF a(comp) = 1 THEN GOTO computerchoice:
  155. IF comp = 1 THEN GOTO compspace1:
  156. IF comp = 2 THEN GOTO compspace2:
  157. IF comp = 3 THEN GOTO compspace3:
  158. IF comp = 4 THEN GOTO compspace4:
  159. IF comp = 5 THEN GOTO compspace5:
  160. IF comp = 6 THEN GOTO compspace6:
  161. IF comp = 7 THEN GOTO compspace7:
  162. IF comp = 8 THEN GOTO compspace8:
  163. IF comp = 9 THEN GOTO compspace9:
  164.  
  165. 'Cat's Game
  166. catsgame:
  167. FOR snd = 400 TO 300 STEP -25
  168.     SOUND snd, .5
  169. NEXT snd
  170. FOR tt = 1 TO 9
  171.     a(tt) = 0
  172.     b(tt) = 0
  173. NEXT tt
  174. t = 0
  175. LOCATE 2, 27: PRINT "Cat's Game - No Winners"
  176. LOCATE 4, 27: PRINT "Press the Space Bar to play again."
  177. LOCATE 6, 27: PRINT "Or any other key to end."
  178. again2:
  179. ag$ = INKEY$
  180. IF ag$ = "" THEN GOTO again:
  181. IF ag$ <> " " THEN END
  182. IF ag$ = " " THEN GOTO start:
  183. GOTO again2:
  184.  
  185. 'Check to see if the computer won.
  186. check:
  187. IF b(1) = 1 AND b(2) = 1 AND b(3) = 1 THEN GOTO compwon:
  188. IF b(4) = 1 AND b(5) = 1 AND b(6) = 1 THEN GOTO compwon:
  189. IF b(7) = 1 AND b(8) = 1 AND b(9) = 1 THEN GOTO compwon
  190. IF b(1) = 1 AND b(4) = 1 AND b(7) = 1 THEN GOTO compwon:
  191. IF b(2) = 1 AND b(5) = 1 AND b(8) = 1 THEN GOTO compwon:
  192. IF b(3) = 1 AND b(6) = 1 AND b(9) = 1 THEN GOTO compwon:
  193. IF b(1) = 1 AND b(5) = 1 AND b(9) = 1 THEN GOTO compwon:
  194. IF b(3) = 1 AND b(5) = 1 AND b(7) = 1 THEN GOTO compwon:
  195. turn = turn + 1
  196. IF turn = 9 THEN GOTO catsgame:
  197. t = 0
  198. GOTO Go:
  199.  
  200. compwon:
  201. FOR snd = 900 TO 300 STEP -50
  202.     SOUND snd, .5
  203. NEXT snd
  204. FOR tt = 1 TO 9
  205.     a(tt) = 0
  206.     b(tt) = 0
  207. NEXT tt
  208. t = 0
  209. LOCATE 2, 27: PRINT "Computer Wins"
  210. LOCATE 4, 27: PRINT "Press the Space Bar to play again."
  211. LOCATE 6, 27: PRINT "Or any other key to end."
  212. again3:
  213. ag$ = INKEY$
  214. IF ag$ = "" THEN GOTO again:
  215. IF ag$ <> " " THEN END
  216. IF ag$ = " " THEN GOTO start:
  217. GOTO again3:
  218.  
  219. 'This part draws the computer's circle.
  220. compspace1:
  221. t = 0
  222. b(1) = 1
  223. CIRCLE (160, 142), 40, _RGB32(255, 0, 0)
  224. GOTO check:
  225. compspace2:
  226. t = 0
  227. b(2) = 1
  228. CIRCLE (300, 142), 40, _RGB32(255, 0, 0)
  229. GOTO check:
  230. compspace3:
  231. t = 0
  232. b(3) = 1
  233. CIRCLE (440, 142), 40, _RGB32(255, 0, 0)
  234. GOTO check:
  235. compspace4:
  236. t = 0
  237. b(4) = 1
  238. CIRCLE (160, 257), 40, _RGB32(255, 0, 0)
  239. GOTO check:
  240. compspace5:
  241. t = 0
  242. b(5) = 1
  243. CIRCLE (300, 257), 40, _RGB32(255, 0, 0)
  244. GOTO check:
  245. compspace6:
  246. t = 0
  247. b(6) = 1
  248. CIRCLE (440, 257), 40, _RGB32(255, 0, 0)
  249. GOTO check:
  250. compspace7:
  251. t = 0
  252. b(7) = 1
  253. CIRCLE (160, 372), 40, _RGB32(255, 0, 0)
  254. GOTO check:
  255. compspace8:
  256. t = 0
  257. b(8) = 1
  258. CIRCLE (300, 372), 40, _RGB32(255, 0, 0)
  259. GOTO check:
  260. compspace9:
  261. t = 0
  262. b(9) = 1
  263. CIRCLE (440, 372), 40, _RGB32(255, 0, 0)
  264. GOTO check:
  265. 'This last part draws your X.
  266. space1:
  267. a(1) = 1
  268. LINE (120, 120)-(200, 165), _RGB32(0, 255, 0)
  269. LINE (200, 120)-(120, 165), _RGB32(0, 255, 0)
  270. t = 1
  271. GOTO checkwin:
  272. space2:
  273. a(2) = 1
  274. LINE (260, 120)-(340, 165), _RGB32(0, 255, 0)
  275. LINE (340, 120)-(260, 165), _RGB32(0, 255, 0)
  276. t = 1
  277. GOTO checkwin:
  278. space3:
  279. a(3) = 1
  280. LINE (400, 120)-(480, 165), _RGB32(0, 255, 0)
  281. LINE (480, 120)-(400, 165), _RGB32(0, 255, 0)
  282. t = 1
  283. GOTO checkwin:
  284. space4:
  285. a(4) = 1
  286. LINE (120, 235)-(200, 280), _RGB32(0, 255, 0)
  287. LINE (200, 235)-(120, 280), _RGB32(0, 255, 0)
  288. t = 1
  289. GOTO checkwin:
  290. space5:
  291. a(5) = 1
  292. LINE (260, 235)-(340, 280), _RGB32(0, 255, 0)
  293. LINE (340, 235)-(260, 280), _RGB32(0, 255, 0)
  294. t = 1
  295. GOTO checkwin:
  296. space6:
  297. a(6) = 1
  298. LINE (400, 235)-(480, 280), _RGB32(0, 255, 0)
  299. LINE (480, 235)-(400, 280), _RGB32(0, 255, 0)
  300. t = 1
  301. GOTO checkwin:
  302. space7:
  303. a(7) = 1
  304. LINE (120, 350)-(200, 395), _RGB32(0, 255, 0)
  305. LINE (200, 350)-(120, 395), _RGB32(0, 255, 0)
  306. t = 1
  307. GOTO checkwin:
  308. space8:
  309. a(8) = 1
  310. LINE (260, 350)-(340, 395), _RGB32(0, 255, 0)
  311. LINE (340, 350)-(260, 395), _RGB32(0, 255, 0)
  312. t = 1
  313. GOTO checkwin:
  314. space9:
  315. a(9) = 1
  316. LINE (400, 350)-(480, 395), _RGB32(0, 255, 0)
  317. LINE (480, 350)-(400, 395), _RGB32(0, 255, 0)
  318. t = 1
  319. GOTO checkwin:
  320.  
  321.  
« Last Edit: August 14, 2019, 01:22:09 pm by SierraKen »

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Tic-Tac-Toe
« Reply #1 on: August 13, 2019, 10:48:04 pm »
Works well with mouse, nice!

Offline SierraKen

  • Forum Resident
  • Posts: 1454
    • View Profile
Re: Tic-Tac-Toe
« Reply #2 on: August 13, 2019, 10:57:28 pm »
Thanks :). Was fun making it. I have code for every block possible and every win possible. Also learned how easy it is to use mouse detection areas.

Offline OldMoses

  • Seasoned Forum Regular
  • Posts: 469
    • View Profile
Re: Tic-Tac-Toe
« Reply #3 on: August 14, 2019, 12:01:04 am »
Best I can do is stalemate it. Good job.

Offline SierraKen

  • Forum Resident
  • Posts: 1454
    • View Profile
Re: Tic-Tac-Toe
« Reply #4 on: August 14, 2019, 12:12:10 am »
Thanks OldMoses! Yeah just like classic Tic-Tac-Toe, it takes a few games to win usually. :)

Offline SierraKen

  • Forum Resident
  • Posts: 1454
    • View Profile
Re: Tic-Tac-Toe
« Reply #5 on: August 14, 2019, 01:21:18 pm »
I decided to make the O's and X's thicker, which also made them brighter. I also added a tiny animation text in the beginning and adjusted some other text. Plus I added a small thump sound every time you choose a space. 

(Note: I found a little problem with these 2 that I've posted, so please scroll down to the next post, thank you.)

Code: QB64: [Select]
  1. 'I've wanted to make this game for decades and finally am able to!
  2. 'This game was made on August 14, 2019 by Ken G.
  3. 'This is Freeware.
  4.  
  5. DIM a(10), b(10)
  6. _TITLE "Tic-Tac-Toe     by Ken G."
  7. SCREEN _NEWIMAGE(600, 480, 32)
  8. LOCATE 10, 32: PRINT "-"
  9. LOCATE 10, 38: PRINT "-"
  10. FOR tic = 1 TO 10
  11.     LOCATE tic, 28: PRINT "TIC"
  12.     _DELAY .2
  13.     LOCATE tic, 28: PRINT "   "
  14. NEXT tic
  15. LOCATE 10, 28: PRINT "TIC"
  16.  
  17. FOR tac = 20 TO 10 STEP -1
  18.     LOCATE tac, 34: PRINT "TAC"
  19.     _DELAY .2
  20.     LOCATE tac, 34: PRINT "   "
  21. NEXT tac
  22. LOCATE 10, 34: PRINT "TAC"
  23.  
  24. FOR toe = 1 TO 10
  25.     LOCATE toe, 40: PRINT "TOE"
  26.     _DELAY .2
  27.     LOCATE toe, 40: PRINT "   "
  28. NEXT toe
  29. LOCATE 10, 40: PRINT "TOE"
  30.  
  31. PRINT "                             by   Ken G."
  32. PRINT "     Play against the computer in this classic game of Tic-Tac-Toe."
  33. PRINT "                    Whoever gets 3 in a row wins."
  34. PRINT "                  Choose a sqace by using your mouse."
  35. PRINT "                             You go first."
  36. PRINT "                        Press Space Bar To Begin."
  37. st:
  38. sta$ = INKEY$
  39. IF sta$ = "" THEN GOTO st:
  40. IF sta$ = " " THEN GOTO start:
  41. GOTO st:
  42. start:
  43. t = 0
  44. turn = 0
  45. 'Draw Grid
  46. 'Vertical Lines
  47. LINE (220, 100)-(240, 410), _RGB32(255, 255, 255), BF
  48. LINE (360, 100)-(380, 410), _RGB32(255, 255, 255), BF
  49. 'Horizontal Lines
  50. LINE (90, 185)-(510, 205), _RGB32(255, 255, 255), BF
  51. LINE (90, 305)-(510, 325), _RGB32(255, 255, 255), BF
  52.  
  53. Go:
  54. mouseWheel = 0
  55.     mouseX = _MOUSEX
  56.     mouseY = _MOUSEY
  57.     mouseLeftButton = _MOUSEBUTTON(1)
  58.     mouseRightButton = _MOUSEBUTTON(2)
  59.     mouseMiddleButton = _MOUSEBUTTON(3)
  60.     mouseWheel = mouseWheel + _MOUSEWHEEL
  61.  
  62. IF mouseLeftButton = -1 THEN
  63.     IF mouseX > 88 AND mouseX < 218 AND mouseY > 93 AND mouseY < 182 AND b(1) = 0 AND a(1) = 0 AND t = 0 THEN GOSUB space1:
  64.     IF mouseX > 241 AND mouseX < 357 AND mouseY > 93 AND mouseY < 182 AND b(2) = 0 AND a(2) = 0 AND t = 0 THEN GOSUB space2:
  65.     IF mouseX > 381 AND mouseX < 509 AND mouseY > 93 AND mouseY < 182 AND b(3) = 0 AND a(3) = 0 AND t = 0 THEN GOSUB space3:
  66.     IF mouseX > 88 AND mouseX < 218 AND mouseY > 205 AND mouseY < 302 AND b(4) = 0 AND a(4) = 0 AND t = 0 THEN GOSUB space4:
  67.     IF mouseX > 241 AND mouseX < 357 AND mouseY > 205 AND mouseY < 302 AND b(5) = 0 AND a(5) = 0 AND t = 0 THEN GOSUB space5:
  68.     IF mouseX > 381 AND mouseX < 509 AND mouseY > 205 AND mouseY < 302 AND b(6) = 0 AND a(6) = 0 AND t = 0 THEN GOSUB space6:
  69.     IF mouseX > 88 AND mouseX < 218 AND mouseY > 326 AND mouseY < 410 AND b(7) = 0 AND a(7) = 0 AND t = 0 THEN GOSUB space7:
  70.     IF mouseX > 241 AND mouseX < 357 AND mouseY > 326 AND mouseY < 410 AND b(8) = 0 AND a(8) = 0 AND t = 0 THEN GOSUB space8:
  71.     IF mouseX > 381 AND mouseX < 509 AND mouseY > 326 AND mouseY < 410 AND b(9) = 0 AND a(9) = 0 AND t = 0 THEN GOSUB space9:
  72.  
  73. IF mouseLeftButton = -1 AND ending = 1 THEN GOTO start:
  74. IF mouseRightButton = -1 AND ending = 1 THEN END
  75.  
  76. IF t = 1 THEN GOSUB computer:
  77.  
  78. GOTO Go:
  79.  
  80. checkwin:
  81. 'Check to see if you won.
  82. IF a(1) = 1 AND a(2) = 1 AND a(3) = 1 THEN GOTO won:
  83. IF a(4) = 1 AND a(5) = 1 AND a(6) = 1 THEN GOTO won:
  84. IF a(7) = 1 AND a(8) = 1 AND a(9) = 1 THEN GOTO won
  85. IF a(1) = 1 AND a(4) = 1 AND a(7) = 1 THEN GOTO won:
  86. IF a(2) = 1 AND a(5) = 1 AND a(8) = 1 THEN GOTO won:
  87. IF a(3) = 1 AND a(6) = 1 AND a(9) = 1 THEN GOTO won:
  88. IF a(1) = 1 AND a(5) = 1 AND a(9) = 1 THEN GOTO won:
  89. IF a(3) = 1 AND a(5) = 1 AND a(7) = 1 THEN GOTO won:
  90. turn = turn + 1
  91. SOUND 100, .25
  92. IF turn = 9 THEN GOTO catsgame:
  93. GOTO Go:
  94. won:
  95. FOR snd = 300 TO 900 STEP 50
  96.     SOUND snd, .5
  97. NEXT snd
  98. FOR tt = 1 TO 9
  99.     a(tt) = 0
  100.     b(tt) = 0
  101. NEXT tt
  102. t = 0
  103. LOCATE 2, 32: PRINT "Y O U   W O N ! !"
  104. LOCATE 4, 27: PRINT "Press Space Bar to play again."
  105. LOCATE 6, 27: PRINT "Or any other key to end."
  106. again:
  107. ag$ = INKEY$
  108. IF ag$ = "" THEN GOTO again:
  109. IF ag$ <> " " THEN END
  110. IF ag$ = " " THEN GOTO start:
  111. GOTO again:
  112.  
  113. computer:
  114.  
  115. 'Check to win.
  116. 'Last space gone.
  117. IF b(1) = 1 AND b(2) = 1 AND a(3) = 0 AND b(3) = 0 THEN GOTO compspace3:
  118. IF b(4) = 1 AND b(5) = 1 AND a(6) = 0 AND b(6) = 0 THEN GOTO compspace6:
  119. IF b(7) = 1 AND b(8) = 1 AND a(9) = 0 AND b(9) = 0 THEN GOTO compspace9:
  120. IF b(1) = 1 AND b(4) = 1 AND a(7) = 0 AND b(7) = 0 THEN GOTO compspace7:
  121. IF b(2) = 1 AND b(5) = 1 AND a(8) = 0 AND b(8) = 0 THEN GOTO compspace8:
  122. IF b(3) = 1 AND b(6) = 1 AND a(9) = 0 AND b(9) = 0 THEN GOTO compspace9:
  123. IF b(1) = 1 AND b(5) = 1 AND a(9) = 0 AND b(9) = 0 THEN GOTO compspace9:
  124. IF b(3) = 1 AND b(5) = 1 AND a(7) = 0 AND b(7) = 0 THEN GOTO compspace7:
  125. 'First space gone.
  126. IF b(2) = 1 AND b(3) = 1 AND a(1) = 0 AND b(1) = 0 THEN GOTO compspace1:
  127. IF b(5) = 1 AND b(6) = 1 AND a(4) = 0 AND b(4) = 0 THEN GOTO compspace4:
  128. IF b(8) = 1 AND b(9) = 1 AND a(7) = 0 AND b(7) = 0 THEN GOTO compspace7:
  129. IF b(4) = 1 AND b(7) = 1 AND a(1) = 0 AND b(1) = 0 THEN GOTO compspace1:
  130. IF b(5) = 1 AND b(8) = 1 AND a(2) = 0 AND b(2) = 0 THEN GOTO compspace2:
  131. IF b(6) = 1 AND b(9) = 1 AND a(3) = 0 AND b(3) = 0 THEN GOTO compspace3:
  132. IF b(5) = 1 AND b(9) = 1 AND a(1) = 0 AND b(1) = 0 THEN GOTO compspace1:
  133. IF b(7) = 1 AND b(5) = 1 AND a(3) = 0 AND b(3) = 0 THEN GOTO compspace3:
  134. 'Middle space gone.
  135. IF b(1) = 1 AND b(3) = 1 AND a(2) = 0 AND b(2) = 0 THEN GOTO compspace2:
  136. IF b(4) = 1 AND b(6) = 1 AND a(5) = 0 AND b(5) = 0 THEN GOTO compspace5:
  137. IF b(7) = 1 AND b(9) = 1 AND a(8) = 0 AND b(8) = 0 THEN GOTO compspace8:
  138. IF b(1) = 1 AND b(7) = 1 AND a(4) = 0 AND b(4) = 0 THEN GOTO compspace4:
  139. IF b(2) = 1 AND b(8) = 1 AND a(5) = 0 AND b(5) = 0 THEN GOTO compspace5:
  140. IF b(3) = 1 AND b(9) = 1 AND a(6) = 0 AND b(6) = 0 THEN GOTO compspace6:
  141. IF b(1) = 1 AND b(9) = 1 AND a(5) = 0 AND b(5) = 0 THEN GOTO compspace5:
  142. IF b(3) = 1 AND b(7) = 1 AND a(5) = 0 AND b(5) = 0 THEN GOTO compspace5:
  143.  
  144. 'Check to block.
  145. 'Last space gone.
  146. IF a(1) = 1 AND a(2) = 1 AND a(3) = 0 AND b(3) = 0 THEN GOTO compspace3:
  147. IF a(4) = 1 AND a(5) = 1 AND a(6) = 0 AND b(6) = 0 THEN GOTO compspace6:
  148. IF a(7) = 1 AND a(8) = 1 AND a(9) = 0 AND b(9) = 0 THEN GOTO compspace9:
  149. IF a(1) = 1 AND a(4) = 1 AND a(7) = 0 AND b(7) = 0 THEN GOTO compspace7:
  150. IF a(2) = 1 AND a(5) = 1 AND a(8) = 0 AND b(8) = 0 THEN GOTO compspace8:
  151. IF a(3) = 1 AND a(6) = 1 AND a(9) = 0 AND b(9) = 0 THEN GOTO compspace9:
  152. IF a(1) = 1 AND a(5) = 1 AND a(9) = 0 AND b(9) = 0 THEN GOTO compspace9:
  153. IF a(3) = 1 AND a(5) = 1 AND a(7) = 0 AND b(7) = 0 THEN GOTO compspace7:
  154. 'First space gone.
  155. IF a(2) = 1 AND a(3) = 1 AND a(1) = 0 AND b(1) = 0 THEN GOTO compspace1:
  156. IF a(5) = 1 AND a(6) = 1 AND a(4) = 0 AND b(4) = 0 THEN GOTO compspace4:
  157. IF a(8) = 1 AND a(9) = 1 AND a(7) = 0 AND b(7) = 0 THEN GOTO compspace7:
  158. IF a(4) = 1 AND a(7) = 1 AND a(1) = 0 AND b(1) = 0 THEN GOTO compspace1:
  159. IF a(5) = 1 AND a(8) = 1 AND a(2) = 0 AND b(2) = 0 THEN GOTO compspace2:
  160. IF a(6) = 1 AND a(9) = 1 AND a(3) = 0 AND b(3) = 0 THEN GOTO compspace3:
  161. IF a(5) = 1 AND a(9) = 1 AND a(1) = 0 AND b(1) = 0 THEN GOTO compspace1:
  162. IF a(7) = 1 AND a(5) = 1 AND a(3) = 0 AND b(3) = 0 THEN GOTO compspace3:
  163. 'Middle space gone.
  164. IF a(1) = 1 AND a(3) = 1 AND a(2) = 0 AND b(2) = 0 THEN GOTO compspace2:
  165. IF a(4) = 1 AND a(6) = 1 AND a(5) = 0 AND b(5) = 0 THEN GOTO compspace5:
  166. IF a(7) = 1 AND a(9) = 1 AND a(8) = 0 AND b(8) = 0 THEN GOTO compspace8:
  167. IF a(1) = 1 AND a(7) = 1 AND a(4) = 0 AND b(4) = 0 THEN GOTO compspace4:
  168. IF a(2) = 1 AND a(8) = 1 AND a(5) = 0 AND b(5) = 0 THEN GOTO compspace5:
  169. IF a(3) = 1 AND a(9) = 1 AND a(6) = 0 AND b(6) = 0 THEN GOTO compspace6:
  170. IF a(1) = 1 AND a(9) = 1 AND a(5) = 0 AND b(5) = 0 THEN GOTO compspace5:
  171. IF a(3) = 1 AND a(7) = 1 AND a(5) = 0 AND b(5) = 0 THEN GOTO compspace5:
  172.  
  173. 'Computer decides a random space.
  174. computerchoice:
  175. comp = INT(RND * 9) + 1
  176. IF b(comp) = 1 THEN GOTO computerchoice:
  177. IF a(comp) = 1 THEN GOTO computerchoice:
  178. IF comp = 1 THEN GOTO compspace1:
  179. IF comp = 2 THEN GOTO compspace2:
  180. IF comp = 3 THEN GOTO compspace3:
  181. IF comp = 4 THEN GOTO compspace4:
  182. IF comp = 5 THEN GOTO compspace5:
  183. IF comp = 6 THEN GOTO compspace6:
  184. IF comp = 7 THEN GOTO compspace7:
  185. IF comp = 8 THEN GOTO compspace8:
  186. IF comp = 9 THEN GOTO compspace9:
  187.  
  188. 'Cat's Game
  189. catsgame:
  190. FOR snd = 400 TO 300 STEP -25
  191.     SOUND snd, .5
  192. NEXT snd
  193. FOR tt = 1 TO 9
  194.     a(tt) = 0
  195.     b(tt) = 0
  196. NEXT tt
  197. t = 0
  198. LOCATE 2, 27: PRINT "Cat's Game - No Winners"
  199. LOCATE 4, 27: PRINT "Press Space Bar to play again."
  200. LOCATE 6, 27: PRINT "Or any other key to end."
  201. again2:
  202. ag$ = INKEY$
  203. IF ag$ = "" THEN GOTO again:
  204. IF ag$ <> " " THEN END
  205. IF ag$ = " " THEN GOTO start:
  206. GOTO again2:
  207.  
  208. 'Check to see if the computer won.
  209. check:
  210. IF b(1) = 1 AND b(2) = 1 AND b(3) = 1 THEN GOTO compwon:
  211. IF b(4) = 1 AND b(5) = 1 AND b(6) = 1 THEN GOTO compwon:
  212. IF b(7) = 1 AND b(8) = 1 AND b(9) = 1 THEN GOTO compwon
  213. IF b(1) = 1 AND b(4) = 1 AND b(7) = 1 THEN GOTO compwon:
  214. IF b(2) = 1 AND b(5) = 1 AND b(8) = 1 THEN GOTO compwon:
  215. IF b(3) = 1 AND b(6) = 1 AND b(9) = 1 THEN GOTO compwon:
  216. IF b(1) = 1 AND b(5) = 1 AND b(9) = 1 THEN GOTO compwon:
  217. IF b(3) = 1 AND b(5) = 1 AND b(7) = 1 THEN GOTO compwon:
  218. turn = turn + 1
  219. IF turn = 9 THEN GOTO catsgame:
  220. t = 0
  221. GOTO Go:
  222.  
  223. compwon:
  224. FOR snd = 900 TO 300 STEP -50
  225.     SOUND snd, .5
  226. NEXT snd
  227. FOR tt = 1 TO 9
  228.     a(tt) = 0
  229.     b(tt) = 0
  230. NEXT tt
  231. t = 0
  232. LOCATE 2, 33: PRINT "Computer Wins"
  233. LOCATE 4, 27: PRINT "Press Space Bar to play again."
  234. LOCATE 6, 27: PRINT "Or any other key to end."
  235. again3:
  236. ag$ = INKEY$
  237. IF ag$ = "" THEN GOTO again:
  238. IF ag$ <> " " THEN END
  239. IF ag$ = " " THEN GOTO start:
  240. GOTO again3:
  241.  
  242. 'This part draws the computer's circle.
  243. compspace1:
  244. t = 0
  245. b(1) = 1
  246. FOR s = .25 TO 5 STEP .25
  247.     CIRCLE (160, 137), 40 - s, _RGB32(255, 0, 0)
  248. GOTO check:
  249. compspace2:
  250. t = 0
  251. b(2) = 1
  252. FOR s = .25 TO 5 STEP .25
  253.     CIRCLE (300, 137), 40 - s, _RGB32(255, 0, 0)
  254. GOTO check:
  255. compspace3:
  256. t = 0
  257. b(3) = 1
  258. FOR s = .25 TO 5 STEP .25
  259.     CIRCLE (440, 137), 40 - s, _RGB32(255, 0, 0)
  260. GOTO check:
  261. compspace4:
  262. t = 0
  263. b(4) = 1
  264. FOR s = .25 TO 5 STEP .25
  265.     CIRCLE (160, 257), 40 - s, _RGB32(255, 0, 0)
  266. GOTO check:
  267. compspace5:
  268. t = 0
  269. b(5) = 1
  270. FOR s = .25 TO 5 STEP .25
  271.     CIRCLE (300, 257), 40 - s, _RGB32(255, 0, 0)
  272. GOTO check:
  273. compspace6:
  274. t = 0
  275. b(6) = 1
  276. FOR s = .25 TO 5 STEP .25
  277.     CIRCLE (440, 257), 40 - s, _RGB32(255, 0, 0)
  278. GOTO check:
  279. compspace7:
  280. t = 0
  281. b(7) = 1
  282. FOR s = .25 TO 5 STEP .25
  283.     CIRCLE (160, 372), 40 - s, _RGB32(255, 0, 0)
  284. GOTO check:
  285. compspace8:
  286. t = 0
  287. b(8) = 1
  288. FOR s = .25 TO 5 STEP .25
  289.     CIRCLE (300, 372), 40 - s, _RGB32(255, 0, 0)
  290. GOTO check:
  291. compspace9:
  292. t = 0
  293. b(9) = 1
  294. FOR s = .25 TO 5 STEP .25
  295.     CIRCLE (440, 372), 40 - s, _RGB32(255, 0, 0)
  296. GOTO check:
  297. 'This last part draws your X.
  298. space1:
  299. a(1) = 1
  300. FOR s = .25 TO 5 STEP .25
  301.     LINE (120 + s, 120)-(200 + s, 165), _RGB32(0, 255, 0)
  302.     LINE (200 + s, 120)-(120 + s, 165), _RGB32(0, 255, 0)
  303. t = 1
  304. GOTO checkwin:
  305. space2:
  306. a(2) = 1
  307. FOR s = .25 TO 5 STEP .25
  308.     LINE (260 + s, 120)-(340 + s, 165), _RGB32(0, 255, 0)
  309.     LINE (340 + s, 120)-(260 + s, 165), _RGB32(0, 255, 0)
  310. t = 1
  311. GOTO checkwin:
  312. space3:
  313. a(3) = 1
  314. FOR s = .25 TO 5 STEP .25
  315.     LINE (400 + s, 120)-(480 + s, 165), _RGB32(0, 255, 0)
  316.     LINE (480 + s, 120)-(400 + s, 165), _RGB32(0, 255, 0)
  317. t = 1
  318. GOTO checkwin:
  319. space4:
  320. a(4) = 1
  321. FOR s = .25 TO 5 STEP .25
  322.     LINE (120 + s, 235)-(200 + s, 280), _RGB32(0, 255, 0)
  323.     LINE (200 + s, 235)-(120 + s, 280), _RGB32(0, 255, 0)
  324. t = 1
  325. GOTO checkwin:
  326. space5:
  327. a(5) = 1
  328. FOR s = .25 TO 5 STEP .25
  329.     LINE (260 + s, 235)-(340 + s, 280), _RGB32(0, 255, 0)
  330.     LINE (340 + s, 235)-(260 + s, 280), _RGB32(0, 255, 0)
  331. t = 1
  332. GOTO checkwin:
  333. space6:
  334. a(6) = 1
  335. FOR s = .25 TO 5 STEP .25
  336.     LINE (400 + s, 235)-(480 + s, 280), _RGB32(0, 255, 0)
  337.     LINE (480 + s, 235)-(400 + s, 280), _RGB32(0, 255, 0)
  338. t = 1
  339. GOTO checkwin:
  340. space7:
  341. a(7) = 1
  342. FOR s = .25 TO 5 STEP .25
  343.     LINE (120 + s, 350)-(200 + s, 395), _RGB32(0, 255, 0)
  344.     LINE (200 + s, 350)-(120 + s, 395), _RGB32(0, 255, 0)
  345. t = 1
  346. GOTO checkwin:
  347. space8:
  348. a(8) = 1
  349. FOR s = .25 TO 5 STEP .25
  350.     LINE (260 + s, 350)-(340 + s, 395), _RGB32(0, 255, 0)
  351.     LINE (340 + s, 350)-(260 + s, 395), _RGB32(0, 255, 0)
  352. t = 1
  353. GOTO checkwin:
  354. space9:
  355. a(9) = 1
  356. FOR s = .25 TO 5 STEP .25
  357.     LINE (400 + s, 350)-(480 + s, 395), _RGB32(0, 255, 0)
  358.     LINE (480 + s, 350)-(400 + s, 395), _RGB32(0, 255, 0)
  359. t = 1
  360. GOTO checkwin:
  361.  
  362.  
« Last Edit: August 14, 2019, 01:40:52 pm by SierraKen »

Marked as best answer by SierraKen on August 14, 2019, 09:44:10 am

Offline SierraKen

  • Forum Resident
  • Posts: 1454
    • View Profile
Re: Tic-Tac-Toe
« Reply #6 on: August 14, 2019, 01:44:03 pm »
Here is the fixed version. I still don't know exactly why this happened, but what was happening was if you pressed the Space Bar in the middle of the game, and when the game was over, it didn't clear the screen completely. Or it was playing part of the last game still. So I added an INKEY$ in the main loop for Esc to end the game and having that INKEY$ stopped anything to do with the Space Bar, until the game was over and then you can press the Space  Bar to play again. :) And as a side note, as I did before in this game, you don't see the welcome screen when you want to play again.

Code: QB64: [Select]
  1. 'I've wanted to make this game for decades and finally am able to!
  2. 'This game was made on August 14, 2019 by Ken G.
  3. 'This is Freeware.
  4.  
  5. DIM a(10), b(10)
  6. _TITLE "Tic-Tac-Toe     by Ken G."
  7. SCREEN _NEWIMAGE(600, 480, 32)
  8. LOCATE 10, 32: PRINT "-"
  9. LOCATE 10, 38: PRINT "-"
  10. FOR tic = 1 TO 10
  11.     LOCATE tic, 28: PRINT "TIC"
  12.     _DELAY .2
  13.     LOCATE tic, 28: PRINT "   "
  14. NEXT tic
  15. LOCATE 10, 28: PRINT "TIC"
  16.  
  17. FOR tac = 20 TO 10 STEP -1
  18.     LOCATE tac, 34: PRINT "TAC"
  19.     _DELAY .2
  20.     LOCATE tac, 34: PRINT "   "
  21. NEXT tac
  22. LOCATE 10, 34: PRINT "TAC"
  23.  
  24. FOR toe = 1 TO 10
  25.     LOCATE toe, 40: PRINT "TOE"
  26.     _DELAY .2
  27.     LOCATE toe, 40: PRINT "   "
  28. NEXT toe
  29. LOCATE 10, 40: PRINT "TOE"
  30.  
  31. PRINT "                             by   Ken G."
  32. PRINT "     Play against the computer in this classic game of Tic-Tac-Toe."
  33. PRINT "                    Whoever gets 3 in a row wins."
  34. PRINT "                  Choose a sqace by using your mouse."
  35. PRINT "                             You go first."
  36. PRINT "                        Press Space Bar To Begin."
  37. st:
  38. sta$ = INKEY$
  39. IF sta$ = "" THEN GOTO st:
  40. IF sta$ = " " THEN CLS: GOTO start:
  41. GOTO st:
  42. start:
  43. ag$ = ""
  44. t = 0
  45. turn = 0
  46. comp = 0
  47.  
  48. 'Draw Grid
  49. 'Vertical Lines
  50. LINE (220, 100)-(240, 410), _RGB32(255, 255, 255), BF
  51. LINE (360, 100)-(380, 410), _RGB32(255, 255, 255), BF
  52. 'Horizontal Lines
  53. LINE (90, 185)-(510, 205), _RGB32(255, 255, 255), BF
  54. LINE (90, 305)-(510, 325), _RGB32(255, 255, 255), BF
  55.  
  56. Go:
  57. a$ = INKEY$
  58. IF a$ = CHR$(27) THEN END
  59. mouseWheel = 0
  60.     mouseX = _MOUSEX
  61.     mouseY = _MOUSEY
  62.     mouseLeftButton = _MOUSEBUTTON(1)
  63.     mouseRightButton = _MOUSEBUTTON(2)
  64.     mouseMiddleButton = _MOUSEBUTTON(3)
  65.     mouseWheel = mouseWheel + _MOUSEWHEEL
  66.  
  67. IF mouseLeftButton = -1 THEN
  68.     IF mouseX > 88 AND mouseX < 218 AND mouseY > 93 AND mouseY < 182 AND b(1) = 0 AND a(1) = 0 AND t = 0 THEN GOSUB space1:
  69.     IF mouseX > 241 AND mouseX < 357 AND mouseY > 93 AND mouseY < 182 AND b(2) = 0 AND a(2) = 0 AND t = 0 THEN GOSUB space2:
  70.     IF mouseX > 381 AND mouseX < 509 AND mouseY > 93 AND mouseY < 182 AND b(3) = 0 AND a(3) = 0 AND t = 0 THEN GOSUB space3:
  71.     IF mouseX > 88 AND mouseX < 218 AND mouseY > 205 AND mouseY < 302 AND b(4) = 0 AND a(4) = 0 AND t = 0 THEN GOSUB space4:
  72.     IF mouseX > 241 AND mouseX < 357 AND mouseY > 205 AND mouseY < 302 AND b(5) = 0 AND a(5) = 0 AND t = 0 THEN GOSUB space5:
  73.     IF mouseX > 381 AND mouseX < 509 AND mouseY > 205 AND mouseY < 302 AND b(6) = 0 AND a(6) = 0 AND t = 0 THEN GOSUB space6:
  74.     IF mouseX > 88 AND mouseX < 218 AND mouseY > 326 AND mouseY < 410 AND b(7) = 0 AND a(7) = 0 AND t = 0 THEN GOSUB space7:
  75.     IF mouseX > 241 AND mouseX < 357 AND mouseY > 326 AND mouseY < 410 AND b(8) = 0 AND a(8) = 0 AND t = 0 THEN GOSUB space8:
  76.     IF mouseX > 381 AND mouseX < 509 AND mouseY > 326 AND mouseY < 410 AND b(9) = 0 AND a(9) = 0 AND t = 0 THEN GOSUB space9:
  77.  
  78. IF mouseLeftButton = -1 AND ending = 1 THEN GOTO start:
  79. IF mouseRightButton = -1 AND ending = 1 THEN END
  80.  
  81. IF t = 1 THEN GOSUB computer:
  82.  
  83. GOTO Go:
  84.  
  85. checkwin:
  86. 'Check to see if you won.
  87. IF a(1) = 1 AND a(2) = 1 AND a(3) = 1 THEN GOTO won:
  88. IF a(4) = 1 AND a(5) = 1 AND a(6) = 1 THEN GOTO won:
  89. IF a(7) = 1 AND a(8) = 1 AND a(9) = 1 THEN GOTO won
  90. IF a(1) = 1 AND a(4) = 1 AND a(7) = 1 THEN GOTO won:
  91. IF a(2) = 1 AND a(5) = 1 AND a(8) = 1 THEN GOTO won:
  92. IF a(3) = 1 AND a(6) = 1 AND a(9) = 1 THEN GOTO won:
  93. IF a(1) = 1 AND a(5) = 1 AND a(9) = 1 THEN GOTO won:
  94. IF a(3) = 1 AND a(5) = 1 AND a(7) = 1 THEN GOTO won:
  95. turn = turn + 1
  96. SOUND 100, .25
  97. IF turn = 9 THEN GOTO catsgame:
  98. GOTO Go:
  99. won:
  100. FOR snd = 300 TO 900 STEP 50
  101.     SOUND snd, .5
  102. NEXT snd
  103. FOR tt = 1 TO 9
  104.     a(tt) = 0
  105.     b(tt) = 0
  106. NEXT tt
  107. t = 0
  108. LOCATE 2, 32: PRINT "Y O U   W O N ! !"
  109. LOCATE 4, 27: PRINT "Press Space Bar to play again."
  110. LOCATE 6, 27: PRINT "Or any other key to end."
  111. again:
  112. ag$ = INKEY$
  113. IF ag$ = "" THEN GOTO again:
  114. IF ag$ <> " " THEN END
  115. IF ag$ = " " THEN CLS: GOTO start:
  116. GOTO again:
  117.  
  118. computer:
  119.  
  120. 'Check to win.
  121. 'Last space gone.
  122. IF b(1) = 1 AND b(2) = 1 AND a(3) = 0 AND b(3) = 0 THEN GOTO compspace3:
  123. IF b(4) = 1 AND b(5) = 1 AND a(6) = 0 AND b(6) = 0 THEN GOTO compspace6:
  124. IF b(7) = 1 AND b(8) = 1 AND a(9) = 0 AND b(9) = 0 THEN GOTO compspace9:
  125. IF b(1) = 1 AND b(4) = 1 AND a(7) = 0 AND b(7) = 0 THEN GOTO compspace7:
  126. IF b(2) = 1 AND b(5) = 1 AND a(8) = 0 AND b(8) = 0 THEN GOTO compspace8:
  127. IF b(3) = 1 AND b(6) = 1 AND a(9) = 0 AND b(9) = 0 THEN GOTO compspace9:
  128. IF b(1) = 1 AND b(5) = 1 AND a(9) = 0 AND b(9) = 0 THEN GOTO compspace9:
  129. IF b(3) = 1 AND b(5) = 1 AND a(7) = 0 AND b(7) = 0 THEN GOTO compspace7:
  130. 'First space gone.
  131. IF b(2) = 1 AND b(3) = 1 AND a(1) = 0 AND b(1) = 0 THEN GOTO compspace1:
  132. IF b(5) = 1 AND b(6) = 1 AND a(4) = 0 AND b(4) = 0 THEN GOTO compspace4:
  133. IF b(8) = 1 AND b(9) = 1 AND a(7) = 0 AND b(7) = 0 THEN GOTO compspace7:
  134. IF b(4) = 1 AND b(7) = 1 AND a(1) = 0 AND b(1) = 0 THEN GOTO compspace1:
  135. IF b(5) = 1 AND b(8) = 1 AND a(2) = 0 AND b(2) = 0 THEN GOTO compspace2:
  136. IF b(6) = 1 AND b(9) = 1 AND a(3) = 0 AND b(3) = 0 THEN GOTO compspace3:
  137. IF b(5) = 1 AND b(9) = 1 AND a(1) = 0 AND b(1) = 0 THEN GOTO compspace1:
  138. IF b(7) = 1 AND b(5) = 1 AND a(3) = 0 AND b(3) = 0 THEN GOTO compspace3:
  139. 'Middle space gone.
  140. IF b(1) = 1 AND b(3) = 1 AND a(2) = 0 AND b(2) = 0 THEN GOTO compspace2:
  141. IF b(4) = 1 AND b(6) = 1 AND a(5) = 0 AND b(5) = 0 THEN GOTO compspace5:
  142. IF b(7) = 1 AND b(9) = 1 AND a(8) = 0 AND b(8) = 0 THEN GOTO compspace8:
  143. IF b(1) = 1 AND b(7) = 1 AND a(4) = 0 AND b(4) = 0 THEN GOTO compspace4:
  144. IF b(2) = 1 AND b(8) = 1 AND a(5) = 0 AND b(5) = 0 THEN GOTO compspace5:
  145. IF b(3) = 1 AND b(9) = 1 AND a(6) = 0 AND b(6) = 0 THEN GOTO compspace6:
  146. IF b(1) = 1 AND b(9) = 1 AND a(5) = 0 AND b(5) = 0 THEN GOTO compspace5:
  147. IF b(3) = 1 AND b(7) = 1 AND a(5) = 0 AND b(5) = 0 THEN GOTO compspace5:
  148.  
  149. 'Check to block.
  150. 'Last space gone.
  151. IF a(1) = 1 AND a(2) = 1 AND a(3) = 0 AND b(3) = 0 THEN GOTO compspace3:
  152. IF a(4) = 1 AND a(5) = 1 AND a(6) = 0 AND b(6) = 0 THEN GOTO compspace6:
  153. IF a(7) = 1 AND a(8) = 1 AND a(9) = 0 AND b(9) = 0 THEN GOTO compspace9:
  154. IF a(1) = 1 AND a(4) = 1 AND a(7) = 0 AND b(7) = 0 THEN GOTO compspace7:
  155. IF a(2) = 1 AND a(5) = 1 AND a(8) = 0 AND b(8) = 0 THEN GOTO compspace8:
  156. IF a(3) = 1 AND a(6) = 1 AND a(9) = 0 AND b(9) = 0 THEN GOTO compspace9:
  157. IF a(1) = 1 AND a(5) = 1 AND a(9) = 0 AND b(9) = 0 THEN GOTO compspace9:
  158. IF a(3) = 1 AND a(5) = 1 AND a(7) = 0 AND b(7) = 0 THEN GOTO compspace7:
  159. 'First space gone.
  160. IF a(2) = 1 AND a(3) = 1 AND a(1) = 0 AND b(1) = 0 THEN GOTO compspace1:
  161. IF a(5) = 1 AND a(6) = 1 AND a(4) = 0 AND b(4) = 0 THEN GOTO compspace4:
  162. IF a(8) = 1 AND a(9) = 1 AND a(7) = 0 AND b(7) = 0 THEN GOTO compspace7:
  163. IF a(4) = 1 AND a(7) = 1 AND a(1) = 0 AND b(1) = 0 THEN GOTO compspace1:
  164. IF a(5) = 1 AND a(8) = 1 AND a(2) = 0 AND b(2) = 0 THEN GOTO compspace2:
  165. IF a(6) = 1 AND a(9) = 1 AND a(3) = 0 AND b(3) = 0 THEN GOTO compspace3:
  166. IF a(5) = 1 AND a(9) = 1 AND a(1) = 0 AND b(1) = 0 THEN GOTO compspace1:
  167. IF a(7) = 1 AND a(5) = 1 AND a(3) = 0 AND b(3) = 0 THEN GOTO compspace3:
  168. 'Middle space gone.
  169. IF a(1) = 1 AND a(3) = 1 AND a(2) = 0 AND b(2) = 0 THEN GOTO compspace2:
  170. IF a(4) = 1 AND a(6) = 1 AND a(5) = 0 AND b(5) = 0 THEN GOTO compspace5:
  171. IF a(7) = 1 AND a(9) = 1 AND a(8) = 0 AND b(8) = 0 THEN GOTO compspace8:
  172. IF a(1) = 1 AND a(7) = 1 AND a(4) = 0 AND b(4) = 0 THEN GOTO compspace4:
  173. IF a(2) = 1 AND a(8) = 1 AND a(5) = 0 AND b(5) = 0 THEN GOTO compspace5:
  174. IF a(3) = 1 AND a(9) = 1 AND a(6) = 0 AND b(6) = 0 THEN GOTO compspace6:
  175. IF a(1) = 1 AND a(9) = 1 AND a(5) = 0 AND b(5) = 0 THEN GOTO compspace5:
  176. IF a(3) = 1 AND a(7) = 1 AND a(5) = 0 AND b(5) = 0 THEN GOTO compspace5:
  177.  
  178. 'Computer decides a random space.
  179. computerchoice:
  180. comp = INT(RND * 9) + 1
  181. IF b(comp) = 1 THEN GOTO computerchoice:
  182. IF a(comp) = 1 THEN GOTO computerchoice:
  183. IF comp = 1 THEN GOTO compspace1:
  184. IF comp = 2 THEN GOTO compspace2:
  185. IF comp = 3 THEN GOTO compspace3:
  186. IF comp = 4 THEN GOTO compspace4:
  187. IF comp = 5 THEN GOTO compspace5:
  188. IF comp = 6 THEN GOTO compspace6:
  189. IF comp = 7 THEN GOTO compspace7:
  190. IF comp = 8 THEN GOTO compspace8:
  191. IF comp = 9 THEN GOTO compspace9:
  192.  
  193. 'Cat's Game
  194. catsgame:
  195. FOR snd = 400 TO 300 STEP -25
  196.     SOUND snd, .5
  197. NEXT snd
  198. FOR tt = 1 TO 9
  199.     a(tt) = 0
  200.     b(tt) = 0
  201. NEXT tt
  202. t = 0
  203. LOCATE 2, 27: PRINT "Cat's Game - No Winners"
  204. LOCATE 4, 27: PRINT "Press Space Bar to play again."
  205. LOCATE 6, 27: PRINT "Or any other key to end."
  206. again2:
  207. ag$ = INKEY$
  208. IF ag$ = "" THEN GOTO again:
  209. IF ag$ <> " " THEN END
  210. IF ag$ = " " THEN CLS: GOTO start:
  211. GOTO again2:
  212.  
  213. 'Check to see if the computer won.
  214. check:
  215. IF b(1) = 1 AND b(2) = 1 AND b(3) = 1 THEN GOTO compwon:
  216. IF b(4) = 1 AND b(5) = 1 AND b(6) = 1 THEN GOTO compwon:
  217. IF b(7) = 1 AND b(8) = 1 AND b(9) = 1 THEN GOTO compwon
  218. IF b(1) = 1 AND b(4) = 1 AND b(7) = 1 THEN GOTO compwon:
  219. IF b(2) = 1 AND b(5) = 1 AND b(8) = 1 THEN GOTO compwon:
  220. IF b(3) = 1 AND b(6) = 1 AND b(9) = 1 THEN GOTO compwon:
  221. IF b(1) = 1 AND b(5) = 1 AND b(9) = 1 THEN GOTO compwon:
  222. IF b(3) = 1 AND b(5) = 1 AND b(7) = 1 THEN GOTO compwon:
  223. turn = turn + 1
  224. IF turn = 9 THEN GOTO catsgame:
  225. t = 0
  226. GOTO Go:
  227.  
  228. compwon:
  229. FOR snd = 900 TO 300 STEP -50
  230.     SOUND snd, .5
  231. NEXT snd
  232. FOR tt = 1 TO 9
  233.     a(tt) = 0
  234.     b(tt) = 0
  235. NEXT tt
  236. t = 0
  237. LOCATE 2, 33: PRINT "Computer Wins"
  238. LOCATE 4, 27: PRINT "Press Space Bar to play again."
  239. LOCATE 6, 27: PRINT "Or any other key to end."
  240. again3:
  241. ag$ = INKEY$
  242. IF ag$ = "" THEN GOTO again:
  243. IF ag$ <> " " THEN END
  244. IF ag$ = " " THEN CLS: GOTO start:
  245. GOTO again3:
  246.  
  247. 'This part draws the computer's circle.
  248. compspace1:
  249. t = 0
  250. b(1) = 1
  251. FOR s = .25 TO 5 STEP .25
  252.     CIRCLE (160, 137), 40 - s, _RGB32(255, 0, 0)
  253. GOTO check:
  254. compspace2:
  255. t = 0
  256. b(2) = 1
  257. FOR s = .25 TO 5 STEP .25
  258.     CIRCLE (300, 137), 40 - s, _RGB32(255, 0, 0)
  259. GOTO check:
  260. compspace3:
  261. t = 0
  262. b(3) = 1
  263. FOR s = .25 TO 5 STEP .25
  264.     CIRCLE (440, 137), 40 - s, _RGB32(255, 0, 0)
  265. GOTO check:
  266. compspace4:
  267. t = 0
  268. b(4) = 1
  269. FOR s = .25 TO 5 STEP .25
  270.     CIRCLE (160, 257), 40 - s, _RGB32(255, 0, 0)
  271. GOTO check:
  272. compspace5:
  273. t = 0
  274. b(5) = 1
  275. FOR s = .25 TO 5 STEP .25
  276.     CIRCLE (300, 257), 40 - s, _RGB32(255, 0, 0)
  277. GOTO check:
  278. compspace6:
  279. t = 0
  280. b(6) = 1
  281. FOR s = .25 TO 5 STEP .25
  282.     CIRCLE (440, 257), 40 - s, _RGB32(255, 0, 0)
  283. GOTO check:
  284. compspace7:
  285. t = 0
  286. b(7) = 1
  287. FOR s = .25 TO 5 STEP .25
  288.     CIRCLE (160, 372), 40 - s, _RGB32(255, 0, 0)
  289. GOTO check:
  290. compspace8:
  291. t = 0
  292. b(8) = 1
  293. FOR s = .25 TO 5 STEP .25
  294.     CIRCLE (300, 372), 40 - s, _RGB32(255, 0, 0)
  295. GOTO check:
  296. compspace9:
  297. t = 0
  298. b(9) = 1
  299. FOR s = .25 TO 5 STEP .25
  300.     CIRCLE (440, 372), 40 - s, _RGB32(255, 0, 0)
  301. GOTO check:
  302. 'This last part draws your X.
  303. space1:
  304. a(1) = 1
  305. FOR s = .25 TO 5 STEP .25
  306.     LINE (120 + s, 120)-(200 + s, 165), _RGB32(0, 255, 0)
  307.     LINE (200 + s, 120)-(120 + s, 165), _RGB32(0, 255, 0)
  308. t = 1
  309. GOTO checkwin:
  310. space2:
  311. a(2) = 1
  312. FOR s = .25 TO 5 STEP .25
  313.     LINE (260 + s, 120)-(340 + s, 165), _RGB32(0, 255, 0)
  314.     LINE (340 + s, 120)-(260 + s, 165), _RGB32(0, 255, 0)
  315. t = 1
  316. GOTO checkwin:
  317. space3:
  318. a(3) = 1
  319. FOR s = .25 TO 5 STEP .25
  320.     LINE (400 + s, 120)-(480 + s, 165), _RGB32(0, 255, 0)
  321.     LINE (480 + s, 120)-(400 + s, 165), _RGB32(0, 255, 0)
  322. t = 1
  323. GOTO checkwin:
  324. space4:
  325. a(4) = 1
  326. FOR s = .25 TO 5 STEP .25
  327.     LINE (120 + s, 235)-(200 + s, 280), _RGB32(0, 255, 0)
  328.     LINE (200 + s, 235)-(120 + s, 280), _RGB32(0, 255, 0)
  329. t = 1
  330. GOTO checkwin:
  331. space5:
  332. a(5) = 1
  333. FOR s = .25 TO 5 STEP .25
  334.     LINE (260 + s, 235)-(340 + s, 280), _RGB32(0, 255, 0)
  335.     LINE (340 + s, 235)-(260 + s, 280), _RGB32(0, 255, 0)
  336. t = 1
  337. GOTO checkwin:
  338. space6:
  339. a(6) = 1
  340. FOR s = .25 TO 5 STEP .25
  341.     LINE (400 + s, 235)-(480 + s, 280), _RGB32(0, 255, 0)
  342.     LINE (480 + s, 235)-(400 + s, 280), _RGB32(0, 255, 0)
  343. t = 1
  344. GOTO checkwin:
  345. space7:
  346. a(7) = 1
  347. FOR s = .25 TO 5 STEP .25
  348.     LINE (120 + s, 350)-(200 + s, 395), _RGB32(0, 255, 0)
  349.     LINE (200 + s, 350)-(120 + s, 395), _RGB32(0, 255, 0)
  350. t = 1
  351. GOTO checkwin:
  352. space8:
  353. a(8) = 1
  354. FOR s = .25 TO 5 STEP .25
  355.     LINE (260 + s, 350)-(340 + s, 395), _RGB32(0, 255, 0)
  356.     LINE (340 + s, 350)-(260 + s, 395), _RGB32(0, 255, 0)
  357. t = 1
  358. GOTO checkwin:
  359. space9:
  360. a(9) = 1
  361. FOR s = .25 TO 5 STEP .25
  362.     LINE (400 + s, 350)-(480 + s, 395), _RGB32(0, 255, 0)
  363.     LINE (480 + s, 350)-(400 + s, 395), _RGB32(0, 255, 0)
  364. t = 1
  365. GOTO checkwin:
  366.  
  367.  
  368.  

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
Re: Tic-Tac-Toe
« Reply #7 on: August 16, 2019, 03:20:16 pm »
Hi
I find it fine, smart (except AI of game) and with funny sounds
Programming isn't difficult, only it's  consuming time and coffee