SHARE
TWEET

Untitled

a guest May 23rd, 2019 79 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <string>
  4. #include <array>
  5.  
  6. // This is a program to play a single game of tic-tac-toe
  7. // between either two human (non-AI) players or an AI.
  8.  
  9. using namespace std;
  10.  
  11. void PrintBoard(array <char, 9>);
  12. int programprogress();
  13. int checkwin(array <char, 9>);
  14. int minimax(array <char, 9>, int, int, bool);
  15. int bestMove(array <char, 9>, int);
  16. int Opposite(int);
  17. char PlayerSymbol(int);
  18.  
  19. const int SIZE = 9;
  20. array <char, SIZE> Pos = { '1', '2', '3', '4', '5', '6', '7', '8', '9' };
  21. int player_number = 1;
  22. int k = -11, result;
  23. bool AI = false, first;
  24.  
  25. // Global variables used by 2 or more functions.
  26. // Array had to be initialized with numbers instead of blank spaces
  27. // because the check win function wouldn't work properly.
  28.  
  29. int main()
  30. {
  31.     string userinp;
  32.  
  33.     cout << "This is tic tac toe! Here's your board!" << endl;
  34.     PrintBoard(Pos);
  35.  
  36.     cout << "Would you like to play versus an AI? (Y/N)" << endl;
  37.     cin >> userinp;
  38.     if (userinp[0] == 'Y')
  39.     {
  40.         cout << "Excellent! Would you like to start first, or second? (F/S)" << endl;
  41.         cin >> userinp;
  42.         if (userinp[0] == 'F')
  43.         {
  44.             cout << "You will start first!" << endl;
  45.             first = false;
  46.             player_number = 2;
  47.         }
  48.  
  49.         else
  50.         {
  51.             cout << "The AI will start first!" << endl;
  52.             first = true;
  53.         }
  54.         AI = true;
  55.     }
  56.  
  57.     else
  58.     {
  59.         cout << "Excellent! Your game will start soon." << endl;
  60.     }
  61.  
  62.     result = programprogress();
  63.     player_number--;
  64.     PrintBoard(Pos);
  65.  
  66.     if (result == 1)
  67.         cout << endl << "Player " << player_number << " has won!!!\n";
  68.     else if (result == 10)
  69.         cout << endl << "The AI has won! Better luck next time!\n";
  70.     else if (result = -10)
  71.         cout << endl << "You beat the world's best AI! Congratulations!\n";
  72.     else
  73.         cout << endl << "The game has been drawn!" << endl;
  74.     return 0;
  75. }
  76.  
  77. void PrintBoard(array <char, 9> Pos)
  78. {
  79.     system("cls");
  80.     cout << setw(6) << "|" << setw(6) << "|" << endl << setw(3) << Pos[0] << setw(3) << "|" << setw(3) << Pos[1] << setw(3) << "|" << setw(3) << Pos[2] << "     TIC           TOE" << endl;
  81.     cout << "_____|_____|_____" << endl;
  82.     cout << setw(6) << "|" << setw(6) << "|" << endl << setw(3) << Pos[3] << setw(3) << "|" << setw(3) << Pos[4] << setw(3) << "|" << setw(3) << Pos[5] << "            TAC   " << endl;
  83.     cout << "_____|_____|_____" << endl;
  84.     cout << setw(6) << "|" << setw(6) << "|" << endl << setw(3) << Pos[6] << setw(3) << "|" << setw(3) << Pos[7] << setw(3) << "|" << setw(3) << Pos[8] << "     TIC            TOE  " << endl;
  85.     cout << "     |     |" << endl;
  86. }
  87.  
  88.  
  89. int programprogress()
  90. {
  91.     while (k == -11 && AI)
  92.     {
  93.         bool InvalidChoice = false;
  94.         char letter;
  95.         //player_number = (player_number % 2) ? 1 : 2;
  96.         int PlayerChoice;
  97.  
  98.         if (player_number == 2)
  99.         {
  100.             cout << endl << "What is your move?" << endl;
  101.             cin >> PlayerChoice;
  102.  
  103.             while ((PlayerChoice < 1) || (PlayerChoice > 9))
  104.             {
  105.                 cout << "That's an invalid choice! Please choose a number that is 1-9!" << endl;
  106.                 cin >> PlayerChoice;
  107.             }
  108.  
  109.             PlayerChoice--;
  110.             letter = (!first) ? 'X' : 'O';
  111.  
  112.             if (Pos[PlayerChoice] == '1' || Pos[PlayerChoice] == '2' || Pos[PlayerChoice] == '3' || Pos[PlayerChoice] == '4' || Pos[PlayerChoice] == '5' || Pos[PlayerChoice] == '6' || Pos[PlayerChoice] == '7' || Pos[PlayerChoice] == '8' || Pos[PlayerChoice] == '9')
  113.             {
  114.                 Pos[PlayerChoice] = letter;
  115.                 PrintBoard(Pos);
  116.             }
  117.             /*else
  118.             {
  119.                 cout << "That space is already taken!" << endl;
  120.                 player_number--;
  121.             }*/
  122.             k = checkwin(Pos);
  123.             if (k != -11)
  124.                 k = k * -10;
  125.             player_number = 1;
  126.         }
  127.         else
  128.         {
  129.             cout << endl << "The computer has made its move!" << endl;
  130.  
  131.             letter = (first) ? 'X' : 'O';
  132.             if (first)
  133.                 PlayerChoice = bestMove(Pos, 1);
  134.             else
  135.                 PlayerChoice = bestMove(Pos, 2);
  136.  
  137.             Pos[PlayerChoice] = letter;
  138.             PrintBoard(Pos);
  139.  
  140.             k = checkwin(Pos);
  141.             if (k != -11)
  142.                 k = k * 10;
  143.  
  144.             player_number = 2;
  145.         }
  146.     }
  147.  
  148.     while (k == -11 && !AI)
  149.     {
  150.         bool InvalidChoice = false;
  151.         char letter;
  152.         player_number = (player_number % 2) ? 1 : 2;
  153.         int PlayerChoice;
  154.  
  155.         cout << endl << "What's player " << player_number << "'s move?" << endl;
  156.         cin >> PlayerChoice;
  157.  
  158.         while ((PlayerChoice < 1) || (PlayerChoice > 9))
  159.         {
  160.             cout << "That's an invalid choice! Please choose a number that is 1-9!" << endl;
  161.             cin >> PlayerChoice;
  162.         }
  163.  
  164.         PlayerChoice--;
  165.         letter = (player_number == 1) ? 'X' : 'O';
  166.  
  167.         if (Pos[PlayerChoice] == '1' || Pos[PlayerChoice] == '2' || Pos[PlayerChoice] == '3' || Pos[PlayerChoice] == '4' || Pos[PlayerChoice] == '5' || Pos[PlayerChoice] == '6' || Pos[PlayerChoice] == '7' || Pos[PlayerChoice] == '8' || Pos[PlayerChoice] == '9')
  168.         {
  169.             Pos[PlayerChoice] = letter;
  170.             PrintBoard(Pos);
  171.         }
  172.         else
  173.         {
  174.             cout << "That space is already taken!" << endl;
  175.             player_number--;
  176.         }
  177.         k = checkwin(Pos);
  178.  
  179.         player_number++;
  180.     }
  181.     return k;
  182. }
  183.  
  184. int checkwin(array <char, SIZE> Pos)
  185. {
  186.     if (Pos[0] == Pos[1] && Pos[1] == Pos[2])
  187.  
  188.         return 1;
  189.     else if (Pos[3] == Pos[4] && Pos[4] == Pos[5])
  190.  
  191.         return 1;
  192.     else if (Pos[6] == Pos[7] && Pos[7] == Pos[8])
  193.  
  194.         return 1;
  195.     else if (Pos[0] == Pos[3] && Pos[3] == Pos[6])
  196.  
  197.         return 1;
  198.     else if (Pos[1] == Pos[4] && Pos[4] == Pos[7])
  199.  
  200.         return 1;
  201.     else if (Pos[2] == Pos[5] && Pos[5] == Pos[8])
  202.  
  203.         return 1;
  204.     else if (Pos[0] == Pos[4] && Pos[4] == Pos[8])
  205.  
  206.         return 1;
  207.     else if (Pos[2] == Pos[4] && Pos[4] == Pos[6])
  208.  
  209.         return 1;
  210.     else if (Pos[0] != '1' && Pos[1] != '2' && Pos[2] != '3'
  211.         && Pos[3] != '4' && Pos[4] != '5' && Pos[5] != '6'
  212.         && Pos[6] != '7' && Pos[7] != '8' && Pos[8] != '9')
  213.  
  214.         return 0;
  215.     else
  216.         return -11;
  217. }
  218.  
  219. int minimax(array <char, SIZE> newpos, int depth, int player, bool opp)
  220. {
  221.     int scale = 0;
  222.  
  223.     if ((player == 1 && first) || (player == 2 && !first))
  224.         scale = 10;
  225.     else
  226.         scale = -10;
  227.     //cout << scale;
  228.     int score = scale*checkwin(newpos);
  229.  
  230.     if (score < 0)
  231.         score += depth;
  232.     else if (score > 0)
  233.         score -= depth;
  234.  
  235.     if (score == -10 || score == 10 || score == 0)
  236.         return score;
  237.  
  238.     if (opp)
  239.     {
  240.         int best = -1000;
  241.  
  242.         for (int i = 0; i < SIZE; i++)
  243.         {
  244.             if (newpos[i] != 'X' && newpos[i] != 'O')
  245.             {
  246.                 char temp = newpos[i];
  247.  
  248.                 newpos[i] = PlayerSymbol(player);
  249.  
  250.                 best = max(best, minimax(newpos, depth + 1, Opposite(player), !opp));
  251.  
  252.                 newpos[i] = temp;
  253.             }
  254.         }
  255.         return best;
  256.     }
  257.    
  258.     else
  259.     {
  260.         int best = 1000;
  261.  
  262.         for (int i = 0; i < SIZE; i++)
  263.         {
  264.             if (newpos[i] != 'X' && newpos[i] != 'O')
  265.             {
  266.                 char temp = newpos[i];
  267.  
  268.                 newpos[i] = PlayerSymbol(player);
  269.  
  270.                 best = min(best, minimax(newpos, depth + 1, Opposite(player), !opp));
  271.  
  272.                 newpos[i] = temp;
  273.             }
  274.         }
  275.         return best;
  276.     }
  277.  
  278. }
  279.  
  280. int bestMove(array <char, SIZE> newpos, int player)
  281. {
  282.     int best = -1000;
  283.     int bestpos = -1;
  284.  
  285.     for (int i = 0; i < SIZE; i++)
  286.     {
  287.         if (newpos[i] != 'X' && newpos[i] != 'O')
  288.         {
  289.             char temp = newpos[i];
  290.  
  291.             newpos[i] = PlayerSymbol(player);
  292.  
  293.             int move = minimax(newpos, 0, player, false);
  294.  
  295.             newpos[i] = temp;
  296.  
  297.             if (move > best)
  298.             {
  299.                 //cout << "I like pineapple on pizza" << endl;
  300.                 bestpos = i;
  301.                 best = move;
  302.             }
  303.             /*if (move == best)
  304.             {
  305.                 cout << "I like pineapple on pizza" << endl;
  306.             }*/
  307.         }
  308.     }
  309.     cout << bestpos;
  310.     return bestpos;
  311. }
  312.  
  313. int Opposite(int x)
  314. {
  315.     if (x == 1)
  316.         return 2;
  317.     else
  318.         return 1;
  319. }
  320.  
  321. char PlayerSymbol(int x)
  322. {
  323.     if (x == 1)
  324.         return 'X';
  325.     else
  326.         return 'O';
  327. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top