Game.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. #include <iostream>
  2. #include "Game.h"
  3. #include "Types.h"
  4. Game::Game() : activeX(-1), activeY(-1), directionX(0), directionY(0), state(SELECTION), lastLocation(0) {
  5. reset();
  6. }
  7. void Game::reset() {
  8. activeX = -1;
  9. activeY = -1;
  10. for(uint x = 0; x < 9; x++) {
  11. for(uint y = 0; y < 5; y++) {
  12. fields[x][y] = (y > 2 ? WHITE : BLACK);
  13. }
  14. }
  15. fields[0][2] = WHITE;
  16. fields[2][2] = WHITE;
  17. fields[4][2] = EMPTY;
  18. fields[5][2] = WHITE;
  19. fields[7][2] = WHITE;
  20. fields[2][0] = WHITE;
  21. fields[1][0] = EMPTY;
  22. fields[1][1] = EMPTY;
  23. }
  24. void Game::reset(String& output) {
  25. reset();
  26. output.append("Fanorona Game\n\r");
  27. print(output);
  28. output.append("Human Color: White(O)\n\rAIColor: Black(#)\n\rHuman turn\n\rselect stone: ");
  29. }
  30. void Game::print(String& s) const {
  31. s.append("\n\r 0 1 2 3 4 5 6 7 8\n\r");
  32. printLine(s, 0);
  33. s.append(" |\\|/|\\|/|\\|/|\\|/|\n\r");
  34. printLine(s, 1);
  35. s.append(" |/|\\|/|\\|/|\\|/|\\|\n\r");
  36. printLine(s, 2);
  37. s.append(" |\\|/|\\|/|\\|/|\\|/|\n\r");
  38. printLine(s, 3);
  39. s.append(" |/|\\|/|\\|/|\\|/|\\|\n\r");
  40. printLine(s, 4);
  41. s.append("\n\r");
  42. }
  43. void Game::printLine(String& s, int index) const {
  44. static const char map[] = {
  45. '#', 'O', '.'
  46. };
  47. s.append(index + '0').append(' ');
  48. for(int x = 0; x < 8; x++) {
  49. if(x == activeX && index == activeY) {
  50. s.append("*-");
  51. continue;
  52. }
  53. s.append(map[fields[x][index]]).append('-');
  54. }
  55. if(8 == activeX && index == activeY) {
  56. s.append('*');
  57. } else {
  58. s.append(map[fields[8][index]]);
  59. }
  60. s.append("\n\r");
  61. }
  62. bool Game::parseLocation(const String& input, int& x, int& y) {
  63. if(input.getLength() <= 2 || !isDigit(input[0]) || input[1] != ' ' || !isDigit(input[2])) {
  64. return true;
  65. }
  66. x = input[0] - '0';
  67. y = input[2] - '0';
  68. return x >= 9 || y >= 6;
  69. }
  70. bool Game::parse(const String& input, String& output) {
  71. switch(state) {
  72. case SELECTION:
  73. parseSelection(input, output);
  74. break;
  75. case MOVE:
  76. parseMove(input, output);
  77. break;
  78. case ANOTHER_MOVE:
  79. parseAnotherMove(input, output);
  80. break;
  81. case REMOVE_SELECTION:
  82. parseRemoveSelection(input, output);
  83. break;
  84. }
  85. return shouldEnd();
  86. }
  87. void Game::parseSelection(const String& input, String& output) {
  88. int x;
  89. int y;
  90. if(parseLocation(input, x, y) || fields[x][y] != WHITE) {
  91. output.append("please choose white stone.\n\r");
  92. print(output);
  93. output.append("Human Color: White(O)\n\rAIColor: Black(#)\n\rHuman turn\n\rselect stone: ");
  94. return;
  95. }
  96. state = MOVE;
  97. activeX = x;
  98. activeY = y;
  99. print(output);
  100. output.append("Human Color: White(O)\n\rAIColor: Black(#)\n\rselect location to move: ");
  101. }
  102. void Game::parseMove(const String& input, String& output) {
  103. int x;
  104. int y;
  105. if(parseLocation(input, x, y)) {
  106. output.append("invalid move location.\n\r");
  107. print(output);
  108. output.append("Human Color: White(O)\n\rAIColor: Black(#)\n\rselect location to move: ");
  109. return;
  110. }
  111. if(fields[x][y] == WHITE) {
  112. activeX = x;
  113. activeY = y;
  114. print(output);
  115. output.append("Human Color: White(O)\n\rAIColor: Black(#)\n\rselect location to move: ");
  116. return;
  117. }
  118. if(fields[x][y] != EMPTY || !areNeighbours(activeX, activeY, x, y)) {
  119. output.append("invalid move location.\n\r");
  120. print(output);
  121. output.append("Human Color: White(O)\n\rAIColor: Black(#)\n\rselect location to move: ");
  122. return;
  123. }
  124. directionX = x - activeX;
  125. directionY = y - activeY;
  126. if(markHitSides(activeX, activeY, x, y, BLACK)) {
  127. state = REMOVE_SELECTION;
  128. print(output);
  129. output.append("Human Color: White(O)\n\rAIColor: Black(#)\n\rselect stone to take: ");
  130. return;
  131. }
  132. lastLocation = 0;
  133. move(activeX, activeY, x, y);
  134. bool removed = false;
  135. removed = removeLine(x, y, activeX, activeY, BLACK) || removed;
  136. removed = removeLine(activeX, activeY, x, y, BLACK) || removed;
  137. activeX = x;
  138. activeY = y;
  139. print(output);
  140. output.append("Human Color: White(O)\n\rAIColor: Black(#)\n\r");
  141. print(output);
  142. if(removed && isAnotherTurnPossible()) {
  143. state = ANOTHER_MOVE;
  144. output.append("Human Color: White(O)\n\rAIColor: Black(#)\n\rselect location to move: ");
  145. return;
  146. }
  147. state = SELECTION;
  148. output.append("Human Color: White(O)\n\rAIColor: Black(#)\n\rAI turn\n\r");
  149. activeX = -1;
  150. activeY = -1;
  151. botMove(output);
  152. }
  153. void Game::parseAnotherMove(const String& input, String& output) {
  154. int x;
  155. int y;
  156. if(parseLocation(input, x, y) || !canMoveActiveStoneTo(x - activeX, y - activeY)) {
  157. output.append("invalid move location.\n\r");
  158. print(output);
  159. output.append("Human Color: White(O)\n\rAIColor: Black(#)\n\rselect location to move: ");
  160. return;
  161. }
  162. directionX = x - activeX;
  163. directionY = y - activeY;
  164. if(markHitSides(activeX, activeY, x, y, BLACK)) {
  165. state = REMOVE_SELECTION;
  166. print(output);
  167. output.append("Human Color: White(O)\n\rAIColor: Black(#)\n\rselect stone to take: ");
  168. return;
  169. }
  170. move(activeX, activeY, x, y);
  171. bool removed = false;
  172. removed = removeLine(x, y, activeX, activeY, BLACK) || removed;
  173. removed = removeLine(activeX, activeY, x, y, BLACK) || removed;
  174. activeX = x;
  175. activeY = y;
  176. print(output);
  177. output.append("Human Color: White(O)\n\rAIColor: Black(#)\n\r");
  178. print(output);
  179. if(removed && isAnotherTurnPossible()) {
  180. output.append("Human Color: White(O)\n\rAIColor: Black(#)\n\rselect location to move: ");
  181. return;
  182. }
  183. state = SELECTION;
  184. output.append("Human Color: White(O)\n\rAIColor: Black(#)\n\rAI turn\n\r");
  185. activeX = -1;
  186. activeY = -1;
  187. botMove(output);
  188. }
  189. void Game::parseRemoveSelection(const String& input, String& output) {
  190. int x;
  191. int y;
  192. if(parseLocation(input, x, y) || !((hitA.x == x && hitA.y == y) || (hitB.x == x && hitB.y == y))) {
  193. output.append("invalid take location.\n\r");
  194. print(output);
  195. output.append("Human Color: White(O)\n\rAIColor: Black(#)\n\rselect stone to take: ");
  196. return;
  197. }
  198. int mx = activeX + directionX;
  199. int my = activeY + directionY;
  200. move(activeX, activeY, mx, my);
  201. if(mx + directionX == x && my + directionY == y) {
  202. removeLine(activeX, activeY, mx, my, BLACK);
  203. } else {
  204. removeLine(mx, my, activeX, activeY, BLACK);
  205. }
  206. activeX = mx;
  207. activeY = my;
  208. print(output);
  209. if(isAnotherTurnPossible()) {
  210. state = ANOTHER_MOVE;
  211. output.append("Human Color: White(O)\n\rAIColor: Black(#)\n\rselect location to move: ");
  212. return;
  213. }
  214. state = SELECTION;
  215. output.append("Human Color: White(O)\n\rAIColor: Black(#)\n\rAI turn\n\r");
  216. activeX = -1;
  217. activeY = -1;
  218. botMove(output);
  219. }
  220. void Game::move(int fromX, int fromY, int toX, int toY) {
  221. addLocation(fromX, fromY);
  222. fields[fromX][fromY] = EMPTY;
  223. fields[toX][toY] = WHITE;
  224. }
  225. bool Game::isDigit(char c) const {
  226. return c >= '0' && c <= '9';
  227. }
  228. bool Game::isInRange(int x, int y) const {
  229. return x >= 0 && x <= 8 && y >= 0 && y <= 4;
  230. }
  231. bool Game::areNeighbours(int x, int y, int x2, int y2) const {
  232. if(x == x2 && y == y2) {
  233. return false;
  234. }
  235. int diffX = x - x2;
  236. int diffY = y - y2;
  237. if(diffX < -1 || diffX > 1 || diffY < -1 || diffY > 1) {
  238. return false;
  239. }
  240. if(diffX == 0 || diffY == 0) {
  241. return true;
  242. }
  243. return !((x & 1) ^ (y & 1));
  244. }
  245. void Game::revertToSelection(String& output) {
  246. activeX = -1;
  247. activeY = -1;
  248. print(output);
  249. output.append("Human Color: White(O)\n\rAIColor: Black(#)\n\rHuman turn\n\rselect stone: ");
  250. }
  251. bool Game::removeLine(int x, int y, int x2, int y2, FieldState remove) {
  252. int diffX = x2 - x;
  253. int diffY = y2 - y;
  254. bool removed = false;
  255. while(true) {
  256. x2 += diffX;
  257. y2 += diffY;
  258. if(!isInRange(x2, y2) || fields[x2][y2] != remove) {
  259. return removed;
  260. }
  261. removed = true;
  262. fields[x2][y2] = EMPTY;
  263. }
  264. }
  265. void Game::botMove(String& output) {
  266. if(shouldEnd()) {
  267. return;
  268. }
  269. while(true) {
  270. int x = 0;
  271. int y = 0;
  272. while(true) {
  273. x = rand() % 9;
  274. y = rand() % 5;
  275. if(fields[x][y] == EMPTY) {
  276. break;
  277. }
  278. }
  279. for(uint i = 0; i < 10; i++) {
  280. int mx = x + (rand() % 3) - 1;
  281. int my = y + (rand() % 3) - 1;
  282. if((mx == x && my == y) || !isInRange(mx, my) || fields[mx][my] != BLACK) {
  283. continue;
  284. }
  285. fields[mx][my] = EMPTY;
  286. fields[x][y] = BLACK;
  287. removeLine(mx, my, x, y, WHITE);
  288. removeLine(x, y, mx, my, WHITE);
  289. activeX = x;
  290. activeY = y;
  291. print(output);
  292. activeX = -1;
  293. activeY = -1;
  294. print(output);
  295. revertToSelection(output);
  296. return;
  297. }
  298. }
  299. }
  300. void Game::addLocation(int x, int y) {
  301. if(lastLocation >= lastLocations.size()) {
  302. // safely to ignore, such long turns will never happen
  303. return;
  304. }
  305. lastLocations[lastLocation].x = x;
  306. lastLocations[lastLocation].y = y;
  307. lastLocation++;
  308. }
  309. bool Game::isInQueue(int x, int y) const {
  310. for(uint i = 0; i < lastLocation; i++) {
  311. if(lastLocations[i].x == x && lastLocations[i].y == y) {
  312. return true;
  313. }
  314. }
  315. return false;
  316. }
  317. bool Game::canMoveActiveStoneTo(int dirX, int dirY) const {
  318. if(directionX == dirX && directionY == dirY) {
  319. return false;
  320. }
  321. int newX = activeX + dirX;
  322. int newY = activeY + dirY;
  323. return isInRange(newX, newY) && areNeighbours(activeX, activeY, newX, newY) &&
  324. fields[newX][newY] == EMPTY && !isInQueue(newX, newY);
  325. }
  326. bool Game::isAnotherTurnPossible() const {
  327. return canMoveActiveStoneTo(-1, -1) || canMoveActiveStoneTo(0, -1) || canMoveActiveStoneTo(1, -1) ||
  328. canMoveActiveStoneTo(-1, 0) || canMoveActiveStoneTo(1, 0) ||
  329. canMoveActiveStoneTo(-1, 1) || canMoveActiveStoneTo(0, 1) || canMoveActiveStoneTo(1, 1);
  330. }
  331. bool Game::markHitSides(int x1, int y1, int x2, int y2, FieldState state) {
  332. hitA.x = x1 + (x1 - x2);
  333. hitA.y = y1 + (y1 - y2);
  334. hitB.x = x2 + (x2 - x1);
  335. hitB.y = y2 + (y2 - y1);
  336. return isInRange(hitA.x, hitA.y) && isInRange(hitB.x, hitB.y) &&
  337. areNeighbours(x1, y1, hitA.x, hitA.y) && areNeighbours(x2, y2, hitB.x, hitB.y) &&
  338. fields[hitA.x][ hitA.y] == state && fields[hitB.x][ hitB.y] == state;
  339. }
  340. bool Game::shouldEnd() const {
  341. int player = 0;
  342. int ai = 0;
  343. for(uint x = 0; x < 9; x++) {
  344. for(uint y = 0; y < 5; y++) {
  345. player += fields[x][y] == WHITE;
  346. ai += fields[x][y] == BLACK;
  347. }
  348. }
  349. return player == 0 || ai == 0;
  350. }