94 lines
2.1 KiB
C++
94 lines
2.1 KiB
C++
#pragma once
|
|
|
|
#include <cstdint>
|
|
#include <string>
|
|
|
|
class Game {
|
|
public:
|
|
enum class SYM : uint_fast8_t {
|
|
NONE = 0,
|
|
A = 1,
|
|
B = 2
|
|
};
|
|
|
|
struct UserInfo {
|
|
uint64_t id = 0;
|
|
std::string name;
|
|
std::string username;
|
|
|
|
std::string getUsernameOrName() const;
|
|
|
|
constexpr operator bool() const {
|
|
return id != 0 || !username.empty();
|
|
}
|
|
|
|
bool operator==(const UserInfo& other) const;
|
|
|
|
bool operator!=(const UserInfo& other) const;
|
|
};
|
|
|
|
Game();
|
|
|
|
void setPlayerA(const UserInfo& player);
|
|
void setPlayerB(const UserInfo& player);
|
|
bool addPlayer(const UserInfo& player);
|
|
|
|
const UserInfo& getPlayerA() const;
|
|
const UserInfo& getPlayerB() const;
|
|
|
|
// true = next turn is playerA, false = next turn is playerB
|
|
constexpr bool getNextTurn() const {
|
|
return nextturna;
|
|
}
|
|
constexpr void setNextTurn(bool isA) {
|
|
nextturna = isA;
|
|
}
|
|
|
|
// are both player set and the game is not over?
|
|
bool ready() const;
|
|
|
|
// is the game over? (won / full)
|
|
bool done() const;
|
|
|
|
// is atleast one space free?
|
|
bool isFull() 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, const UserInfo& 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;
|
|
void setPos(uint_fast8_t x, uint_fast8_t y, Game::SYM);
|
|
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;
|
|
|
|
UserInfo playerA;
|
|
UserInfo playerB;
|
|
|
|
// true = next turn is playerA, false = next turn is playerB
|
|
bool nextturna = true;
|
|
|
|
// fieldnr = x + (y * SIZE)
|
|
SYM field[SIZE * SIZE];
|
|
|
|
}; |