Advertisement
FFFFFFFFFFFFFFFFFFF

Tic-Tac-Toe#1

Nov 20th, 2018
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.69 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2.  
  3. #include <iostream>
  4. #include <vector>
  5. #include <string>
  6. #include <algorithm>
  7.  
  8. using namespace std;
  9.  
  10. const char X = 'X';
  11. const char O = 'O';
  12. const char TIE = 'T';
  13. const char EMPTY = ' ';
  14. const char NO_ONE = 'N';
  15.  
  16. void instructions();
  17. void announceWinner(char winner, char& human, char& computer);
  18. void displayBoard(const vector<char>& board);
  19. bool isLegal(const vector<char>& board, const int& move);
  20. int humanChoice(const vector<char>& board);
  21. int computerChoice(vector<char>& board, const char& computer);
  22. char detWinner(const vector<char>& board);
  23. char choice();
  24.  
  25.  
  26. int main()
  27. {
  28.     int move = 0;
  29.     char computer = O; char human = X; char turn;
  30.     const unsigned int NUM_SQUARES = 9;
  31.     vector<char> board(NUM_SQUARES, EMPTY);
  32.     instructions();
  33.     turn = choice(); //returns who wil be first
  34.  
  35.     //the main cycle of game
  36.     while (detWinner(board) == NO_ONE) //while no a winner
  37.     {
  38.         if (turn == human)//humans move
  39.         {
  40.             move = humanChoice(board);
  41.             board[move] = human;//check a correction and returns a move
  42.         }
  43.         else//computers move
  44.         {
  45.             move = computerChoice(board, computer);
  46.             board[move] = computer;
  47.         }
  48.         turn == human ? turn = computer : turn = human;
  49.         displayBoard(board);
  50.     }
  51.     announceWinner(detWinner(board), human, computer);
  52.    
  53.     system("pause");
  54.     return 0;
  55. }
  56.  
  57. int humanChoice(const vector<char>& board)
  58. {
  59.     unsigned int choice;
  60.     cout << "Your turn. Enter your choice: ";
  61.     cin >> choice;
  62.     if (!isLegal(board, choice))
  63.     {
  64.         cout << "Incorrect choice, try again.\n";
  65.         humanChoice(board);
  66.     }
  67.     cout << "\nAlright..\n";
  68.     return choice;
  69. }
  70.  
  71. int computerChoice(vector<char>& board, const char& computer)
  72. {
  73.     unsigned int choice = -1;
  74.     unsigned int move = 0;
  75.     char human;
  76.     const int BEST_SQUARES[] = {4, 0, 2, 6, 8, 1, 3, 5, 7};
  77.  
  78.     computer == X ? human = O : human = X;
  79.     //if computer can win
  80.     while (move < board.size() && choice == -1)
  81.     {
  82.         if (isLegal(board, move))
  83.         {
  84.             board[move] = computer;
  85.             if (computer == detWinner(board))
  86.                 choice = move;
  87.             board[move] = EMPTY;
  88.         }
  89.         move++;
  90.     }
  91.  
  92.     move = 0;
  93.     //if human can win
  94.     while (move < board.size() && choice == -1)
  95.     {
  96.         if (isLegal(board, move))
  97.         {
  98.             board[move] = human;
  99.             if (human == detWinner(board))
  100.                 choice = move;
  101.             board[move] = EMPTY;
  102.         }
  103.         move++;
  104.     }
  105.     move = 0;
  106.     //make a move to the best empty square
  107.     while (move < board.size() && choice == -1)
  108.     {
  109.         if (isLegal(board, BEST_SQUARES[move]))
  110.             choice = BEST_SQUARES[move];
  111.         move++;
  112.     }
  113.  
  114.     return choice;
  115. }
  116.  
  117. void instructions()
  118. {
  119.     cout << "Welcome to the ultimate man-machine showdown: Tic-Tac-Toe.\n";
  120.     cout << "--where human brain is pit against silicon processor\n\n";
  121.     cout << "Make your move known by entering a number, 0 - 8. The number ";
  122.     cout << "corresponds to the desired board position, as illustrated:\n";
  123.     cout << "\t0 | 1 | 2\n";
  124.     cout << "\t---------\n";
  125.     cout << "\t3 | 4 | 5\n";
  126.     cout << "\t---------\n";
  127.     cout << "\t6 | 7 | 8\n\n";
  128.     cout << "Prepare yourself, human. The battle is about to begin.\n\n";
  129. }
  130.  
  131. void announceWinner(char winner, char& human, char& computer)
  132. {
  133.     char playAgain;
  134.     if (winner == human)
  135.         cout << "\nCongratulations! You won me.\n";
  136.     else if (winner == computer)
  137.         cout << "\nHaha, I won\n";
  138.     else
  139.         cout << "\nSo, that's tie.\n";
  140.     cout << "\nDo you want to play yet? (y/n): ";
  141.     cin >> playAgain;
  142.  
  143.     if (playAgain == 'y')
  144.     {
  145.         system("cls");
  146.         main();
  147.     }
  148.     cout << "\nSee you next time!\n";
  149. }
  150.  
  151. void displayBoard(const vector<char>& board)
  152. {
  153.     cout << "\n\t" << board[0] << " | " << board[1] << " | " << board[2] << "\n";
  154.     cout << "\t---------\n";
  155.     cout << "\t" << board[3] << " | " << board[4] << " | " << board[5] << "\n";
  156.     cout << "\t---------\n";
  157.     cout << "\t" << board[6] << " | " << board[7] << " | " << board[8] << "\n\n";
  158. }
  159.  
  160. bool isLegal(const vector<char>& board, const int& move)
  161. {
  162.     if (board[move] == EMPTY)
  163.         return true;
  164.     else
  165.         return false;
  166. }
  167.  
  168. char detWinner(const vector<char>& board)
  169. {
  170.     const int WIN_POSITION[8][3] = {{0, 1 , 2}, {3, 4, 5}, {6, 7, 8}, {0, 3, 6}, {1, 4, 7}, {2, 5, 8}, {2, 4, 6}, {0, 4, 8}};
  171.  
  172.     for (int i = 0; i < 8; ++i)
  173.     {
  174.         if (board[WIN_POSITION[i][0]] == board[WIN_POSITION[i][1]]
  175.             && board[WIN_POSITION[i][1]] == board[WIN_POSITION[i][2]]
  176.             && board[WIN_POSITION[i][0]] != EMPTY)
  177.         {
  178.             return board[WIN_POSITION[i][0]];
  179.         }
  180.  
  181.         if (count(board.begin(), board.end(), EMPTY) == 0)
  182.             return TIE;
  183.     }
  184.     return NO_ONE;
  185. }
  186.  
  187. char choice()
  188. {
  189.     char yesNo;
  190.     cout << "\nDo you require the first move? (y/n): ";
  191.     cin >> yesNo;
  192.  
  193.     if (yesNo == 'y')
  194.         return X;
  195.     else if (yesNo == 'n')
  196.         return O;
  197.     else
  198.     {
  199.         cout << "\nYou should enter only 'y' or 'n'\n";
  200.         return choice();
  201.     }
  202. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement