Advertisement
UnlimitedSupply

TicTacToe

Jun 19th, 2022
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.21 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <unistd.h>
  4. using namespace std;
  5. bool Complete = false;
  6. bool PlayerTurn = true;
  7. bool LegalMove = false;
  8. bool IsStillPossibleToWin = false;
  9. string PlayerCheck;
  10. string Answer;
  11. string PlayerOne = "X";
  12. string PlayerTwo = "O";
  13. string PlayerThree = "Y";
  14. int PlayerWhoWon;
  15.  
  16. vector<string> TicTacToe {"1", "2", "3", "4", "5", "6", "7", "8", "9"};
  17.  
  18. bool PlayAgain() {
  19.     cout << "Want to play again?\n";
  20.     cin >> Answer;
  21.     if (Answer == "yes" || Answer == "Yes" || Answer == "Y" || Answer == "y") {
  22.         return true;
  23.     } else if (Answer == "No" || Answer == "no" || Answer == "N" || Answer == "n") {
  24.         return false;
  25.     } else {
  26.         PlayAgain();
  27.     }
  28. }
  29.  
  30. void Track() {
  31.     for (int Repeats = 30; Repeats >= 0; Repeats--) {
  32.         cout << "\n";
  33.     }
  34.     cout << " " << TicTacToe[0] << " | " << TicTacToe[1] << " | " << TicTacToe[2] << "\n";
  35.     cout << "----------\n";
  36.     sleep(0.25);
  37.     cout << " " << TicTacToe[3] << " | " << TicTacToe[4] << " | " << TicTacToe[5] << "\n";
  38.     sleep(0.25);
  39.     cout << "----------\n";
  40.     sleep(0.25);
  41.     cout << " " << TicTacToe[6] << " | " << TicTacToe[7] << " | " << TicTacToe[8] << "\n";
  42.     sleep(0.25);
  43.     cout << "\n\n";
  44. }
  45.  
  46. void Turn() {
  47.     do {
  48.         if (PlayerTurn) {
  49.             cout << "X ";
  50.         } else {
  51.             cout << "O ";
  52.         }
  53.         cin >> Answer;
  54.         if (Answer == "1" && TicTacToe[0] == "1") {
  55.             LegalMove = true;
  56.             TicTacToe[0] = PlayerCheck;
  57.         } else if (Answer == "2" && TicTacToe[1] == "2") {
  58.             LegalMove = true;
  59.             TicTacToe[1] = PlayerCheck;
  60.         } else if (Answer == "3" && TicTacToe[2] == "3") {
  61.             LegalMove = true;
  62.             TicTacToe[2] = PlayerCheck;
  63.         } else if (Answer == "4" && TicTacToe[3] == "4") {
  64.             LegalMove = true;
  65.             TicTacToe[3] = PlayerCheck;
  66.         } else if (Answer == "5" && TicTacToe[4] == "5") {
  67.             LegalMove = true;
  68.             TicTacToe[4] = PlayerCheck;
  69.         } else if (Answer == "6" && TicTacToe[5] == "6") {
  70.             LegalMove = true;
  71.             TicTacToe[5] = PlayerCheck;
  72.         } else if (Answer == "7" && TicTacToe[6] == "7") {
  73.             LegalMove = true;
  74.             TicTacToe[6] = PlayerCheck;
  75.         } else if (Answer == "8" && TicTacToe[7] == "8") {
  76.             LegalMove = true;
  77.             TicTacToe[7] = PlayerCheck;
  78.         } else if (Answer == "9" && TicTacToe[8] == "9") {
  79.             LegalMove = true;
  80.             TicTacToe[8] = PlayerCheck;
  81.         }
  82.     } while (LegalMove != true);
  83.     LegalMove = false;
  84.     if (TicTacToe[0] == TicTacToe[4] && TicTacToe[4] == TicTacToe[8] || TicTacToe[0] == TicTacToe[3] && TicTacToe[3] == TicTacToe[6]) {
  85.         Complete = true;
  86.         if (PlayerTurn) {
  87.             PlayerWhoWon = 1;
  88.         } else {
  89.             PlayerWhoWon = 2;
  90.         }
  91.     } else if (TicTacToe[0] == TicTacToe[1] && TicTacToe[1] == TicTacToe[2] || TicTacToe[1] == TicTacToe[4] && TicTacToe[4] == TicTacToe[7]) {
  92.         Complete = true;
  93.         if (PlayerTurn) {
  94.             PlayerWhoWon = 1;
  95.         } else {
  96.             PlayerWhoWon = 2;
  97.         }
  98.     } else if (TicTacToe[2] == TicTacToe[4] && TicTacToe[4] == TicTacToe[6] || TicTacToe[3] == TicTacToe[4] && TicTacToe[4] == TicTacToe[5]) {
  99.         Complete = true;
  100.         if (PlayerTurn) {
  101.             PlayerWhoWon = 1;
  102.         } else {
  103.             PlayerWhoWon = 2;
  104.         }
  105.     } else if (TicTacToe[2] == TicTacToe[5] && TicTacToe[5] == TicTacToe[8] || TicTacToe[6] == TicTacToe[7] && TicTacToe[7] == TicTacToe[8]) {
  106.         Complete = true;
  107.         if (PlayerTurn) {
  108.             PlayerWhoWon = 1;
  109.         } else {
  110.             PlayerWhoWon = 2;
  111.         }
  112.     } else {
  113.         if (TicTacToe[0] == "1" || TicTacToe[1] == "2" || TicTacToe[2] == "3" || TicTacToe[3] == "4" ||TicTacToe[4] == "5" || TicTacToe[5] == "6" ||
  114.             TicTacToe[6] == "7" || TicTacToe[7] == "8" || TicTacToe[8] == "9") {
  115.             IsStillPossibleToWin = true;
  116.         } else {
  117.             IsStillPossibleToWin = false;
  118.         }
  119.         if (IsStillPossibleToWin == false) {
  120.             Complete = true;
  121.             PlayerWhoWon = 3;
  122.         }
  123.     }
  124.     if (PlayerTurn) {
  125.         PlayerTurn = false;
  126.     } else {
  127.         PlayerTurn = true;
  128.     }
  129. }
  130.  
  131. int main() {
  132.     Track();
  133.     do {
  134.         if (PlayerTurn) {
  135.             cout << "Its player ones turn.\n";
  136.             PlayerCheck = PlayerOne;
  137.         } else {
  138.             cout << "Its player twos turn.\n";
  139.             PlayerCheck = PlayerTwo;
  140.         }
  141.         Turn();
  142.         Track();
  143.     } while (Complete != true);
  144.     if (PlayerWhoWon == 1) {
  145.         cout << "Player one won!\n";
  146.     } else if (PlayerWhoWon == 2) {
  147.         cout << "Player two won!\n";
  148.     } else {
  149.         cout << "It was a tie!\n";
  150.     }
  151.     if (PlayAgain() == true) {
  152.         TicTacToe[0] = "1";
  153.         TicTacToe[1] = "2";
  154.         TicTacToe[2] = "3";
  155.         TicTacToe[3] = "4";
  156.         TicTacToe[4] = "5";
  157.         TicTacToe[5] = "6";
  158.         TicTacToe[6] = "7";
  159.         TicTacToe[7] = "8";
  160.         TicTacToe[8] = "9";
  161.         Complete = false;
  162.         PlayerTurn = true;
  163.         main();
  164.     }
  165. }
  166.  
  167.  
  168.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement