BlueBubble 1.0
A recommendation algorithm for movies based on a Netlfix database
Loading...
Searching...
No Matches
game.cc
Go to the documentation of this file.
1#include "board.h"
2#include <iostream>
3#include <string>
4
7
10int main(){
11 board chess_board;
12 std::string movement;
13 bool game_end = false;
14 bool correct_move = false;
15 coordinates start(0,0);
16 coordinates end(0,0);
17 bool white_move = true;
18 winner_type game_result;
19 board *chess_board_copy;
20 while(!game_end){
21 chess_board.print();
22 movement = "";
23 correct_move = false;
24 while(!correct_move){
25 if(white_move){
28 std::cout << "White";
30 std::cout << " turn. ";
31 }else{
34 std::cout << "Black";
36 std::cout << " turn. ";
37 }
38 // Check warning for current player king
39 if(chess_board.is_check(white_move)){
40 std::cout << std::endl;
42 std::cout << "Warning : Check state" << std::endl;
44 }
45 std::cout << "What move ? " << std::endl;
46 std::cin >> movement;
47 if(movement == "/quit" || movement == "/resign" || movement == "/draw"){
48 //The movement input is correct and a game ending one
49 game_end = true;
50 correct_move = true;
51 if(movement == "/quit"){
52 game_result = interrupt;
53 }
54 if(movement == "/resign"){
55 if(white_move){
56 game_result = win_black;
57 }else{
58 game_result = win_white;
59 }
60 }
61 if(movement == "/draw"){
62 game_result = draw;
63 }
64 }else{
65 if(correct_input(movement)){
66 //The movement input is correct
67 if(standard_input(movement)){
68 //Standard movement case
69 coordinates start = get_coord_from_string(movement.substr(0, 2));
70 coordinates end = get_coord_from_string(movement.substr(2, 2));
71 if(chess_board.is_legal(start,end,white_move)){
72 //Apply the movement on a chess board copy and verify the check state of the current player's king
73 chess_board_copy = new board(chess_board);
74 chess_board_copy->move(start,end);
75 if(chess_board_copy->is_check(white_move)){
76 //The move is making the active player in check state, illegal
77 std::cout << "Illegal move. Your king will be checked" << std::endl;
78 }else{
79 chess_board.move(start,end);
80 correct_move = true;
81 if(chess_board.is_promotion_needed(end)){
82 chess_board.print();
83 // Promotion selection
84 std::cout << "Promotion. Choose a piece to substitue the pawn : B/N/R/Q" << std::endl;
85 std::string chose_promotion = "";
86 while((chose_promotion != "B") && (chose_promotion != "N")
87 && (chose_promotion != "R") && (chose_promotion != "Q")){
88 std::cin >> chose_promotion;
89 if((chose_promotion != "B") && (chose_promotion != "N")
90 && (chose_promotion != "R") && (chose_promotion != "Q")){
91 std::cout << "Wrong input format. Input to be among : B,N,R and Q" << std::endl;
92 }
93 }
94 // We promote to the right piece
95 if(chose_promotion == "B"){
96 chess_board.promote(end,bishop_type);
97 }
98 if(chose_promotion == "N"){
99 chess_board.promote(end,knight_type);
100 }
101 if(chose_promotion == "R"){
102 chess_board.promote(end,rook_type);
103 }
104 if(chose_promotion == "Q"){
105 chess_board.promote(end,queen_type);
106 }
107 }
108 }
109 delete chess_board_copy;
110 }else{
111 std::cout << "Illegal move." << std::endl;
112 }
113 }
114 if(king_castle_input(movement)){
115 //King-side castling move
116 if(chess_board.is_king_castling_legal(white_move)){
117 //Apply the movement on a chess board copy and verify the check state of the current player's king
118 chess_board_copy = new board(chess_board);
119 chess_board_copy->king_castle(white_move);
120 if(chess_board_copy->is_check(white_move)){
121 //The move is making the active player in check state, illegal
122 std::cout << "Illegal move. Your king will be checked" << std::endl;
123 }else{
124 chess_board.king_castle(white_move);
125 correct_move = true;
126 }
127 delete chess_board_copy;
128 }else{
129 std::cout << "Illegal move." << std::endl;
130 }
131 }
132 if(queen_castle_input(movement)){
133 //Queen-side castling move
134 if(chess_board.is_queen_castling_legal(white_move)){
135 //Apply the movement on a chess board copy and verify the check state of the current player's king
136 chess_board_copy = new board(chess_board);
137 chess_board_copy->queen_castle(white_move);
138 if(chess_board_copy->is_check(white_move)){
139 //The move is making the active player in check state, illegal
140 std::cout << "Illegal move. Your king will be checked" << std::endl;
141 }else{
142 chess_board.queen_castle(white_move);
143 correct_move = true;
144 }
145 delete chess_board_copy;
146 }else{
147 std::cout << "Illegal move." << std::endl;
148 }
149 }
150 }else{
151 //The movement input is wrong
152 std::cout << "Wrong input format. Format need to be in algrebic notation : b1c3 for example. 0-0-0 and 0-0 for the castlings" << std::endl;
153 }
154 }
155 }
156 //Checkmate and pat detection
157 if(chess_board.is_checkmate_or_pat(white_move)){
158 game_end = true;
159 chess_board.print();
160 if(white_move){
161 if(chess_board.is_check(!white_move)){
162 game_result = win_white;
165 std::cout << "White win.";
167 }else{
168 game_result = draw;
169 std::cout << "Draw.";
171 }
172 }else{
173 if(chess_board.is_check(!white_move)){
174 game_result = win_black;
177 std::cout << "Black win.";
179 }else{
180 game_result = draw;
181 std::cout << "Draw.";
183 }
184 }
185 }
186 white_move = !white_move;
187 }
188 chess_board.canonical_print();
189 switch (game_result)
190 {
191 case win_white:
192 std::cout << " 1-0" << std::endl;
193 break;
194
195 case win_black:
196 std::cout << " 0-1" << std::endl;
197 break;
198
199 case draw:
200 std::cout << " 1/2-1/2" << std::endl;
201 break;
202
203 case interrupt:
204 std::cout << " ?-?" << std::endl;
205 break;
206
207 }
208}
void print_neutral_endl()
print a neutral font color and background color new line
Definition board.cc:129
coordinates get_coord_from_string(std::string move)
Retrieve coordinates from a string.
Definition board.cc:366
A class representing the chess board.
Definition board.h:19
bool is_promotion_needed(coordinates coord)
check if a promotion is needed
Definition board.cc:539
void canonical_print()
print the board in canonical form
Definition board.cc:225
bool is_check(bool white_king)
check if the selected player is in check state
Definition board.cc:402
bool is_king_castling_legal(bool white_move)
check if the king-side castling move is legal, without checking it in regards to king check state
Definition board.cc:421
bool is_checkmate_or_pat(bool white_move)
check if the other player can move without being in a check state
Definition board.cc:546
bool is_legal(coordinates move_from, coordinates move_to, bool white_move)
Check if a move is legal, without checking it in regards to king check state.
Definition board.cc:309
void print()
print the board
Definition board.cc:151
bool is_queen_castling_legal(bool white_move)
check if the queen-side castling move is legal, without checking it in regards to king check state
Definition board.cc:443
void move(coordinates move_from, coordinates move_to)
Move a piece from one position to another, supposing that the move is legal.
Definition board.cc:259
void king_castle(bool white_move)
perform the king-side castling move
Definition board.cc:464
void promote(coordinates to_promote, piece_type new_type)
Perform a promotion on the specified coordinates. Don't check anything.
Definition board.cc:493
void queen_castle(bool white_move)
perform the queen-side castling move
Definition board.cc:477
a class representing coordinates
Definition coordinates.h:5
#define BACKGROUND_COLOR1
Definition config.h:8
#define WARNING_TEXT_COLOR
Definition config.h:11
#define PIECES_COLOR
Definition config.h:10
winner_type
an enumeration to represent the game result
Definition game.cc:6
@ interrupt
Definition game.cc:6
@ win_white
Definition game.cc:6
@ win_black
Definition game.cc:6
@ draw
Definition game.cc:6
int main()
The main function.
Definition game.cc:10
@ knight_type
Definition piece.h:8
@ queen_type
Definition piece.h:8
@ rook_type
Definition piece.h:8
@ bishop_type
Definition piece.h:8
bool king_castle_input(std::string const &cmd)
Check if an input correponds to a king castling move.
Definition utilities.cc:104
void terminal_reset_all()
reset all terminal attributes
Definition utilities.cc:79
bool standard_input(std::string const &cmd)
Check if an input corresponds to a standard move.
Definition utilities.cc:96
bool correct_input(std::string const &cmd)
Check if an input is conform.
Definition utilities.cc:86
void terminal_set_background_color(terminal_color color)
set the terminal background color to the input
Definition utilities.cc:48
bool queen_castle_input(std::string const &cmd)
Check if an input corresponds to a queen castling move.
Definition utilities.cc:112
void terminal_set_text_color(terminal_color color)
set the terminal font color to the input
Definition utilities.cc:16