Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Assignment Specifications implement a console version of the game tic-tac-toe in

ID: 3791180 • Letter: A

Question

Assignment Specifications

implement a console version of the game tic-tac-toe in c++. For this assignment, we are providing an initial source code file which contains skeleton code that you must complete. We also provide complete functions for you to utilize. You are not allowed to change the provided functions and you are not allowed to change the headers of the provided function stubs.

For the functions you must implement, we have provided only a stub. A stub is a function definition that compiles, but does not yet implement the complete specifications for that function. As you develop the program, you should implement each function one at a time and test each as you go.

Implementation Strategies

We provide some variables and two global constants for you to utilize.

We provide string literals for winning or tie game output in comments with provided file

We have also provided comments to help you develop the necessary algorithm for 2 users playing the game of tic-tac-toe on a computer. Use these comments along with the function descriptions below to help you develop your program. One or more lines of your code should exist below each comment. Remove the TODO part when you have completed that step.

DO NOT try to implement the entire game at once. Instead, implement one behavior at a time, only developing one particular function at a time. Functions are listed below.

We highly recommend you unit test the function you are currently developing. You should understand how to walk through your code by hand as well as executing it in unit tests.

Use one of the following statements when stating who won:

Player 1 (x's) wins!

Player 2 (o's) wins!

No one wins

Functions

/// @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)

/// @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 position)

/// @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> &board, int position)

/// @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> &board)

/// @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> &board)

/// @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> &board)

Tie Game Example (user input is bolded and underlined for emphasis)

c

a | b | c

-----|-----|-----

d | e | f

-----|-----|-----

g | h | i

Please choose a position: a

c

x | b | c

-----|-----|-----

d | e | f

-----|-----|-----

g | h | i

Please choose a position: e

c

x | b | c

-----|-----|-----

d | o | f

-----|-----|-----

g | h | i

Please choose a position: e

Please choose a position: b

c

x | x | c

-----|-----|-----

d | o | f

-----|-----|-----

g | h | i

Please choose a position: c

c

x | x | o

-----|-----|-----

d | o | f

-----|-----|-----

g | h | i

Please choose a position: g

c

x | x | o

-----|-----|-----

d | o | f

-----|-----|-----

x | h | i

Please choose a position: d

c

x | x | o

-----|-----|-----

o | o | f

-----|-----|-----

x | h | i

Please choose a position: f

c

x | x | o

-----|-----|-----

o | o | x

-----|-----|-----

x | h | i

Please choose a position: h

c

x | x | o

-----|-----|-----

o | o | x

-----|-----|-----

x | o | i

Please choose a position: i

c

x | x | o

-----|-----|-----

o | o | x

-----|-----|-----

x | o | x

No one wins

USE THIS FUNCTION TEMPLATE:

Player 1 (x's) wins!

Player 2 (o's) wins!

No one wins

Explanation / Answer

// C++ code
#include <iostream>
#include <fstream>
#include <cctype>
#include <cstring>
#include <iomanip>
#include <limits.h>
#include <cmath>
#include <algorithm>
#include <vector>

using namespace std;


const bool CLEAR_SCREEN = true;
/// @brief Utilizes an escape character sequence to clear the screen
void clearScreen()
{
cout << endl;
if (CLEAR_SCREEN)
{
cout << "c";
}
cout << endl;
}
/// @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> &board)
{
clearScreen();
for (int i = 0; i < 9; i += 3)
{
cout << " " << board.at(i) << " | " << board.at(i + 1) << " | "
<< board.at(i + 2) << " " << endl;
if (i < 6)
cout << "-----|-----|-----" << endl;
}
cout << endl;
}

/// @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)
{
for(int i = 0; i < v.size(); i++)
{
v.at(i) = 'a' + i;
}
}
/// @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 position)
{
int index;
index = position - 'a';
return index;
}
/// @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> &board, int position)
{
if(board.size() == 0 && position == 0)
{
return true;
}
if(position >= board.size() || position < 0)
{
return false;
}
if(board.at(position) == 'x' || board.at(position) == 'o')
{
return false;
}
  
return true;
}

/// @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> &board)
{
char p;
int pos;
cout << "Please choose a position: ";
cin >> p;
cout << endl;
  
pos = convertPosition(p);
  
while(!validPlacement(board, pos))
{
cout << "Please choose a position: ";
cin>> p;
cout << endl;
pos = convertPosition(p);
}
return pos;
}
/// @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> &board)
{
if(board.at(0) == board.at(1) && board.at(1) == board.at(2))
return true;
else if(board.at(3) == board.at(4) && board.at(4) == board.at(5))
return true;
else if(board.at(6) == board.at(7) && board.at(7) == board.at(8))
return true;
else if(board.at(0) == board.at(3) && board.at(3) == board.at(6))
return true;
else if(board.at(1) == board.at(4) && board.at(4) == board.at(7))
return true;
else if(board.at(2) == board.at(5) && board.at(5) == board.at(8))
return true;
else if(board.at(0) == board.at(4) && board.at(4) == board.at(8))
return true;
else if(board.at(2) == board.at(4) && board.at(4) == board.at(6))
return true;
else
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> &board)
{
int k = 0;
for(int i = 0; i < board.size(); i++)
{
if(board.at(i) == 'x' || board.at(i) == 'o')
{
k++;
}
}
if(k == board.size())
{
return true;
}
return false;
}

// Global constants for player representation
const int PLAYER1 = 0; // PLAYER1 is always first with 'x'
const int PLAYER2 = 1; // PLAYER2 is 'o'
int main()
{
vector<char> board(9);
int curPlay;
int turn = PLAYER1;
  
initVector(board);
drawBoard(board);
cout << endl;
  
  
while(!gameWon(board) && !boardFull(board))
{
curPlay = getPlay(board);
if(turn == PLAYER1)
{
board.at(curPlay) = 'x';
drawBoard(board);
cout << endl;
turn = PLAYER2;
}
else if (turn == PLAYER2)
{
board.at(curPlay) = 'o';
drawBoard(board);
cout << endl;
turn = PLAYER1;
}
}
  
if(!gameWon(board))
{
cout << "No one wins" << endl;
}
else // gameWon(board)
{
if(turn == PLAYER2)
{
cout << "Player 1 (x's) wins!" << endl;
}
else // turn == PLAYER1
{
cout << "Player 2 (o's) wins!" << endl;
}
}
  
return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote