ChrisCodes365

Tic Tac Toe

Dec 5th, 2022 (edited)
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.13 KB | None | 0 0
  1. #include <iostream>
  2. #include <conio.h>
  3. #include <time.h>
  4. using namespace std;
  5.  
  6. class Game {
  7. public:
  8.  
  9.     void tip()
  10.     {
  11.         srand(time(0));
  12.         int num = rand() % 6 + 1;
  13.  
  14.         switch (num)
  15.         {
  16.         case 1:
  17.             cout << "When starting first place your first X in a corner";
  18.             return;
  19.  
  20.         case 2:
  21.             cout << "If your opponent plays the first O in the center, then try to win by placing your second X in the opposite corner from your first";
  22.             return;
  23.  
  24.         case 3:
  25.             cout << "If your opponent plays the first O in the center, then try placing your second X on an edge square (not a corner), not touching your first X";
  26.             return;
  27.  
  28.         case 4:
  29.             cout << "If your opponent plays their first O in any square besides the center, then your win probability increases";
  30.             return;
  31.  
  32.         case 5:
  33.             cout << "Force a draw when the opponent starts in the center, by placing your first X in a corner";
  34.             return;
  35.  
  36.         case 6:
  37.             cout << "Force a draw if the opponent starts in the corner, by placing your first X in the center";
  38.             return;
  39.  
  40.         default:
  41.             cout << "You are doing great";
  42.             return;
  43.         }
  44.     }
  45.  
  46. };
  47.  
  48. class Board: protected Game {
  49. protected:
  50.     char square[10] = { 'o','1','2','3','4','5','6','7','8','9' };
  51.  
  52. public:
  53.     void board()
  54.     {
  55.         system("cls");
  56.         cout << "\n\n\tTic Tac Toe\n\n";
  57.  
  58.         tip();
  59.         cout << endl << endl;
  60.         cout << "Player 1 (X)  -  Player 2 (O)" << endl << endl;
  61.         cout << endl;
  62.  
  63.         cout << "     |     |     " << endl;
  64.         cout << "  " << square[1] << "  |  " << square[2] << "  |  " << square[3] << endl;
  65.  
  66.         cout << "_____|_____|_____" << endl;
  67.         cout << "     |     |     " << endl;
  68.  
  69.         cout << "  " << square[4] << "  |  " << square[5] << "  |  " << square[6] << endl;
  70.  
  71.         cout << "_____|_____|_____" << endl;
  72.         cout << "     |     |     " << endl;
  73.  
  74.         cout << "  " << square[7] << "  |  " << square[8] << "  |  " << square[9] << endl;
  75.  
  76.         cout << "     |     |     " << endl << endl;
  77.     }
  78. };
  79.  
  80. class Player : protected Board {
  81. protected:
  82.     int i = 1;
  83.  
  84. public:
  85.     int choice()
  86.     {
  87.         int player = 1, choice;
  88.         i = 0;
  89.         char mark;
  90.         do
  91.         {
  92.             board();
  93.             player = (player % 2) ? 1 : 2;
  94.  
  95.             cout << "Player " << player << ", enter a number:  ";
  96.             cin >> choice;
  97.  
  98.             mark = (player == 1) ? 'X' : 'O';
  99.  
  100.             if (choice == 1 && square[1] == '1')
  101.  
  102.                 square[1] = mark;
  103.             else if (choice == 2 && square[2] == '2')
  104.  
  105.                 square[2] = mark;
  106.             else if (choice == 3 && square[3] == '3')
  107.  
  108.                 square[3] = mark;
  109.             else if (choice == 4 && square[4] == '4')
  110.  
  111.                 square[4] = mark;
  112.             else if (choice == 5 && square[5] == '5')
  113.  
  114.                 square[5] = mark;
  115.             else if (choice == 6 && square[6] == '6')
  116.  
  117.                 square[6] = mark;
  118.             else if (choice == 7 && square[7] == '7')
  119.  
  120.                 square[7] = mark;
  121.             else if (choice == 8 && square[8] == '8')
  122.  
  123.                 square[8] = mark;
  124.             else if (choice == 9 && square[9] == '9')
  125.  
  126.                 square[9] = mark;
  127.             else
  128.             {
  129.                 cout << "Invalid move ";
  130.  
  131.                 player--;
  132.                 cin.ignore();
  133.                 cin.get();
  134.             }
  135.             i = checkwin();
  136.  
  137.             player++;
  138.         } while (i == -1);
  139.         board();
  140.         if (i == 1)
  141.  
  142.             cout << "==>\aPlayer " << --player << " win ";
  143.         else
  144.             cout << "==>\aGame draw";
  145.  
  146.         cin.ignore();
  147.         cin.get();
  148.         return 0;
  149.     }
  150.  
  151.     int checkwin()
  152.     {
  153.         if (square[1] == square[2] && square[2] == square[3])
  154.  
  155.             return 1;
  156.         else if (square[4] == square[5] && square[5] == square[6])
  157.  
  158.             return 1;
  159.         else if (square[7] == square[8] && square[8] == square[9])
  160.  
  161.             return 1;
  162.         else if (square[1] == square[4] && square[4] == square[7])
  163.  
  164.             return 1;
  165.         else if (square[2] == square[5] && square[5] == square[8])
  166.  
  167.             return 1;
  168.         else if (square[3] == square[6] && square[6] == square[9])
  169.  
  170.             return 1;
  171.         else if (square[1] == square[5] && square[5] == square[9])
  172.  
  173.             return 1;
  174.         else if (square[3] == square[5] && square[5] == square[7])
  175.  
  176.             return 1;
  177.         else if (square[1] != '1' && square[2] != '2' && square[3] != '3'
  178.             && square[4] != '4' && square[5] != '5' && square[6] != '6'
  179.             && square[7] != '7' && square[8] != '8' && square[9] != '9')
  180.  
  181.             return 0;
  182.         else
  183.             return -1;
  184.     }
  185.  
  186.     void player()
  187.     {
  188.         choice();
  189.     }
  190.  
  191. };
  192.  
  193.  
  194. int main() {
  195.     Player Op;
  196.     Op.player();
  197.  
  198.     _getch();
  199. }
Tags: C++
Advertisement
Add Comment
Please, Sign In to add comment