### Author Topic: Tic Tac Toe in 3D by qbguy  (Read 3774 times)

0 Members and 1 Guest are viewing this topic.

#### The Librarian

• Moderator
• Newbie
• Posts: 42
##### Tic Tac Toe in 3D by qbguy
« on: March 10, 2018, 10:15:17 AM »
Tic Tac Toe in 3D

Author: @qbguy
Source: qb64.net Forum
URL: http://www.qb64.net/forum/index.php?topic=56.0
Version: 2008
Tags: [3d], [game], [ai], [mouse]

Description:
The goal is to get four in a row while preventing the computer from doing the same. Move by clicking the mouse.

Source Code:
Code: QB64: [Select]
1. DECLARE SUB SHOWWIN (C%, R%, p%, COLOUR%)
2. DECLARE SUB MAKEMOVE (X%, Y%, Z%, COLOUR%)
3. DECLARE SUB GETMOVE (X%, Y%, Z%)
4. DEFINT A-Z
5. DIM E(7), PEEKB(1999)
6. GOSUB INIT
7. E(1) = 254: E(2) = 18: E(3) = 2: E(4) = 1: E(5) = 2: E(6) = 66: E(7) = 255
8. Q = 564: G = 628: L = 768
9. FOR K = G TO G + 63
10.     PEEKB(K) = 128
11. FOR K = S TO S + 75
12.     PEEKB(K) = 128
13. 100 CALL GETMOVE(C, R, p)
14. X = 16 * (p - 1) + 4 * (R - 1) + C - 1
15. IF PEEKB(G + X) <> 128 THEN GOTO 100
16. CALL MAKEMOVE(C, R, p, 1)
17. M = -1: GOSUB 1000
18. GOSUB 2000
19. IF W THEN CALL SHOWWIN(C, R, p, 1): END
20. IF T THEN LOCATE 15, 33: PRINT " --- Tie game --- ": END
21. GOSUB 3000
22. M = 1: GOSUB 1000
23. GOSUB 2000
24. GOSUB 7000
25. IF W THEN CALL SHOWWIN(C, R, p, 4): END
26. IF T THEN LOCATE 15, 33: PRINT " --- Tie game --- ": END
27. GOTO 100
28.
29. 1000
30. PEEKB(G + X) = 128 + M
31. FOR K = L TO L + 303
32.     IF PEEKB(K) <> X THEN GOTO 1001
33.     Y = S + (K - L) \ 4: V = PEEKB(Y)
34.     IF V = 0 THEN GOTO 1001
35.     V = V - 128
36.     IF V = 0 THEN
37.         V = M + 128
38.         IF (SGN(V) = SGN(M)) THEN
39.             V = V + M + 128
40.             V = 0
41.     PEEKB(Y) = V
42. 1001 NEXT
43.
44. 2000
45. W = 0: T = 1
46. FOR K = S TO S + 75
47.     V = PEEKB(K)
48.     IF V THEN T = 0
49.     IF ABS(V - 128) = 4 THEN W = 1
50.
51. 3000
52. FOR K = Q TO Q + 63
53.     PEEKB(K) = 0
54. FOR K = S TO S + 75
55.     N = PEEKB(K) - 128
56.     IF N = -128 THEN GOTO 3002
57.     Z = E(N + 4)
58.     F = L + 4 * (K - S)
59.     FOR J = F TO F + 3
60.         X = PEEKB(J)
61.         IF PEEKB(G + X) <> 128 THEN GOTO 3001
62.         V = PEEKB(Q + X)
63.         IF V >= 254 THEN GOTO 3001
64.         V = V + Z: IF Z >= 254 THEN V = Z
65.         IF V > 255 THEN V = 255
66.         PEEKB(Q + X) = V
67.    3001 NEXT
68. 3002 NEXT
69. V9 = 0
70. FOR K = 0 TO 63
71.     V = PEEKB(Q + K)
72.     IF V > 64 AND V < 128 THEN V = V - 64
73.     IF V > 16 AND V < 32 THEN V = V - 16
74.     IF V > V9 THEN V9 = V
75.     PEEKB(Q + K) = V
76. IF V9 < 32 THEN GOTO 4000
77. 3800 X = 0
78.     IF PEEKB(Q + X) = V9 THEN RETURN
79.     X = X + 1
80. 4000 P4 = 16
81. FOR K = L TO L + 287 STEP 16
82.     p = 0
83.     FOR J = K TO K + 15
84.         p = p + PEEKB(PEEKB(J) + G) - 128
85.     IF p > P4 THEN GOTO 4002
86.     IF p < P4 THEN
87.         P4 = p: V4 = 0: N4 = 0
88.     FOR J = K TO K + 15
89.         X1 = PEEKB(J)
90.         V = PEEKB(Q + X1)
91.         IF V = 0 THEN GOTO 4001
92.         IF V < V4 THEN GOTO 4001
93.         IF V > V4 THEN
94.             V4 = V: N4 = 1
95.             N4 = N4 + 1
96.             IF INT(RND(1) * N4) <> 0 THEN GOTO 4001
97.         X = X1
98.    4001 NEXT
99. 4002 NEXT
100. IF V4 = 0 THEN GOTO 3800
101.
102. 7000
103. p = X \ 16 + 1
104. X = X - 16 * (p - 1)
105. R = X \ 4 + 1
106. C = (X MOD 4) + 1
107. CALL MAKEMOVE(C, R, p, 4)
108.
109.
110. INIT:
111. L = 768
112. FOR K = 0 TO 63
113.     PEEKB(L + K) = K
114. L = L + 64
115. a = 4: B = 16
116. FOR S = 1 TO 4
117.     GOSUB 19000
118. a = 16: B = 1
119. FOR S = 1 TO 13 STEP 4
120.     GOSUB 19000
121. S = 1: a = 5: B = 16: GOSUB 19000
122. S = 13: a = -3: B = 16: GOSUB 19000
123. S = 1: a = 20: B = 1: GOSUB 19000
124. S = 49: a = -12: B = 1: GOSUB 19000
125. S = 1: a = 17: B = 4: GOSUB 19000
126. S = 49: a = -15: B = 4: GOSUB 19000
127. S = 1: D = 21: GOSUB 18000
128. S = 16: D = 11: GOSUB 18000
129. S = 4: D = 19: GOSUB 18000
130. S = 13: D = 13: GOSUB 18000
131. GOSUB DRAWBD
132.
133. 18000
134. FOR K = S TO S + 3 * D STEP D
135.     PEEKB(L) = K - 1: L = L + 1
136.
137. 19000
138. FOR J = S TO S + 3 * B STEP B
139.     FOR K = J TO J + 3 * a STEP a
140.         PEEKB(L) = K - 1: L = L + 1
141.
142. DRAWBD:
143. LINE (0, 0)-(639, 479), 7, BF
144. LINE (23, 23)-(616, 456), 0, B
145. LINE (24, 24)-(615, 455), 14, BF
146. Y = 130: GOSUB GRID
147. Y = 230: GOSUB GRID
148. Y = 330: GOSUB GRID
149. Y = 430: GOSUB GRID
150. PAINT (24, 24), 3, 0
151.
152. GRID:
153. FOR K = 0 TO 4
154.     LINE (120 + 20 * K, Y - 20 * K)-(440 + 20 * K, Y - 20 * K), 0
155.     LINE (120 + 80 * K, Y)-(200 + 80 * K, Y - 80), 0
156.     LINE (117 - K, Y + 2)-(201 - K, Y - 82), 0
157.     LINE (437 + K, Y + 2)-(521 + K, Y - 82), 0
158. FOR K = 0 TO 1
159.     LINE (117 - K, Y + K + 1)-(437 + K, Y + K + 1), 0
160.     LINE (201 - K, Y - 81 - K)-(521 + K, Y - 81 - K), 0
161.
162. SUB GETMOVE (X, Y, Z)
163.     GETPOS:
164.     IF INKEY\$ = CHR\$(27) THEN END
165.     CALL getmouse(XX, YY, ZZ)
166.     Z = (YY - 30) \ 100 + 1
167.     IF Z < 1 OR Z > 4 THEN GOTO GETPOS
168.     Y = ((YY - 30) \ 20) MOD 5
169.     IF Y < 1 OR Y > 4 THEN GOTO GETPOS
170.     IF XX + YY - 150 - 100 * Z < 0 THEN GOTO GETPOS
171.     X = (XX + YY - 150 - 100 * Z) \ 80 + 1
172.     IF X < 1 OR X > 4 THEN GOTO GETPOS
173.     IF ZZ = 0 THEN GOTO GETPOS
174.
175. SUB MAKEMOVE (X, Y, Z, COLOUR)
176.     CIRCLE (80 * X - 20 * Y + 170, 100 * Z + 20 * Y - 60), 35, 8, , , 4 * (8 / 35) / 3
177.     PAINT STEP(0, 0), COLOUR, 8
178.     CIRCLE (80 * X - 20 * Y + 170, 100 * Z + 20 * Y - 60), 15, 8, , , 4 * (3 / 15) / 3
179.     PAINT STEP(0, 0), COLOUR + 8, 8
180.
181. SUB SHOWWIN (C, R, p, COLOUR)
182.     DIM CC(0 TO 3), RR(0 TO 3), PP(0 TO 3)
183.     FOR DC = -1 TO 1
184.         FOR DR = -1 TO 1
185.             FOR DP = -1 TO 1
186.                 IF DC <> 0 OR DR <> 0 OR DP <> 0 THEN
187.                     NDX = 0
188.                     FOR K = -3 TO 3
189.                         IF C + K * DC < 1 OR C + K * DC > 4 THEN GOTO 1
190.                         IF R + K * DR < 1 OR R + K * DR > 4 THEN GOTO 1
191.                         IF p + K * DP < 1 OR p + K * DP > 4 THEN GOTO 1
192.                         ID = POINT(80 * (C + K * DC) - 20 * (R + K * DR) + 170, 100 * (p + K * DP) + 20 * (R + K * DR) - 60)
193.                         IF ID <> COLOUR + 8 THEN EXIT FOR
194.                         CC(NDX) = C + K * DC
195.                         RR(NDX) = R + K * DR
196.                         PP(NDX) = p + K * DP
197.                         NDX = NDX + 1
198.                         IF NDX = 4 THEN GOTO SHOW
199.                    1 NEXT
200.     SHOW:
201.     FOR K = 0 TO 3
202.         CIRCLE (80 * CC(K) - 20 * RR(K) + 170, 100 * PP(K) + 20 * RR(K) - 60), 35, COLOUR + 8, , , 4 * (8 / 35) / 3
203.         PAINT STEP(0, 0), COLOUR + 8
204.         CIRCLE STEP(0, 0), 15, 15, , , 4 * (3 / 15) / 3
205.         PAINT STEP(0, 0), 15
206.
207. SUB getmouse (x%, y%, b%)
208.     b% = 0
209.     wheel% = 0
210.         IF _MOUSEBUTTON(1) THEN b% = b% OR 1
211.         IF _MOUSEBUTTON(2) THEN b% = b% OR 2
212.         IF _MOUSEBUTTON(3) THEN b% = b% OR 4
213.     x% = _MOUSEX
214.     y% = _MOUSEY
215.

« Last Edit: March 06, 2020, 05:13:43 AM by Qwerkey »