Question
#include #include #include #define SIZE 15 #define EMPTY 0 #define STONE 1 // TODO: Add any extra #defines here. void printMap(int map[SIZE][SIZE], int playerX); //
#include
#include
#include
#define SIZE 15 #define EMPTY 0 #define STONE 1 // TODO: Add any extra #defines here. void printMap(int map[SIZE][SIZE], int playerX); // TODO: Add any extra function prototypes here. int main (void) { // This line creates our 2D array called "map" and sets all // of the blocks in the map to EMPTY. int map[SIZE][SIZE] = {EMPTY}; // This line creates out playerX variable. The player starts in the // middle of the map, at position 7. int playerX = SIZE / 2; printf("How many lines of stone? "); // TODO: Scan in the number of lines of blocks. printf("Enter lines of stone: "); // TODO: Scan in the lines of blocks. printMap(map, playerX); // TODO: Scan in commands until EOF. // After each command is processed, you should call printMap. return 0; } // Print out the contents of the map array. Then print out the player line // which will depends on the playerX variable. void printMap(int map[SIZE][SIZE], int playerX) { // Print values from the map array. int i = 0; while (i < SIZE) { int j = 0; while (j < SIZE) { printf("%d ", map[i][j]); j++; } printf(" "); i++; } // Print the player line. i = 0; while (i < playerX) { printf(" "); i++; } printf("P "); }
Stage 1
Stage 1 implements the ability to read in lines of stone and place them onto the map, move the player to the left and right (and print this on the screen), and fire the laser to destroy falling STONE.
Placing Stone
The program should first ask for the number of lines of STONEs as an integer. Then, the program will scan the locations of the lines as a group of four integers in the following format:
row column length value
The row and column represent the left-most block of a horizontal line of blocks to the placed on the map. The length tells you how many stones should be in this horizontal line. For stage 1 and 2, the fourth integer will always be 1 representing a stone, however in other stages there will be other possibilities such as exploding TNT and marching blocks. This number is the value which should be placed in the array itself to make up the specified line. For example:
Command | Meaning |
---|---|
0 0 5 1 | Place a line of stone starting at [0][0] and ending at [0][4]. All 5 squares in the line will be set to 1 (STONE). |
6 7 1 1 | Place a line of stone starting at [6][7] and ending at [6][7]. The 1 square in the line will be set to 1 (STONE). |
5 0 15 1 | Place a line of stone starting at [5][0] and ending at [5][14]. All 15 squares in the line will be set to 1 (STONE). |
./freefall How many lines of stone? 3 Enter lines of stone: 0 0 5 1 6 7 1 1 5 0 15 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 P
After scanning in the inputs relating to placing lines of stone on the map, the program should then call printMapto display the map on the screen. The line of code to do this is already in the starter code. After printing the map, the program should start to scan in commands until EOF. After processing each command, your program should call printMap once again to show the changes made to the map. Details of each command that your program must implement are shown below.
In the starter code, the program currently initialises every square in the map to EMPTY then prints the map as a grid of integers.
Invalid Input and Assumptions
- The first number scanned in (i.e. the number of coordinate pairs specified) will always be valid. It will always be a number greater than zero.
- It is possible for the first three integers (row column length) to result in a line which is partially or wholly outside of the map. If this is the case, you should ignore this line completely and not make any changes to the map.
- You can assume the third integer (length) will always be positive.
- The fourth integer can be assumed to be valid. For this stage it will always be 1 (you don't have to check this).
Moving the Player
Summary: Move Player Command
Command Name | "Move Player" | ||||||||
---|---|---|---|---|---|---|---|---|---|
Command Number | 1 | ||||||||
Inputs | direction | ||||||||
Examples |
|
For the next part of stage 1, you will implement a command to move the player to the left or right on the screen.
The first integer of any move player command will always be a 1. The second integer represents the direction. -1means to move the player to the left and 1 means to move the player to the right.
Information about the player's current position is given to the printMap function, so it is recommended to update the playerX variable in the main function when this command is received. This will have the effect of changing the position of the P printed by printMap the next time it is called.
Note that after each command has been scanned in and processed the map is printed once again.
Invalid Input and Assumptions
- The second integer of a move player command may be something else other than -1 or 1. In this case, your program should print out the map unchanged.
- Your program may be given a sequence of move player commands which would theoretically move the player off the map. Ensure the player is always below a column of the map. If the player is on an edge column and a move player command is received which would push it over the edge, your program should print out the map unchanged.
Firing the Laser
Summary: Fire Laser Command
Command Name | "Fire Laser" | ||||
---|---|---|---|---|---|
Command Number | 2 | ||||
Inputs | (none) | ||||
Examples |
|
For the final part of stage 1, you will implement a command to fire the laser and destroy some blocks directly above the player.
This time the first integer of this command will always be a 2. There will not be any additional integers after this.
This command means to fire the laser at the player's current column. The laser will destroy at most 3 stones above the player. Destroying a stone can be done by changing the value in the array from STONE to EMPTY.
If there are more than three stones in the player's current column, the closest three stones should be destroyed only.
Hint: Even though you are looping through a 2D array, only one while loop is required to complete this command as you are looping through a straight line in the array.
Invalid Input and Assumptions
- Since there is only one number in this command, there are no invalid input possibilities.
- Note that there may be no stones in the player's current column, in which case no changes should be made.
Stage 2
In stage 2, you will be implementing a command to shift everything in the map down by one position, and you will be implementing functionality to determine whether the game has been won or lost.
Shift Everything Down
Summary: Shift Everything Down Command
Command Name | "Shift Everything Down" | ||||
---|---|---|---|---|---|
Command Number | 3 | ||||
Inputs | (none) | ||||
Examples |
|
For the first part of stage 2, you will need to shift all items in the array down by one position. This should occur whenever the program reads in a command of: 3.
There are no arguments after this command, it is another single integer command.
When your program shifts the contents of the array down, the integers which were in the bottom row will disappear, and the integers in the top row will all become EMPTY.
End Conditions
There are two ways to complete a game of Freefall:
- The game is won if the entire map is EMPTY.
- The game is lost if there is any STONE on the last row as a 3 (shift everything down) command is given. The only case in which the game is not lost when the shift everything down command is given, is when the entire bottom row of the map is EMPTY.
In stage 2 you must add code to check for these conditions. The win conditions should be checked after every fire laser command. If the game is won, the program should print the map once and then print "Game Won!" and end.
The lose condition should be checked before the contents of the array are shifted down. If the game is lost, the program should not shift the grid down, but should still print the map once and then print "Game Lost!" and end.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started