fork download
  1. #include<iostream>
  2. using namespace std;
  3.  
  4. int main() {
  5. // 0 empty, 1 = x, 2 = o
  6. int grid[100][100] = { 0 };
  7.  
  8. int n;
  9. cout<<"Enter grid dimension: ";
  10. cin >> n;
  11.  
  12. /*
  13. We can write length code to verify N row, N columns and 2 diagonals
  14.  
  15. Notice: the behavior of all of them is SAME
  16. E.g. We have some starting point (e.g. 0 0) and we need to verify its row
  17.  
  18. We can use a direction-array style to write an elegant code
  19.  
  20. We will create a single array with the (2N+2) needed verifications
  21. For every verification we need 4 values:
  22. The starting point (r, c): we need the starting of each row (N), col (N), 2 Diagonals
  23. The direction to move in it for N steps
  24.  
  25. For example, for the starting (0, 0)
  26. To verify its row, we need direction (1, 0)
  27. To verify its col, we need direction (0, 1)
  28. To verify its diagonal, we need direction (1, 1)
  29. To verify the right diagonal: we start from the last cell in first row (0, n-1) and moves (1, -1)
  30. 1 means move to next row. -1 means move to the previous column
  31.  
  32. Once done: we iterate over all such start/direction.
  33. Loop n times to verify they all same play symbol
  34. */
  35.  
  36. int rs[100], cs[100], dr[100], dc[100];
  37.  
  38. int verify = 0;
  39.  
  40. // Add row n positions to verify
  41. for (int r = 0; r < n; ++r)
  42. rs[verify] = r, cs[verify] = 0, dr[verify] = 0, dc[verify++] = 1;
  43.  
  44. // Add col n positions to verify
  45. for (int c = 0; c < n; ++c)
  46. rs[verify] = 0, cs[verify] = c, dr[verify] = 1, dc[verify++] = 0;
  47.  
  48. // Add diagonal 1
  49. rs[verify] = 0, cs[verify] = 0, dr[verify] = 1, dc[verify++] = 1;
  50. // Add diagonal 2
  51. rs[verify] = 0, cs[verify] = n - 1, dr[verify] = 1, dc[verify++] = -1;
  52.  
  53. int turn = 0; // 0 for x, 1 for o. Don't get confused with grid values
  54.  
  55. int steps = 0;
  56. while (true) {
  57. if (steps == n*n) {
  58. cout<<"Tie\n";
  59. break;
  60. }
  61. char symbol = 'x';
  62. if (turn == 1)
  63. symbol = 'o';
  64.  
  65. cout << "Player " << symbol << " turn. Enter empty location (r, c): ";
  66. int r, c;
  67. cin >> r >> c;
  68.  
  69. r--, c--;
  70.  
  71. if (r < 0 || r >= n || c < 0 || c >= n || grid[r][c] != 0) {
  72. cout << "Invalid input. Try again\n";
  73. continue;
  74. }
  75. grid[r][c] = turn + 1;
  76.  
  77. // print the grid
  78. for (int r = 0; r < n; ++r) {
  79. for (int c = 0; c < n; ++c) {
  80. char ch = '.';
  81. if (grid[r][c] == 1)
  82. ch = 'x';
  83. else if (grid[r][c] == 2)
  84. ch = 'o';
  85. cout << ch;
  86. }
  87. cout << "\n";
  88. }
  89.  
  90. // Check win status
  91. for (int check = 0; check < verify; ++check) {
  92. int r = rs[check], c = cs[check], rd = dr[check], cd = dc[check];
  93. int cnt = 0, first = grid[r][c];
  94.  
  95. if (first == 0)
  96. continue;
  97.  
  98. for (int step = 0; step < n; ++step, r += rd, c += cd)
  99. cnt += grid[r][c] == first;
  100.  
  101. if (cnt == n) {
  102. cout << "Player " << symbol << " won\n";
  103. return 0;
  104. }
  105. }
  106.  
  107. turn = !turn; // 0 be 1, 1 be 0 = flip player
  108. steps++;
  109. }
  110.  
  111. return 0;
  112. }
  113. /*
  114. For tie
  115. 3
  116. 1 1
  117. 1 3
  118. 1 2
  119. 2 2
  120. 3 2
  121. 2 1
  122. 2 3
  123. 3 3
  124. 3 1
  125.  */
  126.  
  127.  
Success #stdin #stdout 0.01s 5392KB
stdin
Standard input is empty
stdout
Enter grid dimension: Tie