Advertisement
Guest User

c++ tic-tac-toe

a guest
Sep 9th, 2019
355
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.67 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7. //making the opening text appear
  8. cout << "Welcome to Tic-Tac-Toe!" << endl;
  9.  
  10. //Making the array
  11. char my_array[3][3]
  12. {
  13. { '.', '.', '.' },
  14. { '.', '.', '.' },
  15. { '.', '.', '.' },
  16. };
  17. //int check;
  18. bool winner = false; //Set this to true when somebody wins
  19. string player_one = "Player X's turn! Please press 1-9 to place a piece and press Enter.";
  20. string player_two = "Player O's turn! Please press 1-9 to place a piece and press Enter.";
  21. int turn = 1;
  22. char symbol = 'X';
  23. int points = 0;
  24. char play_again = 'n';
  25. int keyPressed; //variable used in the game function
  26. cout << player_one << endl; //Begins the game with player one
  27.  
  28. cout << "The board treats number inputs like this:\n";
  29. //Making the demonstration array
  30. char demo_array[3][3]
  31. {
  32. { '1', '2', '3' },
  33. { '4', '5', '6' },
  34. { '7', '8', '9' },
  35. };
  36. //Making the demonstration array show up
  37. for (int i = 0; i < 3; i++)
  38. {
  39. for (int j = 0; j < 3; j++)
  40. {
  41. cout << demo_array[i][j];
  42. }
  43. cout << endl;
  44. }
  45.  
  46. cout << "GAMEBOARD \n";
  47. for (int i = 0; i < 3; i++)
  48. {
  49. for (int j = 0; j < 3; j++)
  50. {
  51. cout << my_array[i][j];
  52. }
  53. cout << endl;
  54. }
  55.  
  56. while(!winner)
  57. {
  58. cin >> keyPressed;
  59.  
  60. if (turn == 1)
  61. {
  62. symbol = 'X';
  63. cout << player_one;
  64. cout << endl;
  65. }
  66. else
  67. {
  68. symbol = 'O';
  69. cout << player_two;
  70. cout << endl;
  71. }
  72.  
  73. if (keyPressed <= 3 && my_array[0][keyPressed-1] == '.')
  74. {
  75. my_array[0][keyPressed-1] = symbol;
  76. turn = -turn;
  77. }
  78. if (keyPressed > 3 && keyPressed <= 6 && my_array[1][keyPressed-4] == '.')
  79. {
  80. my_array[1][keyPressed-4] = symbol;
  81. turn = -turn;
  82. }
  83. if (keyPressed > 6 && keyPressed <= 9 && my_array[2][keyPressed-7] == '.')
  84. {
  85. my_array[2][keyPressed-7] = symbol;
  86. turn = -turn;
  87. }
  88.  
  89. for (int i = 0; i < 3; i++)
  90. {
  91. for (int j = 0; j < 3; j++)
  92. {
  93. if (my_array[i][j] == symbol)
  94. points++;
  95. if (points > 2)
  96. {
  97. winner = 1;
  98. break;
  99. }
  100. }
  101. points = 0;
  102. }
  103.  
  104. for (int j = 0; j < 3; j++)
  105. {
  106. for (int i = 0; i < 3; i++)
  107. {
  108. if (my_array[i][j] == symbol)
  109. points++;
  110. if (points > 2)
  111. {
  112. winner = 1;
  113. break;
  114. }
  115. }
  116. points = 0;
  117. }
  118.  
  119. for (int i = 0; i < 3; i++)
  120. {
  121. for (int j = 0; j < 3; j++)
  122. {
  123. cout << my_array[i][j];
  124. }
  125. cout << endl;
  126. }
  127.  
  128. if (my_array[0][0] == symbol && my_array[1][1] == symbol && my_array[2][2] == symbol
  129. or my_array[2][0] == symbol && my_array[1][1] == symbol && my_array[0][2] == symbol)
  130. winner = 1;
  131. }
  132.  
  133. cout << "player " << symbol << " wins\npress 'y' and enter to play again: ";
  134. cin >> play_again;
  135.  
  136. if (play_again == 'y')
  137. main();
  138. else
  139. cout << "thanks for playing";
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement