Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <vector>
- #include <iostream>
- using namespace std;
- /// @brief Draws the provided tic-tac-toe board to the screen
- // @param board is the tic-tac-toe board that should be drawn
- void drawBoard(const vector < char >&gameBoard) {
- for (int i = 0; i < 9; i += 3) {
- cout << " " << gameBoard.at(i) << " | " << gameBoard.at(i + 1) << " | "
- << gameBoard.at(i + 2) << " " << endl;
- if (i < 6) {
- cout << "-----|-----|-----" << endl;
- }
- }
- cout << endl;
- return;
- }
- /// @brief Fills vector with characters starting at lower case a.
- ///
- /// If the vector is size 3 then it will have characters a to c.
- /// If the vector is size 5 then it will have characters a to e.
- /// If the vector is size 26 then it will have characters a to z.
- ///
- /// @param v the vector to initialize
- /// @pre-condition the vector size will never be over 26
- void initVector(vector <char> &v) {
- char c = 'a';
- for (int i = 0; i < 9; i++)
- {
- v.at(i) = c;
- c += 1;
- }
- return;
- }
- /// @brief Converts a character representing a cell to associated vector index
- /// @param the position to be converted to a vector index
- /// @return the integer index in the vector, should be 0 to (vector size - 1)
- int convertPosition(char boardPosition) {
- if (boardPosition == 'a')
- {
- return 0;
- }
- else if (boardPosition == 'b')
- {
- return 1;
- }
- else if (boardPosition == 'c')
- {
- return 2;
- }
- else if (boardPosition == 'd')
- {
- return 3;
- }
- else if (boardPosition == 'e')
- {
- return 4;
- }
- else if (boardPosition == 'f')
- {
- return 5;
- }
- else if (boardPosition == 'g')
- {
- return 6;
- }
- else if (boardPosition == 'h')
- {
- return 7;
- }
- else if (boardPosition == 'i')
- {
- return 8;
- }
- return -1;
- }
- /// @brief Predicate function to determine if a spot in board is available.
- /// @param board the current tic-tac-toe board
- /// @param position is an index into vector to check if available
- /// @return true if position's state is available (not marked) AND is in bounds
- bool validPlacement(const vector<char> &gameBoard, int positionIndex) {
- if (gameBoard.at(positionIndex) != 'X' && gameBoard.at(positionIndex) != 'O' && positionIndex < 9)
- {
- return true;
- }
- return false;
- }
- /// @brief Acquires a play from the user as to where to put her mark
- ///
- /// Utilizes convertPosition and validPlacement functions to convert the
- /// user input and then determine if the converted input is a valid play.
- ///
- /// @param board the current tic-tac-toe board
- /// @return an integer index in board vector of a chosen available board spot
- int getPlay(const vector<char> &gameBoard) {
- // TODO: implement function
- char boardPosition = ' ';
- cout << "Please choose a position: " << endl;
- cin >> boardPosition;
- if (validPlacement(gameBoard, convertPosition(boardPosition)) == false)
- {
- while (validPlacement(gameBoard, convertPosition(boardPosition)) == false)
- {
- cout << "Please choose a position: " << endl;
- cin >> boardPosition;
- }
- }
- return convertPosition(boardPosition);
- return -1;
- }
- /// @brief Predicate function to determine if the game has been won
- ///
- /// Winning conditions in tic-tac-toe require three marks from same
- /// player in a single row, column or diagonal.
- ///
- /// @param board the current tic-tac-toe board
- /// @return true if the game has been won, false otherwise
- bool gameWon(const vector<char> &gameBoard) {
- if(gameBoard.at(0) == 'X' && gameBoard.at(3) == 'X' && gameBoard.at(6) == 'X')
- {
- return true;
- }
- else if(gameBoard.at(0) == 'O' && gameBoard.at(3) == 'O' && gameBoard.at(6) == 'O')
- {
- return true;
- }
- else if(gameBoard.at(1) == 'X' && gameBoard.at(4) == 'X' && gameBoard.at(7) == 'X')
- {
- return true;
- }
- else if(gameBoard.at(1) == 'O' && gameBoard.at(4) == 'O' && gameBoard.at(7) == 'O')
- {
- return true;
- }
- else if(gameBoard.at(2) == 'X' && gameBoard.at(5) == 'X' && gameBoard.at(8) == 'X')
- {
- return true;
- }
- else if(gameBoard.at(2) == 'O' && gameBoard.at(5) == 'O' && gameBoard.at(8) == 'O')
- {
- return true;
- }
- else if(gameBoard.at(2) == 'X' && gameBoard.at(5) == 'X' && gameBoard.at(8) == 'X')
- {
- return true;
- }
- else if(gameBoard.at(0) == 'X' && gameBoard.at(1) == 'X' && gameBoard.at(2) == 'X')
- {
- return true;
- }
- else if(gameBoard.at(0) == 'O' && gameBoard.at(1) == 'O' && gameBoard.at(2) == 'O')
- {
- return true;
- }
- else if(gameBoard.at(3) == 'X' && gameBoard.at(4) == 'X' && gameBoard.at(5) == 'X')
- {
- return true;
- }
- else if(gameBoard.at(3) == 'O' && gameBoard.at(4) == 'O' && gameBoard.at(5) == 'O')
- {
- return true;
- }
- else if(gameBoard.at(6) == 'X' && gameBoard.at(7) == 'X' && gameBoard.at(8) == 'X')
- {
- return true;
- }
- else if(gameBoard.at(6) == 'O' && gameBoard.at(7) == 'O' && gameBoard.at(8) == 'O')
- {
- return true;
- }
- else if(gameBoard.at(0) == 'X' && gameBoard.at(4) == 'X' && gameBoard.at(8) == 'X')
- {
- return true;
- }
- else if(gameBoard.at(0) == 'O' && gameBoard.at(4) == 'O' && gameBoard.at(8) == 'O')
- {
- return true;
- }
- else if(gameBoard.at(2) == 'X' && gameBoard.at(4) == 'X' && gameBoard.at(6) == 'X')
- {
- return true;
- }
- else if(gameBoard.at(2) == 'O' && gameBoard.at(4) == 'O' && gameBoard.at(6) == 'O')
- {
- return true;
- }
- return false;
- }
- /// @brief Predicate function to determine if the board is full
- /// @param board the current tic-tac-toe board
- /// @return true iff the board is full (no cell is available)
- bool boardFull(const vector<char> &gameBoard) {
- // TODO: implement function
- int full = 0;
- for (int i = 0; i < 9; i++)
- {
- if (gameBoard.at(i) == 'X' || gameBoard.at(i) == 'O')
- {
- full += 1;
- }
- }
- if (full == 9)
- {
- return true;
- }
- return false;
- }
- // Global constants for player representation
- const int PLAYER1 = 0;
- const int PLAYER2 = 1;
- int main() {
- // Variables that you may find useful to utilize
- vector<char> gameBoard(9);
- int curPlay;
- int whosTurn = PLAYER1; // Player 1 always goes first and is 'X'
- /// TODO: Initialize board to empty state
- initVector(gameBoard);
- /// TODO: Display empty board
- drawBoard(gameBoard);
- /// TODO: Play until game is over
- while(gameWon(gameBoard) == false || boardFull(gameBoard) == false)
- {
- /// TODO: Get a play
- if (whosTurn == PLAYER1)
- {
- curPlay = getPlay(gameBoard);
- gameBoard.at(curPlay) = 'X';
- whosTurn = PLAYER2;
- drawBoard(gameBoard);
- if (gameWon(gameBoard) == true)
- {
- break;
- }
- }
- else if (whosTurn == PLAYER2)
- {
- curPlay = getPlay(gameBoard);
- gameBoard.at(curPlay) = 'O';
- whosTurn = PLAYER1;
- drawBoard(gameBoard);
- if (gameWon(gameBoard) == true)
- {
- break;
- }
- }
- /// TODO: Set the play on the board
- /// TODO: Switch the turn to the other player
- /// TODO: Output the updated board
- }
- /// TODO: Determine winner and output appropriate message
- if (whosTurn == PLAYER1)
- {
- cout << "PLAYER 2 WINS!" << endl;
- }
- else if (whosTurn == PLAYER2)
- {
- cout << "PLAYER 1 WINS!" << endl;
- }
- else
- {
- cout << "No one wins" << endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement