Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Part A: The Asteroid Type (code 5 / 40 marks + output 9 / 40 marks + documentation 4 / 40]: Asteroid.h and Asteroid.cpp An
Part A: The Asteroid Type (code 5 / 40 marks + output 9 / 40 marks + documentation 4 / 40]: Asteroid.h and Asteroid.cpp An asteroid will contain an ore value and the size and position of its rectangle on the screen. It will be possible for an asteroid to become "dead" (e.g., if it is taken by the player), and this will be represented by setting the ore value to 0. By the end of Part A, you will have created asteroid-related functions with the following prototypes: void asteroidLoadImage(); void asteroidinit (Asteroid& asteroid); int asteroidGetMinRow (const Asteroid& asteroid); int asteroidGetMinColumn (const Asteroid& asteroid); int asteroidGetMaxRow (const Asteroid& asteroid); int asteroidGetMaxColumn (const Asteroid& asteroid); int asteroidGetRowCount (const Asteroid& asteroid); int asteroidGetColumnCount (const Asteroid& asteroid); unsigned int asteroidGetoreValue (const Asteroid& asteroid); bool asteroidIsDead (const Asteroid& asteroid); void asteroidDraw (const Asteroid& asteroid, Buffer buff, int scrolled columns); void asteroidsetPosition (Asteroid& asteroid, int new_min_row, int new_min_column); void asteroidAddPosition (Asteroid& asteroid, int delta_row, int delta_column); void asteroidRandomizeoreValue (Asteroid& asteroid); void asteroidMarkDead (Asteroid& asteroid); Perform the following steps: 1. Define Asteroid as a record (struct) with the following fields: min_row,min_column, row_count, column_count, and ore_value. Of these fields, min_row, min_column, row_count, and column_count are ints and ore_value is an unsigned int. Note: Define the Asteroid record in the Asteroid.h file. 2. Copy in the function prototypes shown above. Note: These prototypes should be in the Asteroid.h file because they are associated with the Asteroid type. The function implementations should be in the Asteroid.cpp file. Note: You will need to #include the Buffer.h header file in Asteroid.h. You will also need to include the Asteroid.h file in Main.cpp. Debugging: Immediately copy all the prototypes into the Asteroid.cpp file. Change them from being prototypes to being functions, by giving each function an empty body. Functions with a return type should contain a single return statement that returns a default value of the correct type (O for int and unsigned int, false for bool). Add a #include for Asteroid.h to the top of the Asteroid.cpp file. At this point, your program should compile and run with the Main2A test program. As you fill in the functions, regularly check to make sure that your program still compiles and runs. If it ever does not compile, stop and fix the error(s) before continuing. 3. Copy the ASTEROID_ROW_COUNT and ASTEROID_COLUMN_COUNT constants from Main.cpp to the Asteroid.cpp file. Rename them to ROW_COUNT and COLUMN_COUNT. Add three new unsigned int constants named ORE_VALUE_DEAD, ORE_VALUE_MIN, and ORE_VALUE_MAX with values of 0, 10, and 100. None of these constants should be visible outside the Asteroid.cpp file. Note: ORE_VALUE_MIN is an inclusive minimum (it can occur) and ORE_VALUE_MAX is an exclusive maximum (it cannot occur). These values define a half-open interval. If they are 10 and 100, then the legal values are 10, 11, 12, ..., 99. 4. Move the array storing the "image" from Main.cpp to the Asteroid.cpp file. The array should not be visible outside this file. See online notes in Section 4.1 on how to encapsulate (i.e. hide) a variable. We will call it an encapsulated variable. 5. Move the loadAsteroid function from Main.cpp to the Asteroid.cpp file and rename it to asteroidLoadImage. Adjust the names of the constants it uses and fix up the #includes in Asteroid.cpp. Note: Your Main.cpp will not compile again successfully until Part C, but Asteroid.cpp should compile. 6. Implement the asteroidinit function. It should initialize the Asteroid to be at row and column (0,0), to have row and column sizes of ROW_COUNT and COLUMN_COUNT, and to have a random ore value in the half-open interval [ORE_VALUE_MIN, ORE_VALUE_MAX). Hint: To assign a value of 25 to the min_row field of a variable named ast of the Asteroid type, you would type: ast.min row = 25; Hint: You can use the rand function in the C standard library to obtain a random number in the half-open interval [0, RAND_MAX). Then use the modulus operator (8) to convert the value to the range you want. For example, the following example yields an integer that has the possible values of 1, 2, 3, ..., 10: int includingland1 Onot11 = 1 + rand() % 10; You will need to #include . For each function in the next few steps, you should assume that some other part of the program has initialized a variable of the Asteroid type and that this variable is being passed into the function as a parameter. Thus, you can assume that the parameter of the Asteroid type has good values for all its fields. Your job is to put code into each of these functions to do one simple thing with the Asteroid parameter. So, for example, the asteroidGetMinRow function receives a good Asteroid as a parameter and it should just return the value of its field that contains the minimum row number. 7. Implement the asteroidGetMinRow function that returns the minimum row of that Asteroid. Implement the asteroidGetMinColumn, asteroidGetRowCount, and asteroidGetColumnCount functions in a similar manner. Hint: Each of these functions will contain a return statement and nothing else. 8. Implement the asteroidGetMaxRow function to return one less than the sum of the minimum row and the row count. Implement the asteroidGetMaxColumn function in a similar manner. 9. Implement the asteroidGetoreValue function to return the Asteroid's current ore value. 10. Implement the asteroidIs Dead function to return a bool indicating whether the ore value for the Asteroid is ORE VALUE_DEAD. 11. Implement the asteroidDraw function. The third parameter indicates how far the display has scrolled (measured in columns). The "image" of the Asteroid should be drawn to the Buffer at its current position, taking the number of columns scrolled into account. Add a precondition (enforced by an assert) that requires the number of columns scrolled to be non- negative. Hint: Start by finding your code from Assignment 1 that puts the asteroid image into the buffer. Move this code into the asteroidDraw function and then modify it as needed. Hint: You will need to include to use the assert function. You should read about preconditions and asserts in the online notes (search for "assert" in the Review 2 part of Section 1 and search for "Precondition" in section 4). 12. Add additional commands to the asteroidDraw function to draw the ore value at the same place as the "XX" in the Buffer. (Assume the ore value is already stored in the ore value field of the parameter of the Asteroid type.) You will need two new calls to buffersetcell to draw the two digits in the number. Hint: You can determine the value of the individual digits in the number using integer division (using the / operator on integer values and modulus (using the operator). Hint: If you have a single-digit number (e.g., 3) stored in an int variable named n, you can calculate the corresponding character value (e.g., '3') by computing: (char)n + 'O' 13. Implement the asteroidset Position and asteroidAddposition functions. The asteroidset Position function should move the asteroid to the row and column specified. The asteroidAddPosition function should increase the current row and column by the changes specified. Remember: A coordinate can be decreased by adding on a negative value. 14. Implement the asteroidRandomizeoreValue to set the ore value to a random number in the half-open interval [ORE_VALUE_MIN, ORE_VALUE_MAX). Note: Calculated the ore value the same way you did in asteroidinit. Your asteroidinit function can be rewritten to call asteroidRandomizeoreValue, although this is not required. Note: Changing the asteroid's ore value will mark it as not dead. This should be mentioned in the function documentation. 15. Implement the asteroidMarkDead function to set the ore value to ORE VALUE DEAD. 16. Write interface specifications in Asteroid.h for the asteroidLoadImage, asteroidInit, asteroidIsDead, and asteroidDraw functions in the format described in class and in Section 4a of the online notes. You do NOT need to write interface specifications for the other Asteroid functions Remember: The asteroidDraw function has a precondition. 17. Test your Asteroid type with the Main2A test program. If your Asteroid module is correct, it should print "*** PERFECT! ***" at the end. Hint: Comment out the existing contents of your Main.cpp file. Then copy the test program into the file. When you are finished with Part A, remove the test program and uncomment your code again. Part A: The Asteroid Type (code 5 / 40 marks + output 9 / 40 marks + documentation 4 / 40]: Asteroid.h and Asteroid.cpp An asteroid will contain an ore value and the size and position of its rectangle on the screen. It will be possible for an asteroid to become "dead" (e.g., if it is taken by the player), and this will be represented by setting the ore value to 0. By the end of Part A, you will have created asteroid-related functions with the following prototypes: void asteroidLoadImage(); void asteroidinit (Asteroid& asteroid); int asteroidGetMinRow (const Asteroid& asteroid); int asteroidGetMinColumn (const Asteroid& asteroid); int asteroidGetMaxRow (const Asteroid& asteroid); int asteroidGetMaxColumn (const Asteroid& asteroid); int asteroidGetRowCount (const Asteroid& asteroid); int asteroidGetColumnCount (const Asteroid& asteroid); unsigned int asteroidGetoreValue (const Asteroid& asteroid); bool asteroidIsDead (const Asteroid& asteroid); void asteroidDraw (const Asteroid& asteroid, Buffer buff, int scrolled columns); void asteroidsetPosition (Asteroid& asteroid, int new_min_row, int new_min_column); void asteroidAddPosition (Asteroid& asteroid, int delta_row, int delta_column); void asteroidRandomizeoreValue (Asteroid& asteroid); void asteroidMarkDead (Asteroid& asteroid); Perform the following steps: 1. Define Asteroid as a record (struct) with the following fields: min_row,min_column, row_count, column_count, and ore_value. Of these fields, min_row, min_column, row_count, and column_count are ints and ore_value is an unsigned int. Note: Define the Asteroid record in the Asteroid.h file. 2. Copy in the function prototypes shown above. Note: These prototypes should be in the Asteroid.h file because they are associated with the Asteroid type. The function implementations should be in the Asteroid.cpp file. Note: You will need to #include the Buffer.h header file in Asteroid.h. You will also need to include the Asteroid.h file in Main.cpp. Debugging: Immediately copy all the prototypes into the Asteroid.cpp file. Change them from being prototypes to being functions, by giving each function an empty body. Functions with a return type should contain a single return statement that returns a default value of the correct type (O for int and unsigned int, false for bool). Add a #include for Asteroid.h to the top of the Asteroid.cpp file. At this point, your program should compile and run with the Main2A test program. As you fill in the functions, regularly check to make sure that your program still compiles and runs. If it ever does not compile, stop and fix the error(s) before continuing. 3. Copy the ASTEROID_ROW_COUNT and ASTEROID_COLUMN_COUNT constants from Main.cpp to the Asteroid.cpp file. Rename them to ROW_COUNT and COLUMN_COUNT. Add three new unsigned int constants named ORE_VALUE_DEAD, ORE_VALUE_MIN, and ORE_VALUE_MAX with values of 0, 10, and 100. None of these constants should be visible outside the Asteroid.cpp file. Note: ORE_VALUE_MIN is an inclusive minimum (it can occur) and ORE_VALUE_MAX is an exclusive maximum (it cannot occur). These values define a half-open interval. If they are 10 and 100, then the legal values are 10, 11, 12, ..., 99. 4. Move the array storing the "image" from Main.cpp to the Asteroid.cpp file. The array should not be visible outside this file. See online notes in Section 4.1 on how to encapsulate (i.e. hide) a variable. We will call it an encapsulated variable. 5. Move the loadAsteroid function from Main.cpp to the Asteroid.cpp file and rename it to asteroidLoadImage. Adjust the names of the constants it uses and fix up the #includes in Asteroid.cpp. Note: Your Main.cpp will not compile again successfully until Part C, but Asteroid.cpp should compile. 6. Implement the asteroidinit function. It should initialize the Asteroid to be at row and column (0,0), to have row and column sizes of ROW_COUNT and COLUMN_COUNT, and to have a random ore value in the half-open interval [ORE_VALUE_MIN, ORE_VALUE_MAX). Hint: To assign a value of 25 to the min_row field of a variable named ast of the Asteroid type, you would type: ast.min row = 25; Hint: You can use the rand function in the C standard library to obtain a random number in the half-open interval [0, RAND_MAX). Then use the modulus operator (8) to convert the value to the range you want. For example, the following example yields an integer that has the possible values of 1, 2, 3, ..., 10: int includingland1 Onot11 = 1 + rand() % 10; You will need to #include . For each function in the next few steps, you should assume that some other part of the program has initialized a variable of the Asteroid type and that this variable is being passed into the function as a parameter. Thus, you can assume that the parameter of the Asteroid type has good values for all its fields. Your job is to put code into each of these functions to do one simple thing with the Asteroid parameter. So, for example, the asteroidGetMinRow function receives a good Asteroid as a parameter and it should just return the value of its field that contains the minimum row number. 7. Implement the asteroidGetMinRow function that returns the minimum row of that Asteroid. Implement the asteroidGetMinColumn, asteroidGetRowCount, and asteroidGetColumnCount functions in a similar manner. Hint: Each of these functions will contain a return statement and nothing else. 8. Implement the asteroidGetMaxRow function to return one less than the sum of the minimum row and the row count. Implement the asteroidGetMaxColumn function in a similar manner. 9. Implement the asteroidGetoreValue function to return the Asteroid's current ore value. 10. Implement the asteroidIs Dead function to return a bool indicating whether the ore value for the Asteroid is ORE VALUE_DEAD. 11. Implement the asteroidDraw function. The third parameter indicates how far the display has scrolled (measured in columns). The "image" of the Asteroid should be drawn to the Buffer at its current position, taking the number of columns scrolled into account. Add a precondition (enforced by an assert) that requires the number of columns scrolled to be non- negative. Hint: Start by finding your code from Assignment 1 that puts the asteroid image into the buffer. Move this code into the asteroidDraw function and then modify it as needed. Hint: You will need to include to use the assert function. You should read about preconditions and asserts in the online notes (search for "assert" in the Review 2 part of Section 1 and search for "Precondition" in section 4). 12. Add additional commands to the asteroidDraw function to draw the ore value at the same place as the "XX" in the Buffer. (Assume the ore value is already stored in the ore value field of the parameter of the Asteroid type.) You will need two new calls to buffersetcell to draw the two digits in the number. Hint: You can determine the value of the individual digits in the number using integer division (using the / operator on integer values and modulus (using the operator). Hint: If you have a single-digit number (e.g., 3) stored in an int variable named n, you can calculate the corresponding character value (e.g., '3') by computing: (char)n + 'O' 13. Implement the asteroidset Position and asteroidAddposition functions. The asteroidset Position function should move the asteroid to the row and column specified. The asteroidAddPosition function should increase the current row and column by the changes specified. Remember: A coordinate can be decreased by adding on a negative value. 14. Implement the asteroidRandomizeoreValue to set the ore value to a random number in the half-open interval [ORE_VALUE_MIN, ORE_VALUE_MAX). Note: Calculated the ore value the same way you did in asteroidinit. Your asteroidinit function can be rewritten to call asteroidRandomizeoreValue, although this is not required. Note: Changing the asteroid's ore value will mark it as not dead. This should be mentioned in the function documentation. 15. Implement the asteroidMarkDead function to set the ore value to ORE VALUE DEAD. 16. Write interface specifications in Asteroid.h for the asteroidLoadImage, asteroidInit, asteroidIsDead, and asteroidDraw functions in the format described in class and in Section 4a of the online notes. You do NOT need to write interface specifications for the other Asteroid functions Remember: The asteroidDraw function has a precondition. 17. Test your Asteroid type with the Main2A test program. If your Asteroid module is correct, it should print "*** PERFECT! ***" at the end. Hint: Comment out the existing contents of your Main.cpp file. Then copy the test program into the file. When you are finished with Part A, remove the test program and uncomment your code again
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