Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Train branch #28

Open
wants to merge 20 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
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
14 changes: 7 additions & 7 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
CXX = g++
CXXFLAGS = -std=c++17 -O2 -pedantic -Wall -Wextra -Werror
CXXFLAGS = -std=c++17 -O2 -pedantic -Wall -Wextra -Werror -fno-stack-protector -fstack-protector
LDFLAGS = -lexpat -lsfml-network -lsfml-system -lsfml-window -lsfml-graphics -lpthread -lcppunit
INC = -Inetwork -Imodel -Iui -Icontroller -Iai

Expand Down Expand Up @@ -48,22 +48,22 @@ $(MAIN): $(OBJDIR) $(OBJDIR)/$(MAIN).o

$(MAIN_TEST_MODEL): $(OBJDIR) $(OBJDIR)/$(MAIN_TEST_MODEL).o

$(OBJDIR)/%.o: $(SRCDIR_MODEL)/%.cpp
$(OBJDIR)/%.o: $(SRCDIR_MODEL)/%.cpp $(SRCDIR_MODEL)/%.hpp
$(CXX) $(CXXFLAGS) $(INC) -c -MMD -o $@ $<

$(OBJDIR)/%.o: $(SRCDIR_NETWORK)/%.cpp
$(OBJDIR)/%.o: $(SRCDIR_NETWORK)/%.cpp $(SRCDIR_NETWORK)/%.hpp
$(CXX) $(CXXFLAGS) $(INC) -c -MMD -o $@ $<

$(OBJDIR)/%.o: $(SRCDIR_UI)/%.cpp
$(OBJDIR)/%.o: $(SRCDIR_UI)/%.cpp $(SRCDIR_UI)/%.hpp
$(CXX) $(CXXFLAGS) $(INC) -c -MMD -o $@ $<

$(OBJDIR)/%.o: $(SRCDIR_CONTROLLER)/%.cpp
$(OBJDIR)/%.o: $(SRCDIR_CONTROLLER)/%.cpp $(SRCDIR_CONTROLLER)/%.hpp
$(CXX) $(CXXFLAGS) $(INC) -c -MMD -o $@ $<

$(OBJDIR)/%.o: $(SRCDIR_AI)/%.cpp
$(OBJDIR)/%.o: $(SRCDIR_AI)/%.cpp $(SRCDIR_AI)/%.hpp
$(CXX) $(CXXFLAGS) $(INC) -c -MMD -o $@ $<

$(OBJDIR)/%.o: $(SRCDIR_TEST_MODEL)/%.cpp
$(OBJDIR)/%.o: $(SRCDIR_TEST_MODEL)/%.cpp $(SRCDIR_TEST_MODEL)/%.hpp
$(CXX) $(CXXFLAGS) $(INC) -c -MMD -o $@ $<

$(OBJDIR)/$(MAIN).o: $(MAIN).cpp
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Project from our (with sergeypospelov and Yurafobus1) team at spring 2020.

`sudo apt-get install libtclap-dev`

install SFML
Для установки библиотеки sfml надо в командной строке ввести:

`sudo apt-get install libsfml-dev`

Expand Down
243 changes: 160 additions & 83 deletions ai/CompPlayer.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
#include "CompPlayer.hpp"
#include <algorithm>
#include <iostream>
#include <thread>

static const int INF = (int)1e9;

int cnt1 = 0, cnt2 = 0;

CompPlayer::CompPlayer(number_of_player turn, int seconds, int deep)
: controller::IPlayer(turn), seconds_(seconds), deep_(deep) {}

Expand Down Expand Up @@ -32,8 +35,44 @@ bool CompPlayer::send_move(const BoardCell &from, const BoardCell &to) {
return false;
}

int CompPlayer::score(GameState G) const {
int ordw = 0, ordb = 0, queenw = 0, queenb = 0, killw = 0, killb = 0;
int CompPlayer::score(const GameState &G) {
int white_position[8][8] =
{
{64, 63, 62, 61, 60, 59, 58, 57},
{56, 55, 54, 53, 52, 51, 50, 49},
{48, 47, 46, 45, 44, 43, 42, 41},
{40, 39, 38, 37, 36, 35, 34, 33},
{32, 31, 30, 29, 28, 27, 26, 25},
{24, 23, 22, 21, 20, 19, 18, 17},
{16, 15, 14, 13, 12, 11, 10, 9},
{8, 7, 6, 5, 4, 3, 2, 1}
};

int black_position[8][8] =
{
{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}
};

int only_queen[8][8] =
{
{1, 2, 3, 4, 4, 3, 2, 1},
{9, 10, 11, 12, 12, 11, 10, 9},
{17, 18, 19, 20, 20, 19, 18, 17},
{25, 26, 27, 28, 28, 27, 26, 25},
{25, 26, 27, 28, 28, 27, 26, 25},
{17, 18, 19, 20, 20, 19, 18, 17},
{9, 10, 11, 12, 12, 11, 10, 9},
{1, 2, 3, 4, 4, 3, 2, 1}
};
int ordw = 0, ordb = 0, queenw = 0, queenb = 0;
int sum_with_w = 0, sum_with_b = 0, sum_out_w = 0, sum_out_b = 0;
for (int i = 0; i < G.SIZE; i++)
for (int j = 0; j < G.SIZE; j++) {
if (G.board[i][j] == 'w')
Expand All @@ -44,105 +83,143 @@ int CompPlayer::score(GameState G) const {
queenw++;
if (G.board[i][j] == 'B')
queenb++;
if ((G.board[i][j] == 'w' || G.board[i][j] == 'W') &&
G.kill(FIRST, BoardCell(i, j)))
killw++;
if ((G.board[i][j] == 'b' || G.board[i][j] == 'B') &&
G.kill(SECOND, BoardCell(i, j)))
killb++;
if (G.board[i][j] == 'w' || G.board[i][j] == 'W')
sum_with_w += white_position[i][j], sum_out_w += only_queen[i][j];
if (G.board[i][j] == 'b' || G.board[i][j] == 'B')
sum_with_b += black_position[i][j], sum_out_b += only_queen[i][j];
}
return ordw - ordb + (2 * queenw + 5) * (2 * queenw + 5) * (2 * queenw + 5) - (2 * queenb + 5) * (2 * queenb + 5) * (2 * queenb + 5) + (killw + 7) * (killw + 7) - (killb + 7) * (killb + 7);
if (ordw + ordb > 0)
return 100 * (ordw - ordb) + 1000 * (queenw - queenb) + sum_with_w - sum_with_b;
else
return 1000 * (queenw - queenb) + sum_out_w - sum_out_b;
}

std::pair<int, Move> CompPlayer::alpha_beta(GameState G, int alpha, int beta,
clock_t start_time, int seconds,
int deep, std::mt19937 gen) const {
state current = G.check_win();
if (current != GAME) {
if (current == DRAW)
return std::make_pair(0, Move());
if (current == FIRST_WIN)
return std::make_pair(INF, Move());
return std::make_pair(-INF, Move());
void CompPlayer::alpha_beta(const GameState &G, int alpha, int beta,
int deep, std::pair <int, Move> &total, bool flow) {
cnt1++;
if (G.move_to_draw >= G.DRAW_MOVE) {
total = std::make_pair(0, Move());
return;
}
if (deep == 0)
return std::make_pair(score(G), Move());
clock_t current_time = clock();
if (1.0 * (current_time - start_time) / CLOCKS_PER_SEC > seconds)
return std::make_pair(score(G), Move());

number_of_player player = G.who_moves();
std::vector<Move> kill;
std::vector<Move> ordinary;
if (deep == 0) {
total = std::make_pair(score(G), Move());
return;
}

bool exist_white = false, exist_black = false;
for (int i = 0; i < G.SIZE; i++)
for (int j = 0; j < G.SIZE; j++) {
std::vector<BoardCell> correct =
G.get_list_of_correct_moves(player, BoardCell(i, j));
for (int z = 0; z < (int)correct.size(); z++) {
if (G.is_kill(player, BoardCell(i, j), correct[z]))
kill.push_back(Move(BoardCell(i, j), correct[z]));
else
ordinary.push_back(Move(BoardCell(i, j), correct[z]));
}
if (G.board[i][j] == 'w' || G.board[i][j] == 'W')
exist_white = true;
if (G.board[i][j] == 'b' || G.board[i][j] == 'B')
exist_black = true;
}
if (!exist_white || !exist_black) {
if (exist_white)
total = std::make_pair(INF, Move());
else
total = std::make_pair(-INF, Move());
return;
}

//clock_t current_time = clock();
//if (1.0 * (current_time - start_time) / CLOCKS_PER_SEC > seconds)
//return std::make_pair(score(G), Move());

number_of_player player = G.who_moves();

std::vector <Move> moves;
int delta, up, down, lef, rig;
if (player == SECOND)
up = 0, down = G.SIZE, lef = 0, rig = G.SIZE, delta = 1;
else
up = G.SIZE - 1, down = -1, lef = G.SIZE - 1, rig = -1, delta = -1;

for (int i = up; i != down; i += delta)
for (int j = lef; j != rig; j += delta) {
std::vector<BoardCell> correct;
G.get_list_of_correct_moves(player, BoardCell(i, j), correct);
for (int z = 0; z < (int)correct.size(); z++)
moves.push_back(Move(BoardCell(i, j), correct[z]));
}
shuffle(kill.begin(), kill.end(), gen);
shuffle(ordinary.begin(), ordinary.end(), gen);

if (moves.empty()) {
if (player == FIRST)
total = std::make_pair(-INF, Move());
else
total = std::make_pair(INF, Move());
return;
}

Move best_move;
int current_score = (player == FIRST ? -INF : INF);
for (int i = 0; i < (int)kill.size(); i++) {
GameState cop = G;
cop.move(player, kill[i].from, kill[i].to);
std::pair<int, Move> result =
alpha_beta(cop, alpha, beta, start_time, seconds, deep, gen);
if (player == FIRST) {
if (result.first >= current_score) {
best_move = kill[i];
current_score = result.first;
}
alpha = std::max(alpha, result.first);
if (alpha > beta)
return std::make_pair(alpha, kill[i]);
} else {
if (result.first <= current_score) {
best_move = kill[i];
current_score = result.first;
bool fl = !G.find_kill(player);
std::vector <std::pair <int, Move> > res((int)moves.size());

if (flow) {
std::vector <std::thread> act;
for (int i = 0; i < (int)moves.size(); i++) {
GameState cop = G;
cop.move(player, moves[i].from, moves[i].to);
act.push_back(std::thread(alpha_beta, std::ref(cop), alpha, beta,
deep - fl, std::ref(res[i]), false));
if ((i+1) % NUMBER_OF_THREADS == 0) {
for (int j = 0; j < (int)act.size(); j++)
act[j].join();
act.clear();
}
beta = std::min(beta, result.first);
if (alpha > beta)
return std::make_pair(beta, kill[i]);
}
for (int j = 0; j < (int)act.size(); j++)
act[j].join();
act.clear();
}
for (int i = 0; i < (int)ordinary.size(); i++) {
GameState cop = G;
cop.move(player, ordinary[i].from, ordinary[i].to);
std::pair<int, Move> result =
alpha_beta(cop, alpha, beta, start_time, seconds, deep - 1, gen);
if (player == FIRST) {
if (result.first >= current_score) {
best_move = ordinary[i];
current_score = result.first;

// std::cerr << "OK!" << std::endl;

for (int i = 0; i < (int)moves.size(); i++) {
if (!flow) {
GameState cop = G;
cop.move(player, moves[i].from, moves[i].to);
alpha_beta(cop, alpha, beta, deep - fl, res[i], false);
}
alpha = std::max(alpha, result.first);
if (alpha > beta)
return std::make_pair(alpha, ordinary[i]);
} else {
if (result.first <= current_score) {
best_move = ordinary[i];
current_score = result.first;

if (player == FIRST) {
if (res[i].first >= current_score) {
best_move = moves[i];
current_score = res[i].first;
}
alpha = std::max(alpha, res[i].first);
if (alpha > beta) {
total = std::make_pair(alpha, moves[i]);
cnt2++;
return;
}
} else {
if (res[i].first <= current_score) {
best_move = moves[i];
current_score = res[i].first;
}
beta = std::min(beta, res[i].first);
if (alpha > beta) {
total = std::make_pair(beta, moves[i]);
cnt2++;
return;
}
}
beta = std::min(beta, result.first);
if (alpha > beta)
return std::make_pair(beta, ordinary[i]);
}
}
return std::make_pair(current_score, best_move);
total = std::make_pair(current_score, best_move);
return;
}

Move CompPlayer::get_next_move(GameState G, int seconds, int deep) const {
std::mt19937 gen(time(0));
Move CompPlayer::get_next_move(const GameState &G, int seconds, int deep) const {
std::pair<int, Move> result;
(void)seconds;
cnt1 = 0, cnt2 = 0;
clock_t start = clock();
std::pair<int, Move> result =
alpha_beta(G, -INF, INF, start, seconds, deep, gen);
std::cout << result.first << '\n';
alpha_beta(G, -INF, INF, deep, result, true);
clock_t finish = clock();
std::cout << 1.0 * (finish - start) / CLOCKS_PER_SEC << '\n' <<
cnt1 << ' ' << cnt2 << '\n';
return result.second;
}
12 changes: 5 additions & 7 deletions ai/CompPlayer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,20 @@

#include "GameState.hpp"
#include "Player.hpp"
#include <ctime>
#include <random>

class CompPlayer : public controller::IPlayer {
private:
int seconds_;
int deep_;

std::pair<int, Move> alpha_beta(GameState G, int alpha, int beta,
clock_t start_time, int seconds, int deep,
std::mt19937 gen) const;
int score(GameState G) const;
Move get_next_move(GameState G, int seconds, int deep) const;
static void alpha_beta(const GameState &G, int alpha, int beta,
int deep, std::pair <int, Move> &total, bool flow);
static int score(const GameState &G);
Move get_next_move(const GameState &G, int seconds, int deep) const;

mutable GameState gs;

static const int NUMBER_OF_THREADS = 4;
public:
CompPlayer(number_of_player, int seconds, int deep);

Expand Down
Binary file added fonts/1.otf
Binary file not shown.
6 changes: 5 additions & 1 deletion main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,10 @@ int main(int argc, char **argv) { // TODO: make own main for every game mode

game_state = game_test.return_current_state();

if (mode == "ai") {
(dynamic_cast<CompPlayer *>(enemy))->set_game_state(game_state);
}

while (!core.get_events().empty()) {
controller::Event *event = core.get_events().front().get();
controller::MoveEvent *move =
Expand All @@ -115,7 +119,7 @@ int main(int argc, char **argv) { // TODO: make own main for every game mode

controller::GiveUpEvent *giveUp =
dynamic_cast<controller::GiveUpEvent *>(event);
controller::process(giveUp, enemy, player, game_test, mode);
controller::process(giveUp, player, enemy, game_test, mode);

// currently, there is only MoveEvent.
// TODO: need to process another events!
Expand Down
Loading