basic_types.h 2.09 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
#ifndef BASIC_TYPES_H
#define BASIC_TYPES_H

// stl
#include <bitset>
#include <array>
#include <vector>
#include <set>
#include <unordered_set>
#include <utility>
#include <memory>
#include <chrono>


namespace gameengine
{

  // 8x8 game board
  namespace detail
  {
    constexpr size_t computeBoardSize() { return 8 * 8; }
    constexpr size_t invalidBoardPosition() { return computeBoardSize() + 1; }
  }   // namespace detail

  struct BitPos {
    constexpr BitPos() : m_bitpos{detail::invalidBoardPosition()} {}
    constexpr explicit BitPos(size_t bitpos) : m_bitpos{bitpos} {}
    constexpr explicit BitPos(int bitpos) : m_bitpos{size_t(bitpos)} {}

    BitPos(const BitPos&) = default;

    BitPos& operator=(const BitPos& o)
    {
      m_bitpos = o.m_bitpos;
      return *this;
    }

    static constexpr BitPos invalid()
    {
      return BitPos{detail::invalidBoardPosition()};
    }
    constexpr size_t value() const { return m_bitpos; }
    constexpr bool   isValid() const
    {
      return m_bitpos not_eq detail::invalidBoardPosition();
    }

    constexpr bool operator<(const BitPos& other) const
    {
      return m_bitpos < other.m_bitpos;
    }

    constexpr bool operator==(const BitPos& other) const
    {
      return m_bitpos == other.m_bitpos;
    }
    constexpr bool operator!=(const BitPos& other) const
    {
      return !(*this == other);
    }

  private:
    size_t m_bitpos;
  };

  enum class MoveDirection { N, S, E, W, NE, NW, SE, SW };

}   // namespace gameengine

namespace std
{

  template <>
  struct hash<gameengine::BitPos> {
    size_t operator()(const gameengine::BitPos& bitpos) const
    {
      return std::hash<size_t>{}(bitpos.value());
    }
  };

}   // END namespace std


namespace gameengine   // resume namespace gameengine
{
  using BitPieces = std::bitset<detail::computeBoardSize()>;
  using BitBoard  = std::array<BitPieces, 2>;

  enum class PlayerId : size_t { One = 0, Two };
  enum class PlayerType { Human, AI };

  using PlayerIdSet = std::set<PlayerId>;

  using BitPosSet = std::unordered_set<BitPos>;

}   // namespace gameengine

#endif   // BASIC_TYPES_H