-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDriver.cpp
More file actions
167 lines (141 loc) · 4.48 KB
/
Driver.cpp
File metadata and controls
167 lines (141 loc) · 4.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#include "Driver.h"
#include <iostream>
#include "Chessboard.h"
#include "Position.h"
using namespace std;
Driver::Driver(int size, bool use_my_chessman) : position(Position{ 0, 0 }), board(new Chessboard{ size, use_my_chessman })
{
}
Driver::~Driver()
{
delete board;
}
void Driver::generate_new_position()
{
position.row = rand() % board->get_size();
position.col = rand() % board->get_size();
}
void Driver::print_start_turn()
{
cout << "\n\n\033[1;47;35m--- --- --- --- --- NEXT TURN --- --- --- --- ---\033[0m\t\n\n" << endl;
if (board->is_whites_turn())
cout << "\033[3;100;30mWhite's turn!\033[0m ";
else
cout << "\033[3;100;30mBlack's turn!\033[0m ";
cout << "Turn: " << board->get_turn_counter() << endl;
}
void Driver::print_game_over()
{
if (board->is_game_over())
cout << "\x1B[32m" << (board->is_whites_turn() ? "White" : "Black") << " won in " << board->get_turn_counter() << " turns!\033[0m\t\t" << endl;
}
void Driver::move_selection()
{
board->move_selection_to(position.row, position.col);
board->clear_selection();
board->show();
}
void Driver::select_piece()
{
bool valid_piece_selected = false;
do
{
cout << "\033[3;100;30mLet's select a chessman.\033[0m " << endl;
cout << "\tInput position.row (number between 1 and " << board->get_size() << "):" << endl;
cin >> position.row;
cout << "\tInput position.col (number between 1 and " << board->get_size() << "):" << endl;
cin >> position.col;
if (position.row > board->get_size() || position.col > board->get_size()) {
cout << "\x1B[31mInput not in range.\033[0m\t\t" << endl;
continue;
}
position.col--;
position.row--;
valid_piece_selected = board->can_select_piece(position.row, position.col);
if (!valid_piece_selected) {
cout << "\x1B[31mSelection not possible! Try again.\033[0m\t\t" << endl;
continue;
}
board->select_piece(position.row, position.col);
valid_piece_selected = board->can_move_selection();
if (!valid_piece_selected) {
cout << "\x1B[31mSelection not possible, because the selected piece can't move anywhere at the moment! Try again.\033[0m\t\t" << endl;
board->clear_selection();
}
} while (!valid_piece_selected);
board->show();
}
void Driver::select_destination()
{
bool valid_destination_selected = false;
do
{
cout << "\033[3;100;30mLet's select a destination.\033[0m " << endl;
cout << "\tInput position.row (number between 1 and " << board->get_size() << "):" << endl;
cin >> position.row;
cout << "\tInput position.col (number between 1 and " << board->get_size() << "):" << endl;
cin >> position.col;
if (position.row > board->get_size() || position.col > board->get_size()) {
cout << "\x1B[31mInput not in range.\033[0m\t\t" << endl;
continue;
}
position.col--;
position.row--;
valid_destination_selected = board->can_move_selection_to(position.row, position.col);
if (!valid_destination_selected)
cout << "\x1B[31mMove not possible! Try again.\033[0m\t\t" << endl;
} while (!valid_destination_selected);
}
void Driver::select_piece_simulation()
{
bool valid_piece_selected = false;
cout << "\033[3;100;30mLet's select a chessman.\033[0m " << endl;
do
{
generate_new_position();
valid_piece_selected = board->can_select_piece(position.row, position.col);
if (!valid_piece_selected) {
continue;
}
board->select_piece(position.row, position.col);
valid_piece_selected = board->can_move_selection();
if (!valid_piece_selected) {
board->clear_selection();
}
} while (!valid_piece_selected);
cout << "\tSelected piece at position.row (" << position.row << ") and position.col (" << position.col << ")" << endl;
board->show();
}
void Driver::select_destination_simulation()
{
bool valid_destination_selected = false;
cout << "\033[3;100;30mLet's select a destination.\033[0m " << endl;
do
{
generate_new_position();
valid_destination_selected = board->can_move_selection_to(position.row, position.col);
} while (!valid_destination_selected);
cout << "\tSelected destination at position.row (" << position.row << ") and position.col (" << position.col << ")" << endl;
}
void Driver::start_simulation() {
// this is our main game loop
while (!board->is_game_over()) {
print_start_turn();
select_piece_simulation();
select_destination_simulation();
move_selection();
}
print_game_over();
}
void Driver::start_game()
{
board->show();
// this is our main game loop
while (!board->is_game_over()) {
print_start_turn();
select_piece();
select_destination();
move_selection();
}
print_game_over();
}