You are on page 1of 4

#include <iostream> using namespace std; #define TAM 8 //funciones genericas y de prueba void initmatriz(int matriz[][TAM]); void creatablero(int

tablero[][TAM]); void pintatablero(int tablero[][TAM], int posx, int posy); //funciones del programa int amenazadas(int tablero[][TAM], int posx, int posy); void listaalcanzables(int tablero[][TAM], int posx, int posy); //funciones de busqueda direccional void verticalsup(int tablero[][TAM], int posx, int posy, int &total); void verticalinf(int tablero[][TAM], int posx, int posy, int &total); void horizizq(int tablero[][TAM], int posx, int posy, int &total); void horizder(int tablero[][TAM], int posx, int posy, int &total); int main() { int tablero[TAM][TAM]; int posx, posy; initmatriz(tablero); cout<<"Programa de prueba para el ajedrez."<<endl; creatablero(tablero); cout<<"Introduce la posicion de la torre (entre 1 y "<<TAM<<"):"<<endl; cout<<"x: "; cin>>posx; cout<<"y: "; cin>>posy; while (posx < 1 || posx > TAM+1 || posy < 1 || posy > TAM+1 || tablero[posx1][posy-1] == 1) { cout<<"Posicion incorrecta u ocupada. Pruebe de nuevo."<<endl; cout<<"Introduce la posicion de la torre (entre 1 y "<<TAM<<"):"<<endl; cout<<"x: "; cin>>posx; cout<<"y: "; cin>>posy; } posx--; posy--; cout<<"El tablero queda tal que asi:"<<endl; pintatablero(tablero, posx, posy); cout<<"Las posiciones de las piezas a tiro son:"<<endl; amenazadas(tablero, posx, posy); cout<<"La lista de las posiciones alcanzables por la torre es:"<<endl; listaalcanzables(tablero, posx, posy); cout<<"Fin del programa."<<endl; system("pause"); return 0;

} //funciones genericas y de prueba void initmatriz(int matriz[][TAM]) { for (int i = 0; i < TAM; i++) { for (int j = 0; j < TAM; j++){ matriz[i][j] = 0; } } } void creatablero(int tablero[][TAM]) { int x = 1, y = 1; cout<<"Introduce las posiciones de los enemigos (del 1 al "<<TAM<<" cada una (x e y en sentido cartesiano), posicion invalida para terminar)"<<endl; while (x >= 0 and x < TAM and y >= 0 and y < TAM) { cout<<"x:"; cin>>x; cout<<"y:"; cin>>y; cout<<endl; x--; y--; tablero[x][y] = 1; } cout<<"Se ha terminado de introducir las posiciones de los enemigos."; } void pintatablero(int tablero[][TAM], int posx, int posy) { for (int i = TAM-1; i >= 0; i--){ cout<<"|"; for (int j = 0; j < TAM; j++){ if (j == posx and i == posy) cout<<"X"; else { if (tablero[j][i] == 1) cout<<"O"; else cout<<" "; } } cout<<"|"<<endl; } } //funciones del programa int amenazadas(int tablero[][TAM], int posx, int posy) { int total = 0; verticalsup(tablero, posx, posy, total); verticalinf(tablero, posx, posy, total); horizizq(tablero, posx, posy, total); horizder(tablero, posx, posy, total); if (total == 0) cout <<"no hay piezas a tiro."<<endl; else cout<<"El numero total de piezas a tiro (amenazadas) es "<<total<<". "<<endl; return total;

} void listaalcanzables(int tablero[][TAM], int posx, int posy) { int x, y; x = posx; y = posy-1; while (y >= 0 && tablero[x][y] != 1){ cout<<"| ("<<x+1<<", "<<y+1<<") "; y--; } y = posy+1; while (y < TAM && tablero[x][y] != 1){ cout<<"| ("<<x+1<<", "<<y+1<<")"; y++; } y = posy; x = posx-1; while (x >= 0 && tablero[x][y] != 1){ cout<<"| ("<<x+1<<", "<<y+1<<")"; x--; } x = posx+1; while (x < TAM && tablero[x][y] != 1){ cout<<"| ("<<x+1<<", "<<y+1<<")"; x++; } cout<<" |"<<endl; } //funciones de busqueda direccional void verticalsup(int tablero[][TAM], int posx, int posy, int &total) { int x = posx, y = posy-1; bool ENCONTRADA = false; while (y >= 0 && ENCONTRADA == false) { if (tablero[x][y] == 1) { total++; cout<<"Pieza amenazada en ("<<x+1<<", "<<y+1<<")."<<endl; ENCONTRADA = true; } y--; } } void verticalinf(int tablero[][TAM], int posx, int posy, int &total) { int x = posx, y = posy+1; bool ENCONTRADA = false; while (y < TAM && ENCONTRADA == false) { if (tablero[x][y] == 1) { total++; cout<<"Pieza amenazada en ("<<x+1<<", "<<y+1<<")."<<endl; ENCONTRADA = true; } y++; }

} void horizizq(int tablero[][TAM], int posx, int posy, int &total) { int x = posx-1, y = posy; bool ENCONTRADA = false; while (x >= 0 && ENCONTRADA == false) { if (tablero[x][y] == 1) { total++; cout<<"Pieza amenazada en ("<<x+1<<", "<<y+1<<")."<<endl; ENCONTRADA = true; } x--; } } void horizder(int tablero[][TAM], int posx, int posy, int &total) { int x = posx+1, y = posy; bool ENCONTRADA = false; while (x < TAM && ENCONTRADA == false) { if (tablero[x][y] == 1) { total++; cout<<"Pieza amenazada en ("<<x+1<<", "<<y+1<<")."<<endl; ENCONTRADA = true; } x++; } }

You might also like