Subject Computer Science C-Family Programming

Question

Question: imagine that you play Tic-Tac-Toe randomly. Specifically, X goes first, placing an X on a random square. Then O goes, placing an O on one of the eight remaining squares. Then X goes again, placing an X on one of the seven remaining squares randomly. Then O goes again, etc., until one player gets three in a row. What is the probability that X wins this random game of Tic-Tac-Toe? 

Create a class called TicTacBoard whose objects represent Tic-Tac-Toe boards. The underlying data will be 3 x 3 arrays of chars, each of whose entries will be either ’x’, ’o’ or ’ ’. The first two should be LOWERCASE; the last one, which will represent an unused square, is two single quotes with a space between them (the space character).
Specifically, you should implement your class to the following specifications. I suggest you deal with the members and functions in the order I write them.
The class should have the following private data member:

1. board, which is a 3x3 array of chars. This will represent the state of the board at any given time.
The class should also have the following member functions, all public unless otherwise noted:
2. a constructor with no parameters. This should simply initialize all of the entries to be ’ ’ – that is, the entries should 
be the space character, which is two single quotes with a space between them. (So, the board starts out as blank.) 

3. board print, which should take no outside inputs and return nothing. This should print out the values held in board 
in a reasonably pretty way, with an extra endl after the board is printed. 

4. set, which should take two ints and a char as input, and change the entry at the given position to the given character. 
For instance, if I have a TicTacBoard object named myGame, then myGame.set(0,2,’x’); 
will set the entry in the upper right corner to be x (because the upper right corner is the first row, third column, which corresponds to board[0][2] with zero-based indexing). 

5. occupied, which should take two ints and return a bool. This function should check if the given position has already been played. For example, 
    myGame.occupied(0,2); should return true if board[0][2] is either ’x’ or ’o’, and it should return false if board[0][2] is ’ ’. 

6. row full, which should be PRIVATE, an which should take an int and a char, and return a bool. This function 
should check if the row corresponding to the given int is entirely occupied by the given char. For example, myGame.row full(2,’x’); 
should return true if each entry in the third row of board is ’x’ (that is, if board[2][0], board[2][1] and board[2][2] are all ’x’). 

7. col full, which should be PRIVATE, an which should take an int and a char, and return a bool. This should be the same as the last function, except for columns instead of rows. 

8. diag full, which should be PRIVATE, an which should take a char, and return a bool. Sort of similar to the last two, this should check whether either of the diagonals contain the given letter all the way down: that is, it should return true if either board[0][0], board[1][1], and board[2][2] all contain the input char, or if board[0][2], board[1][1], and board[2][0] all contain the input char. 

9. win for, which should take a char as input and return a bool. This should take the given char, and return true if that char is in winning position: if there is a row, column or diagonal for which each entry contains that char. The function should return false otherwise. 

10. random move, which should take a char and return nothing. This function should play as the input char: it should take the board, and try to place the given char in any position that isn’t already occupied with an x or an o. So, 
myGame.random move(’x’); 
tries to put an ’x’ into any of the unoccupied squares, each one with equal probability. This is slightly tricky. Suggestion: pick two random numbers representing row and column; check if that square is occupied; if it isn’t place the move, and if it is, pick two new numbers and start over. 

After you write the class well enough so that all the test code works, you should attempt to answer the question I posed before: if two players play Tic Tac Toe by making random moves, what is the probability that the first player, X, is the winner?
The general strategy is: create a loop that runs, say, 1000000 times. Each time through, declare a TicTacBoard, use the member functions to simulate a game, and keep track of how many of those 1000000 games is won by X.
Specifications: your program must
• contain a definition of the class TicTacBoard that includes the 10 functions listed above, and that makes my test code run properly.
• create code that uses the TicTacBoard class to simulate 1000000 randomly-played Tic Tac Toe games, and report the number of games that were won by X.

Solution Preview

This material may consist of step-by-step explanations on how to solve a problem or examples of proper writing, including the use of citations, references, bibliographies, and formatting. This material is made available for the sole purpose of studying and learning - misuse is strictly forbidden.

#include <cstdlib>
#include <iostream>
#include <ctime>
using namespace std;

const int BOARD_SIZE = 3;

class TicTacBoard {
private:
    char board [BOARD_SIZE][BOARD_SIZE];
    int takenCell;
    bool row_full(int a, char b);
    bool col_full(int a, char b);
    bool diag_full(char b);...

This is only a preview of the solution. Please use the purchase button to see the entire solution

Assisting Tutor

Related Homework Solutions

C Programming: ASCII Code Characters to Integers
Homework Solution
$30.00
C Programming
Computer Science
ASCII Codes
Characters
Bits
Integers
Operations
File Management
Statements
Variables
Functions
Input
Output
Encoding
Decoding
Word Search Program in C
Homework Solution
$30.00
Programming
C
Computer Science
Word Search
Characters
Symbols
Strings
Text Files
Functions
Output
Input
Count Function
Loops
Statements
Pointers
Variables
Craps Game in Programming Language C
Homework Solution
$35.00
Programming
C
Craps
Game Development
Coding
Algorithms
Dice
Numbers
Players
Statements
Variables
Bet
Results
Money
Win
Lose
Functions
Bank Roll
Program For Retail Store Using C++
Homework Solution
$30.00
Programming
C++
Computer Science
Coding
Walmart
Amazon
Sales
Business
Products
Prices
Structures
Orders
Arrays
CSV Files
Variables
Statements
Prototypes
Pointers
Repetition Structures
Homework Solution
$30.00
Computer
Science
C
Programming
Repetition
Structures
Table
Creator
Pseudocode
Get help from a qualified tutor
Live Chats