void Tablero::IniciaTablero(int filas, int columnas) { nfils = filas; ncols = columnas; int i, contador, j; matriz = new int* [nfils]; for(i=0;i<nfils;i++) { matriz[i] = new int[ncols]; } contador = 0; for(i=0;i<nfils;i++) { for(j=0;j<ncols;j++) { matriz[i][j] = contador; contador++; } cout<<endl; }cout<<endl; }
void Tablero::DibujaTablero() { int i, j; system("cls"); cout<<"Juego del puzzle deslizante";
for(i = 0; i < nfils; i ++ ) { for(j = 0; j < ncols; j ++ ) { cout << " --"; } cout << endl; for(j = 0; j < ncols; j ++ ) { cout << "|"; cout.width(2); if ( matriz[i][j] != 0 ) cout << matriz[i][j]; else cout << " "; } cout << "|" << endl; } for(j = 0; j < ncols; j ++ ) { cout << " --"; } cout << endl << endl; }
void Tablero::BuscaFicha(int movida1) { int i,j; movida = movida1; for(i=0;i<nfils;i++) { for(j=0;j<ncols;j++) { if(matriz[i][j] == movida) { pos_i = i; pos_j = j; } } } }
void Tablero::MueveFicha(int movida1) { int i,j,x,y,pos_x,pos_y; movida = movida1; for(x=0;x<nfils;x++) { for(y=0;y<ncols;y++) { if(matriz[x][y] == 0) { pos_x = x; pos_y = y; } } } if ((pos_i == pos_x) && ((pos_j+1 == pos_y) || (pos_j-1 == pos_y))) { for(i=0;i<nfils;i++) { for(j=0;j<ncols;j++) { if(matriz[i][j] == 0) { matriz[i][j] = movida; matriz[pos_i][pos_j] = 0; } } } } if ((pos_j == pos_y) && ((pos_i+1 == pos_x) || (pos_i-1 == pos_x))) { for(i=0;i<nfils;i++) { for(j=0;j<ncols;j++) { if(matriz[i][j] == 0) { matriz[i][j] = movida; matriz[pos_i][pos_j] = 0; } } } } }
bool Tablero::Finalizado() { int i ,j; final = false; int a = 0, b = 1; for (i=0 ; i<nfils ; i++) { for(j=0 ; j<ncols ; j++) { if (matriz[i][j] == b) { a++; } b++; } } if (a == b-2) { final = true; cout<<"**********************************"<<endl; cout<<"**** ****"<<endl; cout<<"**** HAS COMPLETADO EL PUZZLE ****"<<endl; cout<<"**** ****"<<endl; cout<<"**********************************"<<endl; } return final; }
Muchas gracias de antemano. Saludos. |