Commit 18dbf9320029cd2dc7a285df0508882ee0a8ecf8

Authored by Hakeem
1 parent ef26ad41

orange monkey working, and stuff

Showing 2 changed files with 37 additions and 13 deletions   Show diff stats
engine.cpp
... ... @@ -12,7 +12,7 @@ namespace othello
12 12 m_board[1].set(35);
13 13 m_board[1].set(28);
14 14  
15   - m_playerid = PlayerId::One;
  15 + current_playerid = PlayerId::One;
16 16  
17 17 return true;
18 18 }
... ... @@ -25,8 +25,8 @@ namespace othello
25 25  
26 26 bool OthelloGameEngine::performMoveForCurrentHuman(const BitPos& board_pos)
27 27 {
28   - if (utility::isLegalMove(m_board, m_playerid, board_pos) /*and !utility::occupiedPositions(m_board).test(board_pos.value())*/){
29   - utility::placeAndFlip(m_board, m_playerid, board_pos);
  28 + if (utility::isLegalMove(m_board, current_playerid, board_pos) /*and !utility::occupiedPositions(m_board).test(board_pos.value())*/){
  29 + utility::placeAndFlip(m_board, current_playerid, board_pos);
30 30 switchCurrentPlayerId();
31 31 return true;
32 32 }
... ... @@ -34,29 +34,43 @@ namespace othello
34 34  
35 35 }
36 36  
37   - void OthelloGameEngine::think(const std::chrono::seconds& /*time_limit*/)
  37 + bool OthelloGameEngine::legalMovesCheck()
38 38 {
  39 + return othello::utility::legalMoves(m_board, current_playerid).empty();
  40 + }
  41 +
  42 + void OthelloGameEngine::think(const std::chrono::seconds& time_limit)
  43 + {
  44 + PlayerType type = currentPlayerType();
  45 + if (type == PlayerType::Human)
  46 + return;
  47 +
  48 + //For AI
  49 + if (current_playerid == PlayerId::One)
  50 + aiThinkAndMove(m_player_one, time_limit);
  51 + else
  52 + aiThinkAndMove(m_player_two, time_limit);
39 53  
  54 + switchCurrentPlayerId();
40 55 }
41 56  
42 57 void OthelloGameEngine::switchCurrentPlayerId()
43 58 {
44   - if (m_playerid == PlayerId::One){
45   - m_playerid = PlayerId::Two;
46   - }
47   - else m_playerid = PlayerId::One;
  59 + current_playerid = utility::opponent(current_playerid);
48 60 }
49 61  
50 62 PlayerId OthelloGameEngine::currentPlayerId() const
51 63 {
52 64  
53   - return m_playerid;
  65 + return current_playerid;
54 66 }
55 67  
56 68 PlayerType OthelloGameEngine::currentPlayerType() const
57 69 {
58   - //for use after implementing AI
59   - return {};
  70 + if (current_playerid == PlayerId::One)
  71 + return m_player_one.obj->type();
  72 + if (current_playerid == PlayerId::Two)
  73 + return m_player_two.obj->type();
60 74 }
61 75  
62 76 BitPieces OthelloGameEngine::pieces(const PlayerId& player_id) const
... ... @@ -66,5 +80,11 @@ namespace othello
66 80  
67 81 const BitBoard& OthelloGameEngine::board() const {return m_board;}
68 82  
  83 + void OthelloGameEngine::aiThinkAndMove(detail::PlayerStruct &player, const std::chrono::seconds &time_limit)
  84 + {
  85 + player.obj->think(m_board, current_playerid, time_limit);
  86 + utility::placeAndFlip(m_board, current_playerid, player.obj->bestMove());
  87 + }
  88 +
69 89  
70 90 } // namespace othello
... ...
engine.h
... ... @@ -29,7 +29,7 @@ namespace othello
29 29 class OthelloGameEngine : public othello::GameEngineInterface {
30 30 // GameEngineInterface interface
31 31  
32   - PlayerId m_playerid = PlayerId::One;
  32 +
33 33  
34 34 public:
35 35 bool initNewGame() override;
... ... @@ -37,8 +37,8 @@ namespace othello
37 37 bool performMoveForCurrentHuman(const othello::BitPos&) override;
38 38 bool legalMovesCheck();
39 39 void switchCurrentPlayerId();
  40 + void think(const std::chrono::seconds&) override;
40 41  
41   - void think(const std::chrono::seconds&) override;
42 42 othello::PlayerId currentPlayerId() const override;
43 43 othello::PlayerType currentPlayerType() const override;
44 44 othello::BitPieces pieces(const othello::PlayerId&) const override;
... ... @@ -47,6 +47,10 @@ namespace othello
47 47  
48 48 // const OpeningBook m_openingbook;
49 49  
  50 + private:
  51 + void aiThinkAndMove(detail::PlayerStruct& player, const std::chrono::seconds& time_limit);
  52 +
  53 + PlayerId current_playerid = PlayerId::One;
50 54 };
51 55  
52 56 } // namespace othello
... ...