2016-03-06 21:51:07 +00:00
|
|
|
#include "main.h"
|
|
|
|
|
|
|
|
/* [0] Point d'amorcage
|
|
|
|
=========================================================*/
|
|
|
|
int main(){
|
2016-03-09 14:30:03 +00:00
|
|
|
|
|
|
|
// On cree un Jeu
|
2016-03-08 18:23:35 +00:00
|
|
|
ChessContext ctx;
|
2016-03-06 21:51:07 +00:00
|
|
|
|
2016-03-09 14:30:03 +00:00
|
|
|
// Message d'accueil
|
|
|
|
clear_screen();
|
2016-03-09 14:44:38 +00:00
|
|
|
multicolor("Bievenue sur Chess Colorful Console Game", BOLD);
|
|
|
|
cout << endl << endl;
|
|
|
|
cout << "by <xdrm-brackets>" << endl;
|
|
|
|
sleep(3);
|
2016-03-09 14:30:03 +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-09 13:23:00 +00:00
|
|
|
// On continue d'attendre que les joueurs jouent
|
|
|
|
// tant que la partie n'est pas finie
|
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
|
|
|
|
2016-03-09 14:30:03 +00:00
|
|
|
// On reset les modifications de police
|
2016-03-06 21:51:07 +00:00
|
|
|
setfont();
|
2016-03-09 14:30:03 +00:00
|
|
|
|
2016-03-06 21:51:07 +00:00
|
|
|
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
|
|
|
|
=========================================================*/
|
2016-03-09 14:30:03 +00:00
|
|
|
clear_screen();
|
2016-03-08 18:23:35 +00:00
|
|
|
|
|
|
|
/* [2] On affiche la banniere
|
|
|
|
=========================================================*/
|
|
|
|
setfont(RED, BOLD);
|
|
|
|
cout << " +------------------------------------+" << endl;
|
|
|
|
cout << " + +" << endl;
|
|
|
|
cout << " + Chess ";
|
|
|
|
|
2016-03-09 14:30:03 +00:00
|
|
|
|
|
|
|
multicolor("Colorful", BOLD);
|
2016-03-08 18:23:35 +00:00
|
|
|
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' )
|
2016-03-09 13:23:00 +00:00
|
|
|
cout << "> TOUR DU JOUEUR 1 <" << endl << "--------------------" << endl;
|
2016-03-08 18:23:35 +00:00
|
|
|
else if( ctx._turn == '2' )
|
2016-03-09 13:23:00 +00:00
|
|
|
cout << "> TOUR DU JOUEUR 2 <" << endl << "--------------------" << endl;
|
2016-03-08 18:23:35 +00:00
|
|
|
else
|
2016-03-09 13:23:00 +00:00
|
|
|
cout << "> PARTIE FINIE <" << endl << "-----------------" << endl;
|
2016-03-08 18:23:35 +00:00
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/* [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
|
2016-03-09 13:23:00 +00:00
|
|
|
Player *curnt = ctx.getCurrent();
|
2016-03-08 18:23:35 +00:00
|
|
|
// Contient l'ennemi
|
2016-03-09 13:23:00 +00:00
|
|
|
Player *enemy = ctx.getEnemy();
|
2016-03-08 18:23:35 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* [2] On prends la saisie de l'utilisateur
|
|
|
|
=========================================================*/
|
|
|
|
chess_input(origin, dest);
|
|
|
|
|
|
|
|
/* [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;
|
|
|
|
|
2016-03-09 13:23:00 +00:00
|
|
|
else
|
|
|
|
err("Aucune piece sur cette case");
|
2016-03-08 18:23:35 +00:00
|
|
|
|
|
|
|
|
|
|
|
/* [4] On verifie qu'il peut atteindre la destination
|
|
|
|
=========================================================*/
|
2016-03-09 13:23:00 +00:00
|
|
|
bool canMove = ctx.checkMove(*toMove, dest[0], dest[1]);
|
2016-03-08 18:23:35 +00:00
|
|
|
|
2016-03-09 13:23:00 +00:00
|
|
|
if( canMove ){
|
2016-03-08 18:23:35 +00:00
|
|
|
|
2016-03-09 13:23:00 +00:00
|
|
|
/* [5] On gere le deplacement
|
|
|
|
=========================================================*/
|
|
|
|
/* (1) Gestion de si on mange l'ennemi */
|
|
|
|
Piece *eaten = enemy->at(dest[0], dest[1]);
|
2016-03-08 18:23:35 +00:00
|
|
|
|
2016-03-09 13:23:00 +00:00
|
|
|
if( eaten != NULL ) // si piece mangee
|
|
|
|
enemy->remPiece( *eaten ); // on mange l'ennemi
|
2016-03-08 18:23:35 +00:00
|
|
|
|
2016-03-09 13:23:00 +00:00
|
|
|
toMove->move(dest[0], dest[1]); // on deplace la piece
|
2016-03-08 18:23:35 +00:00
|
|
|
|
2016-03-09 13:23:00 +00:00
|
|
|
|
|
|
|
/* [n] Gestion du tour ou partie finie
|
|
|
|
=========================================================*/
|
|
|
|
ctx._turn = (ctx._turn=='1') ? '2' : '1';
|
|
|
|
|
|
|
|
}else
|
|
|
|
err("Mouvement interdit");
|
2016-03-08 18:23:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* [3] Gestion de la saisie du tour
|
|
|
|
=========================================================*/
|
|
|
|
void chess_input(int* origin, int* dest){
|
|
|
|
char coord[2] = {'\0'};
|
|
|
|
|
2016-03-09 13:23:00 +00:00
|
|
|
cout << "Piece a deplacer (XN) : ";
|
2016-03-08 18:23:35 +00:00
|
|
|
cin >> coord;
|
|
|
|
Piece::decode(coord, origin);
|
|
|
|
|
2016-03-09 13:23:00 +00:00
|
|
|
cout << "Deplacer vers (XN) : ";
|
2016-03-08 18:23:35 +00:00
|
|
|
cin >> coord;
|
|
|
|
Piece::decode(coord, dest);
|
|
|
|
cout << endl;
|
|
|
|
}
|