Advertisement
OnyRoman

Untitled

Apr 26th, 2020
34
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.88 KB | None | 0 0
  1. /*############################################ #####################################
  2. TIC TAC TOE
  3. in two players
  4. ############################################## #####################################*/
  5.  
  6. #include <iostream>
  7. #include <string.h>
  8. using namespace std;
  9.  
  10. char v[10];
  11.  
  12. void setup() {
  13. for (int i = 1; i <= 9; i++)
  14. v[i] = i + '0';
  15. }
  16.  
  17. void afisare();
  18. int verificare();
  19.  
  20. int main() {
  21. bool restart = true;
  22. while (restart) {
  23. setup();
  24. int turn = 0; // Whem turn it's odd, it's player's 1 turn. When it's even, it's player's two turn.
  25. while (verificare() == 0) {
  26. turn++;
  27. afisare();
  28. cout << "Player " << ((turn % 2 != 0) ? "1" : "2") << ": enter a position for your element - ";
  29. int pos;
  30. cin >> pos;
  31. while (pos != v[pos] - '0' || !(pos > 0 && pos < 10)) {
  32. cout << "\n ! Position ocuppied. Enter an available position: ";
  33. cin >> pos;
  34. }
  35.  
  36. v[pos] = ((turn % 2) != 0 ? 'X' : 'O');
  37. }
  38.  
  39. afisare();
  40.  
  41. if (verificare() == 1)
  42. cout << "\n\n Player " << ((turn % 2 != 0) ? "1" : "2") << " won. Congratulations!";
  43. else
  44. cout << "\nGame draw. Nobody won :(";
  45.  
  46. cout << "\n\n To play again, type 'restart'. To exit, type 'exit'.";
  47. cin.get();
  48. cout << endl;
  49. char cmd[30];
  50. cin.getline(cmd, 30);
  51. while (strcmp(cmd, "restart") && strcmp(cmd, "exit")) {
  52. cout << "\n !! Unknown command. Type an available command: ";
  53. cin.getline(cmd, 30);
  54. }
  55.  
  56. if (!strcmp(cmd, "exit"))
  57. restart = false;
  58. }
  59.  
  60. system("cls");
  61. cout << "\n\n\n Thank you for playing the game! \n\n\n\n\n";
  62. }
  63.  
  64. void afisare() {
  65. system("cls");
  66. cout << "Player 1 (x) Player 2 (O)";
  67.  
  68. cout << "\n | |\n "
  69. << v[1] << " | " << v[2] << " | " << v[3]
  70. << "\n ___|___|___\n | |\n "
  71. << v[4] << " | " << v[5] << " | " << v[6]
  72. << "\n ___|___|___\n | |\n "
  73. << v[7] << " | " << v[8] << " | " << v[9]
  74. << "\n | |\n";
  75. }
  76.  
  77. int verificare() {
  78. /*
  79. 1 2 3
  80. 4 5 6
  81. 7 8 9
  82.  
  83. Possible combinations for winning the game:
  84. 1 2 3
  85. 4 5 6
  86. 7 8 9
  87. 1 4 7
  88. 2 5 8
  89. 3 6 9
  90. 1 5 9
  91. 3 5 7
  92. */
  93.  
  94. if (v[1] == v[2] && v[2] == v[3])
  95. return 1;
  96. if (v[4] == v[5] && v[5] == v[6])
  97. return 1;
  98. if (v[7] == v[8] && v[8] == v[9])
  99. return 1;
  100. if (v[1] == v[4] && v[4] == v[7])
  101. return 1;
  102. if (v[2] == v[5] && v[5] == v[8])
  103. return 1;
  104. if (v[3] == v[6] && v[6] == v[9])
  105. return 1;
  106. if (v[1] == v[5] && v[5] == v[9])
  107. return 1;
  108. if (v[3] == v[5] && v[5] == v[7])
  109. return 1;
  110.  
  111. /*
  112. If all the positions on the board are occupied, the game ends with a draw
  113. and the functions returns -1.
  114. */
  115.  
  116. if (v[1] != '1' && v[2] != '2' && v[3] != '3' && v[4] != '4' && v[5] != '5'
  117. && v[6] != '6' && v[7] != '7' && v[8] != '8' && v[9] != '9')
  118. return -1;
  119.  
  120. /*
  121. Else, the game continues, and the function returns 0;
  122. */
  123. return 0;
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement