74 lines
1.7 KiB
C++
74 lines
1.7 KiB
C++
#pragma once
|
|
|
|
#include <cstdint>
|
|
#include <string>
|
|
|
|
class Game {
|
|
public:
|
|
enum class SYM : uint_fast8_t {
|
|
NONE = 0,
|
|
A = 1,
|
|
B = 2
|
|
};
|
|
|
|
Game();
|
|
|
|
void setMessageID(uint32_t messageid);
|
|
|
|
void setPlayerA(uint64_t player);
|
|
void setPlayerB(uint64_t player);
|
|
bool addPlayer(uint64_t player);
|
|
|
|
uint64_t getPlayerA() const;
|
|
uint64_t getPlayerB() const;
|
|
|
|
// true = next turn is playerA, false = next turn is playerB
|
|
constexpr bool getNextTurn() const {
|
|
return nextturna;
|
|
}
|
|
|
|
// are both player set and the game is not over?
|
|
bool ready() const;
|
|
|
|
// is the game over? (won / full)
|
|
bool done() const;
|
|
|
|
// do one turn
|
|
// returns false if the player doing this turn is not allowed or the game is done
|
|
bool turn(uint_fast8_t x, uint_fast8_t y, uint64_t player);
|
|
|
|
// get the winner or empty string if game is not over yet
|
|
const std::string& getWinner() const;
|
|
|
|
SYM getPos(uint_fast8_t x, uint_fast8_t y) const;
|
|
constexpr uint_fast8_t getSize() const { return SIZE; }
|
|
|
|
private:
|
|
static const uint_fast8_t SIZE = 3;
|
|
|
|
const static std::string SYM_NAMES[3];
|
|
|
|
constexpr bool isInField(uint_fast8_t x, uint_fast8_t y) const { return (x < SIZE) && (y < SIZE); }
|
|
constexpr SYM getField(uint_fast8_t x, uint_fast8_t y) const {
|
|
return field[x + y * SIZE];
|
|
}
|
|
constexpr void setField(uint_fast8_t x, uint_fast8_t y, SYM s) {
|
|
field[x + y * SIZE] = s;
|
|
}
|
|
constexpr bool isEmpty(uint_fast8_t x, uint_fast8_t y) const {
|
|
return field[x + y * SIZE] == SYM::NONE;
|
|
}
|
|
|
|
SYM checkWinner() const;
|
|
|
|
uint32_t messageid = 0;
|
|
uint64_t playerA = 0;
|
|
uint64_t playerB = 0;
|
|
|
|
// true = next turn is playerA, false = next turn is playerB
|
|
bool nextturna = true;
|
|
|
|
// fieldnr = x + (y * SIZE)
|
|
SYM field[SIZE * SIZE];
|
|
|
|
}; |