Browse Source

new protocol and server

Kajetan Johannes Hammerle 3 years ago
parent
commit
91ca08afff
3 changed files with 71 additions and 36 deletions
  1. 1 0
      .gitignore
  2. 67 36
      source/Game.cpp
  3. 3 0
      source/Game.h

+ 1 - 0
.gitignore

@@ -1,3 +1,4 @@
 fanorona
+fanorona_lektor
 nbproject
 *.exe

+ 67 - 36
source/Game.cpp

@@ -6,7 +6,9 @@
 std::array<Vector, 8> Game::neighbours;
 std::array<Vector, 9 * 5> Game::fieldVectors;
 
-Game::Game() : active(-1, -1), direction(0, 0), lastLocation(0) {
+Game::Game() : active(-1, -1), direction(0, 0), mustTake(false), lastLocation(0) {
+    std::cout << "0\n1\n0\n";
+
     neighbours[0].set(1, 0);
     neighbours[1].set(0, 1);
     neighbours[2].set(-1, 0);
@@ -25,17 +27,17 @@ Game::Game() : active(-1, -1), direction(0, 0), lastLocation(0) {
 }
 
 void Game::print(String& s) const {
-    s.append("\n\r  0 1 2 3 4 5 6 7 8\n\r");
+    s.append("  0 1 2 3 4 5 6 7 8\n");
     printLine(s, 0);
-    s.append("  |\\|/|\\|/|\\|/|\\|/|\n\r");
+    s.append("  |\\|/|\\|/|\\|/|\\|/|\n");
     printLine(s, 1);
-    s.append("  |/|\\|/|\\|/|\\|/|\\|\n\r");
+    s.append("  |/|\\|/|\\|/|\\|/|\\|\n");
     printLine(s, 2);
-    s.append("  |\\|/|\\|/|\\|/|\\|/|\n\r");
+    s.append("  |\\|/|\\|/|\\|/|\\|/|\n");
     printLine(s, 3);
-    s.append("  |/|\\|/|\\|/|\\|/|\\|\n\r");
+    s.append("  |/|\\|/|\\|/|\\|/|\\|\n");
     printLine(s, 4);
-    s.append("\n\r");
+    s.append("\n");
 }
 
 void Game::printLine(String& s, int y) const {
@@ -62,29 +64,29 @@ void Game::parse() {
     String s;
     while(true) {
         char c = std::cin.get();
-        if(c == '0') {
-            parseFields();
+        if(c == '\r') {
             continue;
         }
-        if(c == '\n' || c == '\r') {
-            std::cerr << s << "\n";
-            s.clear();
+        if(c != '\n') {
+            s.append(c);
             continue;
         }
-        s.append(c);
-        if(s == "select stone:") {
-            std::cerr << s;
-            s.clear();
+        if(s == "   0   1   2   3   4   5   6   7   8") {
+            parseFields();
+        } else if(s == "Please enter origin x-axis") {
             makeSelection();
-        } else if(s == "select location to move:") {
-            std::cerr << s;
-            s.clear();
+        } else if(s == "Please enter destination x-axis") {
             makeMove();
-        } else if(s == "select stone to take:") {
-            std::cerr << s;
-            s.clear();
+        } else if(s == "Please enter wether you want to Withdraw or Approach [W/A]") {
             takeStone();
+        } else if(s == "Do you want to continue with your turn [Y/N]?") {
+            std::cout << "Y\n";
+        } else if(s == "************************Player 1 won!**********************" ||
+                s == "************************Player 2 won!**********************") {
+            std::cerr << s << "\n";
+            return;
         }
+        s.clear();
     }
 }
 
@@ -100,23 +102,23 @@ void Game::consumeLine() const {
 }
 
 void Game::consumeLine(uint y) {
+    std::cin.get();
     std::cin.get();
     std::cin.get();
     for(uint x = 0; x < 9; x++) {
         char c = std::cin.get();
-        if(c == '#') {
+        if(c == '2') {
             fields[x][y] = BLACK;
-        } else if(c == '.') {
+        } else if(c == '0') {
             fields[x][y] = EMPTY;
-        } else if(c == 'O') {
-            fields[x][y] = WHITE;
-        } else if(c == '*') {
-            active.set(x, y);
+        } else if(c == '1') {
             fields[x][y] = WHITE;
         } else {
             std::cerr << "game field parsing error\n";
         }
         std::cin.get();
+        std::cin.get();
+        std::cin.get();
     }
 }
 
@@ -270,6 +272,8 @@ void Game::makeSelection() {
     lastLocation = 0;
     direction.set(0, 0);
 
+    mustTake = isTakingPossible();
+
     Vector selection(0, 0);
     int maxRank = -100000;
     for(Vector& to : fieldVectors) {
@@ -281,6 +285,9 @@ void Game::makeSelection() {
             if(!isInRange(from) || !hasState(from, WHITE) || !areNeighbours(from, to)) {
                 continue;
             }
+            if(mustTake && getRank(from, to, BLACK) + getRank(to, from, BLACK) == 0) {
+                continue;
+            }
             int rank = getQuality(from, to);
             if(rank > maxRank) {
                 maxRank = rank;
@@ -288,7 +295,14 @@ void Game::makeSelection() {
             }
         }
     }
-    std::cout << selection.x << " " << selection.y << "\n";
+    active = selection;
+    
+    std::cerr << "Selecting (" << selection.x << ", " << selection.y << ")\n";
+    String s;
+    print(s);
+    std::cerr << s;
+    
+    std::cout << selection.x << "\n" << selection.y << "\n";
 }
 
 void Game::makeMove() {
@@ -299,7 +313,7 @@ void Game::makeMove() {
         if(!isInRange(to) || !hasState(to, EMPTY) || isInQueue(to) || !areNeighbours(active, to) || m == direction) {
             continue;
         }
-        if(lastLocation > 0) {
+        if(lastLocation > 0 || mustTake) {
             int rank = getRank(active, to, BLACK) + getRank(to, active, BLACK);
             if(rank == 0) {
                 continue;
@@ -313,20 +327,19 @@ void Game::makeMove() {
     }
     addLocation(active);
     direction = maxTo - active;
-    std::cerr << maxTo.x << " " << maxTo.y << "\n";
-    std::cout << maxTo.x << " " << maxTo.y << "\n";
+    active = maxTo;
+    std::cerr << "Move to (" << maxTo.x << ", " << maxTo.y << ")\n";
+    std::cout << maxTo.x << "\n" << maxTo.y << "\n";
 }
 
 void Game::takeStone() {
     uint rankA = getRank(active, active + direction, BLACK);
     uint rankB = getRank(active + direction, active, BLACK);
-    Vector v;
     if(rankA >= rankB) {
-        v = active + direction + direction;
+        std::cout << "W\n";
     } else {
-        v = active - direction;
+        std::cout << "A\n";
     }
-    std::cout << v.x << " " << v.y << "\n";
 }
 
 int Game::getQuality(const Vector& from, const Vector& to) const {
@@ -377,4 +390,22 @@ int Game::countBlack() const {
         blackCounter += hasState(v, BLACK);
     }
     return blackCounter;
+}
+
+bool Game::isTakingPossible() const {
+    for(Vector& to : fieldVectors) {
+        if(!hasState(to, EMPTY)) {
+            continue;
+        }
+        for(Vector& m : neighbours) {
+            Vector from = to + m;
+            if(!isInRange(from) || !areNeighbours(from, to) || !hasState(from, WHITE)) {
+                continue;
+            }
+            if(getRank(from, to, BLACK) + getRank(to, from, BLACK) > 0) {
+                return true;
+            }
+        }
+    }
+    return false;
 }

+ 3 - 0
source/Game.h

@@ -52,10 +52,13 @@ private:
     int getQuality(const Vector& from, const Vector& to) const;
     
     int countBlack() const;
+    
+    bool isTakingPossible() const;
 
     FieldState fields[9][5];
     Vector active;
     Vector direction;
+    bool mustTake;
     
     std::array<Vector, 20> lastLocations;
     uint lastLocation;