Advertisement
Guest User

tictactoe

a guest
Dec 9th, 2016
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.25 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3.  
  4. using namespace std;
  5. char Square[10] = { 'o', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
  6.  
  7. void Board();
  8. void CheckWin();
  9. void CheckDraw();
  10. void PlayerMark();
  11. bool GameFin = false;
  12. int Turns = 0;
  13. int Player = 1;
  14. char Game = 'P';
  15. char Mark;
  16.  
  17. void Board()
  18. {
  19. system("cls");
  20. cout << "+---+---+---+" << endl;
  21. cout << "| " << Square[1] << " | " << Square[2] << " | " << Square[3] << " |" << endl;
  22. cout << "+---+---+---+" << endl;
  23. cout << "| " << Square[4] << " | " << Square[5] << " | " << Square[6] << " |" << endl;
  24. cout << "+---+---+---+" << endl;
  25. cout << "| " << Square[7] << " | " << Square[8] << " | " << Square[9] << " |" << endl;
  26. cout << "+---+---+---+" << endl;
  27. }
  28.  
  29.  
  30. void PlayerMark()
  31. {
  32. if (Player == 1)
  33. Mark = 'X';
  34. if (Player == 2)
  35. Mark = 'O';
  36. }
  37.  
  38. void NewPlayer()
  39. {
  40. if ((Player == 1))
  41. Player = 2;
  42. else if ((Player == 2))
  43. Player = 1;
  44. }
  45.  
  46. void CheckWin()
  47. {
  48. if (Square[1] == Square[2] && Square[2] == Square[3])
  49. Game = 'W';
  50. else if (Square[4] == Square[5] && Square[5] == Square[6])
  51. Game = 'W';
  52. else if (Square[7] == Square[8] && Square[8] == Square[9])
  53. Game = 'W';
  54. else if (Square[1] == Square[4] && Square[4] == Square[7])
  55. Game = 'W';
  56. else if (Square[2] == Square[5] && Square[5] == Square[8])
  57. Game = 'W';
  58. else if (Square[3] == Square[6] && Square[6] == Square[9])
  59. Game = 'W';
  60. else if (Square[1] == Square[5] && Square[5] == Square[9])
  61. Game = 'W';
  62. else if (Square[3] == Square[5] && Square[5] == Square[7])
  63. Game = 'W';
  64. }
  65.  
  66.  
  67. void CheckDraw()
  68. {
  69. if (Turns == 9)
  70. Game = 'D';
  71. }
  72.  
  73.  
  74. int main()
  75. {
  76. int Choice;
  77.  
  78. cout << "Welcome to tic-tac-toe" << endl;
  79. cout << "Player 1 is X, Player 2 is O." << endl;
  80. while(GameFin == false)
  81. {
  82. Board();
  83. cout << "Player " << Player << "'s turn." << endl;
  84. cout << "Enter a number of where you would like to take your move" << endl;
  85. cin >> Choice;
  86. PlayerMark();
  87.  
  88. if (Choice == 1 && Square[1] == '1')
  89. Square[1] = Mark;
  90. else if (Choice == 2 && Square[2] == '2')
  91. Square[2] = Mark;
  92. else if (Choice == 3 && Square[3] == '3')
  93. Square[3] = Mark;
  94. else if (Choice == 4 && Square[4] == '4')
  95. Square[4] = Mark;
  96. else if (Choice == 5 && Square[5] == '5')
  97. Square[5] = Mark;
  98. else if (Choice == 6 && Square[6] == '6')
  99. Square[6] = Mark;
  100. else if (Choice == 7 && Square[7] == '7')
  101. Square[7] = Mark;
  102. else if (Choice == 8 && Square[8] == '8')
  103. Square[8] = Mark;
  104. else if (Choice == 9 && Square[9] == '9')
  105. Square[9] = Mark;
  106. else
  107. cout << "Move is invalid" << endl;
  108.  
  109. CheckWin();
  110.  
  111. if (Game == 'W')
  112. GameFin = true;
  113.  
  114. Turns = Turns + 1;
  115. CheckDraw();
  116.  
  117. if (Game == 'D')
  118. GameFin = true;
  119.  
  120. if (Game == 'P')
  121. NewPlayer();
  122.  
  123. }
  124.  
  125. Board();
  126.  
  127. if (Game == 'D')
  128. cout << "Game is a draw." << endl;
  129. else if (Game == 'W')
  130. cout << "Congratulations player " << Player << ", you win!" << endl;
  131.  
  132. return 0;
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement