Advertisement
Guest User

Untitled

a guest
Dec 4th, 2016
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 36.29 KB | None | 0 0
  1. //Code written by Joseph Dillman and Gun Min (Richard) Song. CMPT128 D200 assignment 2
  2. //This code is designed to
  3. //Last edited on November 27, 2016
  4. #include <iostream>
  5. #include <iomanip>
  6. #include <cmath>
  7.  
  8. using namespace std;
  9.  
  10. //Declare and initialize all global constants, then display function prototypes
  11. const int MAX_ARRAY_SIZE = 18;
  12. const int MIN_ARRAY_SIZE = 8;
  13. const int MAX_PIECES = 72;
  14. const int NOPLAYER = 0;
  15. const int WHITEWINS = 1;
  16. const int REDWINS = 2;
  17. const int NOONEWINS = 0;
  18. const int WHITESOLDIER = 1;
  19. const int WHITEMULE = 2;
  20. const int WHITEKING = 3;
  21. const int REDSOLDIER = 4;
  22. const int REDMULE = 5;
  23. const int REDKING = 6;
  24. const int WHITEPLAYER = 1;
  25. const int REDPLAYER = 2;
  26.  
  27. void InitializeBoard(int CMCheckersBoard[MAX_ARRAY_SIZE][MAX_ARRAY_SIZE], int numRowsInBoard);
  28. void DisplayBoard(int CMCheckersBoard[MAX_ARRAY_SIZE][MAX_ARRAY_SIZE], int numRowsInBoard);
  29. int CountJumps(int CMCheckersBoard[MAX_ARRAY_SIZE][MAX_ARRAY_SIZE], int numRowsInBoard, int player, int xLocArray[], int yLocArray[]);
  30. bool IsJump(int CMCheckersBoard[MAX_ARRAY_SIZE][MAX_ARRAY_SIZE], int numRowsInBoard, int player, int xLoc, int yLoc);
  31. int CountMove1Squares(int CMCheckersBoard[MAX_ARRAY_SIZE][MAX_ARRAY_SIZE], int numRowsInBoard, int player, int xLocArray[], int yLocArray[]);
  32. bool IsMove1Square(int CMCheckersBoard[MAX_ARRAY_SIZE][MAX_ARRAY_SIZE], int numRowsInBoard, int player, int xLoc, int yLoc);
  33. bool CheckWin(int CMCheckersBoard[MAX_ARRAY_SIZE][MAX_ARRAY_SIZE], int numRowsInBoard);
  34. bool MakeMove(int CMCheckersBoard[MAX_ARRAY_SIZE][MAX_ARRAY_SIZE], int numRowsInBoard, int player, int fromSquareNum, int toSquareNum, bool &jumped);
  35. //bool CheckList(int inArray1[], int inArray2[], int xIndex, int yindex);
  36.  
  37.  
  38.  
  39. int main()
  40. {
  41. //declare and initialize all main variables
  42. int numrowsboard = 0;
  43. int myCMCheckersBoard[MAX_ARRAY_SIZE][MAX_ARRAY_SIZE] = { 0 };
  44. int xIndiciesMove[MAX_PIECES] = { 0 };
  45. int yIndiciesMove[MAX_PIECES] = { 0 };
  46. int xIndiciesJump[MAX_PIECES] = { 0 };
  47. int yIndiciesJump[MAX_PIECES] = { 0 };
  48. int count = 0;
  49. int player = 1;
  50. int turn = 1;
  51. int num1 = 0;
  52. int num2 = 0;
  53. int i = 0;
  54. int j = 0;
  55. int k = 0;
  56. char word[1024] = { '0' };
  57. int jumpablenum[1024] = { '0' };
  58. //prompt user to enter the size of the board, allow a max three tries if input is incorrect
  59.  
  60. for (int count = 0; count<10; count++)
  61. {
  62. if (count >= 3)
  63. {
  64. cerr << "ERROR: Too many errors entering the size of the board";
  65. return(1);
  66. }
  67. cout << "Enter the number of squares along each edge of the board ";
  68. cout << endl;
  69. if (!(cin >> numrowsboard))
  70. {
  71. cerr << "ERROR: Board size is not an integer";
  72. cerr << endl << "8 <= number of squares <= 18" << endl;
  73. cin.clear();
  74. cin.ignore(1000, '\n');
  75. continue;
  76. }
  77. if (numrowsboard < 8)
  78. {
  79. cerr << "ERROR: Board size too small";
  80. cerr << endl << "8 <= number of squares <= 18" << endl;
  81. continue;
  82. }
  83. if (numrowsboard > 18)
  84. {
  85. cerr << "ERROR: Board size too large";
  86. cerr << endl << "8 <= number of squares <= 18" << endl;
  87. continue;
  88. }
  89. if (numrowsboard % 2 != 0)
  90. {
  91. cerr << "ERROR: Board size odd";
  92. cerr << endl << "8 <= number of squares <= 18" << endl;
  93. continue;
  94. }
  95. break;
  96. }
  97. InitializeBoard(myCMCheckersBoard, numrowsboard);
  98. DisplayBoard(myCMCheckersBoard, numrowsboard);
  99. //set a long counter to repeat the next tests for each alternating turn
  100. for (turn = 1; turn < 1000; turn++)
  101. {
  102. if (turn % 2 == 1)
  103. {
  104. player = 1;
  105. if (CountJumps(myCMCheckersBoard, numrowsboard, player, xIndiciesJump, yIndiciesJump) == 0 && CountMove1Squares(myCMCheckersBoard, numrowsboard, player, xIndiciesJump, yIndiciesJump) == 0)
  106. {
  107. cout << "White is unable to move" << endl << "GAME OVER, Red has won" << endl << "Enter any character to close the game";
  108. cin >> word;
  109. exit(1);
  110. }
  111. else
  112. {
  113. cout << "White takes a turn" << endl;
  114. for (int x = 0; x<1000; x++) //ensure that the checker selected by the player is usable
  115. {
  116. cout << "Enter the square number of the checker you want to move" << endl;
  117. if (!(cin >> num1))
  118. {
  119. cerr << "ERROR: you did not enter an integer" << endl;
  120. cerr << "Try again" << endl;
  121. cin.clear();
  122. cin.ignore(1024, '\n');
  123. continue;
  124. }
  125. if (num1 < 0 || num1 > (numrowsboard*numrowsboard)-1)
  126. {
  127. cerr << "ERROR: that square is not on the board." << endl;
  128. cerr << "Try again" << endl;
  129. continue;
  130. }
  131. if (myCMCheckersBoard[num1 / numrowsboard][num1%numrowsboard] == 0)
  132. {
  133. cerr << "ERROR: that square is empty" << endl;
  134. cerr << "Try again" << endl;
  135. continue;
  136. }
  137. if (myCMCheckersBoard[num1 / numrowsboard][num1%numrowsboard] == 4 || myCMCheckersBoard[num1 / numrowsboard][num1%numrowsboard] == 5 || myCMCheckersBoard[num1 / numrowsboard][num1%numrowsboard] == 6)
  138. {
  139. cerr << "ERROR: that square contains an opponent's checker" << endl;
  140. cerr << "Try again" << endl;
  141. continue;
  142. }
  143. if (IsJump(myCMCheckersBoard, numrowsboard, player, num1%numrowsboard, num1 / numrowsboard) == false && CountJumps(myCMCheckersBoard, numrowsboard, player, xIndiciesJump, yIndiciesJump) > 0)
  144. {
  145. for (int y = 0; y < numrowsboard; y++)
  146. {
  147. for (int x = 0; x < numrowsboard; x++)
  148. {
  149. if (IsJump(myCMCheckersBoard, numrowsboard, player, x, y) == true)
  150. {
  151. jumpablenum[count] = numrowsboard*y + x;
  152. count++;
  153. }
  154. }
  155. }
  156. cerr << "ERROR: You can jump with another checker, you may not move your chosen checker" << endl;
  157. cerr << "You can jump using checkers on the following squares:";
  158. for (int g = 0; g < count; g++)
  159. {
  160. cout << " ";
  161. cout << jumpablenum[g];
  162. }
  163. cout << endl;
  164. cerr << "Try again" << endl;
  165. continue;
  166. }
  167. if (IsJump(myCMCheckersBoard, numrowsboard, player, num1%numrowsboard, num1 / numrowsboard) == false && IsMove1Square(myCMCheckersBoard, numrowsboard, player, num1%numrowsboard, num1 / numrowsboard) == false)
  168. {
  169. cerr << "ERROR: There is no legal move for this checker" << endl;
  170. cerr << "Try again" << endl;
  171. continue;
  172. }
  173.  
  174.  
  175.  
  176.  
  177.  
  178. }
  179. }
  180. }
  181. else if (turn % 2 == 2)
  182. {
  183. player = 2;
  184. if (CountJumps(myCMCheckersBoard, numrowsboard, player, xIndiciesJump, yIndiciesJump) == 0 && CountMove1Squares(myCMCheckersBoard, numrowsboard, player, xIndiciesJump, yIndiciesJump) == 0)
  185. {
  186. cout << "Red is unable to move" << endl << "GAME OVER, White has won" << endl << "Enter any character to close the game";
  187. cin >> word;
  188. exit(1);
  189. }
  190. else
  191. {
  192. cout << "Red takes a turn" << endl;
  193. }
  194. }
  195. }
  196.  
  197. return(0);
  198. }
  199.  
  200.  
  201. //FUNCTION DEFINITIONS
  202. void InitializeBoard(int CMCheckersBoard[MAX_ARRAY_SIZE][MAX_ARRAY_SIZE], int numRowsInBoard)
  203. {
  204. //fill board with starting pieces with rows (y) and columns (x) with respect to numRowsInBoard
  205. //begin with two for loops declaring (y) and (x) variables of the 2D array
  206. int x = 0;
  207. int y = 0;
  208. for (y = 0; y < numRowsInBoard; y++)
  209. {
  210. for (x = 0; x < numRowsInBoard; x++)
  211. {
  212. //if the row (y) is the first row in the gameboard (top of board), initialize to {0, 2, 0, 2...}
  213. if (y == 0)
  214. {
  215. if (x == 0)
  216. {
  217. CMCheckersBoard[y][x] = NOPLAYER;
  218. }
  219. else if (x % 2 == 0)
  220. {
  221. CMCheckersBoard[y][x] = NOPLAYER;
  222. }
  223. else if (x % 2 != 0)
  224. {
  225. CMCheckersBoard[y][x] = WHITEMULE;
  226. }
  227. }
  228. //if the row (y) is not the first row but is a row less than the two middle rows in the board, initialize
  229. else if (y < (numRowsInBoard / 2) - 1)
  230. {
  231. //if even, initialize to {0, 1, 0, 1...}
  232. if (y % 2 == 0)
  233. {
  234. if (x % 2 == 0)
  235. {
  236. CMCheckersBoard[y][x] = NOPLAYER;
  237. }
  238. else if (x % 2 != 0)
  239. {
  240. CMCheckersBoard[y][x] = WHITESOLDIER;
  241. }
  242. }
  243. //if the row (y) is an odd number, initialize to {1, 0, 1, 0...}
  244. else if (y % 2 != 0)
  245. {
  246. if (x % 2 == 0)
  247. {
  248. CMCheckersBoard[y][x] = WHITESOLDIER;
  249. }
  250. else if (x % 2 != 0)
  251. {
  252. CMCheckersBoard[y][x] = NOPLAYER;
  253. }
  254. }
  255. }
  256. //if the row (y) is the last row of the board, initialize to {5, 0, 5, 0...}
  257. else if (y == (numRowsInBoard - 1))
  258. {
  259. if (x == 0)
  260. {
  261. CMCheckersBoard[y][x] = REDMULE;
  262. }
  263. else if (x % 2 == 0)
  264. {
  265. CMCheckersBoard[y][x] = REDMULE;
  266. }
  267. else if (x % 2 != 0)
  268. {
  269. CMCheckersBoard[y][x] = NOPLAYER;
  270. }
  271. }
  272. //if the row (y) is not the last row but is a row greater than the two middle rows in the board, initialize
  273. else if (y > (numRowsInBoard / 2))
  274. {
  275. //if the row (y) is an even number, initialize to {0, 4, 0, 4...}
  276. if (y % 2 == 0)
  277. {
  278. if (x % 2 == 0)
  279. {
  280. CMCheckersBoard[y][x] = NOPLAYER;
  281. }
  282. else if (x % 2 != 0)
  283. {
  284. CMCheckersBoard[y][x] = REDSOLDIER;
  285. }
  286. }
  287. //if odd, initialize to {4, 0, 4, 0...}
  288. else if (y % 2 != 0)
  289. {
  290. if (x % 2 == 0)
  291. {
  292. CMCheckersBoard[y][x] = REDSOLDIER;
  293. }
  294. else if (x % 2 != 0)
  295. {
  296. CMCheckersBoard[y][x] = NOPLAYER;
  297. }
  298. }
  299. }
  300. //the middle (y) rows are initialized to {0, 0, 0, 0...}
  301. else if (y == (numRowsInBoard / 2) || y == (numRowsInBoard / 2) - 1)
  302. {
  303. CMCheckersBoard[y][x] = NOPLAYER;
  304. }
  305. }
  306. }
  307. }
  308.  
  309. void DisplayBoard(int CMCheckersBoard[MAX_ARRAY_SIZE][MAX_ARRAY_SIZE], int numRowsInBoard)
  310. {
  311. //print out the board while giving player pieces and empty spaces proper names
  312. int count = 0;
  313. for (int y = 0; y < numRowsInBoard; y++)
  314. {
  315. cout << endl;
  316. for (int x = 0; x < numRowsInBoard; x++)
  317. {
  318. if (CMCheckersBoard[y][x] == WHITESOLDIER)
  319. {
  320. cout << setw(4) << "WS";
  321. }
  322. else if (CMCheckersBoard[y][x] == WHITEMULE)
  323. {
  324. cout << setw(4) << "WM";
  325. }
  326. else if (CMCheckersBoard[y][x] == WHITEKING)
  327. {
  328. cout << setw(4) << "WK";
  329. }
  330. else if (CMCheckersBoard[y][x] == REDSOLDIER)
  331. {
  332. cout << setw(4) << "RS";
  333. }
  334. else if (CMCheckersBoard[y][x] == REDMULE)
  335. {
  336. cout << setw(4) << "RM";
  337. }
  338. else if (CMCheckersBoard[y][x] == REDKING)
  339. {
  340. cout << setw(4) << "RK";
  341. }
  342. else
  343. {
  344. cout << setw(4) << count;
  345. }
  346. count += 1;
  347. }
  348. }
  349. cout << endl << endl << endl << endl;
  350. }
  351.  
  352. int CountJumps(int CMCheckersBoard[MAX_ARRAY_SIZE][MAX_ARRAY_SIZE], int numRowsInBoard, int player, int xLocArray[], int yLocArray[])
  353. {
  354. //this function counts number of checkers which can "jump" at specific turn.
  355. bool TrueOrFalse = false;
  356. int count = 0;
  357. for (int i = 0; i<numRowsInBoard; i++)
  358. {
  359. xLocArray[i] = -1;
  360. yLocArray[i] = -1;
  361. }
  362. for (int x = 0; x<numRowsInBoard; x++)
  363. {
  364. for (int y = 0; y<numRowsInBoard; y++)
  365. {
  366. TrueOrFalse = IsJump(CMCheckersBoard, numRowsInBoard, player, y, x);
  367. if (TrueOrFalse == true)
  368. count++;
  369. }
  370. }
  371. return count;
  372. }
  373.  
  374. bool IsJump(int CMCheckersBoard[MAX_ARRAY_SIZE][MAX_ARRAY_SIZE], int numRowsInBoard, int player, int xLoc, int yLoc)
  375. {
  376. //this function decides whether a specific checker can make a jump
  377. bool moveFL = false; //forward;left
  378. bool moveFR = false; //forward;right
  379. bool moveBL = false; //backward;left
  380. bool moveBR = false; //backward;right
  381. bool TrueOrFalse = false;
  382. if (player == 1) //white's turn
  383. {
  384. if (CMCheckersBoard[yLoc][xLoc] == 4 || CMCheckersBoard[yLoc][xLoc] == 5 || CMCheckersBoard[yLoc][xLoc] == 6 || CMCheckersBoard[yLoc][xLoc] == 0) //considering red checker
  385. {
  386. TrueOrFalse = false;
  387. }
  388. else //considering white checker
  389. {
  390. if (xLoc<numRowsInBoard - 2)// forward; non-cylindrical; left
  391. {
  392. if ((CMCheckersBoard[yLoc + 1][xLoc + 1] == 4 || CMCheckersBoard[yLoc + 1][xLoc + 1] == 5 || CMCheckersBoard[yLoc + 1][xLoc + 1] == 6) && (CMCheckersBoard[yLoc + 2][xLoc + 2] == 0))
  393. moveFL = true;
  394. else
  395. moveFL = false;
  396. }
  397. else if (xLoc == numRowsInBoard - 2)//forward; cylindrical; left
  398. {
  399. if ((CMCheckersBoard[yLoc + 1][xLoc + 1] == 4 || CMCheckersBoard[yLoc + 1][xLoc + 1] == 5 || CMCheckersBoard[yLoc + 1][xLoc + 1] == 6) && (CMCheckersBoard[yLoc + 2][0] == 0))
  400. moveFL = true;
  401. else
  402. moveFL = false;
  403. }
  404. else//forward; cylindrical; left
  405. {
  406. if ((CMCheckersBoard[yLoc + 1][0] == 4 || CMCheckersBoard[yLoc + 1][0] == 5 || CMCheckersBoard[yLoc + 1][0] == 6) && (CMCheckersBoard[yLoc + 2][1] == 0))
  407. moveFL = true;
  408. else
  409. moveFL = false;
  410. }
  411. if (xLoc>1) //forward; non-cylindrical; right
  412. {
  413. if ((CMCheckersBoard[yLoc + 1][xLoc - 1] == 4 || CMCheckersBoard[yLoc + 1][xLoc - 1] == 5 || CMCheckersBoard[yLoc + 1][xLoc - 1] == 6) && (CMCheckersBoard[yLoc + 2][xLoc - 2] == 0))
  414. moveFR = true;
  415. else
  416. moveFR = false;
  417. }
  418. else if (xLoc == 1) //forward; cylindrical; right
  419. {
  420. if ((CMCheckersBoard[yLoc + 1][xLoc - 1] == 4 || CMCheckersBoard[yLoc + 1][xLoc - 1] == 5 || CMCheckersBoard[yLoc + 1][xLoc - 1] == 6) && (CMCheckersBoard[yLoc + 2][numRowsInBoard - 1] == 0))
  421. moveFR = true;
  422. else
  423. moveFR = false;
  424. }
  425. else //forward; cylindrical; right
  426. {
  427. if ((CMCheckersBoard[yLoc + 1][numRowsInBoard - 1] == 4 || CMCheckersBoard[yLoc + 1][numRowsInBoard - 1] == 5 || CMCheckersBoard[yLoc + 1][numRowsInBoard - 1] == 6) && (CMCheckersBoard[yLoc + 2][numRowsInBoard - 2] == 0))
  428. moveFR = true;
  429. else
  430. moveFR = false;
  431. }
  432. if (moveFR == true || moveFL == true)
  433. TrueOrFalse = true;
  434. if (CMCheckersBoard[yLoc][xLoc] == 3)//when the checker is king
  435. {
  436. if (xLoc<numRowsInBoard - 2)// backward; non-cylindrical; left
  437. {
  438. if ((CMCheckersBoard[yLoc - 1][xLoc + 1] == 4 || CMCheckersBoard[yLoc - 1][xLoc + 1] == 5 || CMCheckersBoard[yLoc - 1][xLoc + 1] == 6) && (CMCheckersBoard[yLoc - 2][xLoc + 2] == 0))
  439. moveBL = true;
  440. else
  441. moveBL = false;
  442. }
  443. else if (xLoc == numRowsInBoard - 2)//backward; cylindrical; left
  444. {
  445. if ((CMCheckersBoard[yLoc - 1][xLoc + 1] == 4 || CMCheckersBoard[yLoc - 1][xLoc + 1] == 5 || CMCheckersBoard[yLoc - 1][xLoc + 1] == 6) && (CMCheckersBoard[yLoc - 2][0] == 0))
  446. moveBL = true;
  447. else
  448. moveBL = false;
  449. }
  450. else//backward; cylindrical; left
  451. {
  452. if ((CMCheckersBoard[yLoc - 1][0] == 4 || CMCheckersBoard[yLoc - 1][0] == 5 || CMCheckersBoard[yLoc - 1][0] == 6) && (CMCheckersBoard[yLoc - 2][1] == 0))
  453. moveBL = true;
  454. else
  455. moveBL = false;
  456. }
  457. if (xLoc>1) //backward; non-cylindrical; right
  458. {
  459. if ((CMCheckersBoard[yLoc - 1][xLoc - 1] == 4 || CMCheckersBoard[yLoc - 1][xLoc - 1] == 5 || CMCheckersBoard[yLoc - 1][xLoc - 1] == 6) && (CMCheckersBoard[yLoc - 2][xLoc - 2] == 0))
  460. moveBR = true;
  461. else
  462. moveBR = false;
  463. }
  464. else if (xLoc == 1) //backward; cylindrical; right
  465. {
  466. if ((CMCheckersBoard[yLoc - 1][xLoc - 1] == 4 || CMCheckersBoard[yLoc - 1][xLoc - 1] == 5 || CMCheckersBoard[yLoc - 1][xLoc - 1] == 6) && (CMCheckersBoard[yLoc - 2][numRowsInBoard - 1] == 0))
  467. moveBR = true;
  468. else
  469. moveBR = false;
  470. }
  471. else //backward; cylindrical; right
  472. {
  473. if ((CMCheckersBoard[yLoc - 1][numRowsInBoard - 1] == 4 || CMCheckersBoard[yLoc - 1][numRowsInBoard - 1] == 5 || CMCheckersBoard[yLoc - 1][numRowsInBoard - 1] == 6) && (CMCheckersBoard[yLoc - 2][numRowsInBoard - 2] == 0))
  474. moveBR = true;
  475. else
  476. moveBR = false;
  477. }
  478. if (moveBL == true || moveBR == true)
  479. TrueOrFalse = true;
  480. else
  481. TrueOrFalse = false;
  482. }
  483. }
  484. }
  485. else //red's turn
  486. {
  487. if (CMCheckersBoard[yLoc][xLoc] == 1 || CMCheckersBoard[yLoc][xLoc] == 2 || CMCheckersBoard[yLoc][xLoc] == 3 || CMCheckersBoard[yLoc][xLoc] == 0) //considering white checker
  488. {
  489. TrueOrFalse = false;
  490. }
  491. else //considering red checker
  492. {
  493. if (xLoc<numRowsInBoard - 2)// forward; non-cylindrical; right
  494. {
  495. if ((CMCheckersBoard[yLoc - 1][xLoc + 1] == 1 || CMCheckersBoard[yLoc - 1][xLoc + 1] == 2 || CMCheckersBoard[yLoc - 1][xLoc + 1] == 3) && (CMCheckersBoard[yLoc - 2][xLoc + 2] == 0))
  496. moveFR = true;
  497. else
  498. moveFR = false;
  499. }
  500. else if (xLoc == numRowsInBoard - 2)//forward; cylindrical; right
  501. {
  502. if ((CMCheckersBoard[yLoc - 1][xLoc + 1] == 1 || CMCheckersBoard[yLoc - 1][xLoc + 1] == 2 || CMCheckersBoard[yLoc - 1][xLoc + 1] == 3) && (CMCheckersBoard[yLoc - 2][0] == 0))
  503. moveFR = true;
  504. else
  505. moveFR = false;
  506. }
  507. else//forward; cylindrical; right
  508. {
  509. if ((CMCheckersBoard[yLoc - 1][0] == 1 || CMCheckersBoard[yLoc - 1][0] == 2 || CMCheckersBoard[yLoc - 1][0] == 3) && (CMCheckersBoard[yLoc - 2][1] == 0))
  510. moveFR = true;
  511. else
  512. moveFR = false;
  513. }
  514. if (xLoc>1) //forward; non-cylindrical; left
  515. {
  516. if ((CMCheckersBoard[yLoc - 1][xLoc - 1] == 1 || CMCheckersBoard[yLoc - 1][xLoc - 1] == 2 || CMCheckersBoard[yLoc - 1][xLoc - 1] == 3) && (CMCheckersBoard[yLoc - 2][xLoc - 2] == 0))
  517. moveFL = true;
  518. else
  519. moveFL = false;
  520. }
  521. else if (xLoc == 1) //forward; cylindrical; left
  522. {
  523. if ((CMCheckersBoard[yLoc - 1][xLoc - 1] == 1 || CMCheckersBoard[yLoc - 1][xLoc - 1] == 2 || CMCheckersBoard[yLoc - 1][xLoc - 1] == 3) && (CMCheckersBoard[yLoc - 2][numRowsInBoard - 1] == 0))
  524. moveFL = true;
  525. else
  526. moveFL = false;
  527. }
  528. else //forward; cylindrical; left
  529. {
  530. if ((CMCheckersBoard[yLoc - 1][numRowsInBoard - 1] == 1 || CMCheckersBoard[yLoc - 1][numRowsInBoard - 1] == 2 || CMCheckersBoard[yLoc - 1][numRowsInBoard - 1] == 3) && (CMCheckersBoard[yLoc - 2][numRowsInBoard - 2] == 0))
  531. moveFL = true;
  532. else
  533. moveFL = false;
  534. }
  535. if (moveFR == true || moveFL == true)
  536. TrueOrFalse = true;
  537. if (CMCheckersBoard[yLoc][xLoc] == 6) //if the checker is king
  538. {
  539. if (xLoc<numRowsInBoard - 2)// backward; non-cylindrical; right
  540. {
  541. if ((CMCheckersBoard[yLoc + 1][xLoc + 1] == 1 || CMCheckersBoard[yLoc + 1][xLoc + 1] == 2 || CMCheckersBoard[yLoc + 1][xLoc + 1] == 3) && (CMCheckersBoard[yLoc + 2][xLoc + 2] == 0))
  542. moveBR = true;
  543. else
  544. moveBR = false;
  545. }
  546. else if (xLoc == numRowsInBoard - 2)//backward; cylindrical; right
  547. {
  548. if ((CMCheckersBoard[yLoc + 1][xLoc + 1] == 1 || CMCheckersBoard[yLoc + 1][xLoc + 1] == 2 || CMCheckersBoard[yLoc + 1][xLoc + 1] == 3) && (CMCheckersBoard[yLoc + 2][0] == 0))
  549. moveBR = true;
  550. else
  551. moveBR = false;
  552. }
  553. else//backward; cylindrical; right
  554. {
  555. if ((CMCheckersBoard[yLoc + 1][0] == 1 || CMCheckersBoard[yLoc + 1][0] == 2 || CMCheckersBoard[yLoc + 1][0] == 3) && (CMCheckersBoard[yLoc + 2][1] == 0))
  556. moveBR = true;
  557. else
  558. moveBR = false;
  559. }
  560. if (xLoc>1) //backward; non-cylindrical; left
  561. {
  562. if ((CMCheckersBoard[yLoc + 1][xLoc - 1] == 1 || CMCheckersBoard[yLoc + 1][xLoc - 1] == 2 || CMCheckersBoard[yLoc + 1][xLoc - 1] == 3) && (CMCheckersBoard[yLoc + 2][xLoc - 2] == 0))
  563. moveBL = true;
  564. else
  565. moveBL = false;
  566. }
  567. else if (xLoc == 1) //backward; cylindrical; left
  568. {
  569. if ((CMCheckersBoard[yLoc + 1][xLoc - 1] == 1 || CMCheckersBoard[yLoc + 1][xLoc - 1] == 2 || CMCheckersBoard[yLoc + 1][xLoc - 1] == 3) && (CMCheckersBoard[yLoc + 2][numRowsInBoard - 1] == 0))
  570. moveBL = true;
  571. else
  572. moveBL = false;
  573. }
  574. else //forward; cylindrical; right
  575. {
  576. if ((CMCheckersBoard[yLoc + 1][numRowsInBoard - 1] == 1 || CMCheckersBoard[yLoc + 1][numRowsInBoard - 1] == 2 || CMCheckersBoard[yLoc + 1][numRowsInBoard - 1] == 3) && (CMCheckersBoard[yLoc + 2][numRowsInBoard - 2] == 0))
  577. moveBL = true;
  578. else
  579. moveBL = false;
  580. }
  581. if (moveBR == true || moveBL == true)
  582. TrueOrFalse = true;
  583. else
  584. TrueOrFalse = false;
  585. }
  586.  
  587. }
  588.  
  589. }
  590. return TrueOrFalse;
  591. }
  592.  
  593. int CountMove1Squares(int CMCheckersBoard[MAX_ARRAY_SIZE][MAX_ARRAY_SIZE], int numRowsInBoard, int player, int xLocArray[], int yLocArray[])
  594. {
  595. //This function counts the number of possible pieces that can make a move
  596. int movableCheckers = 0;
  597. for (int i = 0; i<numRowsInBoard; i++) //initialize arrays to -1
  598. {
  599. xLocArray[i] = -1;
  600. yLocArray[i] = -1;
  601. }
  602. if (player == 1) //case for white's turn
  603. {
  604. for (int y = 0; y<numRowsInBoard; y++)
  605. {
  606. for (int x = 0; x<numRowsInBoard; x++)
  607. {
  608. if (CMCheckersBoard[y][x] == WHITESOLDIER || CMCheckersBoard[y][x] == WHITEMULE || CMCheckersBoard[y][x] == WHITEKING) //if a white piece is identified
  609. {
  610. if (IsMove1Square(CMCheckersBoard, numRowsInBoard, player, x, y) == true) //if a (non jump) move is possible increase count
  611. {
  612. movableCheckers++;
  613. }
  614. }
  615. }
  616.  
  617. }
  618. }
  619. else if (player == 2) //case for red's turn
  620. {
  621. for (int y = 0; y<numRowsInBoard; y++)
  622. {
  623. for (int x = 0; x<numRowsInBoard; x++)
  624. {
  625. if (CMCheckersBoard[x][y] == REDSOLDIER || CMCheckersBoard[x][y] == REDMULE || CMCheckersBoard[x][y] == REDKING) //if a red piece is identified
  626. {
  627. if (IsMove1Square(CMCheckersBoard, numRowsInBoard, player, x, y) == true) //if a (non jump) move is possible increase count
  628. {
  629. movableCheckers++;
  630. }
  631. }
  632. }
  633.  
  634. }
  635. }
  636. return movableCheckers;
  637. }
  638.  
  639. bool IsMove1Square(int CMCheckersBoard[MAX_ARRAY_SIZE][MAX_ARRAY_SIZE], int numRowsInBoard, int player, int xLoc, int yLoc)
  640. {
  641. // This function considers one checker and decides if there is a possible (non jump) move to be made
  642. bool makeAMove = false;
  643. if (player == WHITEPLAYER)
  644. {
  645. if (CMCheckersBoard[yLoc][xLoc] == WHITESOLDIER || CMCheckersBoard[yLoc][xLoc] == WHITEMULE || CMCheckersBoard[yLoc][xLoc] == WHITEKING)
  646. {
  647. if (xLoc == 0) //if the piece is on the left edge of the board
  648. {
  649. if (CMCheckersBoard[yLoc + 1][xLoc + 1] == NOPLAYER || CMCheckersBoard[yLoc + 1][numRowsInBoard - 1] == NOPLAYER)
  650. {
  651. makeAMove = true;
  652. }
  653. }
  654. else if (xLoc == (numRowsInBoard - 1)) //if the piece is on the right edge of board
  655. {
  656. if (CMCheckersBoard[yLoc + 1][xLoc - 1] == NOPLAYER || CMCheckersBoard[yLoc + 1][0] == NOPLAYER)
  657. {
  658. makeAMove = true;
  659. }
  660. }
  661. else
  662. {
  663. if (CMCheckersBoard[yLoc + 1][xLoc + 1] == NOPLAYER || CMCheckersBoard[yLoc + 1][xLoc - 1] == NOPLAYER) //non edge cases
  664. {
  665. makeAMove = true;
  666. }
  667. }
  668. }
  669. if (CMCheckersBoard[yLoc][xLoc] == WHITEKING) //only including cases of moving backwards, we already know from previous if it can move forward
  670. {
  671. if (yLoc != 0) //if the king is not at the top of the board (couldn't move up anymore)
  672. {
  673. if (xLoc == 0) //king on left edge
  674. {
  675. if (CMCheckersBoard[yLoc - 1][numRowsInBoard - 1] == NOPLAYER || CMCheckersBoard[yLoc - 1][xLoc + 1] == NOPLAYER)
  676. {
  677. makeAMove = true;
  678. }
  679. }
  680. else if (xLoc == numRowsInBoard - 1) //king on right edge
  681. {
  682. if (CMCheckersBoard[yLoc - 1][0] == NOPLAYER || CMCheckersBoard[yLoc - 1][xLoc - 1] == NOPLAYER)
  683. {
  684. makeAMove = true;
  685. }
  686. }
  687. else //non edge case
  688. {
  689. if (CMCheckersBoard[yLoc - 1][xLoc + 1] == NOPLAYER || CMCheckersBoard[yLoc - 1][xLoc - 1] == NOPLAYER)
  690. {
  691. makeAMove = true;
  692. }
  693. }
  694. }
  695. }
  696. }
  697. else if (player == REDPLAYER)
  698. {
  699. if (CMCheckersBoard[yLoc][xLoc] == REDSOLDIER || CMCheckersBoard[yLoc][xLoc] == REDMULE || CMCheckersBoard[yLoc][xLoc] == REDKING)
  700. {
  701. if (xLoc == 0) //if the piece is on the left edge of the board
  702. {
  703. if (CMCheckersBoard[yLoc - 1][xLoc + 1] == NOPLAYER || CMCheckersBoard[yLoc - 1][numRowsInBoard - 1] == NOPLAYER)
  704. {
  705. makeAMove = true;
  706. }
  707. }
  708. else if (xLoc == (numRowsInBoard - 1)) //if the piece is on the right edge of board
  709. {
  710. if (CMCheckersBoard[yLoc - 1][xLoc - 1] == NOPLAYER || CMCheckersBoard[yLoc - 1][0] == NOPLAYER)
  711. {
  712. makeAMove = true;
  713. }
  714. }
  715. else //non edge cases
  716. {
  717. if (CMCheckersBoard[yLoc - 1][xLoc + 1] == NOPLAYER || CMCheckersBoard[yLoc - 1][xLoc - 1] == NOPLAYER)
  718. {
  719. makeAMove = true;
  720. }
  721. }
  722. }
  723. if (CMCheckersBoard[yLoc][xLoc] == WHITEKING) //only including cases of moving backwards, we already know from previous if it can move forward
  724. {
  725. if (yLoc != numRowsInBoard - 1) //if the king is not at the bottom of the board (couldn't move down anymore)
  726. {
  727. if (xLoc == 0) //king on left edge
  728. {
  729. if (CMCheckersBoard[yLoc + 1][numRowsInBoard - 1] == NOPLAYER || CMCheckersBoard[yLoc + 1][xLoc + 1] == NOPLAYER)
  730. {
  731. makeAMove = true;
  732. }
  733. }
  734. else if (xLoc == numRowsInBoard - 1) //king on right edge
  735. {
  736. if (CMCheckersBoard[yLoc + 1][0] == NOPLAYER || CMCheckersBoard[yLoc + 1][xLoc - 1] == NOPLAYER)
  737. {
  738. makeAMove = true;
  739. }
  740. }
  741. else //non edge cases
  742. {
  743. if (CMCheckersBoard[yLoc + 1][xLoc + 1] == NOPLAYER || CMCheckersBoard[yLoc + 1][xLoc - 1] == NOPLAYER)
  744. {
  745. makeAMove = true;
  746. }
  747. }
  748. }
  749. }
  750. }
  751. return makeAMove;
  752. }
  753.  
  754. bool CheckWin(int CMCheckersBoard[MAX_ARRAY_SIZE][MAX_ARRAY_SIZE], int numRowsInBoard)
  755. {
  756. //this function is designed to check if one of the players has won/lost by loosing all their mules, or only having mules left
  757. //establish bool return value, establish counters for all players. rm = red
  758. bool win = false;
  759. int rm = 0;
  760. int rs = 0;
  761. int rk = 0;
  762. int wm = 0;
  763. int ws = 0;
  764. int wk = 0;
  765. for (int x = 0; x<numRowsInBoard; x++)
  766. {
  767. for (int y = 0; y<numRowsInBoard; y++)
  768. {
  769. if (CMCheckersBoard[y][x] == REDMULE)
  770. {
  771. rm += 1;
  772. }
  773. else if (CMCheckersBoard[y][x] == REDSOLDIER)
  774. {
  775. rs += 1;
  776. }
  777. else if (CMCheckersBoard[y][x] == REDKING)
  778. {
  779. rk += 1;
  780. }
  781. else if (CMCheckersBoard[y][x] == WHITEMULE)
  782. {
  783. wm = +1;
  784. }
  785. else if (CMCheckersBoard[y][x] == WHITESOLDIER)
  786. {
  787. ws += 1;
  788. }
  789. else if (CMCheckersBoard[y][x] == WHITEKING)
  790. {
  791. wk += 1;
  792. }
  793. }
  794. }
  795.  
  796. if (rm == 0)
  797. {
  798. cout << "The Red Player has won the game by losing all of the Red Mules";
  799. win = true;
  800. }
  801. if (wm == 0)
  802. {
  803. cout << "The White Player has won the game by losing all of the White Mules";
  804. win = true;
  805. }
  806. if (rs == 0 && rk == 0)
  807. {
  808. cout << "The White Player has won by capturing all of the red players soldiers and kings";
  809. win = true;
  810. }
  811. if (ws == 0 && wk == 0)
  812. {
  813. cout << "The Red Player has won by capturing all of the white players soldiers and kings";
  814. win = true;
  815. }
  816.  
  817. return win;
  818. }
  819.  
  820. bool MakeMove(int CMCheckersBoard[MAX_ARRAY_SIZE][MAX_ARRAY_SIZE], int numRowsInBoard, int player, int fromSquareNum, int toSquareNum, bool &jumped)
  821. {
  822. bool move = true;
  823. //this function is designed to move or jump a players piece
  824. //begin by making x and y coordinates for the spaces the piece will move t
  825. int x1 = 0;
  826. int y1 = 0;
  827. int x2 = 0;
  828. int y2 = 0;
  829. x1 = (fromSquareNum - 1) % numRowsInBoard;
  830. y1 = (fromSquareNum - 1) / numRowsInBoard;
  831. x2 = (toSquareNum - 1) % numRowsInBoard;
  832. y2 = (toSquareNum - 1) / numRowsInBoard;
  833. char word[1024] = { '0' };
  834.  
  835. if (CMCheckersBoard[y2][x2] != 0)
  836. {
  837. cerr << "Error; illegal move";
  838. return false;
  839. }
  840. if (player == WHITEPLAYER)
  841. {
  842. if (y2 - y1 == 1) //if the player is making a move one space down the board
  843. {
  844. if (x1 == 0) //if the piece is on the left edge
  845. {
  846. if (!(toSquareNum == fromSquareNum + numRowsInBoard + 1 || toSquareNum == fromSquareNum + (2 * numRowsInBoard) - 1)) //if not legal
  847. {
  848. cerr << "Error; illegal move";
  849. return false;
  850. }
  851. }
  852. else if (x1 == (numRowsInBoard - 1)) //if the piece is on the right edge
  853. {
  854. if (!(toSquareNum == fromSquareNum + numRowsInBoard - 1 || toSquareNum == fromSquareNum + 1)) //if not legal
  855. {
  856. cerr << "Error; illegal move";
  857. return false;
  858. }
  859. }
  860. else //if the piece is in the middle area of board
  861. {
  862. if (!(toSquareNum == fromSquareNum + numRowsInBoard + 1 || toSquareNum == fromSquareNum + numRowsInBoard + 1)) //if legal move
  863. {
  864. cerr << "Error; illegal move";
  865. return false;
  866. }
  867. }
  868. CMCheckersBoard[y2][x2] = CMCheckersBoard[y1][x1];
  869. CMCheckersBoard[y1][x1] = 0;
  870. }
  871. else if (y2 - y1 == 2) //if the player is making a jump
  872. {
  873. if (toSquareNum == fromSquareNum + (2 * numRowsInBoard) + 2 || toSquareNum == fromSquareNum + (3 * numRowsInBoard) - 2)// if a legal move from most left or 1 more than most left of the board
  874. {
  875. if (x1 == 0) //if the piece is on the most left
  876. {
  877. //if there is no opponent to be jumped over
  878. if (!(CMCheckersBoard[y1 + 1][x1 + 1] == REDKING || CMCheckersBoard[y1 + 1][x1 + 1] == REDSOLDIER || CMCheckersBoard[y1 + 1][x1 + 1] == REDMULE || CMCheckersBoard[y1 + 1][x1 + (2 * numRowsInBoard) - 1] == REDKING || CMCheckersBoard[y1 + 1][x1 + (2 * numRowsInBoard) - 1] == REDSOLDIER || CMCheckersBoard[y1 + 1][x1 + (2 * numRowsInBoard) - 1] == REDMULE))
  879. {
  880. cerr << "Error; illegal move";
  881. return false;
  882. }
  883. }
  884. }
  885. if (toSquareNum == fromSquareNum + (2 * numRowsInBoard) - 2 || toSquareNum == fromSquareNum + numRowsInBoard + 2) //if a legal move from the most right or 1 less than the most right of the board
  886. {
  887. if (x1 == (numRowsInBoard - 1)) //piece is on the most right column
  888. {
  889. //if there is no opponent to be jumped
  890. if (!(CMCheckersBoard[y1 + 1][x1 - 1] == REDKING || CMCheckersBoard[y1 + 1][x1 - 1] == REDSOLDIER || CMCheckersBoard[y1 + 1][x1 - 1] == REDMULE || CMCheckersBoard[y1 + 1][0] == REDKING || CMCheckersBoard[y1 + 1][0] == REDSOLDIER || CMCheckersBoard[y1 + 1][0] == REDMULE))
  891. {
  892. cerr << "Error; illegal move";
  893. return false;
  894. }
  895. }
  896. }
  897. if (toSquareNum == fromSquareNum + (2 * numRowsInBoard) - 2 || toSquareNum == fromSquareNum + (2 * numRowsInBoard) + 2) //if a legal move from the middle of the board
  898. {
  899. //if there is no opponent to be jumped
  900. if (!(CMCheckersBoard[y1 + 1][x1 + 1] == REDKING || CMCheckersBoard[y1 + 1][x1 + 1] == REDSOLDIER || CMCheckersBoard[y1 + 1][x1 + 1] == REDMULE || CMCheckersBoard[y1 + 1][x1 - 1] == REDKING || CMCheckersBoard[y1 + 1][x1 - 1] == REDSOLDIER || CMCheckersBoard[y1 + 1][x1 - 1] == REDMULE))
  901. {
  902. cerr << "Error; illegal move";
  903. return false;
  904. }
  905. }
  906. else //if jump is not legal
  907. {
  908. cerr << "Error; illegal move";
  909. return false;
  910. }
  911. }
  912. CMCheckersBoard[y2][x2] = CMCheckersBoard[y1][x1];
  913. CMCheckersBoard[y1][x1] = 0;
  914. CMCheckersBoard[y1 + 1][x2 - x1] = 0;
  915. }
  916. if (player == REDPLAYER)
  917. {
  918. if (y2 - y1 == -1) //if the player is making a move one space up the board
  919. {
  920. if (x1 == 0) //if the piece is on the left edge
  921. {
  922. if (!(toSquareNum == fromSquareNum - numRowsInBoard + 1 || toSquareNum == fromSquareNum - 1)) //if not legal
  923. {
  924. cerr << "Error; illegal move";
  925. return false;
  926. }
  927. }
  928. else if (x1 == (numRowsInBoard - 1)) //if the piece is on the right edge
  929. {
  930. if (!(toSquareNum == fromSquareNum - numRowsInBoard - 1 || toSquareNum == fromSquareNum - (2 * numRowsInBoard) + 1)) //if not legal
  931. {
  932. cerr << "Error; illegal move";
  933. return false;
  934. }
  935. }
  936. else //if the piece is in the middle area of board
  937. {
  938. if (!(toSquareNum == fromSquareNum - numRowsInBoard + 1 || toSquareNum == fromSquareNum - numRowsInBoard + 1)) //if legal move
  939. {
  940. cerr << "Error; illegal move";
  941. return false;
  942. }
  943. }
  944. CMCheckersBoard[y2][x2] = CMCheckersBoard[y1][x1];
  945. CMCheckersBoard[y1][x1] = 0;
  946. }
  947. else if (y2 - y1 == -2) //if the player is making a jump
  948. {
  949. if (toSquareNum == fromSquareNum - (2 * numRowsInBoard) + 2 || toSquareNum == fromSquareNum - numRowsInBoard - 2)// if a legal move from most left or 1 more than most left of the board
  950. {
  951. if (x1 == 0) //if the piece is on the most left
  952. {
  953. //if there is no opponent to be jumped over
  954. if (!(CMCheckersBoard[y1 - 1][x1 + 1] == REDKING || CMCheckersBoard[y1 - 1][x1 + 1] == REDSOLDIER || CMCheckersBoard[y1 - 1][x1 + 1] == REDMULE || CMCheckersBoard[y1 - 1][numRowsInBoard - 1] == REDKING || CMCheckersBoard[y1 - 1][numRowsInBoard - 1] == REDSOLDIER || CMCheckersBoard[y1 - 1][numRowsInBoard - 1] == REDMULE))
  955. {
  956. cerr << "Error; illegal move";
  957. return false;
  958. }
  959. }
  960. }
  961. if (toSquareNum == fromSquareNum - (2 * numRowsInBoard) - 2 || toSquareNum == fromSquareNum - (3 * numRowsInBoard) + 2) //if a legal move from the most right or 1 less than the most right of the board
  962. {
  963. if (x1 == (numRowsInBoard - 1)) //piece is on the most right column
  964. {
  965. //if there is no opponent to be jumped
  966. if (!(CMCheckersBoard[y1 - 1][x1 - 1] == REDKING || CMCheckersBoard[y1 - 1][x1 - 1] == REDSOLDIER || CMCheckersBoard[y1 - 1][x1 - 1] == REDMULE || CMCheckersBoard[y1 - 1][0] == REDKING || CMCheckersBoard[y1 - 1][0] == REDSOLDIER || CMCheckersBoard[y1 - 1][0] == REDMULE))
  967. {
  968. cerr << "Error; illegal move";
  969. return false;
  970. }
  971. }
  972. }
  973. if (toSquareNum == fromSquareNum + (2 * numRowsInBoard) - 2 || toSquareNum == fromSquareNum + (2 * numRowsInBoard) + 2) //if a legal move from the middle of the board
  974. {
  975. //if there is no opponent to be jumped
  976. if (!(CMCheckersBoard[y1 - 1][x1 + 1] == REDKING || CMCheckersBoard[y1 - 1][x1 + 1] == REDSOLDIER || CMCheckersBoard[y1 - 1][x1 + 1] == REDMULE || CMCheckersBoard[y1 - 1][x1 - 1] == REDKING || CMCheckersBoard[y1 - 1][x1 - 1] == REDSOLDIER || CMCheckersBoard[y1 - 1][x1 - 1] == REDMULE))
  977. {
  978. cerr << "Error; illegal move";
  979. return false;
  980. }
  981. }
  982. else //if jump is not legal
  983. {
  984. cerr << "Error; illegal move";
  985. return false;
  986. }
  987. CMCheckersBoard[y2][x2] = CMCheckersBoard[y1][x1];
  988. CMCheckersBoard[y1][x1] = 0;
  989. CMCheckersBoard[y1 - 1][x2 - x1] = 0;
  990. }
  991. }
  992. if (CMCheckersBoard[y1][x1] == WHITEKING || CMCheckersBoard[y1][x1] == REDKING)
  993. {
  994. if (y2 - y1 == 1) //if the player is making a move one space up the board
  995. {
  996. if (x1 == 0) //if the piece is on the left edge
  997. {
  998. if (!(toSquareNum == fromSquareNum - numRowsInBoard + 1 || toSquareNum == fromSquareNum - 1)) //if not legal
  999. {
  1000. cerr << "Error; illegal move";
  1001. return false;
  1002. }
  1003. }
  1004. else if (x1 == (numRowsInBoard - 1)) //if the piece is on the right edge
  1005. {
  1006. if (!(toSquareNum == fromSquareNum - numRowsInBoard - 1 || toSquareNum == fromSquareNum - (2 * numRowsInBoard) + 1)) //if not legal
  1007. {
  1008. cerr << "Error; illegal move";
  1009. return false;
  1010. }
  1011. }
  1012. else //if the piece is in the middle area of board
  1013. {
  1014. if (!(toSquareNum == fromSquareNum - numRowsInBoard + 1 || toSquareNum == fromSquareNum - numRowsInBoard + 1)) //if legal move
  1015. {
  1016. cerr << "Error; illegal move";
  1017. return false;
  1018. }
  1019. }
  1020. CMCheckersBoard[y2][x2] = CMCheckersBoard[y1][x1];
  1021. CMCheckersBoard[y1][x1] = 0;
  1022. }
  1023. else if (y2 - y1 == 1) //if the player is making a move one space down the board
  1024. {
  1025. if (x1 == 0) //if the piece is on the left edge
  1026. {
  1027. if (!(toSquareNum == fromSquareNum + numRowsInBoard + 1 || toSquareNum == fromSquareNum + (2 * numRowsInBoard) - 1)) //if not legal
  1028. {
  1029. cerr << "Error; illegal move";
  1030. return false;
  1031. }
  1032. }
  1033. else if (x1 == (numRowsInBoard - 1)) //if the piece is on the right edge
  1034. {
  1035. if (!(toSquareNum == fromSquareNum + numRowsInBoard - 1 || toSquareNum == fromSquareNum + 1)) //if not legal
  1036. {
  1037. cerr << "Error; illegal move";
  1038. return false;
  1039. }
  1040. }
  1041. else //if the piece is in the middle area of board
  1042. {
  1043. if (!(toSquareNum == fromSquareNum + numRowsInBoard + 1 || toSquareNum == fromSquareNum + numRowsInBoard + 1)) //if legal move
  1044. {
  1045. cerr << "Error; illegal move";
  1046. return false;
  1047. }
  1048. }
  1049. CMCheckersBoard[y2][x2] = CMCheckersBoard[y1][x1];
  1050. CMCheckersBoard[y1][x1] = 0;
  1051. }
  1052. }
  1053.  
  1054. for (int x = 0; x< numRowsInBoard; x++) //make any changes to soldiers who make it all the way to the end of the board, end game if a mule does so
  1055. {
  1056. if (CMCheckersBoard[0][x] == REDSOLDIER)
  1057. {
  1058. CMCheckersBoard[0][x] = REDKING;
  1059. }
  1060. if (CMCheckersBoard[0][x] == REDMULE)
  1061. {
  1062. cout << "Red has created a Mule King, White wins the game" << endl;
  1063. cout << "Enter any character to terminate the game then press the enter key" << endl;
  1064. cin >> word;
  1065. exit(1);
  1066. }
  1067. if (CMCheckersBoard[numRowsInBoard - 1][x] == WHITESOLDIER)
  1068. {
  1069. CMCheckersBoard[numRowsInBoard - 1][x] = WHITEKING;
  1070. }
  1071. if (CMCheckersBoard[numRowsInBoard - 1][x] == WHITEMULE)
  1072. {
  1073. cout << "White has created a Mule King, Red wins the game" << endl;
  1074. cout << "Enter any character to terminate the game then press the enter key" << endl;
  1075. cin >> word;
  1076. exit(1);
  1077. }
  1078. }
  1079. return move;
  1080. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement