Tic-Tac-Toe =========== Included with this file is a file named tictactoe.cpp. O
ID: 3713743 • Letter: T
Question
Tic-Tac-Toe
===========
Included with this file is a file named tictactoe.cpp. Once completed, the
program generates random tic-tac-toe boards and outputs the first 10 winning
boards.
Your task is to complete the functions that have "TODO" in them so that the
program works.
(File Contents)
#include <iostream>
#include <string>
#include <cstdlib>
#include <time.h>
using namespace std;
/* Tic-Tac-Toe board represented as a 2-dimensional int array, where 0 is X and
* 1 is O.
*
* See the main function for documentation of the board format.
*/
/* Some nice names for common values. For example, to set the upper right
* "square" of a board to X, we could write this::
*
* board[0][2] = 0;
*
* But I've declared these names here so that we can instead write this::
*
* board[FIRST][THIRD] = X;
*/
const int FIRST = 0;
const int SECOND = 1;
const int THIRD = 2;
const int X = 0;
const int O = 1;
/* Return true if all values in the given row number of the board are the same,
* false otherwise.
*/
bool isWinningRow(int board[][3], int rowNum)
{
return false; // TODO.
}
/* Return true if all values in the given column number of the board are the
* same, false otherwise.
*/
bool isWinningCol(int board[][3], int colNum)
{
return false; // TODO.
}
/* Return true if all values in the diaganol going from left to right are the
* same, false otherwise.
*
* Example winner::
*
* 0 1 0
* 1 0 1
* 1 1 0
*/
bool isWinningLeftToRightDiag(int board[][3])
{
return false; // TODO.
}
/* Return true if all values in the diaganol going from right to left are the
* same, false otherwise.
*
* Example winner::
*
* 0 1 0
* 1 0 1
* 0 1 1
*/
bool isWinningRightToLeftDiag(int board[][3])
{
return false; // TODO.
}
/* Return true if the board has a winning row, column or diagonal, false
* otherwise.
*/
bool isWinningBoard(int board[][3])
{
return false; // TODO.
}
/* Convert a zero to an X and a one to an O.
*/
string intToMark(int i)
{
return "TODO";
}
/* Convert a board into a string.
*
* As an example, given the board::
*
* int exampleBoard[3][3] = { {X, O, X}
* , {O, O, X}
* , {X, X, O}
* };
*
* We want the result to be a string like this::
*
* X O X
* O O X
* X X O
*/
string boardToString(int board[][3])
{
return "TODO";
}
/* Randomly assign 0 or 1 to each element of the 2-dimensional array that
* represents a board.
*/
void randomlyInitBoard(int board[][3])
{
// TODO
}
/* Feel free to write any debugging code you need to before my code, but do not
* change my code.
*/
int main()
{
// Unused, just to show you the structure of a board.
int exampleBoard[3][3] = { {X, O, X} // First row.
, {O, O, X} // Second row.
, {X, X, O} // Third row.
};
srand(time(NULL)); // Salt the random-number generator once.
// We'll generate random boards, and output the first few winning boards.
const int MAX_WINNING_BOARDS = 10;
int winningBoards = 0;
int totalBoardsSeen = 0;
while (winningBoards < MAX_WINNING_BOARDS)
{
int board[3][3]; // Get a fresh, empty board.
randomlyInitBoard(board); // Randomly fill board with Xs and Os.
if (isWinningBoard(board))
{
winningBoards++;
cout << boardToString(board) << endl;
}
totalBoardsSeen++;
if (totalBoardsSeen > 1000)
{
cout << "1000 boards generated. Stopping now."
<< " Should have seen enough winning boards by now."
<< " Fix your code."
<< endl;
return 1;
}
}
}
Explanation / Answer
Given below is the code for the question.
Please do rate the answer if it was helpful. Thank you
#include <iostream>
#include <string>
#include <cstdlib>
#include <time.h>
using namespace std;
/* Tic-Tac-Toe board represented as a 2-dimensional int array, where 0 is X and
* 1 is O.
*
* See the main function for documentation of the board format.
*/
/* Some nice names for common values. For example, to set the upper right
* "square" of a board to X, we could write this::
*
* board[0][2] = 0;
*
* But I've declared these names here so that we can instead write this::
*
* board[FIRST][THIRD] = X;
*/
const int FIRST = 0;
const int SECOND = 1;
const int THIRD = 2;
const int X = 0;
const int O = 1;
/* Return true if all values in the given row number of the board are the same,
* false otherwise.
*/
bool isWinningRow(int board[][3], int rowNum)
{
return board[rowNum][FIRST] == board[rowNum][SECOND] && board[rowNum][SECOND] == board[rowNum][THIRD];
}
/* Return true if all values in the given column number of the board are the
* same, false otherwise.
*/
bool isWinningCol(int board[][3], int colNum)
{
return board[FIRST][colNum] == board[SECOND][colNum] && board[SECOND][colNum] == board[THIRD][colNum];
}
/* Return true if all values in the diaganol going from left to right are the
* same, false otherwise.
*
* Example winner::
*
* 0 1 0
* 1 0 1
* 1 1 0
*/
bool isWinningLeftToRightDiag(int board[][3])
{
return board[FIRST][FIRST] == board[SECOND][SECOND] && board[SECOND][SECOND] == board[THIRD][THIRD];
}
/* Return true if all values in the diaganol going from right to left are the
* same, false otherwise.
*
* Example winner::
*
* 0 1 0
* 1 0 1
* 0 1 1
*/
bool isWinningRightToLeftDiag(int board[][3])
{
return board[FIRST][THIRD] == board[SECOND][SECOND] && board[SECOND][SECOND] == board[THIRD][FIRST];
}
/* Return true if the board has a winning row, column or diagonal, false
* otherwise.
*/
bool isWinningBoard(int board[][3])
{
for(int i = FIRST; i <= THIRD; i++)
if(isWinningRow(board, i))
return true;
for(int i = FIRST; i <= THIRD; i++)
if(isWinningCol(board, i))
return true;
if(isWinningLeftToRightDiag(board) || isWinningRightToLeftDiag(board))
return true;
return false;
}
/* Convert a zero to an X and a one to an O.
*/
string intToMark(int i)
{
if(i == 0)
return "X";
else
return "O";
}
/* Convert a board into a string.
*
* As an example, given the board::
*
* int exampleBoard[3][3] = { {X, O, X}
* , {O, O, X}
* , {X, X, O}
* };
*
* We want the result to be a string like this::
*
* X O X
* O O X
* X X O
*/
string boardToString(int board[][3])
{
string s = "";
for(int row = FIRST; row <= THIRD; row++)
{
for(int col = FIRST; col <= THIRD; col++)
s += intToMark(board[row][col]) + " ";
s += " ";
}
return s;
}
/* Randomly assign 0 or 1 to each element of the 2-dimensional array that
* represents a board.
*/
void randomlyInitBoard(int board[][3])
{
for(int row = FIRST; row <= THIRD; row++)
{
for(int col = FIRST; col <= THIRD; col++)
board[row][col] = rand() % 2;
}
}
/* Feel free to write any debugging code you need to before my code, but do not
* change my code.
*/
int main()
{
// Unused, just to show you the structure of a board.
int exampleBoard[3][3] = { {X, O, X} // First row.
, {O, O, X} // Second row.
, {X, X, O} // Third row.
};
srand(time(NULL)); // Salt the random-number generator once.
// We'll generate random boards, and output the first few winning boards.
const int MAX_WINNING_BOARDS = 10;
int winningBoards = 0;
int totalBoardsSeen = 0;
while (winningBoards < MAX_WINNING_BOARDS)
{
int board[3][3]; // Get a fresh, empty board.
randomlyInitBoard(board); // Randomly fill board with Xs and Os.
if (isWinningBoard(board))
{
winningBoards++;
cout << boardToString(board) << endl;
}
totalBoardsSeen++;
if (totalBoardsSeen > 1000)
{
cout << "1000 boards generated. Stopping now."
<< " Should have seen enough winning boards by now."
<< " Fix your code."
<< endl;
return 1;
}
}
}
output (some randomly generated winning boards)
------
O X X
O O O
O X X
X X O
O X X
O O O
X X O
X X X
O X O
X X O
O X O
O O O
O O X
X O X
X X O
O X X
X O X
O O O
O X O
X X X
X X X
O X X
X O X
X O O
X O X
O O O
X X X
X O X
X X X
O X X
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.