Skip to content

Commit

Permalink
Defensive profile switch
Browse files Browse the repository at this point in the history
  • Loading branch information
federico-terzi committed May 15, 2019
1 parent 2128cd3 commit 313d046
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 12 deletions.
24 changes: 17 additions & 7 deletions cli/GameManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ GameManager::GameManager(Connector &connector, PlayerProfile *defensive, PlayerP

void GameManager::set_aggressive(bool is_aggressive) {
this->is_aggressive = is_aggressive;
std::cout << "Switching to aggressive: " << is_aggressive << std::endl;
if (is_aggressive) {
std::cout << "Switching to AGGRESSIVE profile " << std::endl;
}else{
std::cout << "Switching to DEFENSIVE profile " << std::endl;
}
}


Expand All @@ -32,8 +36,10 @@ void GameManager::send_move(const Board &b) {
timer.update_start_time();
std::string move;
if (is_aggressive) {
std::cout << "Starting elaboration with AGGRESSIVE profile." << std::endl;
move = aggressive_profile->calculate_move(b, timer);
}else{
std::cout << "Starting elaboration with DEFENSIVE profile." << std::endl;
move = defensive_profile->calculate_move(b, timer);
}
connector.send_string(move);
Expand Down Expand Up @@ -65,7 +71,7 @@ void GameManager::game_loop() {
send_move(b);
turn_count++;
}else{

analyze_profile_change_policy(b, Player::BLACK);
}

Board b2;
Expand All @@ -76,15 +82,19 @@ void GameManager::game_loop() {
send_move(b2);
turn_count++;
}else{

analyze_profile_change_policy(b2, Player::WHITE);
}
}
}

void GameManager::analyze_board_status(const Board &b, Player player) {
if (turn_count == 2 && player == Player::WHITE) {
if (b.white_count < 8) {
set_aggressive(true);
void GameManager::analyze_profile_change_policy(const Board &b, Player player) {
if (player == Player::WHITE) {
if (turn_count == 2) {
int defence_score = b.count_black_defensive() + b.white_count;
// If the black player is defensive, start the aggressive strategy
if (defence_score > 9) {
set_aggressive(true);
}
}
}
}
2 changes: 1 addition & 1 deletion cli/GameManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class GameManager {
int turn_count = 0;

void set_aggressive(bool is_aggressive);
void analyze_board_status(const Board& b, Player player);
void analyze_profile_change_policy(const Board &b, Player player);
};


Expand Down
2 changes: 1 addition & 1 deletion cli/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ int main(int argc, char **argv) {
long after_memory = MemoryManager::get_stack_size();
std::cout << "Increased STACK size from "<< before_memory << " to " << after_memory << std::endl;

GameManager game_manager(connector, profilePair.defensive, profilePair.aggressive, config.player, config);
GameManager game_manager(connector, profilePair.defensive.get(), profilePair.aggressive.get(), config.player, config);
game_manager.game_loop();

}
6 changes: 3 additions & 3 deletions cli/profiles/ProfileManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
#include "PlayerProfile.h"

struct ProfilePair {
PlayerProfile* defensive;
PlayerProfile* aggressive;
std::unique_ptr<PlayerProfile> defensive;
std::unique_ptr<PlayerProfile> aggressive;
};

class ProfileManager {
Expand All @@ -22,7 +22,7 @@ class ProfileManager {
ProfilePair get_profile(const std::string& profile);
private:
ConfigSet config;
std::unordered_map<std::string, std::function<ProfilePair>> player_map;
std::unordered_map<std::string, std::function<ProfilePair(Player)>> player_map;
};


Expand Down
20 changes: 20 additions & 0 deletions lib/model/Board.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,18 @@ const uint16_t throne_mask[9] = {
0b0000000'000000000,
};

const uint16_t black_defensive_mask[9] = {
0b0000000'011000110,
0b0000000'111000111,
0b0000000'110000011,
0b0000000'000000000,
0b0000000'000000000,
0b0000000'000000000,
0b0000000'110000011,
0b0000000'111000111,
0b0000000'011000110,
};

const int KING_LOST = -100;

class Board {
Expand Down Expand Up @@ -232,6 +244,14 @@ class Board {
obstacle_rows[row] |= throne_mask[row];
}

inline int count_black_defensive() const {
int count = 0;
for (int i = 0; i<9; i++) {
count += BitUtils::popcount(black_cols[i] & black_defensive_mask[i]);
}
return count;
}

void load_board(const std::string &json_board);

friend std::ostream& operator<<(std::ostream &s, const Board &board){
Expand Down

0 comments on commit 313d046

Please sign in to comment.