Game.cpp 13 KB

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