Commit ef26ad41cb430bb11e2904a303f4d867d44d4ea4

Authored by Hakeem
1 parent be5a5443

human player done without the end game tho

Showing 3 changed files with 36 additions and 23 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-19T08:36:15. -->
  3 +<!-- Written by QtCreator 4.7.0, 2018-09-19T15:34:16. -->
4 4 <qtcreator>
5 5 <data>
6 6 <variable>EnvironmentId</variable>
... ...
engine.cpp
... ... @@ -25,7 +25,7 @@ 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)){
  28 + if (utility::isLegalMove(m_board, m_playerid, board_pos) /*and !utility::occupiedPositions(m_board).test(board_pos.value())*/){
29 29 utility::placeAndFlip(m_board, m_playerid, board_pos);
30 30 switchCurrentPlayerId();
31 31 return true;
... ...
utility.cpp
... ... @@ -49,7 +49,7 @@ namespace othello { namespace utility
49 49 return othello::BitPos(board_pos.value() - 1);
50 50 break;
51 51 case othello::MoveDirection::SE:
52   - if((board_pos.value() > 7) and (board_pos.value() % 8 != 7))
  52 + if((board_pos.value() > 7) and (board_pos.value() % 8 != 0))
53 53 return othello::BitPos(board_pos.value() - 9);
54 54 break;
55 55 case othello::MoveDirection::S:
... ... @@ -89,42 +89,47 @@ namespace othello { namespace utility
89 89 return border_pieces; //not as efficient as it goes through the whole board for every move instead of simply updating
90 90 }
91 91  
  92 +
  93 +
92 94 BitPos findBracketingPiece(const BitBoard& board,
93 95 const BitPos& board_pos,
94 96 const PlayerId& player_id,
95 97 const MoveDirection& dir)
96 98 {
97   - BitPos next_position = nextPosition(board_pos, dir);
  99 + BitPos next_position = nextPosition(board_pos, dir);
98 100  
99   - const BitPieces& current_player_pieces = board.at(size_t(player_id));
100   - const BitPieces& opponent_player_pieces = board.at(size_t(opponent(player_id)));
  101 + const BitPieces& current_player_pieces = board.at(size_t(player_id));
  102 + const BitPieces& opponent_player_pieces = board.at(size_t(opponent(player_id)));
101 103  
102   - if (not next_position.isValid() or not opponent_player_pieces.test(next_position.value())){
103   - return BitPos::invalid();
104   - }
105   - next_position = nextPosition(board_pos, dir);
  104 + if (not next_position.isValid() or not opponent_player_pieces.test(next_position.value())){
  105 + return BitPos::invalid();
  106 + }
  107 + next_position = nextPosition(next_position, dir);
106 108  
107   - while (next_position.isValid()){
108   - if (current_player_pieces.test(next_position.value()))
109   - return next_position;
110   - if (not opponent_player_pieces.test(next_position.value()))
111   - return BitPos::invalid(); // Must be an empty position
  109 + while (next_position.isValid()){
  110 + if (current_player_pieces.test(next_position.value()))
  111 + return next_position;
  112 + if (not opponent_player_pieces.test(next_position.value()))
  113 + return BitPos::invalid(); // Must be an empty position
112 114  
113   - next_position = nextPosition(board_pos, dir);
  115 + next_position = nextPosition(next_position, dir);
114 116  
115   - }
  117 + }
116 118  
117   - return BitPos::invalid();
  119 + return BitPos::invalid();
118 120 }
119 121  
  122 +
  123 +
120 124 BitPosSet legalMoves(const BitBoard& board, const PlayerId& player_id)
121 125 {
122   - BitPosSet legal_moves;
  126 + BitPosSet legal_moves;
123 127 for (size_t i = 0; i < detail::computeBoardSize(); ++i){
124 128 if(isLegalMove(board, player_id, BitPos(i))){
125 129 legal_moves.insert(BitPos(i));
126 130 }
127 131 }
  132 + return legal_moves;
128 133 }
129 134  
130 135 bool isLegalMove(const BitBoard& board, const PlayerId& player_id,
... ... @@ -133,7 +138,7 @@ namespace othello { namespace utility
133 138 for (size_t i = 0; i < 8; ++i){
134 139 auto dir = static_cast<MoveDirection>(i);
135 140 BitPos bracketing_pieces = findBracketingPiece(board, board_pos, player_id, dir);
136   - if (bracketing_pieces.isValid())
  141 + if (bracketing_pieces.isValid()and !occupiedPositions(board).test(board_pos.value()))//added and !occupiedPositions
137 142 return true;
138 143 }
139 144 return false;
... ... @@ -147,9 +152,17 @@ namespace othello { namespace utility
147 152 board.at(size_t(player_id)).set(board_pos.value());
148 153 for (size_t i = 0; i < 8; ++i){
149 154 auto dir = static_cast<MoveDirection>(i);
150   - BitPos captured_piece = nextPosition(board_pos, dir);
151   - board.at(size_t(opponent(player_id))).flip(captured_piece.value());
152   - captured_piece = nextPosition(captured_piece, dir);
  155 + BitPos bracketing_piece = findBracketingPiece(board, board_pos, player_id, dir);
  156 + if (bracketing_piece.isValid()){
  157 + BitPos captured_piece = nextPosition(board_pos, dir);
  158 + while (bracketing_piece != captured_piece){
  159 + std::for_each(std::begin(board),std::end(board),[&captured_piece](auto& piece_set){
  160 + piece_set.flip(captured_piece.value());
  161 + });
  162 + captured_piece = nextPosition(captured_piece, dir);
  163 + }
  164 + }
  165 +
153 166 }
154 167 }
155 168 }
... ...