Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _CRT_SECURE_NO_WARNINGS
- #include <iostream>
- #include <vector>
- #include <string>
- #include <algorithm>
- using namespace std;
- const char X = 'X';
- const char O = 'O';
- const char TIE = 'T';
- const char EMPTY = ' ';
- const char NO_ONE = 'N';
- void instructions();
- void announceWinner(char winner, char& human, char& computer);
- void displayBoard(const vector<char>& board);
- bool isLegal(const vector<char>& board, const int& move);
- int humanChoice(const vector<char>& board);
- int computerChoice(vector<char>& board, const char& computer);
- char detWinner(const vector<char>& board);
- char choice();
- int main()
- {
- int move = 0;
- char computer = O; char human = X; char turn;
- const unsigned int NUM_SQUARES = 9;
- vector<char> board(NUM_SQUARES, EMPTY);
- instructions();
- turn = choice(); //returns who wil be first
- //the main cycle of game
- while (detWinner(board) == NO_ONE) //while no a winner
- {
- if (turn == human)//humans move
- {
- move = humanChoice(board);
- board[move] = human;//check a correction and returns a move
- }
- else//computers move
- {
- move = computerChoice(board, computer);
- board[move] = computer;
- }
- turn == human ? turn = computer : turn = human;
- displayBoard(board);
- }
- announceWinner(detWinner(board), human, computer);
- system("pause");
- return 0;
- }
- int humanChoice(const vector<char>& board)
- {
- unsigned int choice;
- cout << "Your turn. Enter your choice: ";
- cin >> choice;
- if (!isLegal(board, choice))
- {
- cout << "Incorrect choice, try again.\n";
- humanChoice(board);
- }
- cout << "\nAlright..\n";
- return choice;
- }
- int computerChoice(vector<char>& board, const char& computer)
- {
- unsigned int choice = -1;
- unsigned int move = 0;
- char human;
- const int BEST_SQUARES[] = {4, 0, 2, 6, 8, 1, 3, 5, 7};
- computer == X ? human = O : human = X;
- //if computer can win
- while (move < board.size() && choice == -1)
- {
- if (isLegal(board, move))
- {
- board[move] = computer;
- if (computer == detWinner(board))
- choice = move;
- board[move] = EMPTY;
- }
- move++;
- }
- move = 0;
- //if human can win
- while (move < board.size() && choice == -1)
- {
- if (isLegal(board, move))
- {
- board[move] = human;
- if (human == detWinner(board))
- choice = move;
- board[move] = EMPTY;
- }
- move++;
- }
- move = 0;
- //make a move to the best empty square
- while (move < board.size() && choice == -1)
- {
- if (isLegal(board, BEST_SQUARES[move]))
- choice = BEST_SQUARES[move];
- move++;
- }
- return choice;
- }
- void instructions()
- {
- cout << "Welcome to the ultimate man-machine showdown: Tic-Tac-Toe.\n";
- cout << "--where human brain is pit against silicon processor\n\n";
- cout << "Make your move known by entering a number, 0 - 8. The number ";
- cout << "corresponds to the desired board position, as illustrated:\n";
- cout << "\t0 | 1 | 2\n";
- cout << "\t---------\n";
- cout << "\t3 | 4 | 5\n";
- cout << "\t---------\n";
- cout << "\t6 | 7 | 8\n\n";
- cout << "Prepare yourself, human. The battle is about to begin.\n\n";
- }
- void announceWinner(char winner, char& human, char& computer)
- {
- char playAgain;
- if (winner == human)
- cout << "\nCongratulations! You won me.\n";
- else if (winner == computer)
- cout << "\nHaha, I won\n";
- else
- cout << "\nSo, that's tie.\n";
- cout << "\nDo you want to play yet? (y/n): ";
- cin >> playAgain;
- if (playAgain == 'y')
- {
- system("cls");
- main();
- }
- cout << "\nSee you next time!\n";
- }
- void displayBoard(const vector<char>& board)
- {
- cout << "\n\t" << board[0] << " | " << board[1] << " | " << board[2] << "\n";
- cout << "\t---------\n";
- cout << "\t" << board[3] << " | " << board[4] << " | " << board[5] << "\n";
- cout << "\t---------\n";
- cout << "\t" << board[6] << " | " << board[7] << " | " << board[8] << "\n\n";
- }
- bool isLegal(const vector<char>& board, const int& move)
- {
- if (board[move] == EMPTY)
- return true;
- else
- return false;
- }
- char detWinner(const vector<char>& board)
- {
- 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}};
- for (int i = 0; i < 8; ++i)
- {
- if (board[WIN_POSITION[i][0]] == board[WIN_POSITION[i][1]]
- && board[WIN_POSITION[i][1]] == board[WIN_POSITION[i][2]]
- && board[WIN_POSITION[i][0]] != EMPTY)
- {
- return board[WIN_POSITION[i][0]];
- }
- if (count(board.begin(), board.end(), EMPTY) == 0)
- return TIE;
- }
- return NO_ONE;
- }
- char choice()
- {
- char yesNo;
- cout << "\nDo you require the first move? (y/n): ";
- cin >> yesNo;
- if (yesNo == 'y')
- return X;
- else if (yesNo == 'n')
- return O;
- else
- {
- cout << "\nYou should enter only 'y' or 'n'\n";
- return choice();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement