2016-03-06 21:51:07 +00:00
|
|
|
#include "main.h"
|
|
|
|
|
|
|
|
/* [0] Point d'amorcage
|
|
|
|
=========================================================*/
|
|
|
|
int main(){
|
2016-03-08 18:23:35 +00:00
|
|
|
ChessContext ctx;
|
2016-03-06 21:51:07 +00:00
|
|
|
|
2016-03-08 18:23:35 +00:00
|
|
|
cout << typeid(ctx).name() << endl;
|
2016-03-06 21:51:07 +00:00
|
|
|
|
2016-03-08 18:23:35 +00:00
|
|
|
// On initialise la partie
|
|
|
|
ctx.init();
|
2016-03-06 21:51:07 +00:00
|
|
|
|
2016-03-08 18:23:35 +00:00
|
|
|
do{
|
|
|
|
display_board(ctx);
|
|
|
|
game_routine(ctx);
|
2016-03-06 21:51:07 +00:00
|
|
|
|
2016-03-08 18:23:35 +00:00
|
|
|
}while( ctx._turn != 'x' );
|
2016-03-06 21:51:07 +00:00
|
|
|
|
|
|
|
setfont();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-03-08 18:23:35 +00:00
|
|
|
/* [1] Initialisation de la partie
|
|
|
|
=========================================================*/
|
|
|
|
void display_board(ChessContext& ctx){
|
|
|
|
/* [1] On efface l'ecran
|
|
|
|
=========================================================*/
|
|
|
|
clear();
|
|
|
|
|
|
|
|
/* [2] On affiche la banniere
|
|
|
|
=========================================================*/
|
|
|
|
setfont(RED, BOLD);
|
|
|
|
cout << " +------------------------------------+" << endl;
|
|
|
|
cout << " + +" << endl;
|
|
|
|
cout << " + Chess ";
|
|
|
|
|
|
|
|
setfont(DARKBLUE, BOLD); cout << "C";
|
|
|
|
setfont(SKYBLUE, BOLD); cout << "o";
|
|
|
|
setfont(GREEN, BOLD); cout << "l";
|
|
|
|
setfont(YELLOW, BOLD); cout << "o";
|
|
|
|
setfont(BLUE, BOLD); cout << "r";
|
|
|
|
setfont(PURPLE, BOLD); cout << "f";
|
|
|
|
setfont(SKYBLUE, BOLD); cout << "u";
|
|
|
|
setfont(GREEN, BOLD); cout << "l";
|
|
|
|
setfont(RED, BOLD);
|
|
|
|
|
|
|
|
cout << " Console Game +" << endl;
|
|
|
|
cout << " + +" << endl;
|
|
|
|
cout << " +------------------------------------+" << endl;
|
|
|
|
cout << endl;
|
|
|
|
setfont();
|
|
|
|
|
|
|
|
|
|
|
|
/* [3] On affiche le plateau
|
|
|
|
=========================================================*/
|
|
|
|
cout << " A B C D E F G H" << endl;
|
|
|
|
cout << " +-------+-------+-------+-------+-------+-------+-------+-------+" << endl;
|
|
|
|
for( int y = 0 ; y < 8 ; y++ ){
|
|
|
|
|
|
|
|
cout << " + + + + + + + + +" << endl;
|
|
|
|
cout << (8-y) << " ";
|
|
|
|
|
|
|
|
for( int x = 0 ; x < 8 ; x++ ){
|
|
|
|
cout << "+ ";
|
|
|
|
|
|
|
|
Piece *p1_Piece = ctx._p1->at(x, y);
|
|
|
|
Piece *p2_Piece = ctx._p2->at(x, y);
|
|
|
|
/* (1) Si Joueur 1 a une piece sur la case */
|
|
|
|
if( p1_Piece != NULL ){
|
|
|
|
setfont(YELLOW, BOLD);
|
|
|
|
cout << p1_Piece->getchar();
|
|
|
|
setfont();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* (2) Si Joueur 2 a une piece sur la case */
|
|
|
|
else if( p2_Piece != NULL ){
|
|
|
|
setfont(SKYBLUE, BOLD);
|
|
|
|
cout << p2_Piece->getchar();
|
|
|
|
setfont();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* (3) Si la case est vide */
|
|
|
|
else
|
|
|
|
cout << " ";
|
|
|
|
|
|
|
|
cout << " ";
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
cout << "+ " << (8-y) << endl;
|
|
|
|
cout << " + + + + + + + + +" << endl;
|
|
|
|
cout << " +-------+-------+-------+-------+-------+-------+-------+-------+" << endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
cout << " A B C D E F G H" << endl;
|
|
|
|
cout << endl << endl;
|
|
|
|
|
|
|
|
if( ctx._turn == '1' )
|
|
|
|
cout << "TOUR DU JOUEUR 1" << endl;
|
|
|
|
else if( ctx._turn == '2' )
|
|
|
|
cout << "TOUR DU JOUEUR 2" << endl;
|
|
|
|
else
|
|
|
|
cout << "PARTIE FINIE" << endl;
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/* [2] Traitement pour chaque tour
|
|
|
|
=========================================================*/
|
|
|
|
void game_routine(ChessContext& ctx){
|
|
|
|
int origin[2]; // contiendra la position d'origine
|
|
|
|
int dest[2]; // contiendra la position souhaitee
|
|
|
|
|
|
|
|
// Contient le joueur courant
|
|
|
|
Player *curnt = (ctx._turn=='1') ? ctx._p1 : ctx._p2;
|
|
|
|
// Contient l'ennemi
|
|
|
|
Player *enemy = (ctx._turn=='1') ? ctx._p2 : ctx._p1;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* [2] On prends la saisie de l'utilisateur
|
|
|
|
=========================================================*/
|
|
|
|
chess_input(origin, dest);
|
|
|
|
|
|
|
|
cout << "(" << origin[0] << "," << origin[1] << ")" << endl;
|
|
|
|
|
|
|
|
/* [3] On verifie qu'un pion est a cette case
|
|
|
|
=========================================================*/
|
|
|
|
Piece *toMove = ctx._p1->at(origin[0], origin[1]);
|
|
|
|
|
|
|
|
if( toMove == NULL )
|
|
|
|
toMove = ctx._p2->at(origin[0], origin[1]);
|
|
|
|
|
|
|
|
if( toMove != NULL )
|
|
|
|
cout << *toMove << endl;
|
|
|
|
|
|
|
|
else{
|
|
|
|
cout << "Aucune piece sur cette case" << endl;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* [4] On verifie qu'il peut atteindre la destination
|
|
|
|
=========================================================*/
|
|
|
|
cout << "Can move to dest : " << toMove->can(ctx, dest[0], dest[1]) << endl;
|
|
|
|
|
|
|
|
|
|
|
|
/* [5] On gere le deplacement
|
|
|
|
=========================================================*/
|
|
|
|
/* (1) Gestion de si on mange l'ennemi */
|
|
|
|
Piece *eaten = enemy->at(dest[0], dest[1]);
|
|
|
|
if( eaten != NULL ) // si piece mangee
|
|
|
|
enemy->remPiece( *eaten ); // on mange l'ennemi
|
|
|
|
|
|
|
|
toMove->move(dest[0], dest[1]); // on deplace la piece
|
|
|
|
|
|
|
|
|
|
|
|
/* [n] Gestion du tour ou partie finie
|
|
|
|
=========================================================*/
|
|
|
|
ctx._turn = (ctx._turn=='1') ? '2' : '1';
|
|
|
|
}
|
|
|
|
|
|
|
|
/* [3] Gestion de la saisie du tour
|
|
|
|
=========================================================*/
|
|
|
|
void chess_input(int* origin, int* dest){
|
|
|
|
char coord[2] = {'\0'};
|
|
|
|
|
|
|
|
cout << "Piece to move (XN) : ";
|
|
|
|
cin >> coord;
|
|
|
|
Piece::decode(coord, origin);
|
|
|
|
cout << endl;
|
|
|
|
|
|
|
|
cout << "Move to (XN) : ";
|
|
|
|
cin >> coord;
|
|
|
|
Piece::decode(coord, dest);
|
|
|
|
cout << endl;
|
|
|
|
}
|