Snake vs Block Game to be run upon K64F.

Dependencies:   mbed

Committer:
AhmedPlaymaker
Date:
Thu May 09 14:52:19 2019 +0000
Revision:
104:17040265b7b4
Parent:
102:79d7bff6543f
Final Submission. I have read and agreed with Statement of Academic Integrity.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
AhmedPlaymaker 68:b9cfd27987ac 1 #ifndef SNAKE_TEST_H
AhmedPlaymaker 68:b9cfd27987ac 2 #define SNAKE_TEST_H
AhmedPlaymaker 68:b9cfd27987ac 3
AhmedPlaymaker 99:4841f326200f 4 /** @file Snake-test.h
AhmedPlaymaker 99:4841f326200f 5 * @brief Used to check response of the snake to different kinds of inputs.
AhmedPlaymaker 99:4841f326200f 6 */
AhmedPlaymaker 99:4841f326200f 7
AhmedPlaymaker 75:f00c79f79b6a 8 ///////////// prototypes //////////////
AhmedPlaymaker 101:5108621b207f 9 /**
AhmedPlaymaker 102:79d7bff6543f 10 * @brief Checks that Snake object goes to correct position when moved and/ when collides.
AhmedPlaymaker 101:5108621b207f 11 * @returns true if all the tests passed.
AhmedPlaymaker 101:5108621b207f 12 */
AhmedPlaymaker 101:5108621b207f 13 bool Snake_test_movement();
AhmedPlaymaker 101:5108621b207f 14
AhmedPlaymaker 101:5108621b207f 15 /**
AhmedPlaymaker 101:5108621b207f 16 * @brief Checks if that the snake gets initialised properly.
AhmedPlaymaker 101:5108621b207f 17 * @returns true if the test passed.
AhmedPlaymaker 101:5108621b207f 18 */
AhmedPlaymaker 75:f00c79f79b6a 19 bool check_initialisation();
AhmedPlaymaker 101:5108621b207f 20
AhmedPlaymaker 101:5108621b207f 21 /**
AhmedPlaymaker 101:5108621b207f 22 * @brief Checks that when the snake moves west, it updates as expected.
AhmedPlaymaker 101:5108621b207f 23 * @returns true if the test passed.
AhmedPlaymaker 101:5108621b207f 24 */
AhmedPlaymaker 75:f00c79f79b6a 25 bool check_west_movement();
AhmedPlaymaker 101:5108621b207f 26
AhmedPlaymaker 101:5108621b207f 27 /**
AhmedPlaymaker 101:5108621b207f 28 * @brief Checks that when the snake moves east, it updates as expected.
AhmedPlaymaker 101:5108621b207f 29 * @returns true if the test passed.
AhmedPlaymaker 101:5108621b207f 30 */
AhmedPlaymaker 75:f00c79f79b6a 31 bool check_east_movement();
AhmedPlaymaker 101:5108621b207f 32
AhmedPlaymaker 101:5108621b207f 33 /**
AhmedPlaymaker 101:5108621b207f 34 * @brief Checks that the snake properly freezes when the immobile_bead_n is disabled for all of them.
AhmedPlaymaker 101:5108621b207f 35 * @returns true if the test passed.
AhmedPlaymaker 101:5108621b207f 36 */
AhmedPlaymaker 75:f00c79f79b6a 37 bool check_freeze_movement();
AhmedPlaymaker 101:5108621b207f 38
AhmedPlaymaker 101:5108621b207f 39 /**
AhmedPlaymaker 101:5108621b207f 40 * @brief Checks that the snake properly exhibits chain reaction.
AhmedPlaymaker 101:5108621b207f 41 * @returns true if the test passed.
AhmedPlaymaker 101:5108621b207f 42 */
AhmedPlaymaker 75:f00c79f79b6a 43 bool check_chain_reaction();
AhmedPlaymaker 101:5108621b207f 44
AhmedPlaymaker 101:5108621b207f 45 /**
AhmedPlaymaker 101:5108621b207f 46 * @brief Checks that the snake length reduces accordingly when a block collision is simulated
AhmedPlaymaker 101:5108621b207f 47 * @returns true if the test passed.
AhmedPlaymaker 101:5108621b207f 48 */
AhmedPlaymaker 75:f00c79f79b6a 49 bool check_block_collision_and_relative_movement();
AhmedPlaymaker 101:5108621b207f 50
AhmedPlaymaker 101:5108621b207f 51 /**
AhmedPlaymaker 101:5108621b207f 52 * @brief Checks that the snake length increases accordingly when a food collision is simulated
AhmedPlaymaker 101:5108621b207f 53 * @returns true if the test passed.
AhmedPlaymaker 101:5108621b207f 54 */
AhmedPlaymaker 75:f00c79f79b6a 55 bool check_food_collision_and_relative_movement();
AhmedPlaymaker 75:f00c79f79b6a 56
AhmedPlaymaker 102:79d7bff6543f 57 /////////////// objects ///////////////
AhmedPlaymaker 75:f00c79f79b6a 58 Snake snake;
AhmedPlaymaker 75:f00c79f79b6a 59
AhmedPlaymaker 75:f00c79f79b6a 60 //GLOBALS//
AhmedPlaymaker 75:f00c79f79b6a 61 Direction d; //represents the direction of movement of the snake.
AhmedPlaymaker 87:871d9fecb593 62 int immobile_bead_n[10]; //each element in this array represents the beed number in the snake.
AhmedPlaymaker 75:f00c79f79b6a 63 bool success_flag; //stores success / failure from a test.
AhmedPlaymaker 75:f00c79f79b6a 64
AhmedPlaymaker 102:79d7bff6543f 65
AhmedPlaymaker 68:b9cfd27987ac 66 bool Snake_test_movement()
AhmedPlaymaker 68:b9cfd27987ac 67 {
AhmedPlaymaker 75:f00c79f79b6a 68 success_flag = true;
AhmedPlaymaker 76:7fa91122907f 69
AhmedPlaymaker 75:f00c79f79b6a 70 success_flag = check_initialisation();
AhmedPlaymaker 75:f00c79f79b6a 71 success_flag = check_west_movement();
AhmedPlaymaker 75:f00c79f79b6a 72 success_flag = check_east_movement();
AhmedPlaymaker 75:f00c79f79b6a 73 success_flag = check_freeze_movement();
AhmedPlaymaker 75:f00c79f79b6a 74 success_flag = check_chain_reaction();
AhmedPlaymaker 75:f00c79f79b6a 75 success_flag = check_block_collision_and_relative_movement();
AhmedPlaymaker 75:f00c79f79b6a 76 success_flag = check_food_collision_and_relative_movement();
AhmedPlaymaker 75:f00c79f79b6a 77
AhmedPlaymaker 75:f00c79f79b6a 78 return success_flag;
AhmedPlaymaker 75:f00c79f79b6a 79 }
AhmedPlaymaker 75:f00c79f79b6a 80
AhmedPlaymaker 75:f00c79f79b6a 81 bool check_initialisation()
AhmedPlaymaker 75:f00c79f79b6a 82 {
AhmedPlaymaker 75:f00c79f79b6a 83
AhmedPlaymaker 75:f00c79f79b6a 84 // Change Snake to a length of 15, and x axis speed of 2.
AhmedPlaymaker 83:329da564799a 85 snake._setLength(15);
AhmedPlaymaker 83:329da564799a 86 snake._setSpeed(2);
AhmedPlaymaker 69:55e309da7efd 87
AhmedPlaymaker 68:b9cfd27987ac 88 // Set the position to WIDTH/2, HEIGHT - 3
AhmedPlaymaker 68:b9cfd27987ac 89
AhmedPlaymaker 68:b9cfd27987ac 90 Vector2D initial_pos = {WIDTH/2, HEIGHT - 3}; //Spawns player sprite near the middle of the screen.
AhmedPlaymaker 68:b9cfd27987ac 91 snake.set_pos(initial_pos);
AhmedPlaymaker 68:b9cfd27987ac 92
AhmedPlaymaker 68:b9cfd27987ac 93 // Read the position
AhmedPlaymaker 70:7caab8069b9b 94 Vector2D read_pos_1 = snake.get_pos(0);
AhmedPlaymaker 68:b9cfd27987ac 95 printf("%f, %f\n", read_pos_1.x, read_pos_1.y);
AhmedPlaymaker 76:7fa91122907f 96
AhmedPlaymaker 75:f00c79f79b6a 97 // Fail the test if the initial position is wrong
AhmedPlaymaker 75:f00c79f79b6a 98 if (read_pos_1.x != WIDTH/2 || read_pos_1.y != 18) { //18 because I'm testing the position of the first snake beed, from the top.
AhmedPlaymaker 75:f00c79f79b6a 99 success_flag = false;
AhmedPlaymaker 75:f00c79f79b6a 100 }
AhmedPlaymaker 75:f00c79f79b6a 101 return success_flag;
AhmedPlaymaker 75:f00c79f79b6a 102 }
AhmedPlaymaker 75:f00c79f79b6a 103
AhmedPlaymaker 75:f00c79f79b6a 104 bool check_west_movement()
AhmedPlaymaker 75:f00c79f79b6a 105 {
AhmedPlaymaker 68:b9cfd27987ac 106
AhmedPlaymaker 87:871d9fecb593 107 // Set the direction to W and set snake motion free by setting immobile_bead_n as 1;
AhmedPlaymaker 75:f00c79f79b6a 108 d = W;
AhmedPlaymaker 68:b9cfd27987ac 109
AhmedPlaymaker 70:7caab8069b9b 110 for(int i=0; i<=9; i++) {
AhmedPlaymaker 87:871d9fecb593 111 immobile_bead_n[i] = 1;
AhmedPlaymaker 68:b9cfd27987ac 112 }
AhmedPlaymaker 68:b9cfd27987ac 113
AhmedPlaymaker 68:b9cfd27987ac 114 // Update the snake position
AhmedPlaymaker 87:871d9fecb593 115 snake.update(d, immobile_bead_n);
AhmedPlaymaker 68:b9cfd27987ac 116
AhmedPlaymaker 68:b9cfd27987ac 117 // Read the position
AhmedPlaymaker 74:7b6568bc16d5 118 Vector2D read_pos_2 = snake.get_pos(0); //getting the position of the first beed
AhmedPlaymaker 68:b9cfd27987ac 119 printf("%f, %f\n", read_pos_2.x, read_pos_2.y);
AhmedPlaymaker 68:b9cfd27987ac 120
AhmedPlaymaker 75:f00c79f79b6a 121 // Fail the test if the final position after moving West is wrong
AhmedPlaymaker 75:f00c79f79b6a 122 if (read_pos_2.x != ((WIDTH/2)-2) || read_pos_2.y != 18) { //18 because I'm testing the position of the first snake beed, from the top and ((WIDTH/2)-2) because speed is set to 2.
AhmedPlaymaker 75:f00c79f79b6a 123 success_flag = false;
AhmedPlaymaker 75:f00c79f79b6a 124 }
AhmedPlaymaker 75:f00c79f79b6a 125 return success_flag;
AhmedPlaymaker 75:f00c79f79b6a 126 }
AhmedPlaymaker 75:f00c79f79b6a 127
AhmedPlaymaker 75:f00c79f79b6a 128 bool check_east_movement()
AhmedPlaymaker 75:f00c79f79b6a 129 {
AhmedPlaymaker 75:f00c79f79b6a 130
AhmedPlaymaker 87:871d9fecb593 131 // Set the direction to E and set snake motion free by setting immobile_bead_n as 1;
AhmedPlaymaker 74:7b6568bc16d5 132 d = E;
AhmedPlaymaker 74:7b6568bc16d5 133
AhmedPlaymaker 74:7b6568bc16d5 134 for(int i=0; i<=9; i++) {
AhmedPlaymaker 87:871d9fecb593 135 immobile_bead_n[i] = 1;
AhmedPlaymaker 74:7b6568bc16d5 136 }
AhmedPlaymaker 74:7b6568bc16d5 137
AhmedPlaymaker 74:7b6568bc16d5 138 // Update the snake position
AhmedPlaymaker 87:871d9fecb593 139 snake.update(d, immobile_bead_n);
AhmedPlaymaker 74:7b6568bc16d5 140
AhmedPlaymaker 74:7b6568bc16d5 141 // Read the position
AhmedPlaymaker 74:7b6568bc16d5 142 Vector2D read_pos_3 = snake.get_pos(0); //getting the position of the first beed
AhmedPlaymaker 74:7b6568bc16d5 143 printf("%f, %f\n", read_pos_3.x, read_pos_3.y);
AhmedPlaymaker 74:7b6568bc16d5 144
AhmedPlaymaker 75:f00c79f79b6a 145 // Fail the test if the final position after moving East is wrong
AhmedPlaymaker 75:f00c79f79b6a 146 if (read_pos_3.x != ((WIDTH/2)) || read_pos_3.y != 18) { //18 because I'm testing the position of the first snake beed, from the top and ((WIDTH/2)) because speed is set to 2
AhmedPlaymaker 75:f00c79f79b6a 147 //and it moves back from its previous position.
AhmedPlaymaker 75:f00c79f79b6a 148 success_flag = false;
AhmedPlaymaker 75:f00c79f79b6a 149 }
AhmedPlaymaker 75:f00c79f79b6a 150 return success_flag;
AhmedPlaymaker 75:f00c79f79b6a 151 }
AhmedPlaymaker 74:7b6568bc16d5 152
AhmedPlaymaker 75:f00c79f79b6a 153 bool check_freeze_movement()
AhmedPlaymaker 75:f00c79f79b6a 154 {
AhmedPlaymaker 75:f00c79f79b6a 155
AhmedPlaymaker 87:871d9fecb593 156 // Set the direction to W, length and speed both to 10 and set snake motion stopped by setting immobile_bead_n as 0 (simulates block sides collision) ;
AhmedPlaymaker 74:7b6568bc16d5 157 d = W;
AhmedPlaymaker 83:329da564799a 158 snake._setLength(10);
AhmedPlaymaker 83:329da564799a 159 snake._setSpeed(10);
AhmedPlaymaker 74:7b6568bc16d5 160
AhmedPlaymaker 74:7b6568bc16d5 161 for(int i=0; i<=9; i++) {
AhmedPlaymaker 87:871d9fecb593 162 immobile_bead_n[i] = 0;
AhmedPlaymaker 74:7b6568bc16d5 163 }
AhmedPlaymaker 74:7b6568bc16d5 164
AhmedPlaymaker 74:7b6568bc16d5 165 // Update the snake position
AhmedPlaymaker 87:871d9fecb593 166 snake.update(d, immobile_bead_n);
AhmedPlaymaker 74:7b6568bc16d5 167
AhmedPlaymaker 74:7b6568bc16d5 168 // Read the position
AhmedPlaymaker 74:7b6568bc16d5 169 Vector2D read_pos_4 = snake.get_pos(0); //getting the position of the first beed
AhmedPlaymaker 74:7b6568bc16d5 170 printf("%f, %f\n", read_pos_4.x, read_pos_4.y);
AhmedPlaymaker 76:7fa91122907f 171
AhmedPlaymaker 87:871d9fecb593 172 // Fail the test if the final position after moving West but stopping motion using immobile_bead_n[i] = 0; is wrong.
AhmedPlaymaker 87:871d9fecb593 173 if (read_pos_4.x != ((WIDTH/2)) || read_pos_4.y != 18) { //18 because I'm testing the position of the first snake beed, from the top and ((WIDTH/2)) because it is not meant to move.
AhmedPlaymaker 75:f00c79f79b6a 174 success_flag = false;
AhmedPlaymaker 75:f00c79f79b6a 175 }
AhmedPlaymaker 75:f00c79f79b6a 176 return success_flag;
AhmedPlaymaker 75:f00c79f79b6a 177 }
AhmedPlaymaker 74:7b6568bc16d5 178
AhmedPlaymaker 75:f00c79f79b6a 179 bool check_chain_reaction()
AhmedPlaymaker 75:f00c79f79b6a 180 {
AhmedPlaymaker 74:7b6568bc16d5 181
AhmedPlaymaker 87:871d9fecb593 182 // Set the direction to W, length to 20 and speed to 2 and set snake motion free by setting immobile_bead_n as 1;
AhmedPlaymaker 74:7b6568bc16d5 183 d = W;
AhmedPlaymaker 83:329da564799a 184 snake._setLength(20);
AhmedPlaymaker 83:329da564799a 185 snake._setSpeed(2);
AhmedPlaymaker 74:7b6568bc16d5 186
AhmedPlaymaker 74:7b6568bc16d5 187 for(int i=0; i<=9; i++) {
AhmedPlaymaker 87:871d9fecb593 188 immobile_bead_n[i] = 1;
AhmedPlaymaker 74:7b6568bc16d5 189 }
AhmedPlaymaker 74:7b6568bc16d5 190
AhmedPlaymaker 87:871d9fecb593 191 // Update the snake position 3 times because i want to read the position of the third bead that only moves with a chain reaction.
AhmedPlaymaker 87:871d9fecb593 192 snake.update(d, immobile_bead_n);
AhmedPlaymaker 87:871d9fecb593 193 snake.update(d, immobile_bead_n);
AhmedPlaymaker 87:871d9fecb593 194 snake.update(d, immobile_bead_n);
AhmedPlaymaker 74:7b6568bc16d5 195
AhmedPlaymaker 74:7b6568bc16d5 196 // Read the position
AhmedPlaymaker 74:7b6568bc16d5 197 Vector2D read_pos_5 = snake.get_pos(2); //getting the position of the third beed
AhmedPlaymaker 74:7b6568bc16d5 198 printf("%f, %f\n", read_pos_5.x, read_pos_5.y);
AhmedPlaymaker 76:7fa91122907f 199
AhmedPlaymaker 74:7b6568bc16d5 200 // Fail the test if the final position of the third beed after moving West and after 3 itterations; is wrong.
AhmedPlaymaker 87:871d9fecb593 201 if (read_pos_5.x != ((WIDTH/2)-2) || read_pos_5.y != 24) { //24 because I'm testing the position of the third snake beed, from the top and ((WIDTH/2)) because it is not meant to move.
AhmedPlaymaker 74:7b6568bc16d5 202 success_flag = false;
AhmedPlaymaker 74:7b6568bc16d5 203 }
AhmedPlaymaker 68:b9cfd27987ac 204 return success_flag;
AhmedPlaymaker 68:b9cfd27987ac 205 }
AhmedPlaymaker 75:f00c79f79b6a 206
AhmedPlaymaker 75:f00c79f79b6a 207 bool check_block_collision_and_relative_movement()
AhmedPlaymaker 75:f00c79f79b6a 208 {
AhmedPlaymaker 75:f00c79f79b6a 209
AhmedPlaymaker 87:871d9fecb593 210 // Set the direction to E, length to 8 and set snake motion free by setting immobile_bead_n as 1;
AhmedPlaymaker 75:f00c79f79b6a 211 d = E;
AhmedPlaymaker 83:329da564799a 212 snake._setLength(8);
AhmedPlaymaker 75:f00c79f79b6a 213
AhmedPlaymaker 75:f00c79f79b6a 214 for(int i=0; i<=9; i++) {
AhmedPlaymaker 87:871d9fecb593 215 immobile_bead_n[i] = 1;
AhmedPlaymaker 75:f00c79f79b6a 216 }
AhmedPlaymaker 75:f00c79f79b6a 217
AhmedPlaymaker 75:f00c79f79b6a 218 // Update the snake position 2 times and then reduce length to 7 (this can simulate block collision of number 1).
AhmedPlaymaker 87:871d9fecb593 219 snake.update(d, immobile_bead_n);
AhmedPlaymaker 87:871d9fecb593 220 snake.update(d, immobile_bead_n);
AhmedPlaymaker 75:f00c79f79b6a 221
AhmedPlaymaker 83:329da564799a 222 snake._setLength(7);
AhmedPlaymaker 75:f00c79f79b6a 223 d = E;//now direction is set to east as it continues to move after collision and then update snake object again.
AhmedPlaymaker 87:871d9fecb593 224 snake.update(d, immobile_bead_n);
AhmedPlaymaker 75:f00c79f79b6a 225
AhmedPlaymaker 75:f00c79f79b6a 226 // Read the position
AhmedPlaymaker 75:f00c79f79b6a 227 Vector2D read_pos_6 = snake.get_pos(0); //getting the position of the first beed
AhmedPlaymaker 75:f00c79f79b6a 228 printf("%f, %f\n", read_pos_6.x, read_pos_6.y);
AhmedPlaymaker 76:7fa91122907f 229
AhmedPlaymaker 75:f00c79f79b6a 230 // Fail the test if the final position of the first beed after moving East 3 times and colliding with block numbered 1; is wrong.
AhmedPlaymaker 75:f00c79f79b6a 231 if (read_pos_6.x != ((WIDTH/2)+2) || read_pos_6.y != 27) { //27 because I'm testing the position of the first snake beed of length 8, from the top
AhmedPlaymaker 87:871d9fecb593 232 // and colliding once with a block numbered 1, read_pos_6.x = ((WIDTH/2)+2) because the 7th beed from bottom moved only 2 times in effect.
AhmedPlaymaker 75:f00c79f79b6a 233 success_flag = false;
AhmedPlaymaker 75:f00c79f79b6a 234 }
AhmedPlaymaker 75:f00c79f79b6a 235 return success_flag;
AhmedPlaymaker 75:f00c79f79b6a 236 }
AhmedPlaymaker 75:f00c79f79b6a 237
AhmedPlaymaker 75:f00c79f79b6a 238 bool check_food_collision_and_relative_movement()
AhmedPlaymaker 75:f00c79f79b6a 239 {
AhmedPlaymaker 75:f00c79f79b6a 240
AhmedPlaymaker 87:871d9fecb593 241 // Set the direction to W, length to 7 and set snake motion free by setting immobile_bead_n as 1;
AhmedPlaymaker 75:f00c79f79b6a 242 d = W;
AhmedPlaymaker 83:329da564799a 243 snake._setLength(7);
AhmedPlaymaker 75:f00c79f79b6a 244
AhmedPlaymaker 75:f00c79f79b6a 245 for(int i=0; i<=9; i++) {
AhmedPlaymaker 87:871d9fecb593 246 immobile_bead_n[i] = 1;
AhmedPlaymaker 75:f00c79f79b6a 247 }
AhmedPlaymaker 75:f00c79f79b6a 248
AhmedPlaymaker 83:329da564799a 249 // Update the snake position 2 times (2 game loops) and then increase length to 9 (this can simulate food collision of 2 times).
AhmedPlaymaker 87:871d9fecb593 250 snake.update(d, immobile_bead_n);
AhmedPlaymaker 87:871d9fecb593 251 snake.update(d, immobile_bead_n);
AhmedPlaymaker 75:f00c79f79b6a 252
AhmedPlaymaker 83:329da564799a 253 snake._setLength(9);
AhmedPlaymaker 75:f00c79f79b6a 254 d = W;//now direction is set to west as it continues to move after food collision and then update snake object again.
AhmedPlaymaker 87:871d9fecb593 255 snake.update(d, immobile_bead_n);
AhmedPlaymaker 75:f00c79f79b6a 256
AhmedPlaymaker 75:f00c79f79b6a 257 // Read the position
AhmedPlaymaker 75:f00c79f79b6a 258 Vector2D read_pos_7 = snake.get_pos(0); //getting the position of the first beed
AhmedPlaymaker 75:f00c79f79b6a 259 printf("%f, %f\n", read_pos_7.x, read_pos_7.y);
AhmedPlaymaker 76:7fa91122907f 260
AhmedPlaymaker 75:f00c79f79b6a 261 // Fail the test if the final position of the first beed after moving East 3 times and colliding with block numbered 1; is wrong.
AhmedPlaymaker 75:f00c79f79b6a 262 if (read_pos_7.x != ((WIDTH/2)-4) || read_pos_7.y != 21) { //21 because I'm testing the position of the first snake beed of length 9, from the top
AhmedPlaymaker 87:871d9fecb593 263 // and colliding twice with food, read_pos_7.x = ((WIDTH/2)-4) because the current top beed moved same number of times to the top beed before it ate food.
AhmedPlaymaker 75:f00c79f79b6a 264 success_flag = false;
AhmedPlaymaker 75:f00c79f79b6a 265 }
AhmedPlaymaker 75:f00c79f79b6a 266 return success_flag;
AhmedPlaymaker 75:f00c79f79b6a 267
AhmedPlaymaker 75:f00c79f79b6a 268 }
AhmedPlaymaker 75:f00c79f79b6a 269
AhmedPlaymaker 68:b9cfd27987ac 270 #endif