summaryrefslogtreecommitdiffstats
path: root/ai.cpp
diff options
context:
space:
mode:
authorJoursoir <chat@joursoir.net>2021-02-20 13:11:12 +0000
committerJoursoir <chat@joursoir.net>2021-02-20 13:11:12 +0000
commit9a668c78bed7197e0b349d4bd2908a67bedd295f (patch)
tree3b94f44b7714c3c57d265be3b44ae79dad5046fe /ai.cpp
parent34e22b1d5ea04a9bb2d8b8219f5881c90aa8cc70 (diff)
downloadlp-gomoku-9a668c78bed7197e0b349d4bd2908a67bedd295f.tar.gz
lp-gomoku-9a668c78bed7197e0b349d4bd2908a67bedd295f.tar.bz2
lp-gomoku-9a668c78bed7197e0b349d4bd2908a67bedd295f.zip
improve AI, delete dbgprint(), fix some bugs
Diffstat (limited to 'ai.cpp')
-rw-r--r--ai.cpp24
1 files changed, 13 insertions, 11 deletions
diff --git a/ai.cpp b/ai.cpp
index bef4567..05f95af 100644
--- a/ai.cpp
+++ b/ai.cpp
@@ -13,23 +13,25 @@ enum scores {
LOSE_SCORE = -10
};
-AI::AI(int d) : max_depth(d)
+AI::AI(int p, int d) : player(p), max_depth(d), first_move(true)
{
srand(time(NULL));
}
-void AI::GetFirstMove(int &my, int &mx, int rows, int cols)
-{
- my = rand() % rows;
- mx = rand() % cols;
-}
-
void AI::GetBestMove(int &my, int &mx, GameField field)
{
int score = MIN_SCORE;
int rows = field.GetRows();
int cols = field.GetCols();
+ // give chance to user :)
+ if(first_move) {
+ my = rand() % rows;
+ mx = rand() % cols;
+ first_move = false;
+ return;
+ }
+
int y, x;
for(y = 0; y < rows; y++) {
for(x = 0; x < cols; x++)
@@ -37,7 +39,7 @@ void AI::GetBestMove(int &my, int &mx, GameField field)
if(!field.CanMove(y, x))
continue;
field.Move(y, x);
- int result = min(field, 1); // min because next move by player
+ int result = min(field, 1);
field.UndoMove(y, x);
if(result > score) {
score = result;
@@ -54,9 +56,9 @@ int AI::score(GameField field)
if(state == G_DRAW)
return DRAW_SCORE;
if(state == G_XPLAYER)
- return field.GetWhoMove() == G_XPLAYER ? WIN_SCORE : LOSE_SCORE;
+ return player == G_XPLAYER ? WIN_SCORE : LOSE_SCORE;
if(state == G_OPLAYER)
- return field.GetWhoMove() == G_OPLAYER ? WIN_SCORE : LOSE_SCORE;
+ return player == G_OPLAYER ? WIN_SCORE : LOSE_SCORE;
return NONE_SCORE;
}
@@ -104,7 +106,7 @@ int AI::max(GameField field, int depth)
if(!field.CanMove(y, x))
continue;
field.Move(y, x);
- int result = max(field, depth + 1);
+ int result = min(field, depth + 1);
field.UndoMove(y, x);
if(result > score)
score = result;