Guest User

Untitled

a guest
Apr 23rd, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.58 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <iomanip>
  4. using namespace std;
  5.  
  6. void displayBoard(int [][8]);
  7. void displayMenu();
  8. void showAvailablePieces();
  9. void checkMovesBlack(int [][8]);
  10. void checkMovesRed(int [][8]);
  11. int jumpPiece(int [][8], int, int, int, int, bool);
  12.  
  13. int main()
  14. {
  15. bool blackTurn;
  16. bool redTurn;
  17. int board [8][8]= { 1,0,1,0,1,0,1,0,
  18. 0,1,0,1,0,1,0,1,
  19. 1,0,1,0,1,0,1,0,
  20. 0,0,0,0,0,0,0,0,
  21. 0,0,0,0,0,0,0,0,
  22. 0,2,0,2,0,2,0,2,
  23. 2,0,2,0,2,0,2,0,
  24. 0,2,0,2,0,2,0,2};
  25. /*displayMenu();*/
  26. displayBoard(board);
  27. do{
  28. checkMovesBlack(board);
  29. blackTurn=0;
  30. redTurn=1;
  31. system("cls");
  32.  
  33. checkMovesRed(board);
  34. redTurn=0;
  35. blackTurn=1;
  36. system("cls");
  37. } while(blackTurn || redTurn);
  38.  
  39.  
  40.  
  41. system("pause");
  42. return 0;
  43. }
  44.  
  45. void displayBoard(int matrix[][8])
  46. {
  47. for(int i = 8; i > 0; i--)
  48. for(int j = 0; j < 8; j++)
  49. if(matrix[j][i] == 1) cout << 'o'
  50. else if(matrix[j][i] == 2) cout << 'x';
  51. else if(matrix[j][i] == 0) cout << '-';
  52.  
  53. }
  54.  
  55. //void displayMenu()
  56. //{
  57. // string name1;
  58. // string name2;
  59. //
  60. // cout << setw(45) << "CHECKERS!!" << endl;
  61. // cout << "Player 1: ";
  62. // cin >> name1;
  63. // cout << "Player 2: ";
  64. // cin >> name2;
  65. //
  66. // system("cls");
  67. //}
  68.  
  69. void checkMovesBlack(int matrix[][8])
  70. {
  71. bool availablePiece=0;
  72. bool availableMove=0;
  73. int startx=0;
  74. int starty=0;
  75. int nextx=0;
  76. int nexty=0;
  77.  
  78. cout << endl;
  79. cout << "Black's turn!" << endl;
  80. cout << "Please enter the coordinates of the piece you would like to move: ";
  81. cin >> startx;
  82. cin >> starty;
  83.  
  84. if (startx < 0 || startx >7 || starty < 0 || starty > 7)
  85. {
  86. cout << "Not valid coordinate. Please enter the coordinates of the piece you would like to move. " << endl;
  87. cin >> startx;
  88. cin >> starty;
  89. }
  90.  
  91. else if (matrix[startx][starty]==2 && matrix[startx-1][starty-1]==0 || matrix[startx+1][starty-1]==0){availablePiece=1;}
  92. else {availablePiece=0;}
  93.  
  94. if (availablePiece==0)
  95. {
  96. cout << "Not a valid coordinate. Please enter the coordinates of the piece you would like to move.";
  97. cin >> startx;
  98. cin >> starty;
  99. }
  100.  
  101. else {
  102. cout << "Please enter the coordinates of the desired position: ";
  103. cin >> nextx;
  104. cin >> nexty;
  105.  
  106. if (matrix[nextx][nexty]==1 && matrix[nextx-2][nexty-2]==0)
  107. {
  108. bool left=true;
  109. jumpPiece(matrix, startx, starty, nextx, nexty, left);
  110. }
  111. else if (matrix[nextx][nexty]==1 && matrix[nextx+2][nexty-2]==0) {
  112. bool left=false;
  113. jumpPiece(matrix, startx, starty, nextx, nexty, left);
  114. }
  115.  
  116. else if (matrix[nextx][nexty]==2 && matrix[nextx-1][nexty-1]==0 || matrix[nextx+1][nexty-1]==0)
  117. {
  118. availableMove=1;
  119. }
  120.  
  121. }
  122.  
  123. if (availableMove) {
  124. cout << "Valid move!";
  125. startx=nextx;
  126. starty=nexty;
  127. }
  128.  
  129. else {
  130. cout << "Not a valid move. " << endl;
  131. cin >> nextx;
  132. cin >> nexty;
  133. }
  134.  
  135. cout << endl;
  136. }
  137.  
  138. void checkMovesRed(int matrix[][8])
  139. {
  140. bool availablePiece=0;
  141. bool availableMove=0;
  142. int startx=0;
  143. int starty=0;
  144. int nextx=0;
  145. int nexty=0;
  146.  
  147. cout << "Red's turn!" << endl;
  148. cout << "Please enter the coordinates of the piece you would like to move: ";
  149. cin >> startx;
  150. cin >> starty;
  151.  
  152. if (startx < 0 || startx >7 || starty < 0 || starty > 7)
  153. {
  154. cout << "Not valid coordinate. Please enter the coordinates of the piece you would like to move. " << endl;
  155. cin >> startx;
  156. cin >> starty;
  157. }
  158.  
  159. else if (matrix[startx][starty]==2 && matrix[startx-1][starty-1]==0 || matrix[startx+1][starty-1]==0){availablePiece=1;}
  160. else {availablePiece=0;}
  161.  
  162. if (availablePiece==0)
  163. {
  164. cout << "Not a valid coordinate. Please enter the coordinates of the piece you would like to move.";
  165. cin >> startx;
  166. cin >> starty;
  167. }
  168.  
  169. else {
  170. cout << "Please enter the coordinates of the desired position: ";
  171. cin >> nextx;
  172. cin >> nexty;
  173.  
  174. if (matrix[nextx][nexty]==2 && matrix[nextx+1][nexty+1]==0 || matrix[nextx-1][nexty+1]==0)
  175. {
  176. availableMove=1;
  177. }
  178. }
  179.  
  180. if (availableMove) {
  181. cout << "Valid move!";
  182. startx=nextx;
  183. starty=nexty;
  184. }
  185.  
  186. else {
  187. cout << "Not a valid move. " << endl;
  188. cin >> nextx;
  189. cin >> nexty;
  190. }
  191. }
  192.  
  193. int jumpPiece(int matrix[][8], int x, int y, int tempx, int tempy, bool passLeft)
  194. {
  195. if(passLeft)
  196. {
  197. matrix[x][y]=matrix[x-2][y-2];
  198. }
  199.  
  200. if(!passLeft)
  201. {
  202. matrix[x][y]=matrix[x+2][y-2];
  203. }
  204.  
  205. matrix[tempx][tempy]=0;
  206.  
  207. return matrix[x][y];
  208.  
  209. }
Add Comment
Please, Sign In to add comment