Advertisement
Guest User

Untitled

a guest
Feb 19th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.86 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. void printBoard(char[][3]);
  5. void getMove(int, int(&)[2]);
  6. void doMove(int&, char(&)[3][3], int[2]);
  7. void checkBoard(int, char[][3], int&);
  8.  
  9. int main() {
  10. int moveXY[2] = {0, 0}; // Variable used for selecting moves
  11. char tic[3][3]; // Board
  12. int turn = 0; // Current turn
  13. int win = 0; // Has someone won; 0 = ...nah
  14. for (int i = 0; i < 3; i++) // Initialize board
  15. for (int j = 0; j < 3; j++)
  16. tic[i][j] = '*';
  17. printBoard(tic);
  18. while (win == 0) { // The game
  19. getMove(turn, moveXY);
  20. doMove(turn, tic, moveXY);
  21. printBoard(tic);
  22. checkBoard(turn, tic, win);
  23. if (win < 0)
  24. cout << "Player 1 wins!" << endl;
  25. else if (win > 1)
  26. cout << "Player 2 wins!" << endl;
  27. }
  28. return 0;
  29. }
  30.  
  31. void printBoard(char tic[][3]) {
  32. for (int i = 0; i < 3; i++)
  33. printf("%c | %c | %c\n", tic[i][0], tic[i][1], tic[i][2]);
  34. cout << endl;
  35. }
  36.  
  37. void getMove(int turn, int (&xy)[2]) {
  38. cout << "Player " << (turn%2) + 1 << " go!\nPick the x coordinate of your move: ";
  39. cin >> xy[0];
  40. cout << "\nNow input the y: ";
  41. cin >> xy[1];
  42. cout << endl;
  43. }
  44.  
  45. void doMove(int &turn, char (&tic)[3][3], int xy[2]) {
  46. if (tic[xy[1]][xy[0]] != '*') {
  47. cout << "That space is already taken!" << endl;
  48. return;
  49. }
  50. else if (turn%2 == 0)
  51. tic[xy[1]][xy[0]] = 'O';
  52. else if (turn%2 == 1)
  53. tic[xy[1]][xy[0]] = 'X';
  54. turn++;
  55. }
  56.  
  57. void checkBoard(int turn, char tic[][3], int &check) {
  58. if (turn < 5) // not enough turns to declare a winner
  59. return;
  60. int p1 = 0;
  61. int p2 = 0;
  62.  
  63. // program checks if any player has won by rows && colums && diagonals
  64.  
  65. // rows
  66. for (int i = 0; i < 3; i++) {
  67. for (int j = 0; j < 3; j++) {
  68. if (tic[i][j] == 'O')
  69. p1++; // score counted every check cycle
  70. else if (tic[i][j] == 'X')
  71. p2++;
  72. }
  73. if (p1 == 3) { // score of 3 means a player has won
  74. check = -1;
  75. return;
  76. }
  77. else if (p2 == 3) {
  78. check = 1;
  79. return;
  80. }
  81. p1 = 0; // score reset after every row/column is checked
  82. p2 = 0;
  83. }
  84.  
  85. // columns
  86. for (int j = 0; j < 3; j++) {
  87. for (int i = 0; i < 3; i++) {
  88. if (tic[i][j] == 'O')
  89. p1++;
  90. else if (tic[i][j] == 'X')
  91. p2++;
  92. }
  93. if (p1 > 2) {
  94. check = -1;
  95. cout << "Won by column" << endl;
  96. return;
  97. }
  98. else if (p2 > 2) {
  99. check = 1;
  100. cout << "Won by column" << endl;
  101. return;
  102. }
  103. p1 = 0;
  104. p2 = 0;
  105. }
  106.  
  107. // diags
  108. if (tic[0][0] == '*' || tic[2][0] == '*'); // no diagonal wins possible
  109. else if (tic[0][0] == tic[1][1] && tic[1][1] == tic[2][2])
  110. switch (tic[0][0]) {
  111. case ('O'): check = -1;
  112. break;
  113. case ('X'): check = 1;
  114. break;
  115. }
  116. else if (tic[2][0] == tic[1][1] && tic[1][1] == tic[0][2])
  117. switch (tic[2][0]) {
  118. case ('O'): check = -1;
  119. break;
  120. case ('X'): check = 1;
  121. break;
  122. }
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement