diff --git a/Chess/dep/ChessContext.cpp b/Chess/dep/ChessContext.cpp new file mode 100644 index 0000000..75957cf --- /dev/null +++ b/Chess/dep/ChessContext.cpp @@ -0,0 +1,42 @@ +/* [1] Initialisation de la partie +=========================================================*/ +void ChessContext::init(){ + _p1 = new Player(true); + _p2 = new Player(false); + + _turn = '1'; + + _p1->initPieces(); + _p2->initPieces(); +} + +/* [0] Constructeur +=========================================================*/ +ChessContext::ChessContext(){ + _p1 = new Player(true); + _p2 = new Player(false); + + _turn = '1'; + + _p1->initPieces(); + _p2->initPieces(); +} + + +/* [2] Renvoie le pion qui est en case (x, y) +=========================================================*/ +Player* ChessContext::at(const int x, const int y){ + // On verifie que c'est pas le Player 1 qui l'a + Piece *p; + p = _p1->at(x, y); + if( p != NULL ) + return _p1; + + // On verifie que c'est pas le Player 2 qui l'a + p = _p2->at(x, y); + if( p != NULL ) + return _p2; + + + return NULL; +} \ No newline at end of file diff --git a/Chess/dep/ChessContext.h b/Chess/dep/ChessContext.h new file mode 100644 index 0000000..aeeb6fd --- /dev/null +++ b/Chess/dep/ChessContext.h @@ -0,0 +1,38 @@ +#ifndef DEF_CHESSCONTEXT_H + + #define DEF_CHESSCONTEXT_H + + /* [1] Libs + =========================================================*/ + /* (1) Internes */ + #include + #include + #include + + /* (2) Externes */ + #include "term.h" + #include "Player.h" + + /* (3) Namespace */ + using namespace std; + + + /* [2] Definition + =========================================================*/ + class ChessContext{ + public: + ChessContext(); + void init(); + Player* at(const int x, const int y); + + // ATTRIBUTS + char _turn; + Player *_p1; + Player *_p2; + }; + + /* [n] Inclusion du corps + =========================================================*/ + #include "ChessContext.cpp" + +#endif \ No newline at end of file diff --git a/Chess/dep/Pieces/Cavalier.cpp b/Chess/dep/Pieces/Cavalier.cpp index 774178a..c1a6735 100644 --- a/Chess/dep/Pieces/Cavalier.cpp +++ b/Chess/dep/Pieces/Cavalier.cpp @@ -4,10 +4,10 @@ Cavalier::Cavalier(int x, int y) : Piece(CAVALIER, x, y) { } // ABSTRACT -bool Cavalier::can(const int x, const int y) const{ +bool Cavalier::can(const ChessContext& ctx, int x, int y){ return true; } char Cavalier::getchar(){ - return 'P'; + return 'C'; } diff --git a/Chess/dep/Pieces/Cavalier.h b/Chess/dep/Pieces/Cavalier.h index d2941bc..0ef3e68 100644 --- a/Chess/dep/Pieces/Cavalier.h +++ b/Chess/dep/Pieces/Cavalier.h @@ -1,6 +1,6 @@ -#ifndef DEF_CAVALIER +#ifndef DEF_CAVALIER_H - #define DEF_CAVALIER + #define DEF_CAVALIER_H /* [1] Libs =========================================================*/ @@ -19,7 +19,7 @@ Cavalier(int x, int y); // ABSTRACT - bool can(const int x, const int y) const; + bool can(const ChessContext& ctx, int x, int y); char getchar(); }; diff --git a/Chess/dep/Pieces/Fou.cpp b/Chess/dep/Pieces/Fou.cpp index 46ab666..84357ae 100644 --- a/Chess/dep/Pieces/Fou.cpp +++ b/Chess/dep/Pieces/Fou.cpp @@ -4,10 +4,23 @@ Fou::Fou(int x, int y) : Piece(FOU, x, y) { } // ABSTRACT -bool Fou::can(const int x, const int y) const{ - return true; +bool Fou::can(const ChessContext& ctx, int x, int y){ + bool inDiag = abs( _x-x ) == abs( _y-y ); + + // Si deplacement pas en diagonale, on retourne faux + if( !inDiag ) return false; + + // On verifie que la diagonale est degagee + int xstep = ( x-_x ) / abs( x-_x ); + int ystep = ( y-_y ) / abs( y-_y ); + + for( int i = _x+xstep, j = _y+ystep ; i != x && j != y ; i+=xstep, j+=ystep ) + if( ctx._p1->at(i, j) != NULL ) return false; + + + return true; } char Fou::getchar(){ - return 'P'; + return 'F'; } diff --git a/Chess/dep/Pieces/Fou.h b/Chess/dep/Pieces/Fou.h index 926d788..43b548d 100644 --- a/Chess/dep/Pieces/Fou.h +++ b/Chess/dep/Pieces/Fou.h @@ -1,6 +1,6 @@ -#ifndef DEF_FOU +#ifndef DEF_FOU_H - #define DEF_FOU + #define DEF_FOU_H /* [1] Libs =========================================================*/ @@ -19,7 +19,7 @@ Fou(int x, int y); // ABSTRACT - bool can(const int x, const int y) const; + bool can(const ChessContext& ctx, int x, int y); char getchar(); }; diff --git a/Chess/dep/Pieces/Piece.cpp b/Chess/dep/Pieces/Piece.cpp index 7c552c3..1fc1d10 100644 --- a/Chess/dep/Pieces/Piece.cpp +++ b/Chess/dep/Pieces/Piece.cpp @@ -7,6 +7,12 @@ PIECE_TYPE Piece::gett() const{ return _t; } int Piece::getx() const{ return _x; } int Piece::gety() const{ return _y; } +// DECODE DES COORDONNES DE LA FORME "A7" VERS (x,y) +void Piece::decode(char *s, int* xy){ + xy[0] = (s[0] - 'A'); + xy[1] = 8 - (s[1] - '0'); +} + // SETTERS void Piece::move(const int x, const int y){ _x = x; @@ -19,5 +25,5 @@ ostream& operator<<(ostream& o, const Piece& p){ } // ABTRACT -bool Piece::can(const int x, const int y){ return false; } +bool Piece::can(const ChessContext& ctx, int x, int y){ return false; } char Piece::getchar(){ return '?'; } \ No newline at end of file diff --git a/Chess/dep/Pieces/Piece.h b/Chess/dep/Pieces/Piece.h index ad3ba45..01ce9d9 100644 --- a/Chess/dep/Pieces/Piece.h +++ b/Chess/dep/Pieces/Piece.h @@ -1,6 +1,6 @@ -#ifndef DEF_PIECE +#ifndef DEF_PIECE_H - #define DEF_PIECE + #define DEF_PIECE_H /* [1] Libs =========================================================*/ @@ -8,8 +8,10 @@ #include #include #include + #include /* (2) Externes */ + #include "../ChessContext.h" /* (3) Namespace */ using namespace std; @@ -48,6 +50,7 @@ PIECE_TYPE gett() const; int getx() const; int gety() const; + static void decode(char *s, int* xy); // SETTERS void move(const int x, const int y); @@ -56,7 +59,7 @@ friend ostream& operator<<(ostream& o, const Piece& p); // ABSTRACT - virtual bool can(const int x, const int y); + virtual bool can(const ChessContext& ctx, int x, int y); virtual char getchar(); protected: diff --git a/Chess/dep/Pieces/PieceFactory.cpp b/Chess/dep/Pieces/PieceFactory.cpp index 13e81e4..264197a 100644 --- a/Chess/dep/Pieces/PieceFactory.cpp +++ b/Chess/dep/Pieces/PieceFactory.cpp @@ -10,5 +10,13 @@ Piece& PieceFactory::create(PIECE_TYPE t, int x, int y){ case PION: p = new Pion(x, y); break; } + /* DEBUG */ + setfont(SKYBLUE, NORMAL); + + cout << "\t[+] " << p->gett() << " at (" << p->getx() << "," << p->gety() << ")"; + + setfont(); + cout << endl; + return *p; } diff --git a/Chess/dep/Pieces/Pion.cpp b/Chess/dep/Pieces/Pion.cpp index 38c6add..6065f6c 100644 --- a/Chess/dep/Pieces/Pion.cpp +++ b/Chess/dep/Pieces/Pion.cpp @@ -4,7 +4,7 @@ Pion::Pion(int x, int y) : Piece(PION, x, y) { } // ABSTRACT -bool Pion::can(const int x, const int y) const{ +bool Pion::can(const ChessContext& ctx, const int x, const int y) const{ return true; } diff --git a/Chess/dep/Pieces/Pion.h b/Chess/dep/Pieces/Pion.h index bff2dcc..272f556 100644 --- a/Chess/dep/Pieces/Pion.h +++ b/Chess/dep/Pieces/Pion.h @@ -1,6 +1,6 @@ -#ifndef DEF_PION +#ifndef DEF_PION_H - #define DEF_PION + #define DEF_PION_H /* [1] Libs =========================================================*/ @@ -19,7 +19,7 @@ Pion(int x, int y); // ABSTRACT - bool can(const int x, const int y) const; + bool can(const ChessContext& ctx, const int x, const int y) const; char getchar(); }; diff --git a/Chess/dep/Pieces/Reine.cpp b/Chess/dep/Pieces/Reine.cpp index d33f8bb..623e407 100644 --- a/Chess/dep/Pieces/Reine.cpp +++ b/Chess/dep/Pieces/Reine.cpp @@ -4,10 +4,10 @@ Reine::Reine(int x, int y) : Piece(REINE, x, y) { } // ABSTRACT -bool Reine::can(const int x, const int y) const{ +bool Reine::can(const ChessContext& ctx, int x, int y){ return true; } char Reine::getchar(){ - return 'P'; + return 'Q'; } diff --git a/Chess/dep/Pieces/Reine.h b/Chess/dep/Pieces/Reine.h index 7915bc2..b16492f 100644 --- a/Chess/dep/Pieces/Reine.h +++ b/Chess/dep/Pieces/Reine.h @@ -1,6 +1,6 @@ -#ifndef DEF_REINE +#ifndef DEF_REINE_H - #define DEF_REINE + #define DEF_REINE_H /* [1] Libs =========================================================*/ @@ -19,7 +19,7 @@ Reine(int x, int y); // ABSTRACT - bool can(const int x, const int y) const; + bool can(const ChessContext& ctx, int x, int y); char getchar(); }; diff --git a/Chess/dep/Pieces/Roi.cpp b/Chess/dep/Pieces/Roi.cpp index 7717f4b..0a8d9b5 100644 --- a/Chess/dep/Pieces/Roi.cpp +++ b/Chess/dep/Pieces/Roi.cpp @@ -4,10 +4,10 @@ Roi::Roi(int x, int y) : Piece(ROI, x, y) { } // ABSTRACT -bool Roi::can(const int x, const int y) const{ +bool Roi::can(const ChessContext& ctx, int x, int y){ return true; } char Roi::getchar(){ - return 'R'; + return 'K'; } diff --git a/Chess/dep/Pieces/Roi.h b/Chess/dep/Pieces/Roi.h index 2bad8be..7d90f22 100644 --- a/Chess/dep/Pieces/Roi.h +++ b/Chess/dep/Pieces/Roi.h @@ -1,6 +1,6 @@ -#ifndef DEF_Roi +#ifndef DEF_ROI_H - #define DEF_Roi + #define DEF_ROI_H /* [1] Libs =========================================================*/ @@ -19,7 +19,7 @@ Roi(int x, int y); // ABSTRACT - bool can(const int x, const int y) const; + bool can(const ChessContext& ctx, int x, int y); char getchar(); }; diff --git a/Chess/dep/Pieces/Tour.cpp b/Chess/dep/Pieces/Tour.cpp index 76d8aab..be80cb3 100644 --- a/Chess/dep/Pieces/Tour.cpp +++ b/Chess/dep/Pieces/Tour.cpp @@ -4,10 +4,10 @@ Tour::Tour(int x, int y) : Piece(TOUR, x, y) { } // ABSTRACT -bool Tour::can(const int x, const int y) const{ +bool Tour::can(const ChessContext& ctx, int x, int y){ return true; } char Tour::getchar(){ - return 'P'; + return 'T'; } diff --git a/Chess/dep/Pieces/Tour.h b/Chess/dep/Pieces/Tour.h index b490532..9a4565c 100644 --- a/Chess/dep/Pieces/Tour.h +++ b/Chess/dep/Pieces/Tour.h @@ -1,6 +1,6 @@ -#ifndef DEF_TOUR +#ifndef DEF_TOUR_H - #define DEF_TOUR + #define DEF_TOUR_H /* [1] Libs =========================================================*/ @@ -19,7 +19,7 @@ Tour(int x, int y); // ABSTRACT - bool can(const int x, const int y) const; + bool can(const ChessContext& ctx, int x, int y); char getchar(); }; diff --git a/Chess/dep/Player.cpp b/Chess/dep/Player.cpp new file mode 100644 index 0000000..8890250 --- /dev/null +++ b/Chess/dep/Player.cpp @@ -0,0 +1,85 @@ +// CONSTRUCTEUR +Player::Player(bool first) : _first(first){ + setfont(GREEN, BOLD); + + cout << "[+] Player " << (_first?1:2); + + setfont(); + cout << endl; +} + +// GETTERS +bool Player::owns(Piece& p) const{ + for( int i = 0 ; i < _pieces.size() ; i++ ) + if( _pieces.at(i) == &p ) return true; + + return false; +} + +Piece* Player::at(const int x, const int y){ + for( int i = 0 ; i < _pieces.size() ; i++ ) + if( _pieces[i]->getx() == x && _pieces[i]->gety() == y ) + return _pieces[i]; + + return NULL; + +} +// SETTERS +void Player::addPiece(Piece& p){ + _pieces.push_back( &p ); + + /* DEBUG */ + setfont(BLUE, ITALIC); + + cout << "\t\t[+] linked to Player " << (_first?1:2); + + setfont(); + cout << endl; +} + +void Player::remPiece(Piece& p){ + int index = -1; + + // On cherche la piece + for( int i = 0 ; i < _pieces.size() ; i++ ) + if( _pieces.at(i) == &p ) + index = i; + + // On ajoute au cimetiere + if( index > -1 ){ + _cimetery.push_back( &p ); + _pieces.erase(_pieces.begin() + index ); + } + + /* DEBUG */ + setfont(BLUE, ITALIC); + + cout << "\t\t[-] " << p << " to cimetery of Player " << (_first?1:2); + + setfont(); + cout << endl; +} + +// INITIALISATION DES PIECES DE BASE +void Player::initPieces(){ + int y1 = _first ? 0 : 7; // ligne forte + int y2 = _first ? 1 : 6; // ligne de pions + + + // Premier ligne + this->addPiece( PieceFactory::create(TOUR, 0, y1) ); + this->addPiece( PieceFactory::create(CAVALIER, 1, y1) ); + this->addPiece( PieceFactory::create(FOU, 2, y1) ); + + this->addPiece( PieceFactory::create(REINE, 3, y1) ); + this->addPiece( PieceFactory::create(ROI, 4, y1) ); + + this->addPiece( PieceFactory::create(FOU, 5, y1) ); + this->addPiece( PieceFactory::create(CAVALIER, 6, y1) ); + this->addPiece( PieceFactory::create(TOUR, 7, y1) ); + + for( int i = 0 ; i < 8 ; i++ ) + this->addPiece( PieceFactory::create(PION, i, y2) ); + +} + diff --git a/Chess/dep/Player.h b/Chess/dep/Player.h new file mode 100644 index 0000000..bddb4e6 --- /dev/null +++ b/Chess/dep/Player.h @@ -0,0 +1,45 @@ +#ifndef DEF_PAYER_H + + #define DEF_PAYER_H + + /* [1] Libs + =========================================================*/ + /* (1) Internes */ + #include + #include + #include + + /* (2) Externes */ + #include "term.h" + #include "Pieces/PieceFactory.h" + + /* (3) Namespace */ + using namespace std; + + + /* [2] Definition + =========================================================*/ + class Player{ + public: + Player(bool first); + + // GETTERS + bool _first; + bool owns(Piece& p) const; + Piece* at(const int x, const int y) ; + + // SETTERS + void addPiece(Piece& p); + void remPiece(Piece& p); + void initPieces(); + + private: + vector _pieces; + vector _cimetery; + }; + + /* [n] Inclusion du corps + =========================================================*/ + #include "Player.cpp" + +#endif \ No newline at end of file diff --git a/Chess/dep/term.h b/Chess/dep/term.h index 5237c47..c146219 100644 --- a/Chess/dep/term.h +++ b/Chess/dep/term.h @@ -1,6 +1,6 @@ -#ifndef DEF_TERM +#ifndef DEF_TERM_H - #define DEF_TERM + #define DEF_TERM_H /* [1] Libs =========================================================*/ diff --git a/Chess/e b/Chess/e deleted file mode 100755 index 9abe399..0000000 Binary files a/Chess/e and /dev/null differ diff --git a/Chess/main.cpp b/Chess/main.cpp index 5d4cfd8..fbd0269 100644 --- a/Chess/main.cpp +++ b/Chess/main.cpp @@ -3,20 +3,176 @@ /* [0] Point d'amorcage =========================================================*/ int main(){ - Piece *a = new Roi(1, 12); - Piece *b = new Pion(1, 12); + ChessContext ctx; + cout << typeid(ctx).name() << endl; - setfont(RED); - cout << *a << endl; + // On initialise la partie + ctx.init(); - setfont(SKYBLUE); - cout << *b << endl; - cout << *b << endl; + do{ + display_board(ctx); + game_routine(ctx); + }while( ctx._turn != 'x' ); setfont(); return 0; } +/* [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; +} \ No newline at end of file diff --git a/Chess/main.h b/Chess/main.h index e855e20..d6e9b79 100644 --- a/Chess/main.h +++ b/Chess/main.h @@ -1,6 +1,6 @@ -#ifndef DEF_HEADER +#ifndef DEF_MAIN_H - #define DEF_HEADER + #define DEF_MAIN_H /* [1] Libs =========================================================*/ @@ -8,10 +8,12 @@ #include #include #include + #include + #include /* (2) Externes */ #include "dep/term.h" - #include "dep/Grid.h" + #include "dep/ChessContext.h" /* (3) Namespace */ using namespace std; @@ -24,5 +26,8 @@ /* [3] Methodes =========================================================*/ int main(); + void display_board(ChessContext& ctx); + void game_routine(ChessContext& ctx); + void chess_input(int* origin, int* dest); #endif \ No newline at end of file