Chess
chessboard.h
Go to the documentation of this file.
1 
8 #pragma once
9 #include <memory>
10 #include <optional>
11 #include <set>
12 #include <vector>
13 
14 #include "graphics.h"
15 #include "piece.h"
16 
18 struct Tile {
19  bool has_piece() const;
20  std::optional<Piece> piece;
21 };
22 
24 class Chessboard {
25  public:
26  Chessboard();
27  ~Chessboard();
28  Chessboard(Chessboard &&other); // move constructor
29  Chessboard &operator=(Chessboard &&other); // move assignment
30  Chessboard(const Chessboard &other); // copy constructor
31  Chessboard &operator=(const Chessboard &other); // copy assignment
32  bool is_valid_move(int start, int end);
33  bool is_check();
34  bool is_checkmate();
35 
37  std::vector<Tile> chessboard;
46  int w_num_pieces = 16;
47  int b_num_pieces = 16;
48 
49  bool move_piece(int start, int end);
50  void move_piece_temp(int start, int end);
51 
52  bool in_bounds(int pos);
53  bool in_bounds(int row, int col);
54 
55  private:
56  void fill_starting_tiles();
57  void fill_test_tiles();
58  void place_starting_b_pieces();
59  void place_starting_w_pieces();
60 
62  std::vector<std::pair<int, int>> get_all_pseudo_moves();
64  std::vector<std::pair<int, int>> get_all_legal_moves(std::vector<std::pair<int, int>> pseudo_legal);
66  void update_piece_counts(const Tile &t);
68  void recalculate_attackable_tiles(); // MUST BE CALLED AFTER EVERY MOVE
70  std::set<int> attackable_by_white;
72  std::set<int> attackable_by_black;
74  void swap_turn();
75 
76  bool test = false;
77 
78  std::vector<std::pair<int, int>> reshape_vector(std::vector<int> &possible_moves, int start);
79 };
int w_king_index
Store for a fast way to test for checks/mate.
Definition: chessboard.h:43
bool white_to_move
True: white&#39;s turn | False: black&#39;s turn.
Definition: chessboard.h:41
Used to create, store, and make changes to a game state.
Definition: chessboard.h:24
Comprises a Chessboard&#39;s game state, can hold a Piece.
Definition: chessboard.h:18
int b_king_index
Store for a fast way to test for checks/mate.
Definition: chessboard.h:45
int selected_piece_index
Used for highlighting within graphics and also executing moves.
Definition: chessboard.h:39
Handles all SDL2 functionalities require to visualize the game.
Finds possible moves given a Piece.
std::vector< Tile > chessboard
Game state.
Definition: chessboard.h:37