' EnableMouse 1 = Turn cursor on, return coordinates
' EnableMouse 0 = Turn cursor off in order to draw stuff, etc.
' GetUserSignal will set return cC or will return Click
' ----------------------------------------------------------
' Title Screen (Main Program)
' -----------------------------------------------------------
SCREEN 12 ' modalita schermo / screen mode RANDOMIZE TIMER ' inizializza generatore di numeri casuali / it inizializes maker of random numbers ' variabili generali non visibili in SUB per cui usa GOSUB
' general variables non seen in SUB so He uses GOSUB
GOSUB InitializeScreen
' schermo iniziale con pulsanti / initial screen with buttons
DO ' ciclo principale infinito / main loop infinite DO: GetUserSignal:
LOOP UNTIL click
= 1 ' attende il click/ it waits for click GOSUB FindClickedCommand
' decodifica il click / it decodes click CASE 1:
' primo pulsante / first button Hard = False
WhoWon = 0
CASE 2:
' secondo pulsante / second button Hard = True
WhoWon = 0
CASE 3:
' terzo pulsante /third button CASE 4:
'forth button / quarto pulsante EXIT DO ' esce dal ciclo/ it exits from loop COLOR 7:
CLS ' ripristina i caratteri bianchi e pulisce lo schermo/ it restores white character and clear screen SYSTEM ' termina liberando le risorse / it stops releasing resources
' ----------------------------------------------------------
' Game Screen
' -----------------------------------------------------------
FindClickedPosition: ' cerca la posizione del mouse/ it finds position of mouse
theRow = 0: theColumn = 0: theBox = 0
SELECT CASE cH
' posizione orizzontale / horizontal position CASE IS < 269 - Delta: theColumn
= 1 ' tra 174 e 265 colonna 1 / between 174 and 265 column 1 CASE IS < 368 - Delta: theColumn
= 2 ' tra 274 e 364 colonna 2 / between 274 and 364 column 2 CASE IS < 467 - Delta: theColumn
= 3 ' tra 372 e 463 colonna 3/ between 372 and 463 column 3 SELECT CASE cV
' posizione verticale / vertical position CASE IS < 190 - Delta: theRow
= 1 ' tra 95 e 186 riga 1/ between 95 and 186 row 1 CASE IS < 289 - Delta: theRow
= 2 'tra 194 e 285 riga 2/between 194 3 285 row 2 CASE IS < 388 - Delta: theRow
= 3 ' tra 294 e 384 riga 3 / between 294 e 384 row 3 theBox = (3 * (theRow - 1)) + theColumn ' calcola la cella/ it calculates cell
' nota che si possono invertire TheRow e TheColumn nella formula
' see that it can invert TheRow and TheColumn in formula
' ----------------------------------------------------------
' Play Game
' -----------------------------------------------------------
PlayGame: ' schermata di gioco / screen of game
DrawSCREEN 'draw the screen and create X and O symbols.
FOR i
= 1 TO 9: zO
(i
) = False: zX
(i
) = False: zE
(i
) = True:
NEXT i
' inizializza i 3 arrays Empty a vero X e 0 a falso
' it inizializes the three arrays Empty to true X and 0 to false
MovesMade = 0 ' mosse fatte = 0 / moves made = 0
GetUserSignal 'aspetta azione utente/ it waits action of user
MadeAMove = False ' flag fatta una mossa / flag made a move
GOSUB MakeX
' fa la X / if it maked X IF MadeAMove
THEN ' se Š fatta la mossa / if it made a move 'WhoWon flag per cercare il vincitore / WhoWon is flag to seek for winner
t% = 0
' se ci sono 5 X allora ci saranno 4 0 e sar… pari
' if there is 5 X then there shoude be 4 0s and it is tie
MovesMade = MovesMade + 1 ' conta mosse/ it counts moves
'WhoWon flag per cercare il vincitore / WhoWon is flag to seek for winner
IF cC
= "d" OR cC
= CHR$(27) THEN WhoWon
= 3 ' utente vuole uscire / users wants to exit IF WhoWon
> 0 THEN RETURN ' se qualcuno vince ritorna al ciclo principale / if someone wins it returns to main loop
MakeX:
GOSUB FindClickedPosition
' decodifica il click/ it decodes click IF theBox
= 0 THEN RETURN ' esce se non ha scelto / t exits if it doesn't choose IF NOT zE
(theBox
) THEN RETURN ' esce se la cella non Š vuota/ it exits if cell is not void xo theRow, theColumn, 1: ' Places an X
zX(theBox) = True: zE(theBox) = False ' aggiorna array X e Empty / it refreshes arrays X and Empty
MadeAMove = True ' flag fatta la mossa
MakeO:
GOSUB FindPlaceForO
' modulo IA per 0/ modulo AI for 0 xo theRow, theColumn, 0: 'Places an 0 /mette uno 0
zO(theBox) = True: zE(theBox) = False ' imposta gli array / it sets arrays
ComputeWin: ' PC usando 0 vince / PC using 0 wins
IF WhoWon
= 1 THEN ' calcola se ha vinto la X/ it calculates if X wins ELSE ' calcola se ha vinto lo 0/ it calculates if 0 wins WhoWon = 0 ' nessuno vince/ nobody wins
FindPlaceForO: ' qui c'Š il problema ovvero opera incompiuta / here is problem otherwise work unfinished
' See if there is a win for O. If so, take it.
' See if there is a threat of a win for X. If so, block it.
' cerca se c'Š una posizione che fa vincere 0 in tal caso la sceglie
' cerca se c'Š una posizione che fa vincere X in tal caso le blocca
theBox = 0 ' nessuna cella scelta /no cell choosen
' per tutte le righe e per tutte le colonne
' for all rows and all columns
theBox = theBox + 1 ' aumenta il numero di cella /it increases number of cell
IF zE
(theBox
) THEN ' se Š vuota / if it is void tk$ = ""
SELECT CASE theBox
' sceglie le celle da controllare/ it chooses cells to control ' controlla le celle / it controls cells
' prende due celle successive / it takes two cells successive
' cerca 2 celle con 0 se si, fa tris / it seeks for 2 cells with 0 if yes it will make tris
' cerca 2 celle con X se si blocca il tris di X / it searches 2 cells with X if yes it will block tris of X
' No move selected above to win or block win, so / nessuna mossa per vincere o bloccare X quindi
IF Hard
THEN ' modalit… difficile / hard mode IF MovesMade
= 1 THEN ' fatta una sola mossa/ it has made one move IF zE
(5) THEN ' se la cella centrale Š vuota la prende/ if central cell is empty it takes theRow = 2: theColumn = 2: theBox = 5
IF RND > .5 THEN theRow
= 1 ELSE theRow
= 3 ' sceglie a caso riga superiore o inferiore/ it chooses at random upper row or lower row IF RND > .5 THEN theColumn
= 1 ELSE theColumn
= 3 ' sceglia a caso colonna 1 o 3/ it choosed at random column 1 or 3 theBox = (3 * (theRow - 1)) + theColumn ' calcola uno degli angoli della griglia/ it calculates one of angles of grid
' anche qui TheRow e TheColumn sono intercambiabili
' also here TheRow e TheColumn are interchangeable
ELSEIF MovesMade
= 2 THEN 'se X ha fatto 2 mosse / se X has made two moves IF zX
(5) THEN ' se X sta al centro / if X is at centre cell 'se due angoli sono presi esamina gli altri due
' if 2 angles are taken it examines the other two angles
tk$ = ""
IF zO
(1) AND zX
(9) THEN ' se angoli 1 e 9 sono presi/ if angles 1 and 9 are taken tk$ = "37"
ELSEIF zO
(3) AND zX
(7) THEN ' se angoli 3 e 7 sono presi/ if angles 3 and 7 are taken tk$ = "19"
ELSEIF zO
(7) AND zX
(3) THEN ' se angoli 3 e 7 sono presi/ if angles 3 and 7 are taken tk$ = "19"
ELSEIF zO
(9) AND zX
(1) THEN ' se angoli 1 e 9 sono presi/ if angles 1 and 9 are taken tk$ = "37"
IF tk$
<> "" THEN 'se ci sono celle da esaminare/ if there are cells to examine ' sceglie a caso tra la prima cella della riga e la prima cella della riga???
'forse ci vuole RIGHT$ nell'ELSE
' it chooses at random between first cell of row and first cell of row! ! ??
' maybe here is right RIGHT$ in ELSE
theRow = (theBox + 2) \ 3 ' ottiene come risultato 1 o 3 / it gets as results 1 or 3
theColumn = theBox - (3 * (theRow - 1)) ' ottiene come risultato 1 o 3/ it gets as results 1 or 3
' le celle da scegliere sono 1 (1,1) 3 (1,3) 7(3,1) 9(3,3) Cella(TheRow,TheColumn)
' the cells to choose are 1(1,1) 3 (1,3) 7 (3,1) 9 (3,3) Cell(TheRow,TheColumn)
'se la X non sta al centro / if X is not at centre
escape% = 0 ' variabile per uscire dal loop infinito / variable to exit from infinite loop
theBox
= 2 * FIX(1 + (RND * 4)):
' genera a caso un numero da 1 a 10 /it makes a rnd_number from 1 to 10
' il 10 capita all'incirca ogni 20 secondi e crea un errore in zE(TheBox)/ the 10 arrives about each 20 seconds and it makes error in zE(TheBox)
'cos ci vuole un controllo forte ad esempio un if TheBox = 10 then TheBox = MaxPossibleNotAngle (8)/so it need a strong control for example if TheBox = 10 then TheBox = MaxPossibleNotAngle (8)
' codice di verifica/ degub code
' LOCATE 28, 2: PRINT "theBox "; theBox, "zE(theBox) "; zE(theBox), "NOT zE(theBox) "; NOT zE(theBox)
CASE 2:
IF NOT zX
(8) THEN EXIT DO ELSE escape%
= escape%
+ 1 'prende la casella 2 se la casella 8 Š vuota/ it chooses cell 2 if cell 8 is empty CASE 4:
IF NOT zX
(6) THEN EXIT DO ELSE escape%
= escape%
+ 1 'prende la casella 4 se la casella 6 Š vuota/ it chooses cell 4 if cell 6 is empty CASE 6:
IF NOT zX
(4) THEN EXIT DO ELSE escape%
= escape%
+ 1 'prende la casella 6 se la casella 4 Š vuota/ it chooses cell 6 if cell 4 is empty CASE 8:
IF NOT zX
(2) THEN EXIT DO ELSE escape%
= escape%
+ 1 'prende la casella 8 se la casella 2 Š vuota/ it chooses cell 8 if cell 2 is empty ' 1 perchŠ non sceglie un angolo? / why doesn't it choose an angle?
'2 cosa fa se la casella opposta Š occupata?/ what does it, if the opposite cell is plenty?
' qui manca del codice per uscire dal loop/ here there is no code to exit loop
LOOP UNTIL (((theBox
+ 1) MOD 2) OR escape%
= 16) '4 * 4 = 16 , in 16 loop tester… almeno una volta le 4 condizioni/ 4*4 = 16 in 16 loops it will test at least a time each of 4 conditions theRow = (theBox + 2) \ 3 ' calcola 1,2,2,3 rispettivamente a 2,4,6,8/ it calculates 1,2,2,3 for respectively 2,4,6,8
theColumn = theBox - (3 * (theRow - 1)) ' calcola 2,1,3,2 seguendo i valori superiori/ it calculates 2,1,3,2 following the above values
' OK, no good move was found. Make a random one
' nessuna buona mossa da fare ne fa una a caso
theRow = (theBox + 2) \ 3
theColumn = theBox - (3 * (theRow - 1))
Shuffle:
w1$
= w1$
+ MID$(w2$
, r%
, 1)
ShowWhoWon: ' mostra vincitore/ it shows winner
SELECT CASE WhoWon
' 4 casi Pari , Vinci, Perdi, Abbandoni / 4 cases Tie, win, lose, resigne CASE 2: c$
= "YOU LOSE! " CASE 3: c$
= "YOU RESIGNED?"
InitializeScreen: ' crea la schermata iniziale del gioco / it creates initial screen game
LOCATE 4, 23:
PRINT "TIC TAC TOE by Paul Meyer & TheBOB" LOCATE 5, 23:
PRINT "Debugged and improved IA by TempodiBasic" ds% = 131: dd% = 97: dz% = 75 ' i 4 pulsanti / 4 buttons
LINE (ds%
, 343)-(ds%
+ dz%
, 380), , BF
LINE (ds%
+ (1 * dd%
), 343)-(ds%
+ (1 * dd%
) + dz%
, 380), , BF
LINE (ds%
+ (2 * dd%
), 343)-(ds%
+ (2 * dd%
) + dz%
, 380), , BF
LINE (ds%
+ (3 * dd%
), 343)-(ds%
+ (3 * dd%
) + dz%
, 380), , BF
LOCATE 23, 19:
PRINT " Easy ";
' 4 comandi per pulsanti / 4 command for buttons
FindClickedCommand: ' da un significato al click / it gives a signify to click
Command = 0
SELECT CASE cV
' click in posizione verticale fuori oggetti / click in vertical position out of objects SELECT CASE cH
' click in posizione orizzontale / click in horizontal position CASE IS < 205: Command
= 1 ' tra 130 e 204 /between 130 and 204 CASE IS < 303: Command
= 2 'tra 227 e 302 / between 227 e 302 CASE IS < 400: Command
= 3 ' tra 325 e 399/ between 325 e 399 CASE IS < 497: Command
= 4 ' tra 421 e 496/ between 421 and 496
DoHelp: ' subroutine vecchio stile con gosub_return/ soubroutine old style by gosub_return
' mostra info / it shows info
PRINT "This game was created by Paul Meyer in the year 2007." PRINT:
PRINT "Improved mouse driver, modularity, machine play-to-win";
PRINT " Debugging and improve IA by TempodiBasic" PRINT "http://www.network54.com/Forum/190883/message/1175106480" PRINT "following QB64 package for years, debugged in 09 07 2016" PRINT "This is freeware, you may change this as much as you want" PRINT "as long as you don't claim it as yours." PRINT "This is just a simple TIC TAC TOE game with mouse drivers." PRINT "This game was created in QuickBasic." CALL GetUserSignal
'aspetta un click o tasto premuto /it waits for a click o key_pressed
SUB DrawSCREEN
' crea schermata gioco / it draws screen of game ' scrive direttamente codice esadecimale alla RAM video
' it writes directly Hex code into RAM of video
LINE (170, 90)-(490, 410), 0, BF
LINE (160, 81)-(479, 399), 1, BF
LINE (155, 76)-(483, 404), 8, B
LINE (152, 73)-(487, 407), 8, B
LINE (160, 81)-(160, 399), 9 LINE (160, 81)-(479, 81), 9 LINE (371, 92)-(372, 393), 0, B
LINE (271, 92)-(272, 392), 0, B
LINE (171, 191)-(472, 192), 0, B
LINE (171, 291)-(472, 292), 0, B
LINE (369, 90)-(370, 390), 13, B
LINE (269, 90)-(270, 390), 13, B
LINE (169, 189)-(470, 190), 13, B
LINE (169, 289)-(470, 290), 13, B
LINE (5, 5)-(634, 474), 8, B
LINE (10, 10)-(629, 469), 8, B
Finished = True ' la prima volta imposta finished = vero / first time it sets finished = true
' e poi genera i simboli 0 e X e li mette in array per immagini alle posizioni 0 e 3000
' and the it makes symbols 0 and X and it puts them in array for images at positions 0 and 3000
DRAW "E3 F30 E30 F6 G30 F30 G6 H30 G30 H6 E30 H30 E3 BF2 P0,0" DRAW "E3 F30 E30 F6 G30 F30 G6 H30 G30 H6 E30 H30 E3 BF2 P10,10" GET STEP(-40, -40)-STEP(81, 81), SymbolBOX
() ' mette 0 in posizione 0 / it puts 0 in position 0 GET (179, 98)-(260, 178), SymbolBOX
(3000) 'mette X in posizione 3000 / it puts X in position 3000 xo 1, 1, 2: xo 1, 2, 2 ' cancella i simboli dalle celle / it cancels symbols from cells
' status indica lo stato del mouse 0 spento 1 acceso / status shows 0 mouse off 1 mouse on
' c% Š la funzione da eseguire 0 spegnere 1 accendere 3 rilevare/ C% is function to act 0 turn off 3 detect
IF Mx
= "" THEN 'inizializza Mx / it inizializes Mx m$ = "58E85080585080585080850815510C358508058508085080850815C00"
n$ = "595BECB70BEAB70BE8BFBE6B7B8E7D33BEC978BEA97BE89FBE697DA80"
Mx
= SPACE$(57) ' Mx Š di 57 bytes / Mx is of 57 bytes ' virtuosismo che carica codici esadecimali in Mx suddivisi nelle due stringhe
' virtuosity it loads Hexadecimal codexes in Mx broke down them in two strings
IF c%
= 0 THEN ' se deve spegnerlo/ if it must turn off 'usa interrupt scritto in Mx per il mouse tramite call absolute
' it uses interrupt written in Mx for mouse by call absolute
Status = 0
' se Š spento il mouse lo accende / if mouse is off, it turn on
'usa interrupt scritto in Mx per il mouse tramite call absolute
' it uses interrupt written in Mx for mouse by call absolute
Status = 1
' rileva il mouse/ it detects mouse
'usa interrupt scritto in Mx per il mouse tramite call absolute
' it uses interrupt written in Mx for mouse by call absolute
' la variabile click Š True se l'utente ha cliccato
'variable click is True if user have made click
' prende input / get input
IF 0 THEN ' Set to 1 for Debugging printout, otherwise 0 PRINT cH;
"ch (Horizontal)" PRINT cV;
"cv (Verticle)" EnableMouse 1 ' attiva mouse / it enables mouse
' se c'Š click la conservo in K% e aspetto che il tasto sia rilasciato
' if there is click I store it in k% and I wait until button is released
k% = click
click = k%
EXIT DO ' esce dal loop esterno / it exits from outer loop cC
= INKEY$ ' utente pu• uscire premendo un tasto / user can exit pressing a keyEnableMouse 0 ' spegne il mouse / it turns off mouse
' verifica se O ha vinto / it verifies if O won
IF zO
(b1
) = 0 OR zO
(b2
) = 0 OR zO
(b3
) = 0 THEN EXIT FUNCTION ' se una celle Š vuota esce / if one of these cells is void it exits Winner l ' evidenzia graficamente il tris / it marks up graphically tris
OWin% = -1
SUB Winner
(Lineup
AS INTEGER) 'evidenzia il tris con una linea/ it shows tris by a line CASE 1:
LINE (200, 140)-(440, 142), 14, BF:
LINE (200, 143)-(440, 144), 0, B
CASE 2:
LINE (200, 240)-(440, 242), 14, BF:
LINE (200, 243)-(440, 244), 0, B
CASE 3:
LINE (200, 340)-(440, 342), 14, BF:
LINE (200, 343)-(440, 344), 0, B
CASE 4:
LINE (220, 120)-(222, 360), 14, BF:
LINE (223, 120)-(223, 360), 0 CASE 5:
LINE (320, 120)-(322, 360), 14, BF:
LINE (323, 120)-(323, 360), 0 CASE 6:
LINE (420, 120)-(422, 360), 14, BF:
LINE (423, 120)-(423, 360), 0 CASE 7:
PSET (200, 120), 14:
DRAW "F240 d H240 d F240 d H240 d C0 F240 d H240" CASE 8:
PSET (440, 120), 14:
DRAW "G240 d E240 d G240 d E240 d C0 G240 d E240"
' disegna la cella con simbolo se symbol Š 0 o 1 o la cella vuota se symbol Š 2
' it draws cell with symbol if symbol is 0 or 1 or cell void if symbol is 2
x = (Col - 1) * 100 + 180
y = (Row - 1) * 100 + 100
Index = symbol * 3000
LINE (x
, y
)-(x
+ 80, y
+ 80), 1, BF
'verifica che X ha fatto tris / it verifies if X have got tris
IF zX
(b1
) = 0 OR zX
(b2
) = 0 OR zX
(b3
) = 0 THEN EXIT FUNCTION ' se una delle celle Š vuota esce/ if one of these cells is voit it exits Winner l ' evidenzia grafica del tris / it marks up graphically the tris
XWin% = -1