Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "myLibraryBMR.h"
- using namespace std;
- enum gamePiece { X_PIECE='X', O_PIECE='O', EMPTY_PIECE = ' '};
- enum gameState { X_WINS=1, O_WINS=2, TIE=3, NOT_OVER=4 };
- void displayRules();
- void displayGame(gamePiece[], const int);
- enum gamePiece whoFirst();
- int getHumanMove();
- int getComputerMove(gamePiece[], const int);
- enum gameState whoWins(gamePiece[], const int);
- int main()
- {
- gamePiece gameBoard[9];
- gamePiece computer, human;
- int choice;
- const int gameBoardSize = 9;
- int keepGoing;
- while(1)//allows game to be played until exit
- {
- displayRules();
- keepGoing = 4;
- for (int i = 0; i < gameBoardSize; i++)
- {
- gameBoard[i] = EMPTY_PIECE;
- } //intializes gameBoard array to black space
- human = whoFirst();
- if (human == X_PIECE)
- {
- computer = O_PIECE; //if human is X_PIECE then computer assigned O
- }
- else
- {
- computer = X_PIECE; //if human not X_PIECE assigns computer X.
- } //human already assigned O at this point
- do
- {
- if (human == X_PIECE && keepGoing >TIE )//if human is first it uses this sequence
- {
- displayGame(gameBoard, gameBoardSize);
- choice = getHumanMove();
- gameBoard[choice] = human;
- keepGoing=whoWins(gameBoard, gameBoardSize);
- //human cycle over
- if (keepGoing > TIE)
- {
- displayGame(gameBoard, gameBoardSize);
- choice = getComputerMove(gameBoard, gameBoardSize);
- gameBoard[choice] = computer;
- keepGoing = whoWins(gameBoard, gameBoardSize);
- //computer cycle over
- }
- }
- else if (computer == X_PIECE && keepGoing > TIE)//if computer is first it uses this sequence
- {
- displayGame(gameBoard, gameBoardSize);
- choice = getComputerMove(gameBoard, gameBoardSize);
- gameBoard[choice] = computer;
- keepGoing = whoWins(gameBoard, gameBoardSize);
- //computer cycle over
- if (keepGoing > TIE) {
- displayGame(gameBoard, gameBoardSize);
- choice = getHumanMove();
- gameBoard[choice] = human;
- keepGoing = whoWins(gameBoard, gameBoardSize);
- //human cycle over
- }
- }
- } while (keepGoing > TIE); //if greater than tie should always keep going
- switch (keepGoing)
- {
- case 1:
- cout << "\n\nX WINS!\n \n\nX WINS!\n \n\nX WINS!\n\n";
- displayGame(gameBoard, gameBoardSize);
- break;
- case 2:
- cout << "\n\nO WINS!\n \n\nO WINS!\n \n\nO WINS!\n\n";
- displayGame(gameBoard, gameBoardSize);
- break;
- case 3:
- cout << "\n\nIt's a Tie, better luck next time.\n";
- displayGame(gameBoard, gameBoardSize);
- break;
- }
- cout << "\nPress any key to play again.\n";
- system("pause");
- }
- system("pause");
- return 0;
- }
- void displayRules()
- {
- cout << "\nWelcome to the game of Tic-Tac-Toe!";
- cout << "\n\nMake your move known by entering a number, 0-8.";
- cout << "The number \ncorresponds to the desired board position as shown below: \n\n";
- for (int i = 0; i < 9; i++)
- {
- cout << i;
- if (i == 0 || i == 1 || i == 3 || i == 4 || i == 6 || i == 7)
- {
- cout << " | ";
- }
- if (i == 2 || i == 5)
- {
- cout << "\n----------\n";
- }
- }
- cout << "\n\n";
- }
- void displayGame(gamePiece game[], const int size) //passes this function gameBoard array and size of the array
- {
- for (int i = 0; i < 9; i++)
- {
- cout << (char)game[i]; //cast enums to X, O, Blank space
- if (i == 0 || i == 1 || i == 3 || i == 4 || i == 6 || i == 7)
- {
- cout << " | ";
- }
- if (i == 2 || i == 5)
- {
- cout << "\n----------\n";
- }
- }
- cout << "\n\n";
- }
- gamePiece whoFirst()
- {
- cout << "\n\Would you like to go first? Enter Y for yes or N for no. ";
- char move = readChar(); //checks for valid char, function from fund.1
- if (move == 'Y')
- {
- return X_PIECE; //based on y or n they will be assigned to X or O
- }
- else
- {
- return O_PIECE;
- }
- }
- int getHumanMove()
- {
- cout << "\nWhere will you move? (0-8): ";
- return readInt("your choice", 0, 8);
- }
- int getComputerMove(gamePiece game[], const int size)
- {
- int choice;
- const int BEST_MOVES[] = { 4, 0, 2, 6, 8, 1, 3, 5, 7 };
- for (int i = 0; i < 9; i++)
- {
- choice = BEST_MOVES[i];
- if(game[choice]==EMPTY_PIECE) //checks if square chosen is empty returns choice if true
- {
- cout << "You cannot out smart me. I choose square " << choice << endl << endl;
- return choice;
- }
- }
- }
- gameState whoWins(gamePiece game[], const int size)
- { //checks all winning combos
- if (game[0] == X_PIECE && game[1] == X_PIECE && game[2] == X_PIECE ||
- game[3] == X_PIECE && game[4] == X_PIECE && game[5] == X_PIECE ||
- game[6] == X_PIECE && game[7] == X_PIECE && game[8] == X_PIECE ||
- game[0] == X_PIECE && game[3] == X_PIECE && game[6] == X_PIECE ||
- game[1] == X_PIECE && game[4] == X_PIECE && game[7] == X_PIECE ||
- game[2] == X_PIECE && game[5] == X_PIECE && game[8] == X_PIECE ||
- game[0] == X_PIECE && game[4] == X_PIECE && game[8] == X_PIECE ||
- game[2] == X_PIECE && game[4] == X_PIECE && game[6] == X_PIECE)
- {
- return X_WINS;
- }
- else if (game[0] == O_PIECE && game[1] == O_PIECE && game[2] == O_PIECE ||
- game[3] == O_PIECE && game[4] == O_PIECE && game[5] == O_PIECE ||
- game[6] == O_PIECE && game[7] == O_PIECE && game[8] == O_PIECE ||
- game[0] == O_PIECE && game[3] == O_PIECE && game[6] == O_PIECE ||
- game[1] == O_PIECE && game[4] == O_PIECE && game[7] == O_PIECE ||
- game[2] == O_PIECE && game[5] == O_PIECE && game[8] == O_PIECE ||
- game[0] == O_PIECE && game[4] == O_PIECE && game[8] == O_PIECE ||
- game[2] == O_PIECE && game[4] == O_PIECE && game[6] == O_PIECE)
- {
- return O_WINS;
- }
- for (int i = 0; i < 9; i++)
- {
- if (game[i]==EMPTY_PIECE)
- {
- return NOT_OVER;
- }
- }
- //if all prior fail it must be a tie
- return TIE;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement