Dev - C++ - Snake c++

 
Vista:

Snake c++

Publicado por paahca (1 intervención) el 26/11/2015 05:56:16
Buenas noches señores, resulta que tengo que hacer el juego de la culebrita en c++, y ya tengo todo el programa base, lo unico que me falta es hacer una función especial para hacer la matriz transpuesta (el tablero girado ) e invertir los controles (si es arriba vaya abajo, si es derecha vaya a izquierda) con este ultimo punto es que tengo problemas, por favor ayuda.
Esto es lo que tengo

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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
#include <iostream>
#include <windows.h>
#include <conio.h>
 
using namespace std;
 
int** crear_matriz_int(int n, int m){
   int** X = new int*[n];
   for(int i = 0; i < n; i++){
      X = new int[m];
   };
   return X;
};
 
void liberar_matriz_int(int** X, int n, int m){
    for(int i = 0; i < n; i++){
       delete[] X;
    };
    delete[] X;
    return;
};
 
int** leer_matriz_int(istream& is, int** X, int n, int m){
    for(int i = 0; i < n; i++){
       for(int j = 0; j < m; j++){
           is >> X[j];
       };
    };
    return X;
};
 
ostream& escribir_matriz_int(ostream& os, int** X, int n, int m){
    for(int i = 0; i < n; i++){
        for(int j = 0; j < m; j++){
           os << X[j];
           if(j < m - 1){
              os << "\t";
           };
        };
        os << "\n";
    };
    return os;
};
 
ostream& imprime_matriz_game(ostream& os, int** X, int n, int m){
    os << "\n";
    for(int i = 0; i < n; i++){
        for(int j = 0; j < m; j++){
           if(X[j] == 1){
               os << (char)254;
               if(j < m - 1){
                  os << " ";
               };
           }else if(X[j] == -1){
               os << (char)2;
               if(j < m - 1){
                  os << " ";
               };
           }else if(X[j] == -2){
               os << (char)177;
               if(j < m - 1){
                  os << " ";
               };
           }else{
               os << " ";
               if(j < m - 1){
                  os << " ";
               };
           };
        };
        os << "\n";
    };
    return os;
};
 
 
int** inicializar_matriz(int** X, int n, int m){
    for(int i = 0; i < n; i++){
       for(int j = 0; j < m; j++){
           X[j]=0;
       };
    };
    return X;
};
 
int** poner_rastro(int** X, int n, int m){
    for(int i = 0; i < n; i++){
       for(int j = 0; j < m; j++){
           if(X[j] == 1){
               X[j] = -1;
           };
       };
    };
    return X;
};
 
int** poner_personaje(int** X, int n, int m, int posf, int posc){
    X[posf][posc] = 1;
    return X;
};
 
 
int** poner_obstaculos(int** X, int n, int m){
    X[2][0] = -2;
    X[2][1] = -2;
    X[2][2] = -2;
    X[2][3] = -2;
    X[2][4] = -2;
    X[2][5] = -2;
    X[5][4] = -2;
    X[6][3] = -2;
    X[7][2] = -2;
    X[8][1] = -2;
    X[9][0] = -2;
    return X;
};
 
int calcular_puntaje(int** X, int n, int m){
int puntaje=0;
for(int i = 0; i < n; i++){
      for(int j = 0; j < m; j++){
        if(X[j]==-1){
  puntaje++;
}}}
return puntaje;
};
 
int** transponer_mapa(int** X, int**Y, int n, int m, int posf, int posc){//transponer
 
  for(int i = 0; i < n; i++){
       for(int j = 0; j < m; j++){
           Y[j]=X[j];
       };
       Y[posf][posc]=X[posc][posf];
    };
    return Y;
};
 
 
int main(){
    int n = 10;
    int m = 10;
    int r = 2;
    int k = 500;
    //ifstream ifs("mat.txt");
    int **A = crear_matriz_int(n,m);
    int **B = crear_matriz_int(n,m);
    char c = 0;
    int x = 0;
    int posf = 4;
    int posc = 3;
    int i;
 
    int modo = 0;
 
    do{
        cout << "Bienvenido\n";
        cout << "Digite el modo de juego:\n";
        cout << "1. No Toroidal\n";
        cout << "2. Toroidal\n";
        cin >> modo;
        system("cls"); //limpia pantalla
    }while(modo != 1 && modo != 2);
 
    A = inicializar_matriz(A, n, m);
    A = poner_obstaculos(A,n,m);
 
    A = poner_personaje(A, n, m, posf, posc);
    i = 750;
 
    while((c != 'p')){
       cout << "Presione 'p' para salir\n";
       cout << "fil:" << posf;
       cout << ", col:" << posc;
       cout << ", puntaje:" << calcular_puntaje(A, n, m);
 
       if(c == 't'){ //aqui se transpone el mapa, me falta cambiar los controles
            A = transponer_mapa(A,B,n, m,posf,posc);
            c = 0;
       };
 
       A = poner_rastro(A, n, m);
       A = poner_personaje(A, n, m, posf, posc);
       imprime_matriz_game(cout, A, n, m);
 
 
       Beep( i, k );
       k -= 10;
 
       if(k < 10){
        k = 10;
       };
 
       switch(c){
         case 72:
             posf --;
             if(posf < 0){
                 if(modo == 1){
                    posf = 0;
                 }else{
                posf=n-1;
                 };
             };
             break;
         case 80:
             posf ++;
             break;
         case 75:
             posc --;
             if(posc < 0){
                if(modo == 1){
                    posc = n;
                }else{
                  posc=m-1;
                };
             };
             break;
         case 77:
             posc ++;
             break;
       };
 
       if(modo == 1){
          if(posf == n){
            posf = n-1;
          };
 
          if(posc == m){
            posc = m-1;
          };
       }else{
      if(posf == n){
            posf = 0;
          };
 
          if(posc == m){
            posc = 0;
          };
 
       };
 
       if(A[posf][posc] == -1 || A[posf][posc] == -2){
           cout << "muere!";
           Beep( 900 , 1000 );
           break;
       };
 
       if(kbhit()){
          c = getch();
       };
       system("cls");
     };
 
     liberar_matriz_int(A, n, m);
 
     if (c == 'p'){
          system("cls");
          cout<< "       Gracias por jugar         \n";
          cout<< "             :D ";
          cout<< "\n";
     };
     system("pause");
     return 0;
};
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder