Selaa lähdekoodia

basic aggressive ai

Kajetan Johannes Hammerle 3 vuotta sitten
vanhempi
commit
1b93959be3
2 muutettua tiedostoa jossa 100 lisäystä ja 34 poistoa
  1. 99 33
      Game.cpp
  2. 1 1
      Game.h

+ 99 - 33
Game.cpp

@@ -263,13 +263,6 @@ bool Game::areNeighbours(int x, int y, int x2, int y2) const {
     return !((x & 1) ^ (y & 1));
 }
 
-void Game::revertToSelection(String& output) {
-    activeX = -1;
-    activeY = -1;
-    print(output);
-    output.append("Human Color: White(O)\n\rAIColor: Black(#)\n\rHuman turn\n\rselect stone: ");
-}
-
 bool Game::removeLine(int x, int y, int x2, int y2, FieldState remove) {
     int diffX = x2 - x;
     int diffY = y2 - y;
@@ -285,41 +278,114 @@ bool Game::removeLine(int x, int y, int x2, int y2, FieldState remove) {
     }
 }
 
+int Game::getRank(int x, int y, int x2, int y2) const {
+    int diffX = x2 - x;
+    int diffY = y2 - y;
+    int rank = 0;
+    while(true) {
+        x2 += diffX;
+        y2 += diffY;
+        if(!isInRange(x2, y2) || fields[x2][y2] != WHITE) {
+            return rank;
+        }
+        rank++;
+    }
+}
+
 void Game::botMove(String& output) {
     if(shouldEnd()) {
         return;
     }
-    while(true) {
-        int x = 0;
-        int y = 0;
-        while(true) {
-            x = rand() % 9;
-            y = rand() % 5;
-            if(fields[x][y] == EMPTY) {
-                break;
+
+    Location from;
+    Location to;
+    Location takeFrom;
+    Location takeTo;
+    int rank = -1;
+
+    for(int bx = 0; bx < 9; bx++) {
+        for(int by = 0; by < 5; by++) {
+            if(fields[bx][by] != EMPTY) {
+                continue;
+            }
+            for(int mx = -1; mx <= 1; mx++) {
+                for(int my = -1; my <= 1; my++) {
+                    int x = bx + mx;
+                    int y = by + my;
+                    if((mx == 0 && my == 0) || !isInRange(x, y) || !areNeighbours(bx, by, x, y) || fields[x][y] != BLACK) {
+                        continue;
+                    }
+                    int rankA = getRank(bx, by, x, y);
+                    int rankB = getRank(x, y, bx, by);
+                    if(rankA > rank) {
+                        from = {x, y};
+                        to = {bx, by};
+                        takeFrom = {bx, by};
+                        takeTo = {x, y};
+                        rank = rankA;
+                    }
+                    if(rankB > rank) {
+                        from = {x, y};
+                        to = {bx, by};
+                        takeFrom = {x, y};
+                        takeTo = {bx, by};
+                        rank = rankB;
+                    }
+                }
             }
         }
-        for(uint i = 0; i < 10; i++) {
-            int mx = x + (rand() % 3) - 1;
-            int my = y + (rand() % 3) - 1;
-            if((mx == x && my == y) || !isInRange(mx, my) || fields[mx][my] != BLACK) {
-                continue;
+    }
+    lastLocation = 0;
+    bool removed = true;
+    while(removed) {
+        fields[from.x][from.y] = EMPTY;
+        fields[to.x][to.y] = BLACK;
+        removed = removeLine(takeFrom.x, takeFrom.y, takeTo.x, takeTo.y, WHITE);
+
+        activeX = to.x;
+        activeY = to.y;
+        print(output);
+
+        addLocation(from.x, from.y);
+        directionX = to.x - from.x;
+        directionY = to.y - from.y;
+        
+        int x = to.x;
+        int y = to.y;
+        rank = -1;
+        for(int mx = -1; mx <= 1; mx++) {
+            for(int my = -1; my <= 1; my++) {
+                if((mx == 0 && my == 0) || !canMoveActiveStoneTo(mx, my)) {
+                    continue;
+                }
+                int bx = x + mx;
+                int by = y + my;
+                
+                int rankA = getRank(bx, by, x, y);
+                int rankB = getRank(x, y, bx, by);
+                if(rankA > rank) {
+                    from = {x, y};
+                    to = {bx, by};
+                    takeFrom = {bx, by};
+                    takeTo = {x, y};
+                    rank = rankA;
+                }
+                if(rankB > rank) {
+                    from = {x, y};
+                    to = {bx, by};
+                    takeFrom = {x, y};
+                    takeTo = {bx, by};
+                    rank = rankB;
+                }
             }
-            fields[mx][my] = EMPTY;
-            fields[x][y] = BLACK;
-            removeLine(mx, my, x, y, WHITE);
-            removeLine(x, y, mx, my, WHITE);
-
-            activeX = x;
-            activeY = y;
-            print(output);
-            activeX = -1;
-            activeY = -1;
-            print(output);
-            revertToSelection(output);
-            return;
         }
     }
+
+    activeX = -1;
+    activeY = -1;
+    print(output);
+    print(output);
+    output.append("Human Color: White(O)\n\rAIColor: Black(#)\n\rHuman turn\n\rselect stone: ");
 }
 
 void Game::addLocation(int x, int y) {

+ 1 - 1
Game.h

@@ -36,7 +36,7 @@ private:
     void print(String& s) const;
     void printLine(String& s, int index) const;
     bool removeLine(int x, int y, int x2, int y2, FieldState remove);
-    void revertToSelection(String& output);
+    int getRank(int x, int y, int x2, int y2) const;
     bool areNeighbours(int x, int y, int x2, int y2) const;
     void botMove(String& output);