+1 (315) 557-6473 

TTT Game Building Programming with C Assignment Solution


TTT Notation Parser and Printer

Your friend has fallen in love with Webflix’s hot new show: TicTacToe’s Gambit - and now all they talk about is TicTacToe. Inspired to compete the C assignment on the world stage, they’ve purchased a strategy book containing scripts of past high profile TicTacToe games in TTTNotation.

Unfortunately, they can’t read TTTNotation. Build a program for them that reads lines from a text file, and prints out the associated moves on a TicTacToe board.

Program Execution

The program should accept 1 command-line argument: A relative file path. When the program runs, it should:

  1. Open the file inputed from the command line argument
  2. Read one line in the file
  3. Print the current state of the TicTacToe board
  4. Repeat steps 2 and 3 until all lines in the file have been read
  5. Close the file
  6. Print the total number of moves made in the game
  7. End the program

TicTacToe Board

A TicTacToe board consists of 3 rows, and 3 columns, for a total of 9 squares. An empty square is represented by a period. Player x’s symbols are represented by a lower case x, and player o’s symbols are represented by a lower case o. Each square is separated by a space character. For example, an empty board is printed as follows:

. . .

. . .

. . .

And a board in which player x has gone in the square in the first row and first column, and player o has gone in the last row and the last column is printed as:

x . .

. . .

. . o

When printing multiple boards to the screen, each board should be separated by six hyphens.

For example:

. o .

. . .

. . .

. o x

. . .

. . .

TTTNotation Files

TTTNotation is a made-up representation for moves played on a tic tac toe board. You won’t find references to it on Google. You’ll need to parse it following the specification in this document.

A complete TTTNotation file contains multiple lines. At the beginning of the file, assume the TicTacToe board is empty. The first line of the file represents the first move on the TicTacToe board. The second line of the file represents the second move. The third line represents the third move. .etc until the last line of the file, which represents the final move in that game. For example, if a file contains 5 lines - then there were five total moves made in the game. These moves are the only things present in a TTTNotation file.

Each line in the TTTNotation file is in the following format:

player,row,column

Where:

- a player is either x or o and represents the player's symbol. The symbols are lowercase.

- row is either 1, 2, or 3 and represents the row on the board the player places their symbol

- column is either 1, 2, or 3 and represents the column on the board the player places their symbol

Each line is played after the previous line. So as you read more than online, you will print the state of the board with all the moves played so far.

For example, if the following file was inputted:

x,1,1

o,2,2

x,1,2

o,3,2

x,1,3

Your program would print:

x . .

. . .

. . .

x . .

. o .

. . .

x x .

. o .

. . .

x x .

. o .

. o .

x x x

. o .

. o .

Total moves: 5

Solution:

#include < stdio.h> #include < string.h> #define STRLEN 50 #define ROWS 3 #define COLS 3 // Make the board empty, put dots as content for each cell void initBoard(char board[ROWS][COLS]) { for (int r = 0; r < ROWS; r++) for (int c = 0; c < COLS; c++) board[r][c] = '.'; } // Display a divider void printDivider() { for (int i = 0; i < COLS * 2; i++) printf("-"); printf("\n"); } // Print out the contents of the board void printBoard(char board[ROWS][COLS]) { for (int r = 0; r < ROWS; r++) { for (int c = 0; c < COLS; c++) { printf("%c", board[r][c]); if (c + 1 < COLS) printf(" "); } printf("\n"); } } // Entry point of the program int main(int argc, char *argv[]) { // Check that the filename is provided if (argc < 2) { printf("Usage: %s \n", argv[0]); return 0; } // Initialize the board char board[ROWS][COLS]; initBoard(board); // Load the file and start accepting inputs FILE *file = fopen(argv[1], "r"); if (!file) { printf("Error: Failed to open file.\n"); return 0; } // Read each and every line and apply the move to the board char line[STRLEN]; int totalMoves = 0; int dividersPrinted = 0; while (fgets(line, STRLEN, file)) { if (dividersPrinted > 0) printDivider(); else dividersPrinted++; char token = line[0]; int row = line[2] - '0' - 1; int col = line[4] - '0' - 1; board[row][col] = token; totalMoves++; printBoard(board); } printf("\n"); printf("Total moves: %d\n", totalMoves); fclose(file); return 0; }