Othello8.hpp

A simple Othello game state handler limited to an 8x8 board.

Todo:

Add Hash for boards to be able to cachce moves.

Setup OPTIONAL caching of expensive board measures.

class Othello8
#include <Othello8.hpp>

Class for size-8 othello games.

NOTE: This game could be made more black-box.

  • Hide almost everything. Only give users access to game-advancing functions (don’t allow willy-nilly board manipulation, etc). This would let us make lots of assumptions about calculating flip lists, scores, etc, which would speed up asking for flip lists, etc. multiple times during a turn.

Public Types

enum Player

Values:

enumerator DARK
enumerator LIGHT
enumerator NONE
enum Facing

Values:

enumerator N
enumerator NE
enumerator E
enumerator SE
enumerator S
enumerator SW
enumerator W
enumerator NW
using this_t = Othello8

Public Functions

inline Othello8()
inline ~Othello8()
inline void Reset()

Reset the board to the starting condition.

inline size_t GetNumCells() const
inline Player GetCurPlayer() const
inline size_t GetHash() const
inline Player GetOpponent(Player player) const

Get opponent ID of give player ID.

inline bool IsValidPlayer(Player player) const

Is the given player ID a valid player?

inline Index GetNeighbor(Index id, Facing dir)

Get location adjacent to ID in direction dir. GetNeighbor function is save with garbage ID values.

inline Player GetPosOwner(Index id) const

Get the value (light, dark, or open) at a position on the board.

inline Board &GetBoard()
inline const Board &GetBoard() const
inline bool IsValidMove(Player player, Index pos)

Is given move valid?

inline bool IsOver() const
inline void SetupCache()
inline const vector<Index> &GetFlipList(Player player, Index pos)

Get positions that would flip if a player (player) made a particular move (pos). Note: May be called before or after piece is placed.

inline size_t GetFlipCount(Player player, Index pos)

Count the number of positions that would flip if we placed a piece at a specific location.

inline bool HasValidFlips(Player player, Index pos)

Are there any valid flips from this position?

inline vector<Index> GetMoveOptions(Player player)

Get a list of valid move options for given player.

inline vector<Index> GetMoveOptions()

GetMoveOptions() without a specified player used current player.

inline bool HasMoveOptions(Player player)

Determine if there are any move options for given player.

inline double GetScore(Player player)

Get the current score for a given player.

inline size_t CountFrontierPos(Player player)

Count the number of empty squares adjacent to a player’s pieces (frontier size)

inline bool IsAdjacentTo(Index pos, Player owner)

Is position given by ID adjacent to the given owner?

inline void SetPos(Index pos, Player player)

Set board position (ID) to given space value.

inline void ClearPos(Index pos)
inline void SetPositions(vector<Index> ids, Player player)

Set positions given by ids to be owned by the given player.

inline void SetBoard(const Board &other_board)

Configure board as given by copy_board input. copy_board size must match game_board’s size.

inline void SetBoard(const this_t &other_othello)

Set current board to be the same as board from other othello game.

inline void SetCurPlayer(Player player)

Set the current player.

inline bool DoNextMove(Index pos)

Do current player’s move (moveID). Return bool indicating whether current player goes again. (false=new cur player or game over)

inline bool DoMove(Player player, Index pos)

Do move (at pos) for specified player. Return bool whether player can go again. After making move, update current player. NOTE: Does not verify validity. Will switch cur_player from player to Opp(player) if opponent has a move to make.

inline void DoFlips(Player player, Index pos)

NOTE: does not check for move validity.

inline void Print(std::ostream &os = std::cout, std::string dark_token = "D", std::string light_token = "L", std::string open_space = "O")

Print board state to given ostream.

Public Static Functions

static inline constexpr Index GetIndex(size_t x, size_t y)
static inline constexpr size_t GetBoardWidth()

Public Static Attributes

static constexpr size_t NUM_DIRECTIONS = 8

Number of neighbors each board space has.

static constexpr size_t BOARD_SIZE = 8

Size of a side of the board.

static constexpr size_t NUM_CELLS = 64

Number of cells on total board.

Protected Types

using flip_list_t = vector<Index>

Protected Attributes

bool over = false

Is the game over?

Player cur_player

Who is the current player set to move next?

Board game_board

Game board.

std::array<flip_list_t, NUM_CELLS> light_flips
std::array<flip_list_t, NUM_CELLS> dark_flips
bool cache_ok

Protected Static Functions

static inline const auto &ALL_DIRECTIONS()

All eight cardinal directions.

static inline size_t GetNeighborIndex(Index pos, Facing dir)

Internal function for accessing the neighbors vector.

static inline const auto &NEIGHBORS()

Precalculated neighbors.

struct Board
#include <Othello8.hpp>

Public Functions

inline void Clear()
inline void Clear(Index pos)
inline Player Owner(Index pos) const
inline void SetOwner(Index pos, Player owner)
inline bool Occupied(Index pos)
inline size_t Score(Player owner)

Public Members

uint64_t occupied
uint64_t player
struct Index
#include <Othello8.hpp>

Public Functions

inline constexpr Index()
inline constexpr Index(size_t _pos)
inline constexpr Index(size_t x, size_t y)
inline constexpr Index(const Index &_in)
inline constexpr operator size_t() const
inline constexpr size_t x() const
inline constexpr size_t y() const
inline constexpr void Set(size_t x, size_t y)
inline constexpr bool IsValid() const
inline Index CalcNeighbor(Facing dir)

Public Members

size_t pos