Browse Source

Chessengine is ready

jezv 11 months ago
parent
commit
904e8f69d5
11 changed files with 358 additions and 15 deletions
  1. 1 1
      Cell.cpp
  2. 27 12
      Chessboard.cpp
  3. 4 1
      Chessboard.h
  4. 141 0
      Chessengine.cpp
  5. 28 0
      Chessengine.h
  6. 9 0
      Makefile
  7. 2 1
      Pawn.cpp
  8. 14 0
      Player.h
  9. 20 0
      RealPlayer.cpp
  10. 15 0
      RealPlayer.h
  11. 97 0
      game.cpp

+ 1 - 1
Cell.cpp

@@ -2,7 +2,7 @@
 #include <iostream>
 
 Cell::Cell()
-{}
+{figure = nullptr;}
 
 Cell::Cell(Figure* fig)
 {  

+ 27 - 12
Chessboard.cpp

@@ -8,9 +8,20 @@
 #include <utility>
 #include <stdexcept>
 
+std::ostream& operator <<(std::ostream& out, Chessboard& chess)
+{
+    for (int i = 7; i >= 0; i--) {
+        for(int j = 0; j < 8; j++) {
+            out << chess.board[i][j];            
+        }
+        out << '\n';
+    }
+    return out;
+}
+
 Cell Chessboard::get_cell(Position p)
 {
-    return board[p.posNum-'0'-1][p.posSym-'A'];
+    return get_icell(p.posNum-'0'-1, p.posSym-'A');
 }
 
 bool compare_by_angle(std::pair<int, int> pos1, std::pair<int, int> pos2)
@@ -38,7 +49,8 @@ vector<Position> Chessboard::get_possible_moves(Figure* fig)
 
     bool blockedGroup = false;
 
-    Figure* check_cell = board[grouped_moves[0].second+pos.posNum-'0'-1][grouped_moves[0].first+pos.posSym-'A'].get_figure();
+    Figure* check_cell;
+    check_cell = board[grouped_moves[0].second+pos.posNum-'0'-1][grouped_moves[0].first+pos.posSym-'A'].get_figure();
     if (check_cell != nullptr) {
         blockedGroup = true;
         if (check_cell->get_color() != fig->get_color()) possible_moves.push_back(Position(grouped_moves[0].first+pos.posSym, (char)(grouped_moves[0].second+pos.posNum)));     
@@ -51,7 +63,7 @@ vector<Position> Chessboard::get_possible_moves(Figure* fig)
         }
         if (blockedGroup == true) continue;
 
-        Figure* check_cell = board[grouped_moves[i].second+pos.posNum-'0'-1][grouped_moves[i].first+pos.posSym-'A'].get_figure();
+        check_cell = board[grouped_moves[i].second+pos.posNum-'0'-1][grouped_moves[i].first+pos.posSym-'A'].get_figure();
 
         if (check_cell != nullptr) {
             blockedGroup = true;
@@ -62,9 +74,16 @@ vector<Position> Chessboard::get_possible_moves(Figure* fig)
         possible_moves.push_back(Position((char)(grouped_moves[i].first+pos.posSym), (char)(grouped_moves[i].second+pos.posNum)));
     }
 
+
     return possible_moves;
 }
 
+Chessboard::Chessboard()
+{
+    board = new Cell*[8];
+    for(int i = 0; i < 8; i++) board[i] = new Cell[8];
+}
+
 Chessboard::Chessboard(vector<Figure*> figures)
 {
     board = new Cell*[8];
@@ -104,15 +123,11 @@ void Chessboard::create_move(Position pos1, Position pos2)
         }
     }
     throw std::invalid_argument("Impossible move");
-};
+}
 
-std::ostream& operator <<(std::ostream& out, Chessboard& chess)
+Cell Chessboard::get_icell(int index, int jindex)
 {
-    for (int i = 7; i >= 0; i--) {
-        for(int j = 0; j < 8; j++) {
-            out << chess.board[i][j];            
-        }
-        out << '\n';
-    }
-    return out;
+    if (not (index >= 0 and index < 8 and jindex >= 0 and jindex < 8))
+        throw std::invalid_argument("Trying to get figure out of board");
+    return board[index][jindex];
 }

+ 4 - 1
Chessboard.h

@@ -15,13 +15,16 @@ class Chessboard
     vector<std::pair<Position, Position>> moves;
 
     vector<Position> get_possible_moves(Figure* fig);
-    Cell get_cell(Position pos);
 
     public:
+    Chessboard();
     Chessboard(vector<Figure*>);
 
     void create_move(Position pos1, Position pos2);
 
+    Cell get_cell(Position pos);
+    Cell get_icell(int index, int jindex);
+
     friend std::ostream& operator <<(std::ostream& out, Chessboard& chess);
 };
 

+ 141 - 0
Chessengine.cpp

@@ -0,0 +1,141 @@
+#include "Chessengine.h"
+#include "Player.h"
+#include "Chessboard.h"
+#include "King.h"
+#include "Queen.h"
+#include "Pawn.h"
+#include "Position.h"
+#include "Rook.h"
+#include "Bishop.h"
+#include "Horse.h"
+#include <utility>
+#include <iostream>
+#include <stdexcept>
+
+Chessengine::Chessengine(Player* p1, Player* p2)
+{
+    turn = 'W';
+    player1 = p1;
+    player2 = p2;
+    player = player1;
+}
+
+bool Chessengine::king_on_board(char color)
+{
+    const char* need_find = King(Position(), color).print();
+
+    Figure* fig;
+    for(int i = 0; i < 8; i++) {
+        for(int j = 0; j < 8; j++) {
+            fig = board.get_icell(i, j).get_figure();
+            if (fig != nullptr and fig->print() == need_find) return true;  
+        }
+    }
+
+    return false;
+}
+
+void Chessengine::start()
+{
+    while (king_on_board('W') == true and king_on_board('B') == true)
+    {
+        while (true) {
+            try {
+                std::cout << board << "How turn is " << turn << '\n';
+                std::pair<Position, Position> move = player->get_move(board, turn);
+                board.create_move(move.first, move.second);
+                turn = turn == 'W' ? 'B' : 'W';
+                player = player == player1 ? player2 : player1;
+                break;
+            }
+            catch (std::invalid_argument) {
+                continue;
+            }
+        }
+    }
+}
+
+void Chessengine::load(vector<Figure*> figures, char turn)
+{
+    board = Chessboard(figures);
+    turn = turn;
+    player = turn == 'W' ? player1 : player2;
+}
+
+void Chessengine::load_base()
+{
+    // Деструктор уничтожает все переменные при выходе из функции
+    vector<Figure*> figures;
+    
+    // Создание чёрных
+    Pawn bp1(Position('A', '7'), 'B');
+    Pawn bp2(Position('B', '7'), 'B');
+    Pawn bp3(Position('C', '7'), 'B');
+    Pawn bp4(Position('D', '7'), 'B');
+    Pawn bp5(Position('E', '7'), 'B');
+    Pawn bp6(Position('F', '7'), 'B');
+    Pawn bp7(Position('G', '7'), 'B');
+    Pawn bp8(Position('H', '7'), 'B');
+    Rook br1(Position('A', '8'), 'B');
+    Rook br2(Position('H', '8'), 'B');
+    Horse bh1(Position('B', '8'), 'B');
+    Horse bh2(Position('G', '8'), 'B');
+    Bishop bb1(Position('C', '8'), 'B');
+    Bishop bb2(Position('F', '8'), 'B');
+    King bk1(Position('E', '8'), 'B');
+    Queen bq1(Position('D', '8'), 'B');
+
+    figures.push_back(&bp1);
+    figures.push_back(&bp2);
+    figures.push_back(&bp3);
+    figures.push_back(&bp4);
+    figures.push_back(&bp5);
+    figures.push_back(&bp6);
+    figures.push_back(&bp7);
+    figures.push_back(&bp8);
+    figures.push_back(&br1);
+    figures.push_back(&br2);
+    figures.push_back(&bh1);
+    figures.push_back(&bh2);
+    figures.push_back(&bb1);
+    figures.push_back(&bb2);
+    figures.push_back(&bk1);
+    figures.push_back(&bq1);
+
+    // Создание белых
+    Pawn wp1(Position('A', '2'), 'W');
+    Pawn wp2(Position('B', '2'), 'W');
+    Pawn wp3(Position('C', '2'), 'W');
+    Pawn wp4(Position('D', '2'), 'W');
+    Pawn wp5(Position('E', '2'), 'W');
+    Pawn wp6(Position('F', '2'), 'W');
+    Pawn wp7(Position('G', '2'), 'W');
+    Pawn wp8(Position('H', '2'), 'W');
+    Rook wr1(Position('A', '1'), 'W');
+    Rook wr2(Position('H', '1'), 'W');
+    Horse wh1(Position('B', '1'), 'W');
+    Horse wh2(Position('G', '1'), 'W');
+    Bishop wb1(Position('C', '1'), 'W');
+    Bishop wb2(Position('F', '1'), 'W');
+    King wk1(Position('E', '1'), 'W');
+    Queen wq1(Position('D', '1'), 'W');
+
+    figures.push_back(&wp1);
+    figures.push_back(&wp2);
+    figures.push_back(&wp3);
+    figures.push_back(&wp4);
+    figures.push_back(&wp5);
+    figures.push_back(&wp6);
+    figures.push_back(&wp7);
+    figures.push_back(&wp8);
+    figures.push_back(&wr1);
+    figures.push_back(&wr2);
+    figures.push_back(&wh1);
+    figures.push_back(&wh2);
+    figures.push_back(&wb1);
+    figures.push_back(&wb2);
+    figures.push_back(&wk1);
+    figures.push_back(&wq1);
+
+    load(figures, 'W');
+}

+ 28 - 0
Chessengine.h

@@ -0,0 +1,28 @@
+#ifndef _CHESSENGINE_H
+#define _CHESSENGINE_H
+
+#include "Player.h"
+#include "Figure.h"
+#include "Chessboard.h"
+#include <vector>
+
+class Chessengine
+{
+    private:
+    char turn;
+    Player* player1;
+    Player* player2;
+    Player* player;
+    Chessboard board;
+
+    bool king_on_board(char color);
+
+    public:
+    Chessengine(Player* p1, Player* p2);
+    void load(vector<Figure*> figures, char turn);
+    void load_base();
+
+    void start();
+};
+
+#endif

+ 9 - 0
Makefile

@@ -1,9 +1,18 @@
+game: Position.o Pawn.o Horse.o Bishop.o Rook.o King.o Queen.o Cell.o Chessboard.o RealPlayer.o Chessengine.o game.cpp
+	g++ Position.o Pawn.o Horse.o Bishop.o Rook.o King.o Queen.o Cell.o Chessboard.o RealPlayer.o Chessengine.o game.cpp
+
 test_figures: Position.o Pawn.o Horse.o Bishop.o Rook.o King.o Queen.o test_figures.cpp
 	g++ Position.o Pawn.o Horse.o Bishop.o Rook.o King.o Queen.o test_figures.cpp
 
 test_board: Position.o Pawn.o Horse.o Bishop.o Rook.o King.o Queen.o Cell.o Chessboard.o test_chessboard.cpp
 	g++ Position.o Pawn.o Horse.o Bishop.o Rook.o King.o Queen.o Cell.o Chessboard.o test_chessboard.cpp
 
+Chessengine.o: Chessengine.cpp
+	g++ -c Chessengine.cpp
+
+RealPlayer.o: RealPlayer.cpp
+	g++ -c RealPlayer.cpp
+
 Chessboard.o: Chessboard.cpp
 	g++ -c Chessboard.cpp
 

+ 2 - 1
Pawn.cpp

@@ -17,7 +17,8 @@ vector<Position> Pawn::get_moves()
     vector<Position> moves;
     Position p;
     try {
-        p = Position(position.posSym, (char)(position.posNum+1));
+        if (color == 'W') p = Position(position.posSym, (char)(position.posNum+1));
+        if (color == 'B') p = Position(position.posSym, (char)(position.posNum-1));
     }
     catch (const std::invalid_argument) {}
     moves.push_back(p);

+ 14 - 0
Player.h

@@ -0,0 +1,14 @@
+#ifndef _PLAYER_H
+#define _PLAYER_H
+
+#include "Chessboard.h"
+#include <utility>
+
+
+class Player
+{
+    public:
+    virtual std::pair<Position, Position> get_move(Chessboard board, char color) = 0;
+};
+
+#endif

+ 20 - 0
RealPlayer.cpp

@@ -0,0 +1,20 @@
+#include "RealPlayer.h"
+#include "Position.h"
+#include <iostream>
+#include <utility>
+#include <stdexcept>
+
+std::pair<Position, Position> RealPlayer::get_move(Chessboard board, char color)
+{
+    char pos1[2];
+    char pos2[2];
+
+    std::cout << "Please enter your move: ";
+    std::cin >> pos1;
+    std::cin >> pos2;
+
+    if (board.get_cell(Position(pos1[0], pos1[1])).get_figure() == nullptr or board.get_cell(Position(pos1[0], pos1[1])).get_figure()->get_color() != color )
+        throw std::invalid_argument("Player Plays Against the Rules");
+
+    return std::pair<Position, Position>(Position(pos1[0], pos1[1]), Position(pos1[2], pos1[3]));
+}

+ 15 - 0
RealPlayer.h

@@ -0,0 +1,15 @@
+#ifndef _REALPLAYER_H
+#define _REALPLAYER_H
+
+#include "Player.h"
+#include <utility>
+
+class RealPlayer: public Player
+{
+    public:
+    RealPlayer(){};
+
+    virtual std::pair<Position, Position> get_move(Chessboard board, char color);
+};
+
+#endif

+ 97 - 0
game.cpp

@@ -0,0 +1,97 @@
+#include "Chessboard.h"
+#include "Chessengine.h"
+#include "RealPlayer.h"
+#include "Position.h"
+#include "Figure.h"
+#include "Pawn.h"
+#include "Horse.h"
+#include "Bishop.h"
+#include "Rook.h"
+#include "Queen.h"
+#include "King.h"
+#include <iostream>
+#include <vector>
+
+int main() {
+    vector<Figure*> figures;
+    Pawn bp1(Position('A', '7'), 'B');
+    Pawn bp2(Position('B', '7'), 'B');
+    Pawn bp3(Position('C', '7'), 'B');
+    Pawn bp4(Position('D', '7'), 'B');
+    Pawn bp5(Position('E', '7'), 'B');
+    Pawn bp6(Position('F', '7'), 'B');
+    Pawn bp7(Position('G', '7'), 'B');
+    Pawn bp8(Position('H', '7'), 'B');
+    Rook br1(Position('A', '8'), 'B');
+    Rook br2(Position('H', '8'), 'B');
+    Horse bh1(Position('B', '8'), 'B');
+    Horse bh2(Position('G', '8'), 'B');
+    Bishop bb1(Position('C', '8'), 'B');
+    Bishop bb2(Position('F', '8'), 'B');
+    King bk1(Position('E', '8'), 'B');
+    Queen bq1(Position('D', '8'), 'B');
+
+    figures.push_back(&bp1);
+    figures.push_back(&bp2);
+    figures.push_back(&bp3);
+    figures.push_back(&bp4);
+    figures.push_back(&bp5);
+    figures.push_back(&bp6);
+    figures.push_back(&bp7);
+    figures.push_back(&bp8);
+    figures.push_back(&br1);
+    figures.push_back(&br2);
+    figures.push_back(&bh1);
+    figures.push_back(&bh2);
+    figures.push_back(&bb1);
+    figures.push_back(&bb2);
+    figures.push_back(&bk1);
+    figures.push_back(&bq1);
+
+    // Создание белых
+    Pawn wp1(Position('A', '2'), 'W');
+    Pawn wp2(Position('B', '2'), 'W');
+    Pawn wp3(Position('C', '2'), 'W');
+    Pawn wp4(Position('D', '2'), 'W');
+    Pawn wp5(Position('E', '2'), 'W');
+    Pawn wp6(Position('F', '2'), 'W');
+    Pawn wp7(Position('G', '2'), 'W');
+    Pawn wp8(Position('H', '2'), 'W');
+    Rook wr1(Position('A', '1'), 'W');
+    Rook wr2(Position('H', '1'), 'W');
+    Horse wh1(Position('B', '1'), 'W');
+    Horse wh2(Position('G', '1'), 'W');
+    Bishop wb1(Position('C', '1'), 'W');
+    Bishop wb2(Position('F', '1'), 'W');
+    King wk1(Position('E', '1'), 'W');
+    Queen wq1(Position('D', '1'), 'W');
+
+    figures.push_back(&wp1);
+    figures.push_back(&wp2);
+    figures.push_back(&wp3);
+    figures.push_back(&wp4);
+    figures.push_back(&wp5);
+    figures.push_back(&wp6);
+    figures.push_back(&wp7);
+    figures.push_back(&wp8);
+    figures.push_back(&wr1);
+    figures.push_back(&wr2);
+    figures.push_back(&wh1);
+    figures.push_back(&wh2);
+    figures.push_back(&wb1);
+    figures.push_back(&wb2);
+    figures.push_back(&wk1);
+    figures.push_back(&wq1);
+
+    RealPlayer player1;
+    RealPlayer player2;
+
+    Chessengine engine(&player1, &player2);
+
+    engine.load(figures, 'W');
+    // engine.load_base();
+
+    engine.start();
+    
+    return 0;
+}