engine.cpp 3 KB
#include "engine.h"

namespace othello
{

    bool OthelloGameEngine::initNewGame()
    {
      clearGame();

      m_board[0].set(36);
      m_board[0].set(27);
      m_board[1].set(35);
      m_board[1].set(28);

      current_playerid = PlayerId::One;
      game_started = true;
      forfeited = false;

      return true;
    }

    void OthelloGameEngine::clearGame() {
        //removes all pieces from the board
        m_board[0].reset();
        m_board[1].reset();
    }

    bool OthelloGameEngine::performMoveForCurrentHuman(const BitPos& board_pos)
    {
        if (!noLegalMoves(current_playerid)){
            forfeitTurn(false);
            if (utility::isLegalMove(m_board, current_playerid, board_pos)){
                utility::placeAndFlip(m_board, current_playerid, board_pos);
                switchCurrentPlayerId();
                return true;
            }
            return false;
        }
        //forfeitTurn(true);
        //switchCurrentPlayerId();
        return true;
    }

    bool OthelloGameEngine::noLegalMoves(const othello::PlayerId& playerid)
    {
        return othello::utility::legalMoves(m_board, playerid).empty();
    }

    void OthelloGameEngine::think(const std::chrono::seconds& time_limit)
    {
       PlayerType type = currentPlayerType();
       if (type == PlayerType::Human)
        return;

       //For AI
       if (!noLegalMoves(current_playerid)){
           forfeitTurn(false);
           if (current_playerid == PlayerId::One)
               aiThinkAndMove(m_player_one, time_limit);
           else
               aiThinkAndMove(m_player_two, time_limit);

           switchCurrentPlayerId();
       }
       //forfeitTurn(true);
       //switchCurrentPlayerId();
    }

    void OthelloGameEngine::forfeitTurn(bool flag)
    {
        if (flag)
            forfeited = true;
        else
            forfeited = false;
    }

    bool OthelloGameEngine::ifGameStarted()
    {
        return game_started;
    }

    bool OthelloGameEngine::forfeitedLastTurn()
    {
        return forfeited;
    }

    void OthelloGameEngine::switchCurrentPlayerId()
    {
        current_playerid = utility::opponent(current_playerid);
    }

    PlayerId OthelloGameEngine::currentPlayerId() const
    {

        return current_playerid;
    }

    PlayerType OthelloGameEngine::currentPlayerType() const
    {
        if (current_playerid == PlayerId::One)
            return m_player_one.obj->type();
        if (current_playerid == PlayerId::Two)
            return m_player_two.obj->type();
    }

    BitPieces OthelloGameEngine::pieces(const PlayerId& player_id) const
    {
        return m_board[static_cast<size_t>(player_id)];
    }

    const BitBoard& OthelloGameEngine::board() const {return m_board;}

    void OthelloGameEngine::aiThinkAndMove(detail::PlayerStruct &player, const std::chrono::seconds &time_limit)
    {
        player.obj->think(m_board, current_playerid, time_limit);
        utility::placeAndFlip(m_board, current_playerid, player.obj->bestMove());
    }


}   // namespace othello