O Jogo do QUINZE - Problemas com HBMK2 (e solução)

Foto de Major Anilto

Categoria: 

Às vezes o HBMK2 reclama da falta de alguma biblioteca e dá uma dica, como, por exemplo: (quinze_2.pdf)

Veja a dica:

1
hbmk2: Dica: Adicionar opção 'hbnf.hbc' faltando nas funções: ft_Rand1()

Neste caso, o comum é adicionar o arquivo faltante em um arquivo de projeto. Por exemplo, o programa abaixo eu compilei
usando 'HBMK2 QUINZE.PRG' e indicou a falta de uma função, sugerindo acrescentar o arquivo 'hbnf.hbc' à compilação.
Para resolver isso criei um arquivo 'quinze.hbp' (aquivo de projeto) com o seguinte conteúdo e compilei com 'hbmk2 quinze.hbp':

1
2
hbnf.hbc
quinze.prg

Com isso o HBMK2 encontra as funções faltantes e compila corretamente.

(quinze.pdf)

Programa quinze.prg (é um jogo -sliding puzzle game- onde você tem que colocar as peças na posição correta):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#include "inkey.ch"
#include "box.ch"
 
 
procedure Main()
    // console init
    SET SCOREBOARD OFF
    SetMode(30,120)
    ret := 0
 
    // main loop
    yn := .T. 
    DO WHILE yn == .T.
        // draw console
        cls
        @ 0, 0 TO MaxRow(), MaxCol() DOUBLE
        SetColor("BG+/B,W/N")
        @ 0, 4 SAY "  Sliding puzzle game  "
        SetColor()
 
        // input size of grid
        tam := 0          
        @ MaxRow() - 2, 4 SAY "Tamanho do grid: " GET tam PICTURE "9"
        READ
 
        // Initialize numbers
        lista := ARRAY (tam * tam)
        FOR z := 1 TO tam * tam
            lista[z] := z
        NEXT
        lista1 := lista
        grid := ARRAY (tam,tam)
 
        // populate grid with numbers
        FOR i := 1 TO tam
            FOR j := 1 TO tam
                grid[i,j] := lista1[ (i-1) * tam + j]
            NEXT
        NEXT
        Mostra(@grid)
        InKey(0)
 
        // initialize the game
        n := 0
        t := 0
        lista := lista1     // lista for scrambling, lista1 preserve numbers in order
        DO WHILE .T.
            // scrambling numbers
            FOR i := 1 TO tam*tam
                n := Int ( ft_Rand1(tam * tam - 1) + 1 )
                t := lista[n]
                lista[n] := lista[i]
                lista[i] := t
            NEXT
            // have solution?
            possp := 0
            invct := 0  // change counter
            FOR i := 1 TO tam * tam -1
                IF lista[i] != tam*tam
                    FOR j := i + 1 TO tam * tam
                        IF lista[j] != tam*tam
                            IF lista[i] > lista[j]
                                invct++
                            ENDIF
                        ENDIF
                    NEXT
                ELSE
                    possp := i
                ENDIF
            NEXT
            linv := If( ( (invct % 2) == 0 ), .T., .F.)
            lkin := If( ( (tam - Int( (possp -1) / tam )) % 2) == 0, .T., .F. )
             
            IF ( (tam % 2) != 0)    // if grid size is odd
                IF linv                 // if number of positions changes is even, solvable
                    EXIT
                ELSE
                    LOOP                // if is odd, not solvable, scramble more
                ENDIF               // if grid size is even
            ELSE                   
                                        // If changes is even and space position is in odd line
                                        // or changes is odd and space position is in even line
                                        // (XOR condition) is solvable
                IF (linv .AND. !lkin) .OR. (!linv .AND. lkin) // XOR !!!
                    EXIT
                ElSE                    // else scramble more
                    LOOP   
                ENDIF
            ENDIF
 
        ENDDO
 
        // populate the grid with scrambled numbers
        FOR i := 1 TO tam
            FOR j := 1 TO tam
                grid[i,j] := lista[ (i-1) * tam + j]
            NEXT
        NEXT
        ret := Mostra(@grid)
 
    // play
        key := 0
        DO WHILE LastKey() != K_ESC
            key := 0
            // find the space coords
            xe := 0
            ye := 0
            lv := tam*tam
            FOR i := 1 TO tam
                FOR j := 1 TO tam
                    IF grid[i,j] == lv
                        xe :=i
                        ye :=j
                    ENDIF
                NEXT
            NEXT
            // the direction keys
            key := inkey(0)
            DO CASE
                CASE key == K_UP
                    IF xe > 1
                        grid[xe,ye] := grid[xe-1,ye]
                        grid[xe-1,ye] := lv
                    ENDIF
                    ret := Mostra(@grid)           
                CASE key == K_DOWN
                    IF xe < tam
                        grid[xe,ye] := grid[xe+1,ye]
                        grid[xe+1,ye] := lv
                    ENDIF
                    ret := Mostra(@grid)  
                CASE key == K_LEFT
                    IF ye > 1
                        grid[xe,ye] := grid[xe,ye-1]
                        grid[xe,ye-1] := lv
                    ENDIF
                    ret := Mostra(@grid)  
                CASE key == K_RIGHT
                    IF ye < tam
                        grid[xe,ye] := grid[xe,ye+1]
                        grid[xe,ye+1] := lv
                    ENDIF
                    ret := Mostra(@grid)  
            ENDCASE 
            IF ret == tam*tam-1                             // ret is qtty numbers in position
                @ MaxRow() - 3, 4 SAY "Solucionado!"        // if ret == (size*size) -1
                key := K_ESC                                // all numbers in position
                EXIT                                        // game solved
            ENDIF
        ENDDO
        @ MaxRow() - 2, 4 SAY "Novo jogo??? (yn): " GET yn PICTURE "Y"
        READ
        cls
        @ MaxRow() - 3, 4 SAY "                  "
    ENDDO
return NIL
 
FUNCTION Mostra(grid)
    // Show the gris
    fim := 0                                                    // how many numbers in position?
    SetColor("BG+/B,W/N")
    @ 5,10 , 5 + tam * 2, 9 + tam * 4 BOX B_SINGLE + Space(1)
    i := 0
    FOR y := 1 TO tam
        FOR x := 1 TO tam
            IF grid[x,y] == tam * tam                           // show space
                SetColor(" B/GR+, W/N")
                @ x*2 + 4, i + 11 SAY "  "
                SetColor("BG+/B,W/N")
            ELSE
                IF ( (x-1) * tam + y ) == grid[x,y]             // show number in position
                    SetColor("W/G,W/N")
                    @ x*2 + 4, i + 11 SAY grid[x,y] PICTURE "99"
                    fim++
                ELSE                                            // show number out position
                    SetColor("BG+/B,W/N")
                    @ x*2 + 4, i + 11 SAY grid[x,y] PICTURE "99"
                ENDIF
            ENDIF
        NEXT
        i = i + 4
    NEXT
    SetColor(" W/N, BG+/B")
RETURN fim

Downloads: 

Programa "Jogo do quinze" — Baixado 1202 vezes
erro_hbmk2 — Baixado 443 vezes
tela_do_jogo — Baixado 408 vezes
Total votes: 0

Comentários

Páginas