Advertisement
avr39-ripe

tickTacToe

Mar 27th, 2020
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.80 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. //char checkWin(char arr[3][3])
  4. //{
  5. //  char same{' '};
  6. //  char sameD{ arr[0][0] };
  7. //  int cnt{ 0 };
  8. //  int cntD{ 0 };
  9. //  int x;
  10. //  int y;
  11. //
  12. //  for (x = 0; x < 3; ++x)
  13. //  {
  14. //      for (y = 0, cnt = 0, same = arr[y][x]; y < 3; ++y)
  15. //      {
  16. //          //std::cout << "Same = " << same << '\n';
  17. //          if (arr[y][x] != same)
  18. //          {
  19. //              //std::cout << same << " # " << arr[y][x] << '\n';
  20. //              //break;
  21. //              cnt = 0;
  22. //          }
  23. //          else
  24. //          {
  25. //              ++cnt;
  26. //              std::cout << " cnt = " << cnt << '\n';
  27. //          }
  28. //
  29. //          if (x == y and arr[y][x] != sameD)
  30. //          {
  31. //              std::cout << sameD << " M " << arr[y][x] << '\n';
  32. //              //break;
  33. //              cntD = 0;
  34. //          }
  35. //          if (x == y and arr[y][x] == sameD)
  36. //          {
  37. //              ++cntD;
  38. //              std::cout << " cntD = " << cntD << '\n';
  39. //          }
  40. //      }
  41. //      if (cnt == 3 and same != ' ') { return same; };
  42. //      if (cntD == 3 and sameD != ' ') { return same; };
  43. //  }
  44. //
  45. //  sameD = arr[0][2];
  46. //  cntD = 0;
  47. //
  48. //  for (y = 0; y < 3; ++y)
  49. //  {
  50. //      for (x = 0, cnt = 0, same = arr[y][x]; x < 3; ++x)
  51. //      {
  52. //          //std::cout << "Same = " << same << '\n';
  53. //          if (arr[y][x] != same)
  54. //          {
  55. //              //std::cout << same << " # " << arr[y][x] << '\n';
  56. //              //break;
  57. //              cnt = 0;
  58. //          }
  59. //          else
  60. //          {
  61. //              ++cnt;
  62. //              std::cout << " cnt = " << cnt << '\n';
  63. //          }
  64. //          if (x + y == 2 and arr[y][x] != sameD)
  65. //          {
  66. //              std::cout << sameD << " R " << arr[y][x] << '\n';
  67. //              //break;
  68. //              cntD = 0;
  69. //          }
  70. //          if (x + y == 2 and arr[y][x] == sameD)
  71. //          {
  72. //              ++cntD;
  73. //              std::cout << " cntD = " << cntD << '\n';
  74. //          }
  75. //      }
  76. //      if (cnt == 3 and same != ' ') { return same; };
  77. //      if (cntD == 3 and sameD != ' ') { return same; };
  78. //  }
  79. //
  80. //}
  81.  
  82. char checkWin(char arr[3][3])
  83. {
  84.     char same{ ' ' };
  85.     char sameD{ arr[0][0] };
  86.     int cnt{ 0 };
  87.     int cntD{ 0 };
  88.     int x{ 0 };
  89.     int y{ 0 };
  90.  
  91.     for (x = 0; x < 3; ++x)
  92.     {
  93.         for (y = 0, cnt = 0, same = arr[y][x]; y < 3; ++y)
  94.         {
  95.             (arr[y][x] != same) ? cnt = 0 : ++cnt;
  96.  
  97.             if (x == y)
  98.             {
  99.                 (arr[y][x] != sameD) ? cntD = 0 : ++cntD;
  100.             }
  101.         }
  102.         if (cnt == 3 and same != ' ') { return same; };
  103.         if (cntD == 3 and sameD != ' ') { return same; };
  104.     }
  105.  
  106.     sameD = arr[0][2];
  107.     cntD = 0;
  108.  
  109.     for (y = 0; y < 3; ++y)
  110.     {
  111.         for (x = 0, cnt = 0, same = arr[y][x]; x < 3; ++x)
  112.         {
  113.             (arr[y][x] != same) ? cnt = 0 : ++cnt;
  114.  
  115.             if (x + y == 2)
  116.             {
  117.                 (arr[y][x] != sameD) ? cntD = 0 : ++cntD;
  118.             }
  119.         }
  120.         if (cnt == 3 and same != ' ') { return same; };
  121.         if (cntD == 3 and sameD != ' ') { return sameD; };
  122.     }
  123.     return 'Z';
  124. }
  125.  
  126. int main()
  127. {
  128.     //char field[3][3]{ {'x','o',' '}, {'x','o',' '} ,{'x',' ','o'} };
  129.     //char field[3][3]{ {' ','x',' '}, {'o','x',' '} ,{'x','x','o'} };
  130.     //char field[3][3]{ {'x','o','x'}, {'o','o','x'} ,{'o',' ','x'} };
  131.     //char field[3][3]{ {'x','x','x'}, {'o','o',' '}, {'o',' ','x'} };
  132.     //char field[3][3]{ {'o','o',' '}, {'x','x','x'}, {'o',' ','x'} };
  133.     //char field[3][3]{ {'o','o',' '}, {'o',' ','x'}, {'x','x','x'} };
  134.     char field[3][3]{ {'x','o',' '}, {'o','x','x'}, {'o',' ','x'} };
  135.     //char field[3][3]{ {'o','o','x'}, {' ','x','x'}, {'x','o',' '} };
  136.  
  137.     std::cout << checkWin(field) << '\n';
  138.  
  139.  
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement