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:


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':


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):


#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 1107 vezes
erro_hbmk2 — Baixado 353 vezes
tela_do_jogo — Baixado 338 vezes
Total votes: 0

Comentários

Páginas