add multithread game logic. add tests. edit cmake

This commit is contained in:
2026-04-27 12:13:05 +02:00
parent 727caa7b90
commit 29868a704c
90 changed files with 19017 additions and 196 deletions
+15 -6
View File
@@ -1,8 +1,12 @@
#pragma once
#include <memory>
#include <mutex>
#include <string>
#include <vector>
#include "Robot.h"
#include "Timer.h"
#include "World.h"
/* Coordinates game setup, turn order, world updates, and final scoring output. */
@@ -13,18 +17,23 @@ class Game {
private:
World world_;
std::unique_ptr<Robot> player_;
std::unique_ptr<Robot> computer_;
bool autoMode_ = false;
std::vector<std::unique_ptr<Robot>> robots_;
std::vector<int> deadRobotScores_;
std::mutex turnMutex_;
int lastThreshold_ = 0;
int round = 0;
std::vector<Timer::Duration> threadTimes_;
void setup ();
void play ( Robot &robot, bool isPlayer );
void robotLoop ( int idx );
void fightNearby ( Robot &robot );
int computeWorldSum () const;
bool isGameOver () const;
void log ( const std::string &message ) const;
void checkRearrange ( Robot &robot );
void applyEffect ( Robot &robot, int effect );
bool isGameOver () const;
void printScores () const;
void printResult () const;
void printResults () const;
std::unique_ptr<Robot> createRobot ( int choice, int x, int y ) const;
};
+20
View File
@@ -0,0 +1,20 @@
#pragma once
#include <chrono>
#include <iostream>
struct Timer {
using Clock = std::chrono::high_resolution_clock;
using Duration = std::chrono::duration<float>;
std::chrono::time_point<Clock> start, end;
Duration duration;
explicit Timer () { start = Clock::now(); }
~Timer () {
end = Clock::now();
duration = end - start;
// float ms = duration.count() * 1000;
// std::cout << "Turn took " << ms << " ms" << std::endl;
}
};