Implement a tic-tac-toe game.
Currently, if the variables provided in main are commented out, the program will compile. Complete the specifications for each function. As you develop the program, implement one function at a time, and test that function. The provided comments provide hints as to what each function should do and when each should be called. Add variables where you see fit.
Implementation Strategies:
The template has variables and two global constants for you to utilize.
The template has comments to help you develop the necessary algorithm for 2 users playing tic-tac-toe on a computer. Use these comments along with the function descriptions (comments) to help 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, develop and test one function at a time. Understand how to walk through your code by hand as well as executing it in unit tests.
The system will test your functions as you attempt to complete each. In other words, submit as you go. Use the submission feedback and walk through your code to fix problems.
Submit and achieve a full score on each function before implementing the main game algorithm.
Suggested order of completion:
initVector: Define, test, and submit this function before moving on. (Should now have 2 points.)
Now call initVector and drawBoard functions where told to in main (before getting first play) to see your first board output. Submit. (Should now have 4 points.)
convertPosition: You'll need this to get the first play, so define, test, and submit this one before tackling getPlay function. (Should now have 6 points.)
validPlacement: Again, you'll need this to get the first play, so define, test, and submit this one before tackling getPlay function. (Should now have 8 points.)
getPlay: Now that you have convertPosition and validPlacement working, use these functions to help you define the getPlay function. Be sure this function continues to ask the user for input (does not return) until they provide a valid board position. Test and submit for feedback. (Should now have 10 points.)
Now call getPlay function in main to get just 1 play. Use the return value to place an X in that spot and call drawBoard again. Do not try to get the loop working yet. Just call getPlay once. (Should now have 12 points.)
gameWon: Define, test, and submit this function. (Should now have 16 points.)
boardFull: Define, test, and submit this function. (Should now have 18 points.)
You now have all functions fully tested and working. Go ahead and complete the main function. Get the loop working. Figure out how to switch who's turn it is and implement that. Output the winner or tie game after the loop exits. (Should now have full points!)
Tie Game Example:
a | b | c -----|-----|----- d | e | f -----|-----|----- g | h | i Please choose a position: X | b | c -----|-----|----- d | e | f -----|-----|----- g | h | i Please choose a position: X | O | c -----|-----|----- d | e | f -----|-----|----- g | h | i Please choose a position: X | O | c -----|-----|----- d | X | f -----|-----|----- g | h | i Please choose a position: X | O | c -----|-----|----- d | X | f -----|-----|----- g | h | O Please choose a position: X | O | c -----|-----|----- d | X | X -----|-----|----- g | h | O Please choose a position: X | O | c -----|-----|----- O | X | X -----|-----|----- g | h | O Please choose a position: X | O | X -----|-----|----- O | X | X -----|-----|----- g | h | O Please choose a position: X | O | X -----|-----|----- O | X | X -----|-----|----- O | h | O Please choose a position: X | O | X -----|-----|----- O | X | X -----|-----|----- O | X | O No one wins
in c++ please
existing code :
#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) {
// TODO: implement function
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) {
// TODO: implement function
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) {
// TODO: implement function
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.
/// Continues asking for a position until a valid position has been
entered.
///
/// @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: ";
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) {
// TODO: implement function
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
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
/// TODO: Display empty board
/// TODO: Play until game is over
/// TODO: Get a play
/// 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
return 0;
}
Code:
#include <vector>
#include <iostream>
using namespace std;
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;
}
void initVector(vector <char> &v) {
for (unsigned i = 0; i < v.size(); ++i) {
v.at(i) = static_cast<char>(97 + i);
}
return;
}
int convertPosition(char boardPosition) {
return static_cast<int> (boardPosition) - 97;
}
bool validPlacement(const vector<char> &gameBoard, int positionIndex) {
if (gameBoard.at(positionIndex) != 'X' &&
gameBoard.at(positionIndex) != 'O') {
return true;
}
else {
return false;
}
}
int getPlay(const vector<char> &gameBoard) {
char position = ' ';
unsigned int conversion;
cout << "Please choose a position: " << endl;
cin >> position;
conversion = convertPosition(position);
while (conversion >= gameBoard.size()) {
cout << "Please choose a position: " << endl;
cin >> position;
conversion = convertPosition(position);
}
while (!validPlacement(gameBoard, conversion)) {
cout << "Please choose a position: " << endl;
cin >> position;
conversion = convertPosition(position);
}
return convertPosition(position);
}
bool gameWon(const vector<char> &gameBoard) {
if ((gameBoard.at(0) == gameBoard.at(1) &&
gameBoard.at(1) == gameBoard.at(2)) ||
(gameBoard.at(3) == gameBoard.at(4) && gameBoard.at(4) ==
gameBoard.at(5)) ||
(gameBoard.at(6) == gameBoard.at(7) && gameBoard.at(7) ==
gameBoard.at(8)) ||
(gameBoard.at(0) == gameBoard.at(3) && gameBoard.at(3) ==
gameBoard.at(6)) ||
(gameBoard.at(1) == gameBoard.at(4) && gameBoard.at(4) ==
gameBoard.at(7)) ||
(gameBoard.at(2) == gameBoard.at(5) && gameBoard.at(5) ==
gameBoard.at(8)) ||
(gameBoard.at(0) == gameBoard.at(4) && gameBoard.at(4) ==
gameBoard.at(8)) ||
(gameBoard.at(2) == gameBoard.at(4) && gameBoard.at(4) ==
gameBoard.at(6))) {
return true;
}
else {
return false;
}
}
bool boardFull(const vector<char> &gameBoard) {
bool place0 = validPlacement(gameBoard, 0);
bool place1 = validPlacement(gameBoard, 1);
bool place2 = validPlacement(gameBoard, 2);
bool place3 = validPlacement(gameBoard, 3);
bool place4 = validPlacement(gameBoard, 4);
bool place5 = validPlacement(gameBoard, 5);
bool place6 = validPlacement(gameBoard, 6);
bool place7 = validPlacement(gameBoard, 7);
bool place8 = validPlacement(gameBoard, 8);
if (!place0 & !place1 & !place2 & !place3 &
!place4 &
!place5 & !place6 & !place7 & !place8) {
return true;
}
return false;
}
int main() {
vector<char> gameBoard(9);
int curPlay;
int whosTurn = 0;
bool winner;
bool full;
initVector(gameBoard);
drawBoard(gameBoard);
winner = gameWon(gameBoard);
full = boardFull(gameBoard);
while (!winner & !full) {
curPlay = getPlay(gameBoard);
if (whosTurn % 2 == 0) {
gameBoard.at(curPlay) = 'X';
}
else {
gameBoard.at(curPlay) = 'O';
}
drawBoard(gameBoard);
cout << endl;
++whosTurn;
winner = gameWon(gameBoard);
full = boardFull(gameBoard);
}
if (winner) {
if (whosTurn % 2 == 0) {
cout << "Player 2 Wins!!";
}
else {
cout << "Player 1 Wins!!";
}
}
else if (full) {
cout << "No one wins";
}
return 0;
}



Implement a tic-tac-toe game. Currently, if the variables provided in main are commented out, the program...
Implement a tic-tac-toe game. At the bottom of these specifications you will see a template you must copy and paste to cloud9. Do not change the provided complete functions, or the function stub headers / return values. Currently, if the variables provided in main are commented out, the program will compile. Complete the specifications for each function. As you develop the program, implement one function at a time, and test that function. The provided comments provide hints as to what...
In traditional Tic Tac Toe game, two players take turns to mark grids with noughts and crosses on the 3 x 3 game board. The player who succeeds in placing three of their marks in a horizontal, vertical, or diagonal row in the game board wins the game. Super Tic Tac Toe game also has a 3 x 3 game board with super grid. Each super grid itself is a traditional Tic Tac Toe board. Winning a game of Tic...
(Tic-Tac-Toe) Create a class Tic-Tac-Toe that will enable you to write a program to play Tic-Tac-Toe. The class contains a private 3-by-3 two-dimensional array. Use an enumeration to represent the value in each cell of the array. The enumeration’s constants should be named X, O and EMPTY (for a position that does not contain an X or an O). The constructor should initialize the board elements to EMPTY. Allow two human players. Wherever the first player moves, place an X...
18. Tic-Tac-Toe Game rite a program that allows two players to play a game of tic-tac-toe. Use dimensional char array with three rows and three columns as the game board. Each element of the array should be initialized with an asterisk (*). The program should run a loop that does the following: Write . Displays the contents of the board array. . Allows player 1 to select a location on the board for an X. The program should ask the...
(Game: play a tic-tac-toe game) In a game of tic-tac-toe, two players take turns marking an available cell in a grid with their respective tokens (either X or O). When one player has placed three tokens in a horizontal, vertical, or diagonal row on the grid, the game is over and that player has won. A draw (no winner) occurs when all the cells in the grid have been filled with tokens and neither player has achieved a win. Create...
Tic Tac Toe Game: Help, please. Design and implement a console based Tic Tac Toe game. The objective of this project is to demonstrate your understanding of various programming concepts including Object Oriented Programming (OOP) and design. Tic Tac Toe is a two player game. In your implementation one opponent will be a human player and the other a computer player. ? The game is played on a 3 x 3 game board. ? The first player is known as...
JAVA TIC TAC TOE - please help me correct my code Write a program that will allow two players to play a game of TIC TAC TOE. When the program starts, it will display a tic tac toe board as below | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 The program will assign X to Player 1, and O to Player The program will ask Player 1, to...
You will create a PYTHON program for Tic-Tac-Toe game. Tic-Tac-Toe is normally played with two people. One player is X and the other player is O. Players take turns placing their X or O. If a player gets three of his/her marks on the board in a row, column, or diagonal, he/she wins. When the board fills up with neither player winning, the game ends in a draw 1) Player 1 VS Player 2: Two players are playing the game...
Write a program to Simulate a game of tic tac toe. A game of tic tac toe has two players. A Player class is required to store /represent information about each player. The UML diagram is given below.Player-name: string-symbol :charPlayer (name:string,symbol:char)getName():stringgetSymbol():chargetInfo():string The tic tac toe board will be represented by a two dimensional array of size 3 by 3 characters. At the start of the game each cell is empty (must be set to the underscore character ‘_’). Program flow:1) ...
Objective: To write a program to allow a game of Tic Tac Toe to be played, and to determine when the game is over Complete the TicTacToe program below. It will allow for a full game of Tic Tac Toe between two players, and it will tell you who won or if the game is over with no winner. Details: The TicTacToe board is stored in a 3x3 multidimensional list of characters containing spaces at the start of the game...