Browse Source

more comments

Kajetan Johannes Hammerle 3 years ago
parent
commit
e339ec46dd
1 changed files with 20 additions and 1 deletions
  1. 20 1
      source/Game.cpp

+ 20 - 1
source/Game.cpp

@@ -8,8 +8,13 @@ std::array<Vector, 8> Game::neighbours;
 std::array<Vector, 9 * 5> Game::fieldVectors;
 
 Game::Game() : active(-1, -1), mustTake(false) {
+    // sets the basic game up:
+    // 0 - start game
+    // 1 - player vs ai
+    // 0 - player starts
     std::cout << "0\n1\n0\n";
-
+    
+    // allows for each neighbor iterating
     neighbours[0].set(1, 0);
     neighbours[1].set(0, 1);
     neighbours[2].set(-1, 0);
@@ -19,6 +24,7 @@ Game::Game() : active(-1, -1), mustTake(false) {
     neighbours[6].set(1, -1);
     neighbours[7].set(-1, -1);
 
+    // allows for each for all fields 
     uint index = 0;
     for(uint y = 0; y < 5; y++) {
         for(uint x = 0; x < 9; x++) {
@@ -30,19 +36,24 @@ Game::Game() : active(-1, -1), mustTake(false) {
 void Game::readLine() {
     String line;
     while(true) {
+        // read input by each character
         char c = std::cin.get();
         if(c == '\n') {
+            // parse the command if the line ended
             if(parseLine(line)) {
                 return;
             }
+            // clear the buffer for the next command
             line.clear();
         } else if(c != '\r') {
+            // line carriages are ignored
             line.append(c);
         }
     }
 }
 
 bool Game::parseLine(const String& line) {
+    // identify when the server expects an answer or a new game field is available
     if(line == "   0   1   2   3   4   5   6   7   8") {
         fields.read();
         fields.print(active);
@@ -53,9 +64,11 @@ bool Game::parseLine(const String& line) {
     } else if(line == "Please enter wether you want to Withdraw or Approach [W/A]") {
         takeStone();
     } else if(line == "Do you want to continue with your turn [Y/N]?") {
+        // we always want to continue if possible
         std::cout << "Y\n";
     } else if(line == "************************Player 1 won!**********************" ||
             line == "************************Player 2 won!**********************") {
+        // stop the game if anybody wins - the server does not do that
         std::cerr << line << "\n";
         return true;
     }
@@ -67,10 +80,12 @@ bool Game::isInRange(const Vector& v) const {
 }
 
 bool Game::areNeighbors(const Vector& from, const Vector& to) const {
+    // fields are not neighbors with themselves
     if(from == to) {
         return false;
     }
     Vector diff = from - to;
+    // fields with a bigger distance than one cannot be neighbors
     if(diff.x < -1 || diff.x > 1 || diff.y < -1 || diff.y > 1) {
         return false;
     }
@@ -83,11 +98,14 @@ bool Game::areNeighbors(const Vector& from, const Vector& to) const {
 }
 
 uint Game::removeLine(const Vector& from, const Vector& to, Fields::State state) {
+    // walk along the line and removes opponent stones
     Vector current = to;
     Vector unit = to - from;
+    // counter for removed stones
     uint rank = 0;
     while(true) {
         current += unit;
+        // stop removing on wrong stones or the end of the game field
         if(!isInRange(current) || !fields.hasState(current, state)) {
             return rank;
         }
@@ -97,6 +115,7 @@ uint Game::removeLine(const Vector& from, const Vector& to, Fields::State state)
 }
 
 uint Game::getRank(const Vector& from, const Vector& to, Fields::State state) const {
+    // same as removeLine but the removable stones are only counted
     Vector current = to;
     Vector unit = to - from;
     uint rank = 0;