Initial commit
This commit is contained in:
commit
4f4397b3e1
48 changed files with 2002 additions and 0 deletions
22
NimGame/CMakeLists.txt
Normal file
22
NimGame/CMakeLists.txt
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
cmake_minimum_required(VERSION 3.30)
|
||||
project(NimGameProject LANGUAGES CXX)
|
||||
|
||||
set(CMAKE_CXX_STANDARD 17)
|
||||
|
||||
if (CMAKE_EXPORT_COMPILE_COMMANDS)
|
||||
set(CMAKE_CXX_STANDARD_INCLUDE_DIRECTORIES ${CMAKE_CXX_IMPLICIT_INCLUDE_DIRECTORIES})
|
||||
endif()
|
||||
|
||||
add_executable(NimGameProject main.cpp
|
||||
game/Game.h
|
||||
client/GameClient.h
|
||||
game/nimgame/NimGame.h
|
||||
game/nimgame/player/AbstractNimGamePlayer.h
|
||||
game/nimgame/player/HumanPlayer.h
|
||||
game/nimgame/player/ComputerPlayer.h
|
||||
io/Writer.h
|
||||
io/ConsoleWriter.h
|
||||
game/player/Player.h
|
||||
game/AbstractGame.h
|
||||
game/Foo.h
|
||||
)
|
||||
24
NimGame/client/GameClient.h
Normal file
24
NimGame/client/GameClient.h
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
//
|
||||
// Created by JoachimWagner on 10.03.2025.
|
||||
//
|
||||
|
||||
#pragma once
|
||||
#include "../game/Game.h"
|
||||
namespace client {
|
||||
|
||||
using Game = atlas::game::Game;
|
||||
|
||||
class GameClient {
|
||||
public:
|
||||
explicit GameClient(Game &game) : game(game) {}
|
||||
|
||||
auto go()->void {
|
||||
game.play();
|
||||
}
|
||||
|
||||
private:
|
||||
Game &game;
|
||||
|
||||
};
|
||||
|
||||
} // client
|
||||
122
NimGame/game/AbstractGame.h
Normal file
122
NimGame/game/AbstractGame.h
Normal file
|
|
@ -0,0 +1,122 @@
|
|||
//
|
||||
// Created by JoachimWagner on 11.03.2025.
|
||||
//
|
||||
|
||||
#pragma once
|
||||
#include "Game.h"
|
||||
#include <vector>
|
||||
#include "../io/Writer.h"
|
||||
#include "player/Player.h"
|
||||
namespace atlas::game {
|
||||
|
||||
template<class BOARD, class TURN>
|
||||
class AbstractGame: public Game {
|
||||
using PLAYER = player::Player<BOARD,TURN> *;
|
||||
using PLAYERS = std::vector<PLAYER>;
|
||||
|
||||
public:
|
||||
|
||||
explicit AbstractGame(io::Writer &writer) : writer(writer) {}
|
||||
|
||||
auto addPlayer(PLAYER player) -> void {
|
||||
players.push_back(player);
|
||||
}
|
||||
auto removePlayer(PLAYER player) -> void {
|
||||
// Not implemented yet
|
||||
// TODO implememt feature
|
||||
}
|
||||
|
||||
auto play()->void override {
|
||||
while(! isGameOver()) playRound();
|
||||
|
||||
}
|
||||
|
||||
private:
|
||||
auto playRound()-> void { // Integration
|
||||
for(auto player: players) {
|
||||
setCurrentPlayer(player);
|
||||
playSingleTurn();
|
||||
}
|
||||
}
|
||||
auto playSingleTurn()->void{
|
||||
if(isGameOver()) return;
|
||||
executeTurn();
|
||||
terminateTurn();
|
||||
}
|
||||
|
||||
auto executeTurn()->void {
|
||||
do {
|
||||
_turn = getCurrentPlayer()->doTurn(_board);
|
||||
} while(turnIsNotValid());
|
||||
}
|
||||
|
||||
|
||||
auto terminateTurn() -> void {
|
||||
updateBoard();
|
||||
printGameOverMessageIfGameIsOver();
|
||||
}
|
||||
|
||||
auto turnIsNotValid()-> bool {
|
||||
if( isTurnValid()) return false;
|
||||
write("Ungueltiger Zug");
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
auto printGameOverMessageIfGameIsOver() -> void {
|
||||
if(isGameOver()) {
|
||||
write(getCurrentPlayer()->getName() + " hat verloren.");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
PLAYERS players;
|
||||
PLAYER _currentPlayer;
|
||||
atlas::io::Writer &writer;
|
||||
BOARD _board;
|
||||
TURN _turn;
|
||||
|
||||
void setCurrentPlayer(const PLAYER currentPlayer) {
|
||||
AbstractGame::_currentPlayer = currentPlayer;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
protected:
|
||||
|
||||
BOARD getBoard() const {
|
||||
return _board;
|
||||
}
|
||||
|
||||
void setBoard(BOARD board) {
|
||||
_board = board;
|
||||
}
|
||||
|
||||
TURN getTurn() const {
|
||||
return _turn;
|
||||
}
|
||||
|
||||
void setTurn(TURN turn) {
|
||||
_turn = turn;
|
||||
}
|
||||
virtual auto updateBoard()-> void = 0;
|
||||
|
||||
virtual auto isGameOver()->bool = 0;
|
||||
|
||||
virtual auto isTurnValid() const -> bool = 0;
|
||||
|
||||
const PLAYER getCurrentPlayer() const {
|
||||
return _currentPlayer;
|
||||
}
|
||||
|
||||
const PLAYERS &getPlayers() const {
|
||||
return players;
|
||||
}
|
||||
|
||||
auto write(std::string message)->void {
|
||||
writer.write(message);
|
||||
}
|
||||
|
||||
};
|
||||
}
|
||||
45
NimGame/game/Foo.h
Normal file
45
NimGame/game/Foo.h
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
//
|
||||
// Created by JoachimWagner on 11.03.2025.
|
||||
//
|
||||
|
||||
#pragma once
|
||||
|
||||
|
||||
#include <ostream>
|
||||
|
||||
class Punkt {
|
||||
private:
|
||||
int x{0};
|
||||
int y{0};
|
||||
|
||||
public:
|
||||
int getX() const {
|
||||
return x;
|
||||
}
|
||||
|
||||
void setX(int x) {
|
||||
Punkt::x = x;
|
||||
}
|
||||
|
||||
int getY() const {
|
||||
return y;
|
||||
}
|
||||
|
||||
void setY(int y) {
|
||||
Punkt::y = y;
|
||||
}
|
||||
|
||||
bool operator==(const Punkt &rhs) const {
|
||||
return x == rhs.x &&
|
||||
y == rhs.y;
|
||||
}
|
||||
|
||||
bool operator!=(const Punkt &rhs) const {
|
||||
return !(rhs == *this);
|
||||
}
|
||||
|
||||
friend std::ostream &operator<<(std::ostream &os, const Punkt &punkt) {
|
||||
os << "x: " << punkt.x << " y: " << punkt.y;
|
||||
return os;
|
||||
}
|
||||
};
|
||||
17
NimGame/game/Game.h
Normal file
17
NimGame/game/Game.h
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
//
|
||||
// Created by JoachimWagner on 10.03.2025.
|
||||
//
|
||||
|
||||
#pragma once
|
||||
|
||||
namespace atlas::game {
|
||||
|
||||
class Game {
|
||||
public:
|
||||
Game() = default;
|
||||
virtual ~Game() = default;
|
||||
|
||||
virtual void play() = 0;
|
||||
};
|
||||
|
||||
} // game
|
||||
38
NimGame/game/nimgame/NimGame.h
Normal file
38
NimGame/game/nimgame/NimGame.h
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
//
|
||||
// Created by JoachimWagner on 10.03.2025.
|
||||
//
|
||||
|
||||
#pragma once
|
||||
#include <vector>
|
||||
#include "../AbstractGame.h"
|
||||
#include "../../io/Writer.h"
|
||||
#include "../player/Player.h"
|
||||
|
||||
namespace atlas::game::nimgame {
|
||||
|
||||
class NimGame: public AbstractGame<int, int> {
|
||||
|
||||
|
||||
public:
|
||||
explicit NimGame(io::Writer &writer) : AbstractGame(writer) {
|
||||
setBoard(23);
|
||||
}
|
||||
|
||||
|
||||
protected:
|
||||
auto updateBoard()-> void override{ setBoard(getBoard()- getTurn());}
|
||||
|
||||
auto isGameOver()->bool override{ // Operation
|
||||
return getBoard() < 1 || getPlayers().empty();
|
||||
}
|
||||
|
||||
[[nodiscard]] auto isTurnValid() const -> bool override { return getTurn() >= 1 && getTurn() <= 3; }
|
||||
|
||||
|
||||
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
17
NimGame/game/nimgame/player/AbstractNimGamePlayer.h
Normal file
17
NimGame/game/nimgame/player/AbstractNimGamePlayer.h
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
//
|
||||
// Created by JoachimWagner on 11.03.2025.
|
||||
//
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include "../../player/AbstractPlayer.h"
|
||||
namespace atlas::game::nimgame::player {
|
||||
|
||||
class AbstractNimGamePlayer: public atlas::game::player::AbstractPlayer<int, int>{
|
||||
|
||||
public:
|
||||
explicit AbstractNimGamePlayer(const std::string &name) : AbstractPlayer(name) {}
|
||||
};
|
||||
|
||||
}
|
||||
22
NimGame/game/nimgame/player/ComputerPlayer.h
Normal file
22
NimGame/game/nimgame/player/ComputerPlayer.h
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
//
|
||||
// Created by JoachimWagner on 11.03.2025.
|
||||
//
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <iostream>
|
||||
#include "AbstractNimGamePlayer.h"
|
||||
namespace atlas::game::nimgame::player {
|
||||
class ComputerPlayer :public AbstractNimGamePlayer{
|
||||
static inline int turns[] = {3,1,1,2};
|
||||
public:
|
||||
explicit ComputerPlayer(const std::string &name) : AbstractNimGamePlayer(name) {}
|
||||
|
||||
auto doTurn(const int &stones) const -> int override {
|
||||
|
||||
const int turn = turns[stones % 4];
|
||||
std::cout << "Computer nimmt " << turn << " Steine." << std::endl;
|
||||
return turn;
|
||||
}
|
||||
};
|
||||
}
|
||||
26
NimGame/game/nimgame/player/HumanPlayer.h
Normal file
26
NimGame/game/nimgame/player/HumanPlayer.h
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
//
|
||||
// Created by JoachimWagner on 11.03.2025.
|
||||
//
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <iostream>
|
||||
#include "AbstractNimGamePlayer.h"
|
||||
|
||||
namespace atlas::game::nimgame::player {
|
||||
|
||||
class HumanPlayer: public AbstractNimGamePlayer{
|
||||
|
||||
public:
|
||||
explicit HumanPlayer(const std::string &name) : AbstractNimGamePlayer(name) {}
|
||||
|
||||
auto doTurn(const int &stones) const -> int override {
|
||||
|
||||
int turn;
|
||||
std::cout << "Es gibt " << stones << " Steine. Bitte nehmen Sie 1,2 oder 3!" << std::endl;
|
||||
std::cin >> turn;
|
||||
return turn;
|
||||
}
|
||||
};
|
||||
|
||||
} // atlas::game::nimgame::player
|
||||
33
NimGame/game/nimgame/player/OmaPlayer.h
Normal file
33
NimGame/game/nimgame/player/OmaPlayer.h
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
//
|
||||
// Created by JoachimWagner on 28.01.2025.
|
||||
//
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <iostream>
|
||||
|
||||
#include "time.h"
|
||||
#include <cstdlib>
|
||||
#include "AbstractNimGamePlayer.h"
|
||||
namespace atlas::game::nimgame::player {
|
||||
|
||||
class OmaPlayer: public AbstractNimGamePlayer{
|
||||
|
||||
public:
|
||||
explicit OmaPlayer(const std::string &name) : AbstractNimGamePlayer(name) {
|
||||
std::srand(time(nullptr));
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
[[nodiscard]]
|
||||
auto doTurn(const int &stones) const -> int override {
|
||||
const int result = std::rand() % 6;
|
||||
std::cout << "Oma nimmt " << result << " Steine." << std::endl;
|
||||
|
||||
return result;
|
||||
}
|
||||
};
|
||||
|
||||
} // game
|
||||
26
NimGame/game/player/AbstractPlayer.h
Normal file
26
NimGame/game/player/AbstractPlayer.h
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
//
|
||||
// Created by JoachimWagner on 11.03.2025.
|
||||
//
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include "Player.h"
|
||||
namespace atlas::game::player {
|
||||
|
||||
template<class BOARD, class TURN>
|
||||
class AbstractPlayer: public Player<BOARD, TURN>{
|
||||
|
||||
public:
|
||||
explicit AbstractPlayer(const std::string &name) : name(name) {}
|
||||
|
||||
const std::string getName() const override{
|
||||
return name;
|
||||
}
|
||||
|
||||
private:
|
||||
std::string name;
|
||||
|
||||
};
|
||||
|
||||
}
|
||||
19
NimGame/game/player/Player.h
Normal file
19
NimGame/game/player/Player.h
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
//
|
||||
// Created by JoachimWagner on 11.03.2025.
|
||||
//
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
|
||||
namespace atlas::game::player {
|
||||
|
||||
template<class BOARD, class TURN>
|
||||
class Player {
|
||||
public:
|
||||
virtual ~Player() = default;
|
||||
virtual auto getName() const -> const std::string = 0;
|
||||
virtual auto doTurn(const BOARD &board) const -> TURN = 0;
|
||||
|
||||
};
|
||||
}
|
||||
19
NimGame/io/ConsoleWriter.h
Normal file
19
NimGame/io/ConsoleWriter.h
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
//
|
||||
// Created by JoachimWagner on 11.03.2025.
|
||||
//
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <iostream>
|
||||
#include "Writer.h"
|
||||
namespace atlas::io {
|
||||
|
||||
class ConsoleWriter :public Writer{
|
||||
public:
|
||||
auto write(std::string message) const -> void override {
|
||||
std::cout << message << std::endl;
|
||||
}
|
||||
|
||||
|
||||
};
|
||||
}
|
||||
18
NimGame/io/Writer.h
Normal file
18
NimGame/io/Writer.h
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
//
|
||||
// Created by JoachimWagner on 11.03.2025.
|
||||
//
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
|
||||
namespace atlas::io {
|
||||
|
||||
|
||||
|
||||
class Writer {
|
||||
public:
|
||||
virtual ~Writer() = default;
|
||||
virtual auto write(std::string message) const -> void = 0;
|
||||
};
|
||||
}
|
||||
24
NimGame/main.cpp
Normal file
24
NimGame/main.cpp
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
#include "client/GameClient.h"
|
||||
#include "game/nimgame/NimGame.h"
|
||||
#include "game/nimgame/player/ComputerPlayer.h"
|
||||
#include "game/nimgame/player/HumanPlayer.h"
|
||||
#include "game/nimgame/player/OmaPlayer.h"
|
||||
#include "io/ConsoleWriter.h"
|
||||
#include <iostream>
|
||||
|
||||
using namespace atlas;
|
||||
int main() {
|
||||
|
||||
atlas::io::ConsoleWriter writer;
|
||||
atlas::game::nimgame::player::HumanPlayer human{"Fritz"};
|
||||
atlas::game::nimgame::player::ComputerPlayer computer{"Computer"};
|
||||
atlas::game::nimgame::player::OmaPlayer oma{"Gisela"};
|
||||
game::nimgame::NimGame game{writer};
|
||||
game.addPlayer(&human);
|
||||
game.addPlayer(&oma);
|
||||
game.addPlayer(&computer);
|
||||
client::GameClient client{game};
|
||||
client.go();
|
||||
|
||||
return 0;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue