Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions Game/Board.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@

using namespace std;

class Board


class Board // drawing the board classs
{
public:
Board() = default;
Expand Down Expand Up @@ -370,7 +372,7 @@ class Board
vector<vector<bool>> is_highlighted_ = vector<vector<bool>>(8, vector<bool>(8, 0));
// matrix of possible moves
// 1 - white, 2 - black, 3 - white queen, 4 - black queen
vector<vector<POS_T>> mtx = vector<vector<POS_T>>(8, vector<POS_T>(8, 0));
vector<vector<POS_T>> mtx = vector<vector<POS_T>>(8, vector<POS_T>(8, 0)); // ������� �����
// series of beats for each move
vector<int> history_beat_series;
};
12 changes: 6 additions & 6 deletions Game/Config.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,26 @@ using json = nlohmann::json;

#include "../Models/Project_path.h"

class Config
class Config // processing game settings
{
public:
Config()
Config() // creating settings
{
reload();
}

void reload()
void reload() // loading settings from file
{
std::ifstream fin(project_path + "settings.json");
std::ifstream fin(project_path + "settings.json"); // path to the .json game settings file
fin >> config;
fin.close();
}

auto operator()(const string &setting_dir, const string &setting_name) const
auto operator()(const string &setting_dir, const string &setting_name) const // getting setting from setting_dir and setting_name
{
return config[setting_dir][setting_name];
}

private:
private: // save
json config;
};
42 changes: 21 additions & 21 deletions Game/Game.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
class Game
{
public:
Game() : board(config("WindowSize", "Width"), config("WindowSize", "Hight")), hand(&board), logic(&board, &config)
Game() : board(config("WindowSize", "Width"), config("WindowSize", "Hight")), hand(&board), logic(&board, &config) // constructor
{
ofstream fout(project_path + "log.txt", ios_base::trunc);
fout.close();
Expand All @@ -20,7 +20,7 @@ class Game
// to start checkers
int play()
{
auto start = chrono::steady_clock::now();
auto start = chrono::steady_clock::now(); // program start timer
if (is_replay)
{
logic = Logic(&board, &config);
Expand All @@ -35,28 +35,28 @@ class Game

int turn_num = -1;
bool is_quit = false;
const int Max_turns = config("Game", "MaxNumTurns");
while (++turn_num < Max_turns)
const int Max_turns = config("Game", "MaxNumTurns"); // getting the maximum number of moves
while (++turn_num < Max_turns) // the beginning of the game, we play one by one until the move number becomes the maximum (indicated in settings.json)
{
beat_series = 0;
logic.find_turns(turn_num % 2);
if (logic.turns.empty())
break;
logic.Max_depth = config("Bot", string((turn_num % 2) ? "Black" : "White") + string("BotLevel"));
if (!config("Bot", string("Is") + string((turn_num % 2) ? "Black" : "White") + string("Bot")))
if (!config("Bot", string("Is") + string((turn_num % 2) ? "Black" : "White") + string("Bot"))) // determine whether a bot is walking or not
{
auto resp = player_turn(turn_num % 2);
if (resp == Response::QUIT)
auto resp = player_turn(turn_num % 2); // if human go
if (resp == Response::QUIT) // exit the program
{
is_quit = true;
break;
}
else if (resp == Response::REPLAY)
else if (resp == Response::REPLAY) // replay game
{
is_replay = true;
break;
}
else if (resp == Response::BACK)
else if (resp == Response::BACK) // move back
{
if (config("Bot", string("Is") + string((1 - turn_num % 2) ? "Black" : "White") + string("Bot")) &&
!beat_series && board.history_mtx.size() > 2)
Expand All @@ -73,9 +73,9 @@ class Game
}
}
else
bot_turn(turn_num % 2);
bot_turn(turn_num % 2); // else bot go
}
auto end = chrono::steady_clock::now();
auto end = chrono::steady_clock::now(); // Program end timer
ofstream fout(project_path + "log.txt", ios_base::app);
fout << "Game time: " << (int)chrono::duration<double, milli>(end - start).count() << " millisec\n";
fout.close();
Expand Down Expand Up @@ -104,14 +104,14 @@ class Game
}

private:
void bot_turn(const bool color)
void bot_turn(const bool color) // bot go
{
auto start = chrono::steady_clock::now();
auto start = chrono::steady_clock::now(); // Bot start timer

auto delay_ms = config("Bot", "BotDelayMS");
// new thread for equal delay for each turn
thread th(SDL_Delay, delay_ms);
auto turns = logic.find_best_turns(color);
auto turns = logic.find_best_turns(color); // find the best move for the bot depending on the color
th.join();
bool is_first = true;
// making moves
Expand All @@ -126,33 +126,33 @@ class Game
board.move_piece(turn, beat_series);
}

auto end = chrono::steady_clock::now();
auto end = chrono::steady_clock::now(); // Bot end timer
ofstream fout(project_path + "log.txt", ios_base::app);
fout << "Bot turn time: " << (int)chrono::duration<double, milli>(end - start).count() << " millisec\n";
fout.close();
}

Response player_turn(const bool color)
Response player_turn(const bool color) // enum class, action options
{
// return 1 if quit
vector<pair<POS_T, POS_T>> cells;
for (auto turn : logic.turns)
{
cells.emplace_back(turn.x, turn.y);
}
board.highlight_cells(cells);
board.highlight_cells(cells); // cell illumination
move_pos pos = {-1, -1, -1, -1};
POS_T x = -1, y = -1;
// trying to make first move
while (true)
while (true) // endless click loop
{
auto resp = hand.get_cell();
auto resp = hand.get_cell(); // waiting for a click
if (get<0>(resp) != Response::CELL)
return get<0>(resp);
pair<POS_T, POS_T> cell{get<1>(resp), get<2>(resp)};

bool is_correct = false;
for (auto turn : logic.turns)
for (auto turn : logic.turns) // checking whether it is possible to go to the selected cell
{
if (turn.x == cell.first && turn.y == cell.second)
{
Expand Down Expand Up @@ -191,7 +191,7 @@ class Game
cells2.emplace_back(turn.x2, turn.y2);
}
}
board.highlight_cells(cells2);
board.highlight_cells(cells2); // cell
}
board.clear_highlight();
board.clear_active();
Expand Down
22 changes: 11 additions & 11 deletions Game/Hand.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
class Hand
{
public:
Hand(Board *board) : board(board)
Hand(Board *board) : board(board) // click functionality
{
}
tuple<Response, POS_T, POS_T> get_cell() const
Expand All @@ -18,39 +18,39 @@ class Hand
Response resp = Response::OK;
int x = -1, y = -1;
int xc = -1, yc = -1;
while (true)
while (true) // endless loop
{
if (SDL_PollEvent(&windowEvent))
if (SDL_PollEvent(&windowEvent)) // expect a click (uses the SDL library)
{
switch (windowEvent.type)
{
case SDL_QUIT:
case SDL_QUIT: // exit event
resp = Response::QUIT;
break;
case SDL_MOUSEBUTTONDOWN:
case SDL_MOUSEBUTTONDOWN: // click event
x = windowEvent.motion.x;
y = windowEvent.motion.y;
xc = int(y / (board->H / 10) - 1);
yc = int(x / (board->W / 10) - 1);
if (xc == -1 && yc == -1 && board->history_mtx.size() > 1)
{
resp = Response::BACK;
resp = Response::BACK; // go back event
}
else if (xc == -1 && yc == 8)
{
resp = Response::REPLAY;
resp = Response::REPLAY; // replay game event
}
else if (xc >= 0 && xc < 8 && yc >= 0 && yc < 8)
{
resp = Response::CELL;
resp = Response::CELL; // clicking on a cell event
}
else
{
xc = -1;
yc = -1;
}
break;
case SDL_WINDOWEVENT:
case SDL_WINDOWEVENT: // window resizing event
if (windowEvent.window.event == SDL_WINDOWEVENT_SIZE_CHANGED)
{
board->reset_window_size();
Expand All @@ -61,10 +61,10 @@ class Hand
break;
}
}
return {resp, xc, yc};
return {resp, xc, yc}; // if there was a click, then a response with coordinates or an option from the enum class is returned
}

Response wait() const
Response wait() const // at the end of the game, we expect further actions
{
SDL_Event windowEvent;
Response resp = Response::OK;
Expand Down
Loading