Utilize functions, multidimensional arrays

Cause Related Marketing
January 8, 2018
ECOLOGICAL ECONOMICS
January 8, 2018

 

1. Formatting: Make sure that you follow the precise recommendations for the output
content and formatting. Your assignment will be auto­graded and any change in
formatting will result in a reduced grade.
2. Comments: Header comments are required on all files and recommended for the rest of
the course. Points will be deducted if no header comments are included.
Please save your code in a file named: tictacimage.c. In this assignment, we will develop a tic­tac­toe game.
But this time, the tic­tac­toe game should be 4×4. The rules are mostly the same, but you need 4­in­a­row to
win. The game should follow the same rules and implement the same functions as for project[6] with the
following exceptions:
check_three_in_a_row should now be check_four_in_a_row, per the rules of 4×4
tic­tac­toe
● When a user enters a move (legal or illegal) the program will not automatically print out the state
of the board. The board will only be printed when the user enters the ‘s’ command (described
below).
The prompt should ask a user to enter a command, as so:
Enter a command for Player 1 ([row,col], c, s, p):
The legal commands include one of the following:
● a legal move
● c: clear the board
● s: print the state of the board as displayed in Figure 1 (as a set of characters, dashes for horizontal
lines, pipes for vertical lines, plus signs where the lines cross, and x’s and o’s for the filled in
spaces; or
Column:1 2 3 4
Row: ___ ___ ___ ___
| | | | |
1 | | | O | |
|___|___|___|___|
| | | | |
2 | | X | | |
|___|___|___|___|
| | | | |
3 | | | | |
|___|___|___|___|
| | | | |
4 | | | | |
|___|___|___|___|
Figure 1: state of the board
● p: print the state of the board as an image (see below for more info)
For this assignment, we will be using the same image format from project[5], review that project
description for more info on images. For more information on image file formats, see this article on
Wikipedia (https://en.wikipedia.org/wiki/Image_file_formats)
Your program should add the following functions to the ones originally required for project[6]:
● clear_table: this function should take as an argument an array used to store the state of the tic­tactoe board. The function should then clear the moves from the board. After the function should run,
the state of the board should be a blank tic­tac­toe board. This function should not return anything.
● display_image: this function should take as an argument an array used to store the state of the tictac­toe board. The function should then print the board as shown in Figure 2 (below). This
function should not return anything.
display_image
This function should print a tic­tac­toe board with a square size of 5, and line thickness of 1. O’s moves
should show up as a black square, X’s moves should show up as a grey square. So, if a user makes the
moves as described above, the print_image should print:
P2
# image.pgm
23 23
255
255 255 255 255 255 0 255 255 255 255 255 0 0 0 0 0 0 0 255 255 255 255 255
255 255 255 255 255 0 255 255 255 255 255 0 0 0 0 0 0 0 255 255 255 255 255
255 255 255 255 255 0 255 255 255 255 255 0 0 0 0 0 0 0 255 255 255 255 255
255 255 255 255 255 0 255 255 255 255 255 0 0 0 0 0 0 0 255 255 255 255 255
255 255 255 255 255 0 255 255 255 255 255 0 0 0 0 0 0 0 255 255 255 255 255
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
255 255 255 255 255 0 128 128 128 128 128 0 255 255 255 255 255 0 255 255 255 255 255
255 255 255 255 255 0 128 128 128 128 128 0 255 255 255 255 255 0 255 255 255 255 255
255 255 255 255 255 0 128 128 128 128 128 0 255 255 255 255 255 0 255 255 255 255 255
255 255 255 255 255 0 128 128 128 128 128 0 255 255 255 255 255 0 255 255 255 255 255
255 255 255 255 255 0 128 128 128 128 128 0 255 255 255 255 255 0 255 255 255 255 255
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
255 255 255 255 255 0 255 255 255 255 255 0 255 255 255 255 255 0 255 255 255 255 255
255 255 255 255 255 0 255 255 255 255 255 0 255 255 255 255 255 0 255 255 255 255 255
255 255 255 255 255 0 255 255 255 255 255 0 255 255 255 255 255 0 255 255 255 255 255
255 255 255 255 255 0 255 255 255 255 255 0 255 255 255 255 255 0 255 255 255 255 255
255 255 255 255 255 0 255 255 255 255 255 0 255 255 255 255 255 0 255 255 255 255 255
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
255 255 255 255 255 0 255 255 255 255 255 0 255 255 255 255 255 0 255 255 255 255 255
255 255 255 255 255 0 255 255 255 255 255 0 255 255 255 255 255 0 255 255 255 255 255
255 255 255 255 255 0 255 255 255 255 255 0 255 255 255 255 255 0 255 255 255 255 255
255 255 255 255 255 0 255 255 255 255 255 0 255 255 255 255 255 0 255 255 255 255 255
255 255 255 255 255 0 255 255 255 255 255 0 255 255 255 255 255 0 255 255 255 255 255
and this would be the resultant image:
Figure 2

 

For this project you will write a program that implements the game of Tic-Tac-Toe. Your
program should go through the following steps:
1. Generate an empty Tic-Tac-Toe table (3×3 matrix)
2. Run a loop as long as none of the player has placed three in a row and the table
is not full
a. Display the current layout of the table
b. Ask the user (select player 1 and player 2 alternatively) to enter his/her
selection (the location in the table where the X or O should be placed)
c. Check that the user entered a legal option (an empty cell); if the
selection is on a cell that is already occupied, the program should ask
the user to enter the option again
d. Update the table with the user provided option
3. If the above loop ends because one of the player has placed three in a row, the
program should print a winning message of that player (see below). Else, it
should print the following : “Game over, no player wins.”
The program should function as follows (items underlined are to be entered by the
user):
This program plays the game of tic­tac­toe
The current state of the game is:
Column:1 2 3
Row: ___ ___ ___
| | | |
1 | | | |
|___|___|___|
| | | |
2 | | | |
|___|___|___|
| | | |
3 | | | |
|___|___|___|
Enter the section of X for Player 1 [row,col]: 1,2
The current state of the game is:
Column:1 2 3
Row: ___ ___ ___
| | | |
1 | | X | |
|___|___|___|
| | | |
2 | | | |
|___|___|___|
| | | |
3 | | | |
|___|___|___|
Enter the section of 0 for Player 2 [row,col]: 1,2
Invalid selection
Enter the section of 0 for Player 2 [row,col]: 2,2
The current state of the game is:
Column:1 2 3
Row: ___ ___ ___
| | | |
1 | | X | |
|___|___|___|
| | | |
2 | | 0 | |
|___|___|___|
| | | |
3 | | | |
|___|___|___|
… E
nter the selection of X for Player 1 [row, col]: 1,3
The current state of the game is:
Column:1 2 3
Row: ___ ___ ___
| | | |
1 | X | X | X |
|___|___|___|
| | | |
2 | 0 | 0 | |
|___|___|___|
| | | |
3 | X | | 0 |
|___|___|___|
Congratulations, Player 1 wins!
Constraints: your program should use the following functions:
– create_clear_table: this function should take as input a 3×3 array and clear it
out for the beginning of the game. You are free to chose the type of the array and how
you store the empty, 0 or X.
– check_table_full: this function takes as input the table array and returns true or
false depending on whether all the cells are occupied or not
– check_three_in_a_row: this function takes as input the table array and returns 0 if
no player has three in a row (on rows, columns, or diagonals) or the ID of the player (1 or
2) who has three in a row (three Xs represent player 1, three 0s represent player 2)
– display_table: this function takes as input the table array and prints out the current
status as shown above
– check_legal_option: this function takes as input the table array and the option
currently entered by the user and returns true or false depending on whether the option is
valid or not
– update_table: this function takes as input the table array and the option currently
entered by the user and updates the table with the latest entered move
Your program should be saved in a file called tictactoe.c.

 

 


WE ARE THE LEADING ACADEMIC ASSIGNMENTS WRITING COMPANY, BUY THIS ASSIGNMENT OR ANY OTHER ASSIGNMENT FROM US AND WE WILL GUARANTEE AN A+ GRADE

Login to Account
ORDER NOW!
  • Enjoy a lifetime discount of 30%!
  • Enter the code UPE30
toggle