Error on Sokoban code

Hey guys,

I'm developing a similar Sokoban code, for a beginner programmer.
But, some error is occuring when the "gamer" put the object in front of the wall and the gamer take the position of the object.
This error only appears in the 's' and 'd' directions.

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
  ///Esqueleto do SOKOBAN no LIA
#include <iostream>
#include<windows.h>
#include<conio.h>

#define L 10
#define C 20

using namespace std;


int main()
{

    ///ALERTA: NÃO MODIFICAR O TRECHO DE CÓDIGO, A SEGUIR.
    //INICIO: COMANDOS PARA QUE O CURSOR NÃO FIQUE PISCANDO NA TELA
    HANDLE out = GetStdHandle(STD_OUTPUT_HANDLE);
    CONSOLE_CURSOR_INFO     cursorInfo;
    GetConsoleCursorInfo(out, &cursorInfo);
    cursorInfo.bVisible = false; // set the cursor visibility
    SetConsoleCursorInfo(out, &cursorInfo);
    //FIM: COMANDOS PARA QUE O CURSOR NÃO FIQUE PISCANDO NA TELA
    //INÍCIO: COMANDOS PARA REPOSICIONAR O CURSOR NO INÍCIO DA TELA
    short int CX=0, CY=0;
    COORD coord;
    coord.X = CX;
    coord.Y = CY;
    //FIM: COMANDOS PARA REPOSICIONAR O CURSOR NO INÍCIO DA TELA
    ///ALERTA: NÃO MODIFICAR O TRECHO DE CÓDIGO, ACIMA.


    //Posição inicial do personagem no console
    int x=3, y=3;
    //Variável para tecla precionada
    char tecla;
    //Coordenadas para impressão na tela
    int i,j;
    //Posição inicial do objeto no console
    int cx = 7, cy = 7;
    //Posição do objetivo
    int fx = 3, fy = 15;

    while(1)
    {
        //Imprime o jogo: mapa e personagem.
        i=0;
        while (i<L)
        {
            j=0;
            while (j<C)
            {
                if (x==i and y==j)
                {
                    cout<<"*";
                }
                else if (i==0 or i==L-1 or j==0 or j==C-1)
                {
                    cout<<"°";
                }
                else if (cx==i and cy==j)
                {
                    cout<<"@";
                }
                else if (fx==i and fy==j)
                {
                    cout<<"+";
                }
                else
                {
                    cout<<" ";
                }
                j++;
            }
            cout<<"\n";
            i++;
        }

        //Função para encerrar o jogo, caso a caixa chegue no objetivo
        if (cx == fx and cy == fy)
        {
            setlocale(LC_ALL, "Portuguese");

            cout<<"\n\nVocê venceu!!!";
            return 0;
        }

        if ( _kbhit() )
        {
            tecla = getch();
            switch(tecla)
            {
                case 'w':
                    //função para impedir o personagem de atravessar a parede e a caixa
                    if (x != 1 and cx != (x == cx+1 and y == cy))
                    {
                        //função para o personagem empurrar a caixa e impedir a caixa de atravessar a parede
                        if ((x == cx+1 and y == cy) and cx != 1 )
                        {
                            cx--;

                        }
                        x--;
                    }
                    break; ///cima

                case 's':
                    if (x != 8 and cx != (x == cx-1 and y == cy))
                    {
                        if ((x == cx-1 and y == cy) and cx != 8)
                        {
                            cx++;
                        }
                        x++;
                    }
                    break; ///baixo

                case 'a':
                    if (y != 1 and cy != (y == cy+1 and x == cx))
                    {
                        if ((y == cy+1 and x == cx) and cy != 1)
                        {
                            cy--;
                        }
                        y--;
                    }
                    break; ///direita

                case 'd':
                    if (y != C-2 and cy != (y == cy-1 and x == cx))
                    {
                        if ((y == cy-1 and x == cx) and cy != C-2)
                        {
                            cy++;
                        }
                        y++;
                    }
                    break; ///esquerda
            }
        }
        /*
            Recoloca o mapa no inicio da tela,
            isso faz com que o jogo seja escrito sempre no mesmo lugar.
            se remover essa linha ele fica imprimindo sempre embaixo do mapa anterior
        */

        SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
    }
}



Please, help me to find a solution!
What exactly do you think this is testing?

 
if (x != 8 and cx != (x == cx-1 and y == cy))

It compares cx to the value of the parenthesized boolean expression (which will be 0 or 1 depending on whether it is false or true). Is that really what you want to do?
Last edited on
Topic archived. No new replies allowed.