#include <iostream>
#include <graphics.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
using namespace std;
#define ROJO 1
#define VERDE 2
#define TRANSPARENTE 0
#define AMARILLA 9
#define BLANCA 10
#define TORRE 3
#define REY 4
#define CABALLO 5
#define PEON 6
#define REINA 7
#define DIMENSION_TABLERO 8
struct Celda {
int x;
int y;
int tipo;
int color;
};
Celda tablero[DIMENSION_TABLERO][DIMENSION_TABLERO];
const int VACIA = 0;
bool turnoUsuario = true;
bool fichaSeleccionada = false;
int xInicio;
int yInicio;
int xFin;
int yFin;
// Comprueba si es posible mover la ficha TORRE:
bool esPosibleMoverTorre(Celda ficha, int x, int y){
bool movimientoPosible = false;
cout << "OK" << endl;
if (ficha.x > x && ficha.y == y){
movimientoPosible = true;
} else if (ficha.x == x && ficha.y < y){
movimientoPosible = true;
} else if (ficha.x < x && ficha.y == y){
movimientoPosible = true;
} else if (ficha.x == x && ficha.y > y){
movimientoPosible = true;
}
cout << "PM: " << movimientoPosible << endl;
if (movimientoPosible){
if (ficha.x == 0 && ficha.y == 1){
return (x == 0 && y == 0) || (x == 0 && y <= 4) || (x <= 2 && y == 1);
} else if (ficha.x == 0 && ficha.y == 5){
return (x == 0 && y >= 2) || (x == 0 && y == 6) || (x <= 7 && y == 5);
} else {
return false;
}
} else {
return false;
}
}
// Comprueba si es posible mover la ficha REY:
bool esPosibleMoverRey(Celda ficha, int x, int y){
if (abs(ficha.x - x) == 1 && ficha.y == y){
return true;
} else if (ficha.x == x && abs(ficha.y - y) == 1){
return true;
} else if (abs(ficha.x - x) == 1 && abs(ficha.y - y) == 1){
return true;
} else {
return false;
}
}
// Comprueba si es posible mover la ficha PEON:
bool esPosibleMoverPeon(Celda ficha, int x, int y){
return ficha.x < x && abs(ficha.x - x) == 1 && ficha.y == y;
}
// Comprueba si es posible mover la ficha REINA:
bool esPosibleMoverReina(Celda ficha, int x, int y){
if (ficha.x > x && ficha.y > y){
return false;
} else if (ficha.x > x && ficha.y == y){
return true
} else if ((abs(ficha.x - x) == 1 && abs(ficha.y - y) == 1) && ficha.x > x && ficha.y < y){
return true
} else if (ficha.x == x && ficha.y <= 7){
return true;
} else if ((x == 5 && y == 3) || (x == 6 && y == 4) || (x == 7 && y == 5)){
return true;
} else if (ficha.x <= 6 && ficha.y == y){
return true;
} else if ((x == 5 && y == 1) || (x == 6 && y == 0)){
return true;
} else if (ficha.x == x && (y >= 0 && y < ficha.y){
return true;
} else {
return false;
}
}
bool sePuedeRealizarMovimiento(Celda ficha, int x, int y){
switch(ficha.tipo){
case TORRE:
return esPosibleMoverTorre(ficha, x, y);
case REY:
return esPosibleMoverRey(ficha, x, y);
case PEON:
return esPosibleMoverPeon(ficha, x, y);
case REINA:
return esPosibleMoverReina(ficha, x, y);
}
return false;
}
void click_handler(int x, int y)
{
if ((x >= 120 && x <= 520) && (y >= 40 && y <= 440)){
if(turnoUsuario){
int posY = (x - 120) / 50;
int posX = (y - 40) / 50;
cout << "Pos X: " << posX << " Pos Y: " << posY << endl;
if (tablero[posX][posY].tipo == VACIA && !fichaSeleccionada){
return;
} else if (tablero[posX][posY].color == AMARILLA || fichaSeleccionada) {
if (!fichaSeleccionada){
fichaSeleccionada = true;
xInicio = posX;
yInicio = posY;
cout << tablero[posX][posY].tipo << endl;
} else if (tablero[posX][posY].tipo == VACIA) {
if (sePuedeRealizarMovimiento(tablero[xInicio][yInicio], posX, posY)){
turnoUsuario = false;
cout << "Yep" << endl;
} else {
fichaSeleccionada = false;
cout << "Nop" << endl;
}
}
}
}
}
}
int main()
{
initwindow(650, 500, "A");
int dimension = 8;
Celda celda;
registermousehandler(WM_LBUTTONDOWN, click_handler);
// Carga el tablero:
for(int i = 0; i < DIMENSION_TABLERO; ++i){
for(int j = 0; j < DIMENSION_TABLERO; ++j){
celda.x = i;
celda.y = j;
if( ( i + j ) % 2 == 0 ){
celda.color = ROJO;
}
else{
celda.color = VERDE;
}
celda.tipo = VACIA;
tablero[i][j] = celda;
}
}
celda.x = 0;
celda.y = 1;
celda.color = AMARILLA;
celda.tipo = TORRE;
tablero[0][1] = celda;
celda.x = 0;
celda.y = 5;
celda.color = AMARILLA;
celda.tipo = TORRE;
tablero[0][5] = celda;
celda.x = 0;
celda.y = 7;
celda.color = AMARILLA;
celda.tipo = REY;
tablero[0][7] = celda;
celda.x = 1;
celda.y = 4;
celda.color = BLANCA;
celda.tipo = CABALLO;
tablero[1][4] = celda;
celda.x = 2;
celda.y = 0;
celda.color = AMARILLA;
celda.tipo = PEON;
tablero[2][0] = celda;
celda.x = 2;
celda.y = 3;
celda.color = AMARILLA;
celda.tipo = PEON;
tablero[2][3] = celda;
celda.x = 2;
celda.y = 4;
celda.color = AMARILLA;
celda.tipo = PEON;
tablero[2][4] = celda;
celda.x = 3;
celda.y = 1;
celda.color = AMARILLA;
celda.tipo = PEON;
tablero[3][1] = celda;
celda.x = 4;
celda.y = 2;
celda.color = AMARILLA;
celda.tipo = REINA;
tablero[4][2] = celda;
celda.x = 4;
celda.y = 4;
celda.color = BLANCA;
celda.tipo = REINA;
tablero[4][4] = celda;
celda.x = 6;
celda.y = 0;
celda.color = BLANCA;
celda.tipo = PEON;
tablero[6][0] = celda;
celda.x = 6;
celda.y = 1;
celda.color = BLANCA;
celda.tipo = PEON;
tablero[6][1] = celda;
celda.x = 6;
celda.y = 2;
celda.color = BLANCA;
celda.tipo = PEON;
tablero[6][2] = celda;
celda.x = 7;
celda.y = 2;
celda.color = BLANCA;
celda.tipo = REY;
tablero[7][2] = celda;
celda.x = 7;
celda.y = 3;
celda.color = BLANCA;
celda.tipo = TORRE;
tablero[7][3] = celda;
int x;
int y;
int ancho = 50;
int alto = 50;
int color;
y = 0;
int i, j;
rectangle( 120, 40, 120 + ( 50 * 8 ), 40 + ( 50 * 8 ));
char columnas[] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'};
while(y < 210){
if (i != 7){
cleardevice();
}
for(i = 0 ; i < 8 ; i++ ) {
for(j = 0 ; j < 8 ; j++ ) {
if( ( i + j ) % 2 == 0 ){
setfillstyle(SOLID_FILL, RED);
}
else{
setfillstyle(SOLID_FILL, GREEN);
}
bar(120 + (j * alto), 40 + (i * ancho), 170 + (j * alto), 90 + (i * ancho));
}
char fila [10];
sprintf(fila, "%d", (8 - i));
outtextxy(95, 60 + (i * 50), fila);
char columna [10];
sprintf(columna, "%c", columnas[i]);
outtextxy(140 + (i * 50), 450, columna);
}
setfillstyle(SOLID_FILL, YELLOW);
// Torres amarillas:
bar(180, 40, 185, 50);
bar(193, 40, 198, 50);
bar(205, 40, 210, 50);
bar(120 + (1 * alto + 10), 40 + (0 * ancho + 10), 170 + (1 * alto - 10), 90 + (0 * ancho - 10));
bar(380, 40, 385, 50);
bar(393, 40, 398, 50);
bar(405, 40, 410, 50);
bar(120 + (5 * alto + 10), 40 + (0 * ancho + 10), 170 + (5 * alto - 10), 90 + (0 * ancho - 10));
// Rey amarillo:
setfillstyle(SOLID_FILL, YELLOW);
setcolor(YELLOW);
circle(135 + (7 * alto + 10), 55 + (0 * ancho + 10), 10);
// Caballo blanco:
setfillstyle(SOLID_FILL, WHITE);
bar(355, 90, 360, 100);
bar(120 + (4 * alto + 10), 40 + (1 * ancho + 10), 170 + (4 * alto - 10), 90 + (1 * ancho - 10));
// Peones amarillos:
setfillstyle(SOLID_FILL, YELLOW);
bar(120 + (0 * alto + 10), 40 + (2 * ancho + 10), 170 + (0 * alto - 10), 90 + (2 * ancho - 10));
bar(120 + (3 * alto + 10), 40 + (2 * ancho + 10), 170 + (3 * alto - 10), 90 + (2 * ancho - 10));
bar(120 + (4 * alto + 10), 40 + (2 * ancho + 10), 170 + (4 * alto - 10), 90 + (2 * ancho - 10));
bar(120 + (1 * alto + 10), 40 + (3 * ancho + 10), 170 + (1 * alto - 10), 90 + (3 * ancho - 10));
// Reina amarilla:
circle(110 + (2 * alto + 35), 50 + (4 * ancho), 10);
bar(120 + (2 * alto + 10), 40 + (4 * ancho + 10), 170 + (2 * alto - 10), 90 + (4 * ancho - 10));
// Reina blanca:
setfillstyle(SOLID_FILL, WHITE);
circle(210 + (2 * alto + 35), 50 + (4 * ancho), 10);
bar(120 + (4 * alto + 10), 40 + (4 * ancho + 10), 170 + (4 * alto - 10), 90 + (4 * ancho - 10));
// Peones blancos:
bar(120 + (0 * alto + 10), 40 + (6 * ancho + 10), 170 + (0 * alto - 10), 90 + (6 * ancho - 10));
bar(120 + (1 * alto + 10), 40 + (6 * ancho + 10), 170 + (1 * alto - 10), 90 + (6 * ancho - 10));
bar(120 + (2 * alto + 10), 40 + (6 * ancho + 10), 170 + (2 * alto - 10), 90 + (6 * ancho - 10));
// Rey blanco:
setfillstyle(SOLID_FILL, WHITE);
setcolor(WHITE);
circle(135 + (2 * alto + 10), 53 + (7 * ancho + 10), 10);
setfillstyle(SOLID_FILL, WHITE);
rectangle(120, 40, 120 + (50 * 8), 40 + (50 * 8));
bar(280, 390, 285, 400);
bar(293, 390, 298, 400);
bar(305, 390, 310, 400);
bar(120 + (3 * alto + 10), 40 + (7 * ancho + 10), 170 + (3 * alto - 10), 90 + (7 * ancho - 10));
y += 10;
}
y = 0;
delay(300);
getch();
closegraph();
return 0;
}
Comments
0 B
|👍
/👎