Board.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. // ----------------------------------------------------------------
  2. // From Game Programming in C++ by Sanjay Madhav
  3. // Copyright (C) 2017 Sanjay Madhav. All rights reserved.
  4. //
  5. // Released under the BSD License
  6. // See LICENSE in root directory for full details.
  7. // ----------------------------------------------------------------
  8. #include "Board.h"
  9. #include "Random.h"
  10. BoardState::BoardState()
  11. {
  12. for (int i = 0; i < 6; i++)
  13. {
  14. for (int j = 0; j < 7; j++)
  15. {
  16. mBoard[i][j] = Empty;
  17. }
  18. }
  19. }
  20. std::vector<BoardState*> BoardState::GetPossibleMoves(SquareState player) const
  21. {
  22. std::vector<BoardState*> retVal;
  23. // For each column, find if a move is possible
  24. for (int col = 0; col < 7; col++)
  25. {
  26. for (int row = 5; row >= 0; row--)
  27. {
  28. if (mBoard[row][col] == BoardState::Empty)
  29. {
  30. retVal.emplace_back(new BoardState(*this));
  31. retVal.back()->mBoard[row][col] = player;
  32. break;
  33. }
  34. }
  35. }
  36. return retVal;
  37. }
  38. bool BoardState::IsTerminal() const
  39. {
  40. // Is the board full?
  41. if (IsFull())
  42. {
  43. return true;
  44. }
  45. // Is there a four-in-a-row?
  46. int fourInRow = GetFourInARow();
  47. if (fourInRow != 0)
  48. {
  49. return true;
  50. }
  51. return false;
  52. }
  53. float BoardState::GetScore() const
  54. {
  55. // If the board is full, the score is 0
  56. if (IsFull())
  57. {
  58. return 0.0f;
  59. }
  60. // Is there a four-in-a-row?
  61. int fourInRow = GetFourInARow();
  62. if (fourInRow != 0)
  63. {
  64. return static_cast<float>(fourInRow);
  65. }
  66. return CalculateHeuristic();
  67. }
  68. bool BoardState::IsFull() const
  69. {
  70. bool isFull = true;
  71. for (int i = 0; i < 6; i++)
  72. {
  73. for (int j = 0; j < 7; j++)
  74. {
  75. if (mBoard[i][j] == Empty)
  76. {
  77. isFull = false;
  78. }
  79. }
  80. }
  81. return isFull;
  82. }
  83. int BoardState::GetFourInARow() const
  84. {
  85. // Returns -1 if yellow wins, 1 if red wins, 0 otherwise
  86. // Check if there's a row with four in a row
  87. for (int row = 0; row < 6; row++)
  88. {
  89. for (int col = 0; col < 4; col++)
  90. {
  91. if (mBoard[row][col] == mBoard[row][col + 1] &&
  92. mBoard[row][col] == mBoard[row][col + 2] &&
  93. mBoard[row][col] == mBoard[row][col + 3])
  94. {
  95. if (mBoard[row][col] == BoardState::Yellow)
  96. {
  97. return -1;
  98. }
  99. else if (mBoard[row][col] == BoardState::Red)
  100. {
  101. return 1;
  102. }
  103. }
  104. }
  105. }
  106. // Check if there's a column with four in a row
  107. for (int col = 0; col < 7; col++)
  108. {
  109. for (int row = 0; row < 3; row++)
  110. {
  111. if (mBoard[row][col] == mBoard[row + 1][col] &&
  112. mBoard[row][col] == mBoard[row + 2][col] &&
  113. mBoard[row][col] == mBoard[row + 3][col])
  114. {
  115. if (mBoard[row][col] == BoardState::Yellow)
  116. {
  117. return -1;
  118. }
  119. else if (mBoard[row][col] == BoardState::Red)
  120. {
  121. return 1;
  122. }
  123. }
  124. }
  125. }
  126. // Check if there's a right diagonal four in a row
  127. for (int col = 0; col < 4; col++)
  128. {
  129. for (int row = 0; row < 3; row++)
  130. {
  131. if (mBoard[row][col] == mBoard[row + 1][col + 1] &&
  132. mBoard[row][col] == mBoard[row + 2][col + 2] &&
  133. mBoard[row][col] == mBoard[row + 3][col + 3])
  134. {
  135. if (mBoard[row][col] == BoardState::Yellow)
  136. {
  137. return -1;
  138. }
  139. else if (mBoard[row][col] == BoardState::Red)
  140. {
  141. return 1;
  142. }
  143. }
  144. }
  145. }
  146. // Check if there's a left diagonal for in a row
  147. for (int col = 0; col < 4; col++)
  148. {
  149. for (int row = 3; row < 6; row++)
  150. {
  151. if (mBoard[row][col] == mBoard[row - 1][col + 1] &&
  152. mBoard[row][col] == mBoard[row - 2][col + 2] &&
  153. mBoard[row][col] == mBoard[row - 3][col + 3])
  154. {
  155. if (mBoard[row][col] == BoardState::Yellow)
  156. {
  157. return -1;
  158. }
  159. else if (mBoard[row][col] == BoardState::Red)
  160. {
  161. return 1;
  162. }
  163. }
  164. }
  165. }
  166. return 0;
  167. }
  168. float BoardState::CalculateHeuristic() const
  169. {
  170. // TODO: You could change this to calculate an actual heuristic
  171. return 0.0f;
  172. }
  173. bool TryPlayerMove(BoardState* state, int column)
  174. {
  175. // Find the first row in that column that's available
  176. // (if any)
  177. for (int row = 5; row >= 0; row--)
  178. {
  179. if (state->mBoard[row][column] == BoardState::Empty)
  180. {
  181. state->mBoard[row][column] = BoardState::Yellow;
  182. return true;
  183. }
  184. }
  185. return false;
  186. }
  187. void CPUMove(BoardState* state)
  188. {
  189. // For now, this just randomly picks one of the possible moves
  190. std::vector<BoardState*> moves = state->GetPossibleMoves(BoardState::Red);
  191. int index = Random::GetIntRange(0, moves.size() - 1);
  192. *state = *moves[index];
  193. // Clear up memory from possible moves
  194. for (auto state : moves)
  195. {
  196. delete state;
  197. }
  198. }