07.08.2016
Visual C++ 2010 Code
#include<iostream>#include<string>#include <windows.h>#include <sapi.h> // for voice synthesisusing namespace std;const int peon = 100;const int alfil = 305;const int caballo = 300;const int torre = 500;const int dama = 900;const int rey = 2000;const char blancas = 1;const char negras = -1;int tablero[8][8];char turno = blancas;char nueva_dama = 0;// para verificar enroquechar enroque_blancas_a1= 1;char enroque_blancas_h1 = 1;char enroque_negras_a8 = 1;char enroque_negras_h8 = 1;string juego = ""; //aca se escriben todas las jugadasint valor_blancas;int valor_negras;int jugada = 0;// board [fila] [columna];// where fila = 0 - 7 (1 to 8 on a real chess board) and columna = 0 - 7 (a - h)const int comienzo[8][8] = { torre, caballo, alfil, dama, rey, alfil, caballo, torre, peon, peon, peon, peon, peon, peon, peon, peon, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -peon, -peon, -peon, -peon, -peon, -peon, -peon, -peon, -torre, -caballo, -alfil, -dama, -rey, -alfil, -caballo, -torre};// openings database//const string E[99] = {"1.d2d4g8f6 2.c2c4e7e6 3.g2g3d7d5 4.g1f3", "1.d2d4g8f6 2.c2c4e7e6 3.g2g3d7d5 4.f1g2"};const string E[99] = {"d2d4g8f6c2c4e7e6g2g3d7d5g1f3", "d2d4g8f6c2c4e7e6g2g3d7d5f1g2"};void comenzar (void) {int i, j; for (i = 0; i < 8; i++){ for (j = 0; j < 8; j++){ tablero[i][j] = comienzo[i][j]; } }}void imprimir_tablero (void){ int a, b; string pieza; valor_blancas = valor_negras = 0; for (a = 7; a > -1; a--){ cout << endl << a+1 << " "; for (b = 0; b < 8; b++){ switch (tablero[a][b]) { case 0: pieza = "."; break; case peon: pieza = "P"; break; case caballo: pieza = "C"; break; case alfil: pieza = "A"; break; case torre: pieza = "T"; break; case dama: pieza = "D"; break; case rey: pieza = "R"; break; case -peon: pieza = "p"; break; case -caballo: pieza = "c"; break; case -alfil: pieza = "a"; break; case -torre: pieza = "t"; break; case -dama: pieza = "d"; break; case -rey: pieza = "r"; break; } if (tablero[a][b] > 0) valor_blancas += tablero[a][b]; if (tablero[a][b] < 0) valor_negras += tablero[a][b]; cout << " " << pieza << " "; } } cout << endl << endl << " A B C D E F G H" << endl << endl; cout << "Blancas : " << valor_blancas << " Negras : " << valor_negras << endl;}char camino_libre (int a, int b, int c, int d){ int inc_y = 0; int inc_x = 0; if (c!=a) inc_y = (c-a) / abs(c-a); if (d!=b) inc_x = (d-b) / abs(d-b); //cout << " a,b,c,d " << a << "," << b << "," << c << "," << d << endl; //cout << "inc x (b) : " << inc_x << " inc y (a) : " << inc_y << endl; do { a += inc_y; b += inc_x; //cout << " a,b " << a << "," << b << " tablero[b][a] = " << tablero[b][a] << endl; } while (tablero[b][a] == 0 && (a!=c || b!=d)); //cout << " cuando a - c = 0 termina : " << a - c << endl; if ((a == c) && (b == d)) return 1; else return 0;}char movimiento_valido (int a, int b, int c, int d){ char valid_move = 0; if ((turno == blancas && tablero[b][a] > 0 && tablero[d][c] <= 0) || (turno == negras && tablero[b][a] < 0 && tablero[d][c] >= 0)) { switch (tablero[b][a]) { case peon: if ((a-c) == 0 && (d-b) == 1 && tablero[d][c] == 0) valid_move = 1; // uno adelante if ((a-c) == 0 && (d-b) == 2 && b == 1 && tablero[d][c] == 0 && tablero[d-1][c] == 0) valid_move = 1; // dos adelante if ((a-c) == 1 && (d-b) == 1 && tablero[d][c] != 0) valid_move = 1; // comer derecha if ((a-c) == -1 && (d-b) == 1 && tablero[d][c] != 0) valid_move = 1; // comer izquierda if (valid_move && d == 7) nueva_dama = 1; break; case caballo: if ((abs(a-c) == 2 && abs(d-b) == 1) || (abs(a-c) == 1 && abs(d-b) == 2)) valid_move = 1; break; case alfil: if (abs(a-c) == abs (d-b) && camino_libre(a,b,c,d)) valid_move = 1; break; case torre: if (((a == c && b != d) || (a != c && b == d)) && camino_libre(a,b,c,d)) valid_move =1; break; case dama: if ((abs(a-c) == abs (d-b)) || ((a == c && b != d) || (a != c && b == d)) && camino_libre(a,b,c,d)) valid_move = 1; break; case rey: if ((abs(a-c) < 2) && (abs(d-b) < 2)) valid_move = 1; break; case -peon: if ((a-c) == 0 && (d-b) == -1 && tablero[d][c] == 0) valid_move = 1; if ((a-c) == 0 && (d-b) == -2 && b == 6 && tablero[d][c] == 0 && tablero[d+1][c] == 0) valid_move = 1; if ((a-c) == 1 && (d-b) == -1 && tablero[d][c] != 0) valid_move = 1; if ((a-c) == -1 && (d-b) == -1 && tablero[d][c] != 0) valid_move = 1; if (valid_move && d == 0) nueva_dama = 1; break; case -caballo: if ((abs(a-c) == 2 && abs(d-b) == 1) || (abs(a-c) == 1 && abs(d-b) == 2)) valid_move = 1; break; case -alfil: if (abs(a-c) == abs (d-b) && camino_libre(a,b,c,d)) valid_move = 1; break; case -torre: if (((a == c && b != d) || (a != c && b == d)) && camino_libre(a,b,c,d)) valid_move =1; break; case -dama: if ((abs(a-c) == abs (d-b)) || ((a == c && b != d) || (a != c && b == d)) && camino_libre(a,b,c,d)) valid_move = 1; break; case -rey: if ((abs(a-c) < 2) && (abs(d-b) < 2)) valid_move = 1; break; } return valid_move; // OK } return 0; // NOK}int main (void){ ISpVoice * pVoice = NULL; cout << " Ajedrez v0.1 - por E.Knorr\n============================" << endl << endl; cout << "x = salir del programa, n = nuevo juego" << endl << endl; cout << "Ingresar movidas en letra minuscula (origen-destino); ejemplo: e2e4" << endl; string passd; comenzar(); imprimir_tablero(); if (FAILED(::CoInitialize(NULL))) return FALSE; HRESULT hr = CoCreateInstance(CLSID_SpVoice, NULL, CLSCTX_ALL, IID_ISpVoice, (void **)&pVoice); if( SUCCEEDED( hr ) ) { hr = pVoice->Speak(L"Welcome to the chess player ! written by Lulee.", 0, NULL); } else { cout << "Not succeeded" << endl; } while (1) { cout << "ECO[0] : " << E[0] << endl; // just as test cout << "ECO[1] : " << E[1] << endl; // just as test cout << "Turno: "; if (turno == blancas) { jugada += 1; cout << "blancas" << endl; hr = pVoice->Speak(L"white, you move!", 0, NULL); getline (cin, passd ); } else { cout << "negras" << endl; hr = pVoice->Speak(L"black, I move !", 0, NULL); //cout << "suguiente jugada compu : " << E[0].substr(juego.length(), 4) << endl; for (int i = 0; i < 2; i++) if (juego == E[i].substr(0, juego.length())) { passd = E[i].substr(juego.length(), 4); cout << "Juego : " << juego << " ECO extract : " << E[i].substr(0, juego.length()) << endl; } } if (passd.substr(0, 1) == "x" || passd.substr(0, 1) == "X") { hr = pVoice->Speak(L"Thank you for playing Lulee chess player, bye", 0, NULL); pVoice->Release(); pVoice = NULL; ::CoUninitialize(); break; } if (passd.substr(0, 1) == "n" || passd.substr(0, 1) == "N") { comenzar(); imprimir_tablero(); turno = blancas; } if (passd.substr(0, 1) >= "a" && passd.substr(0, 1) <= "h" && passd.substr(1, 1) >= "1" && passd.substr(1, 1) <= "8" && passd.substr(2, 1) >= "a" && passd.substr(2, 1) <= "h" && passd.substr(3, 1) >= "1" && passd.substr(3, 1) <= "8") { int a, b, c, d; a = passd[0] - 'a'; b = passd[1] - '1'; c = passd[2] - 'a'; d = passd[3] - '1'; if (movimiento_valido(a,b,c,d) ) { //executes the move if its on the board! juego = juego + passd; cout << "Juego : " << juego << endl; if (nueva_dama) { tablero[d][c] = turno*dama; nueva_dama =0; } else tablero[d][c] = tablero[b][a]; tablero[b][a] = 0; imprimir_tablero(); //prints out to the screen the updated position after moving the pieces turno = -turno; } else { cout << "movimiento invalido; repita" << endl; imprimir_tablero(); } } }}