Commit 80f590bd6b00f5cd176868d7ae5e1ce523fc56b2

Authored by Hakeem
1 parent 18dbf932

orange vs orange done

Showing 3 changed files with 49 additions and 16 deletions   Show diff stats
CMakeLists.txt.user
1 1 <?xml version="1.0" encoding="UTF-8"?>
2 2 <!DOCTYPE QtCreatorProject>
3   -<!-- Written by QtCreator 4.7.0, 2018-09-19T15:34:16. -->
  3 +<!-- Written by QtCreator 4.7.0, 2018-10-02T18:59:24. -->
4 4 <qtcreator>
5 5 <data>
6 6 <variable>EnvironmentId</variable>
... ...
engine.cpp
... ... @@ -13,6 +13,8 @@ namespace othello
13 13 m_board[1].set(28);
14 14  
15 15 current_playerid = PlayerId::One;
  16 + game_started = true;
  17 + forfeited = false;
16 18  
17 19 return true;
18 20 }
... ... @@ -25,38 +27,62 @@ namespace othello
25 27  
26 28 bool OthelloGameEngine::performMoveForCurrentHuman(const BitPos& board_pos)
27 29 {
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   - switchCurrentPlayerId();
31   - return true;
32   - }
33   - return false;
34   -
  30 +// if (!noLegalMoves(current_playerid)){
  31 + if (utility::isLegalMove(m_board, current_playerid, board_pos)){
  32 + utility::placeAndFlip(m_board, current_playerid, board_pos);
  33 + switchCurrentPlayerId();
  34 + return true;
  35 + }
  36 + return false;
  37 +// }
  38 + switchCurrentPlayerId();
  39 + return true;
35 40 }
36 41  
37   - bool OthelloGameEngine::legalMovesCheck()
  42 + bool OthelloGameEngine::noLegalMoves(const othello::PlayerId& playerid)
38 43 {
39   - return othello::utility::legalMoves(m_board, current_playerid).empty();
  44 + return othello::utility::legalMoves(m_board, playerid).empty();
40 45 }
41 46  
42 47 void OthelloGameEngine::think(const std::chrono::seconds& time_limit)
43 48 {
  49 + forfeitTurn(false);
44 50 PlayerType type = currentPlayerType();
45 51 if (type == PlayerType::Human)
46 52 return;
47 53  
48 54 //For AI
49   - if (current_playerid == PlayerId::One)
50   - aiThinkAndMove(m_player_one, time_limit);
  55 + if (!noLegalMoves(current_playerid)){
  56 + if (current_playerid == PlayerId::One)
  57 + aiThinkAndMove(m_player_one, time_limit);
  58 + else
  59 + aiThinkAndMove(m_player_two, time_limit);
  60 +
  61 + switchCurrentPlayerId();
  62 + }
  63 + }
  64 +
  65 + void OthelloGameEngine::forfeitTurn(bool flag)
  66 + {
  67 + if (flag)
  68 + forfeited = true;
51 69 else
52   - aiThinkAndMove(m_player_two, time_limit);
  70 + forfeited = false;
  71 + }
53 72  
54   - switchCurrentPlayerId();
  73 + bool OthelloGameEngine::ifGameStarted()
  74 + {
  75 + return game_started;
  76 + }
  77 +
  78 + bool OthelloGameEngine::forfeitedLastTurn()
  79 + {
  80 + return forfeited;
55 81 }
56 82  
57 83 void OthelloGameEngine::switchCurrentPlayerId()
58 84 {
59   - current_playerid = utility::opponent(current_playerid);
  85 + current_playerid = utility::opponent(current_playerid);
60 86 }
61 87  
62 88 PlayerId OthelloGameEngine::currentPlayerId() const
... ...
engine.h
... ... @@ -35,10 +35,15 @@ namespace othello
35 35 bool initNewGame() override;
36 36 void clearGame() override;
37 37 bool performMoveForCurrentHuman(const othello::BitPos&) override;
38   - bool legalMovesCheck();
  38 + bool noLegalMoves(const othello::PlayerId& playerid);
39 39 void switchCurrentPlayerId();
40 40 void think(const std::chrono::seconds&) override;
41 41  
  42 + void forfeitTurn(bool flag);
  43 + bool ifGameStarted();
  44 + bool forfeitedLastTurn();
  45 +
  46 +
42 47 othello::PlayerId currentPlayerId() const override;
43 48 othello::PlayerType currentPlayerType() const override;
44 49 othello::BitPieces pieces(const othello::PlayerId&) const override;
... ... @@ -50,6 +55,8 @@ namespace othello
50 55 private:
51 56 void aiThinkAndMove(detail::PlayerStruct& player, const std::chrono::seconds& time_limit);
52 57  
  58 + bool forfeited;
  59 + bool game_started;
53 60 PlayerId current_playerid = PlayerId::One;
54 61 };
55 62  
... ...