Othello.hpp

A simple Othello game state handler.

Todo:

Add Hash for boards to be able to cachce moves.

Setup OPTIONAL caching of expensive board measures.

Typedefs

using Othello = Othello_Game<8>
struct Othello_Base
#include <Othello.hpp>

Base class for all sizes of 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.

Subclassed by Othello_Game< BOARD_SIZE >

Public Types

enum Player

Values:

enumerator NONE
enumerator DARK
enumerator LIGHT
enum Facing

Values:

enumerator N
enumerator NE
enumerator E
enumerator SE
enumerator S
enumerator SW
enumerator W
enumerator NW

Public Static Attributes

static constexpr size_t NUM_DIRECTIONS = 8

Number of neighbors each board space has.

template<size_t BOARD_SIZE>
class Othello_Game : public Othello_Base
#include <Othello.hpp>

Class for othello games of a specific size.

Public Types

using this_t = Othello_Game<BOARD_SIZE>
using board_t = std::array<Player, NUM_CELLS>

Public Functions

inline Othello_Game()
inline ~Othello_Game()
inline void Reset()

Reset the board to the starting condition.

inline constexpr size_t GetBoardWidth() const
inline size_t GetNumCells() const
inline Player GetCurPlayer() 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 auto BuildNeighbors()
inline Index GetNeighbor(Index id, Facing dir) const

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_t &GetBoard()
inline const board_t &GetBoard() const
inline bool IsValidMove(Player player, Index pos)

Is given move valid?

inline bool IsOver() const
inline 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 SetPositions(vector<Index> ids, Player player)

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

inline void SetBoard(const board_t &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)

Public Static Attributes

static constexpr size_t NUM_CELLS = BOARD_SIZE * BOARD_SIZE

Protected Attributes

std::array<Facing, NUM_DIRECTIONS> ALL_DIRECTIONS
vector<Index> neighbors
bool over = false

Is the game over?

Player cur_player

Who is the current player set to move next?

board_t game_board

Game board.

Protected Static Functions

static inline size_t GetNeighborIndex(Index pos, Facing dir)

Internal function for accessing the neighbors vector.

struct Index
#include <Othello.hpp>

Public Functions

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

Public Members

size_t pos