3 using namespace std; I/ The following 3 functions are helper functions to convert \( 1 / \) between 10 and 20 array indices. The grid itself is stored \( 1 / \) as a 10 array. However, for printing, checking who won, // etc. it may be easier to write loops that generate separate I/ row/column indices (i.e. treating the array like a \( 2 D \) array). // The functions below should be written using the hints in the I/ writeup for performing the appropriate arithmetic conversion \( 1 / \) between 1D- and 2D-indices, and can then be called in any // of the other functions when you find yourself wanting to I/ convert one to the other. /** * Helper function - Given the grid array and its dimension * as well as a particular row ( \( r \) ) and column (c), this * function performs the arithmetic to convert \( r \) and \( c \). * For example, for dim \( =3 \) and \( r=2, c=1 \), this function * should compute the corresponding index: 7 and return * grid[7]. * Use this function wherever you generate, row/column * indices and want to access that element in the grid. * Pass the row/column indices las \( r \) and c, respectively.
30 char getEntryAtRC (char grid[], int dim, int \( r \), int \( c \) ); 31 34 * that corresponds to this 10 index. Use this in 35 * conjunction with idxToCol() anytime you have a ID index 36 * and want to convert to \( 2 D \) indices. \( 37 \quad * / \) 38 int idxToRow(int idx, int dim); 39 \( 40 \quad / * * \) 41 * Helper function - Given a 10 index returns the column 42 * that corresponds to this 10 index. Use this in 43 * conjunction with idxToRow() anytime you have a \( 1 D \) index \( 44 * \) and want to convert to \( 2 D \) indices. 45 */ 46 int idxtoCol(int idx, int dim); 47 48 \( 49 / * * \) Should print the tic-tac-toe board in a nice grid \( 50 * \) format as shown below: \( 51 \quad * \) Parameters : 53 * grid: Array of dim^2 characters representing the state 54 * of each square. Each entry contains ' \( X \) ', ' 0 ' \( 55 * \) (the letter oh), or '?'. \( 56 \quad * \) 57 void printTTT (char grid[], int dim);
\( 59 / * * \) 60 * Should check if any player has won the game yet. \( 61 \star \) 62 * Parameters: 63 * grid: Array of dim^2 characters representing the state \( 64 * \) of each square. Each entry contains ' \( X \) ', ' 0 ', or '?' \( 65 * \) 66 * Return value: 67 * Should return 1 if ' \( X \) ' has won, 2 if ' \( O \) ' has won, or \( \theta \) (zero) \( 68 * \) if the game is still undecided. 69 * 70 */ 71 int checkForWinner (char grid[], int dim); 72 73 75 * Should check if there is no possible way any player can win. 76 * More specifically, if there does not exist a row, column, 77 * or diagonal that can still have 3 of a kind, then the game 78 * will be a draw. \begin{tabular}{l|l} 79 & \( \star \) \\ 80 & \( * \) \\ 81 & \( \star \) Parameters: \end{tabular} 82 * grid: Array of dim^2 2 characters representing the state \( 83 * \) of each square. Each entry contains ' \( X \) ', ' 0 ', or '?' 84 * Return value: 85 * Return value: \( 86 \star \) Return true if no player can win given the current \( 87 * \) game state, false otherwise. 88 */ 89 bool checkforDraw(char grid[], int dim);
93 * Write your implementations for each function prototyped \( 94 * \) above in the space below 97 char getentryAtRC(char grid[], int dim, int \( r \), int \( c \) ) \( 98\{ \) return grid \( [* \) Convert \( r, c \) to 10 index here \( \star /] \); \( 103\{ \) return \( / * \) Add Expression here to convert idx to appropriate row \( \star / ; \) 104 ; \( 105 \quad 3 \) 106 int idxTocol(int idx, int dim) 108\{ 109 return / \( * \) Add Expression here to convert idx to appropriate column \( * / \); \( 110 \quad 3 \) 111 112 void printTTT (char grid[], int dim)
27 * Complete the indicated parts of main(), below. int main() \{ 1/ This array holds the actual board/grid of \( X \) and 0 . It is sized I/ for the biggest possible case (11x11), but you should only I/ use dim^2 entries (i.e. if dim=3, only use 9 entries: 0 to 8) char tttata[121]; 11 dim stands for dimension and is the side length of the 11 tic-tac-toe board i.e. dim \( x \) dim \( (3 \times 3 \) or \( 5 \times 5) \). int dim; II Get the dimension from the user cin \( \gg \) dim; int dim_sq \( = \) dim*dim; for (int \( i=0 ; i< \) dim_sq; \( i++)\{ \) tttdata[i] = '?'; \} 1/ Print one of these messages when the game is over II and before you quit const char xwins_msg[] = "X player wins!"; const char owins_msg[] = "0 player wins!"; const char draw_msg[] = "Draw...game over!"; bool done = false; char player = ' \( \mathrm{X} \) '; II Show the initial starting board printTTT(tttdata, dim);
II Show the initial starting board printTTT(tttdata, dim); while(!done) \{ II choose to place their mark [X or 0]) and update the tttdata array. \( 1 / \) Be sure to follow the requirements defined in the guide/writeup \( 1 / \) for quitting immediately if the user input is out-of-bounds 11 (i.e. not in the range 0 to dim_sq-1) as well as continuing to \( 1 / \) prompt for an input if the user chooses locations that have already II been chosen (already marked with an \( x \) or 0 ). II Show the updated board if the user eventually chose a valid location \( 1 / \) (i.e. you should have quit the loop and propram by now without any \( 1 / \) other output message if the user chosen an out-of-bounds input). printTTT(ttdata, dim); \( 1 / \) received to check for a winner or draw and update other status, as 11 needed. 11 II To match our automated checkers' expected output, you must output \( 1 / \) one of the messages defined above using *one* of the cout commands
printTTT(tttdata, dim); I/ Complete the body of the while loop to process the input that was just \( 1 / \) received to check for a winner or draw and update other status, as 11 needed. 11 1/ To match our automated checkers' expected output, you must output 11 one of the messages defined above using *one* of the cout commands II (under the appropriate condition) below: 11 cout \( \ll \) xwins_msg \( \ll \) end \( l ; \quad O R \) 11 cout \( \ll \) owins_msg \( \ll \) endl; \( O R \) I/ cout \( \ll \) draw_msg \( \ll \) endl; 1/ Note: Our automated checkers will examine a specific number of lines /1 at the end of the program's output and expect to see the updated board \( 1 / \) and game status message. You may certainly add some debug print II statements during development but they will need to be removed to II pass the automated checks.