Advertisement
YorKnEz

Untitled

Feb 27th, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.41 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7.  
  8. // First, we declare the matrix or our grid.
  9.  
  10. char GRID[3][3];
  11.  
  12. // We initialize all of the elements from the matrix as '*' too see the grid.
  13.  
  14. for(int row = 0; row <= 2; ++row)
  15. {
  16. for(int col = 0; col <= 2; ++col)
  17. {
  18. GRID[row][col]=' ';
  19. }
  20.  
  21. cout << '\n';
  22. }
  23.  
  24. int player_move, player_won = 0;
  25.  
  26. for(int moves = 1; moves <= 9; ++moves)
  27. {
  28.  
  29. // We ask for next players move.
  30.  
  31. cout << "Your move please: \n";
  32.  
  33. cin >> player_move;
  34.  
  35. // If the move is an odd number then it's Player1 turn, if not it's Player2 turn.
  36.  
  37. if(moves % 2 == 1)
  38. {
  39.  
  40. // We will use these ifs to see where our X/0 will go.
  41.  
  42. if(player_move > 0 && player_move <= 3)
  43. {
  44. GRID[0][player_move-1] = 'X';
  45. }
  46. else if(player_move > 3 && player_move <= 6)
  47. {
  48. GRID[1][player_move-4] = 'X';
  49. }
  50. else if(player_move > 6 && player_move <=9)
  51. {
  52. GRID[2][player_move-7] = 'X';
  53. }
  54.  
  55. // We show the grid after every move.
  56.  
  57. for(int row = 0; row <= 2; ++row)
  58. {
  59. for(int col = 0; col <= 2; ++col)
  60. {
  61. cout << GRID[row][col];
  62. }
  63.  
  64. cout << '\n';
  65. }
  66.  
  67. // The program compares the grid we have to all known cases.
  68.  
  69. if(GRID[0][0] == 'X' && GRID[0][1] == 'X' && GRID[0][2] == 'X') player_won = 1;
  70. else if(GRID[1][0] == 'X' && GRID[1][1] == 'X' && GRID[1][2] == 'X') player_won = 1;
  71. else if(GRID[2][0] == 'X' && GRID[2][1] == 'X' && GRID[2][2] == 'X') player_won = 1;
  72. else if(GRID[0][0] == 'X' && GRID[1][0] == 'X' && GRID[2][0] == 'X') player_won = 1;
  73. else if(GRID[0][1] == 'X' && GRID[1][1] == 'X' && GRID[2][1] == 'X') player_won = 1;
  74. else if(GRID[0][2] == 'X' && GRID[1][2] == 'X' && GRID[2][2] == 'X') player_won = 1;
  75. else if(GRID[0][0] == 'X' && GRID[1][1] == 'X' && GRID[2][2] == 'X') player_won = 1;
  76. else if(GRID[0][2] == 'X' && GRID[1][1] == 'X' && GRID[2][0] == 'X') player_won = 1;
  77.  
  78. // Here the program checks if the game has been won by any of the players. If someone won then it will show who and return 0.
  79. if(player_won == 1)
  80. {
  81. cout << "Player1 won!";
  82. return 0;
  83. }
  84. }
  85.  
  86. // This else is the exact replica of the if above.
  87.  
  88. else
  89. {
  90. if(player_move > 0 && player_move <= 3)
  91. {
  92. GRID[0][player_move-1] = '0';
  93. }
  94. else if(player_move > 3 && player_move <= 6)
  95. {
  96. GRID[1][player_move-4] = '0';
  97. }
  98. else if(player_move > 6 && player_move <=9)
  99. {
  100. GRID[2][player_move-7] = '0';
  101. }
  102.  
  103. for(int row = 0; row <= 2; ++row)
  104. {
  105. for(int col = 0; col <= 2; ++col)
  106. {
  107. cout << GRID[row][col];
  108. }
  109.  
  110. cout << '\n';
  111. }
  112.  
  113. if(GRID[0][0] == '0' && GRID[0][1] == '0' && GRID[0][2] == '0') player_won = 1;
  114. else if(GRID[1][0] == '0' && GRID[1][1] == '0' && GRID[1][2] == '0') player_won = 1;
  115. else if(GRID[2][0] == '0' && GRID[2][1] == '0' && GRID[2][2] == '0') player_won = 1;
  116. else if(GRID[0][0] == '0' && GRID[1][0] == '0' && GRID[2][0] == '0') player_won = 1;
  117. else if(GRID[0][1] == '0' && GRID[1][1] == '0' && GRID[2][1] == '0') player_won = 1;
  118. else if(GRID[0][2] == '0' && GRID[1][2] == '0' && GRID[2][2] == '0') player_won = 1;
  119. else if(GRID[0][0] == '0' && GRID[1][1] == '0' && GRID[2][2] == '0') player_won = 1;
  120. else if(GRID[0][2] == '0' && GRID[1][1] == '0' && GRID[2][0] == '0') player_won = 1;
  121.  
  122. if(player_won == 1)
  123. {
  124. cout << "Player1 won!";
  125. return 0;
  126. }
  127. }
  128.  
  129. // Test if the moves are equal to 9 then the game is over.
  130.  
  131. if(moves == 9)
  132. {
  133. cout << "Draw!";
  134. return 0;
  135. }
  136.  
  137. }
  138.  
  139. return 0;
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement