Advertisement
Guest User

Untitled

a guest
May 23rd, 2018
301
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.16 KB | None | 0 0
  1. // X si 0.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include <iostream>
  5. using namespace std;
  6. char matrix[3][3] = { '1' , '2' , '3' , '4' , '5' , '6' , '7' , '8' , '9' };
  7. char player = 'X';
  8. int contor = 0;
  9. int PlayerX = 0;
  10. int PLayerO = 0;
  11. int n;
  12. // Aceasta functie afiseaza tabla de joc
  13. void Draw(void)
  14. {
  15. system("cls");
  16. cout << "Tic Tac Toe";
  17. cout << endl;
  18. for (int i = 0; i < 3; ++i) {
  19. for (int j = 0; j < 3; ++j)
  20. cout << matrix[i][j] << " | ";
  21. cout << "\n";
  22. cout << endl;
  23.  
  24.  
  25. }
  26. }
  27.  
  28. void Input() {
  29. int choice;
  30. cout << "Press a number from 1 to 9: ";
  31. cin >> choice;
  32.  
  33. if (choice == 1){
  34. if (matrix[0][0] == '1'){
  35. matrix[0][0] = player;
  36. }
  37. else {
  38. cout << "Try again!" << endl;
  39. Input();
  40. }
  41. }
  42. else if (choice == 2){
  43. if (matrix[0][1] == '2'){
  44. matrix[0][1] = player;
  45. }
  46. else {
  47. cout << "Try again!"<<endl;
  48. Input();
  49. }
  50. }
  51. else if (choice == 3){
  52. if (matrix[0][2] == '3'){
  53. matrix[0][2] = player;
  54. }
  55. else {
  56. cout << "Try again!"<<endl;
  57. Input();
  58. }
  59. }
  60. else if (choice == 4){
  61. if (matrix[1][0] == '4'){
  62. matrix[1][0] = player;
  63. }
  64. else {
  65. cout << "Try again!"<<endl;
  66. Input();
  67. }
  68. }
  69. else if (choice == 5){
  70. if (matrix[1][1] == '5'){
  71. matrix[1][1] = player;
  72. }
  73. else {
  74. cout << "Try again!"<<endl;
  75. Input();
  76. }
  77. }
  78. else if (choice == 6){
  79. if (matrix[1][2] == '6'){
  80. matrix[1][2] = player;
  81. }
  82. else {
  83. cout << "Try again!"<<endl;
  84. Input();
  85. }
  86. }
  87. else if (choice == 7){
  88. if (matrix[2][0] == '7'){
  89. matrix[2][0] = player;
  90. }
  91. else {
  92. cout << "Try again!"<<endl;
  93. Input();
  94. }
  95. }
  96. else if (choice == 8){
  97. if (matrix[2][1] == '8'){
  98. matrix[2][1] = player;
  99. }
  100. else {
  101. cout << "Try again!"<<endl;
  102. Input();
  103. }
  104. }
  105. else if (choice == 9){
  106. if (matrix[2][2] == '9'){
  107. matrix[2][2] = player;
  108. }
  109. else {
  110. cout << "Try again!"<<endl;
  111. Input();
  112. }
  113. }
  114.  
  115.  
  116. }
  117.  
  118. void TogglePlayer() {
  119. if (player == 'X')
  120. player = 'O';
  121.  
  122. else
  123. player = 'X';
  124.  
  125. }
  126.  
  127.  
  128. char Win() {
  129.  
  130. // X won
  131. if (matrix[0][0] == 'X' && matrix[0][1] == 'X' && matrix[0][2] == 'X') {
  132. return 'X';
  133. }
  134. if (matrix[1][0] == 'X' && matrix[1][1] == 'X' && matrix[1][2] == 'X') {
  135. return 'X';
  136. }
  137. if (matrix[2][0] == 'X' && matrix[2][1] == 'X' && matrix[2][2] == 'X') {
  138. return 'X';
  139. }
  140. if (matrix[0][0] == 'X' && matrix[1][1] == 'X' && matrix[2][2] == 'X') {
  141. return 'X';
  142. }
  143. if (matrix[0][2] == 'X' && matrix[1][1] == 'X' && matrix[2][0] == 'X') {
  144. return 'X';
  145. }
  146. if (matrix[0][0] == 'X' && matrix[1][0] == 'X' && matrix[2][0] == 'X') {
  147. return 'X';
  148. }
  149. if (matrix[0][1] == 'X' && matrix[1][1] == 'X' && matrix[2][1] == 'X') {
  150. return 'X';
  151. }
  152. if (matrix[0][2] == 'X' && matrix[1][2] == 'X' && matrix[2][2] == 'X') {
  153. return 'X';
  154. }
  155.  
  156. // O won
  157.  
  158. if (matrix[0][0] == 'O' && matrix[0][1] == 'O' && matrix[0][2] == 'O') {
  159. return 'O';
  160. }
  161. if (matrix[1][0] == 'O' && matrix[1][1] == 'O' && matrix[1][2] == 'O') {
  162. return 'O';
  163. }
  164. if (matrix[2][0] == 'O' && matrix[2][1] == 'O' && matrix[2][2] == 'O') {
  165. return 'O';
  166. }
  167. if (matrix[0][0] == 'O' && matrix[1][1] == 'O' && matrix[2][2] == 'O') {
  168. return 'O';
  169. }
  170. if (matrix[0][2] == 'O' && matrix[1][1] == 'O' && matrix[2][0] == 'O') {
  171. return 'O';
  172. }
  173. if (matrix[0][0] == 'O' && matrix[1][0] == 'O' && matrix[2][0] == 'O') {
  174. return 'O';
  175. }
  176. if (matrix[0][1] == 'O' && matrix[1][1] == 'O' && matrix[2][1] == 'O') {
  177. return 'O';
  178. }
  179. if (matrix[0][2] == 'O' && matrix[1][2] == 'O' && matrix[2][2] == 'O') {
  180. return 'O';
  181. }
  182. return '/';
  183. }
  184.  
  185. void PlayerTurns (){
  186. if (player == 'X'){
  187. cout << " " << "Player X turn!";
  188. cout << endl;
  189. }
  190. else {
  191. cout << " " << "Player O turn!";
  192. cout << endl;
  193. }
  194. }
  195.  
  196. void PlayAgain (){
  197. matrix[0][0] = '1';
  198. matrix[0][1] = '2';
  199. matrix[0][2] = '3';
  200. matrix[1][0] = '4';
  201. matrix[1][1] = '5';
  202. matrix[1][2] = '6';
  203. matrix[2][0] = '7';
  204. matrix[2][1] = '8';
  205. matrix[2][2] = '9';
  206. contor = 0;
  207. }
  208.  
  209. void AllFunctions (){
  210. Draw();
  211.  
  212. while (1) {
  213. PlayerTurns();
  214. ++contor;
  215. Input();
  216. Draw();
  217. TogglePlayer();
  218.  
  219. if (Win() == 'X'){
  220. cout << "X wins" << endl;
  221. cout << "Do you want to play again ? pres 1 for yes or anything else for no: ";
  222. cin >> n;
  223. if (n == 1){
  224. PlayAgain();
  225. AllFunctions();
  226.  
  227. }
  228. else {
  229. break;
  230. }
  231.  
  232. }
  233. if (Win() == 'O'){
  234. cout << "O wins" << endl;
  235. cout << "Do you want to play again ? pres 1 for yes or anything else for no: ";
  236. cin >> n;
  237. if (n == 1){
  238. PlayAgain();
  239. AllFunctions();
  240.  
  241. }
  242. else {
  243. break;
  244. };
  245. }
  246. if (Win() == '/' && contor == 9){
  247. cout << "Draw" << endl;
  248. cout << "Do you want to play again ? pres 1 for yes or anything else for no: ";
  249. cin >> n;
  250. if (n == 1){
  251. PlayAgain();
  252. AllFunctions();
  253.  
  254. }
  255. else {
  256. break;
  257. }
  258. }
  259.  
  260.  
  261. }
  262. }
  263.  
  264. int main()
  265. {
  266. Draw();
  267.  
  268. while (1) {
  269. PlayerTurns();
  270. ++contor;
  271. Input();
  272. Draw();
  273. TogglePlayer();
  274.  
  275. if (Win() == 'X'){
  276. ++PLayerX;
  277. cout << "X wins" << endl;
  278. cout << "Player X has " << PlayerX << " points" << endl;
  279. cout << "Player O has " << PlayerO << " points" << endl;
  280. cout << "Do you want to play again ? pres 1 for yes or anything else for no: ";
  281. cin >> n;
  282. if (n == 1){
  283. PlayAgain();
  284. AllFunctions();
  285.  
  286. }
  287. else {
  288. break;
  289. }
  290.  
  291. }
  292. if (Win() == 'O'){
  293. ++PlayerO;
  294. cout << "O wins" << endl;
  295. cout << "Player X has " << PlayerX << " points" << endl;
  296. cout << "Player O has " << PlayerO << " points" << endl;
  297. cout << "Do you want to play again ? pres 1 for yes or anything else for no: ";
  298. cin >> n;
  299. if (n == 1){
  300. PlayAgain();
  301. AllFunctions();
  302.  
  303. }
  304. else {
  305. break;
  306. };
  307. }
  308. if (Win() == '/' && contor == 9){
  309. cout << "Draw" << endl;
  310. cout << "Player X has " << PlayerX << " points" << endl;
  311. cout << "Player O has " << PlayerO << " points" << endl;
  312. cout << "Do you want to play again ? pres 1 for yes or anything else for no: ";
  313. cin >> n;
  314. if (n == 1){
  315. PlayAgain();
  316. AllFunctions();
  317.  
  318. }
  319. else {
  320. break;
  321. }
  322. }
  323.  
  324.  
  325. }
  326.  
  327. cout << "To report bugs contact me on gmail (patrickondreovici2017@gmail.com)";
  328.  
  329. system("Pause");
  330. return 0;
  331. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement