Browse Source

added time and turn statistics

Kajetan Johannes Hammerle 3 years ago
parent
commit
2c82afd7ed
3 changed files with 45 additions and 11 deletions
  1. 3 0
      read.me
  2. 25 0
      source/Game.cpp
  3. 17 11
      source/Game.h

+ 3 - 0
read.me

@@ -0,0 +1,3 @@
+instructions:
+to build the program: make
+to build and test the program: make test

+ 25 - 0
source/Game.cpp

@@ -1,11 +1,16 @@
 #include <iostream>
 #include <climits>
+#include <chrono>
 
 #include "Game.h"
 #include "Types.h"
 
 std::array<Vector, 8> Game::neighbours;
 std::array<Vector, 9 * 5> Game::fieldVectors;
+long int Game::selectionTime = 0;
+long int Game::moveTime = 0;
+int Game::turns = 0;
+int Game::moves = 0;
 
 Game::Game() : active(-1, -1), mustTake(false) {
     // sets the basic game up:
@@ -70,6 +75,12 @@ bool Game::parseLine(const String& line) {
             line == "************************Player 2 won!**********************") {
         // stop the game if anybody wins - the server does not do that
         std::cerr << line << "\n";
+        
+        //std::cerr << "Turns: " << turns << "\n";
+        //std::cerr << "Moves: " << moves << "\n";
+        //std::cerr << "Total selection time: " << selectionTime << "\n";
+        //std::cerr << "Total move time: " << moveTime << "\n";
+        std::cerr << turns << "," << moves << "," << selectionTime << "," << moveTime << "\n";
         return true;
     }
     return false;
@@ -197,6 +208,9 @@ uint Game::move(const Vector& from, const Vector& to) {
 }
 
 void Game::makeSelection() {
+    // add statistics
+    ssize_t time = getNanos();
+    turns++;
     // new turn starts - reset previously blocked turns
     lastLocations.clear();
     direction.set(0, 0);
@@ -230,6 +244,8 @@ void Game::makeSelection() {
     }
     // mark the stone as active
     active = selection;
+    // add selection time to statistics, do not measure IO
+    selectionTime += getNanos() - time;
     // send the selection to the server
     std::cerr << "Selecting (" << selection.x << ", " << selection.y << ")\n";
     // print the selection and the current game field for logging purposes
@@ -238,6 +254,9 @@ void Game::makeSelection() {
 }
 
 void Game::makeMove() {
+    // add statistics
+    ssize_t time = getNanos();
+    moves++;
     // check all neighbors of active selection and choose the move with the highest rank
     Vector maxTo(0, 0);
     int maxRank = INT_MIN;
@@ -268,6 +287,8 @@ void Game::makeMove() {
     direction = maxTo - active;
     // mark the location as active
     active = maxTo;
+    // add selection time to statistics, do not measure IO
+    moveTime += getNanos() - time;
     // send the move to the server
     std::cerr << "Move to (" << maxTo.x << ", " << maxTo.y << ")\n";
     // log the move
@@ -360,4 +381,8 @@ uint Game::getBestEnemyRank() const {
         }
     }
     return maxRank;
+}
+
+long int Game::getNanos() {
+    return std::chrono::high_resolution_clock::now().time_since_epoch().count();
 }

+ 17 - 11
source/Game.h

@@ -18,9 +18,9 @@ private:
     bool parseLine(const String& command);
 
     bool isInRange(const Vector& v) const;
-    
+
     bool areNeighbors(const Vector& from, const Vector& to) const;
-    
+
     uint removeLine(const Vector& from, const Vector& to, Fields::State state);
     uint getRank(const Vector& from, const Vector& to, Fields::State state) const;
 
@@ -28,31 +28,37 @@ private:
 
     uint getStoneFreedom(const Vector& from, Fields::State state) const;
     uint getFreedom(Fields::State state) const;
-    
+
     uint move(const Vector& from, const Vector& to);
-    
+
     void makeSelection();
-    
+
     void makeMove();
     void takeStone();
-    
+
     int getQuality(const Vector& from, const Vector& to) const;
-    
+
     int countBlack() const;
-    
+
     bool isTakingPossible() const;
-    
+
     uint getBestEnemyRank() const;
-    
+
     Fields fields;
     Vector active;
     Vector direction;
     bool mustTake;
-    
+
     List<Vector, 20> lastLocations;
 
     static std::array<Vector, 8> neighbours;
     static std::array<Vector, 9 * 5> fieldVectors;
+    
+    static long int getNanos();
+    static long int selectionTime;
+    static long int moveTime;
+    static int turns;
+    static int moves;
 };
 
 #endif