Snake vs Block Game to be run upon K64F.

Dependencies:   mbed

Committer:
AhmedPlaymaker
Date:
Thu May 09 11:23:16 2019 +0000
Revision:
101:5108621b207f
Parent:
99:4841f326200f
Child:
102:79d7bff6543f
added documentation to snake-test.h

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 101:5108621b207f 10 * @brief Check 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 75:f00c79f79b6a 57 /////////////// object ///////////////
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 99:4841f326200f 65 /**
AhmedPlaymaker 99:4841f326200f 66 * @brief Check that Snake object goes to correct position when moved and/ when collides.
AhmedPlaymaker 68:b9cfd27987ac 67 *
AhmedPlaymaker 99:4841f326200f 68 * @returns true if all the tests passed
AhmedPlaymaker 68:b9cfd27987ac 69 */
AhmedPlaymaker 68:b9cfd27987ac 70 bool Snake_test_movement()
AhmedPlaymaker 68:b9cfd27987ac 71 {
AhmedPlaymaker 75:f00c79f79b6a 72 success_flag = true;
AhmedPlaymaker 76:7fa91122907f 73
AhmedPlaymaker 75:f00c79f79b6a 74 success_flag = check_initialisation();
AhmedPlaymaker 75:f00c79f79b6a 75 success_flag = check_west_movement();
AhmedPlaymaker 75:f00c79f79b6a 76 success_flag = check_east_movement();
AhmedPlaymaker 75:f00c79f79b6a 77 success_flag = check_freeze_movement();
AhmedPlaymaker 75:f00c79f79b6a 78 success_flag = check_chain_reaction();
AhmedPlaymaker 75:f00c79f79b6a 79 success_flag = check_block_collision_and_relative_movement();
AhmedPlaymaker 75:f00c79f79b6a 80 success_flag = check_food_collision_and_relative_movement();
AhmedPlaymaker 75:f00c79f79b6a 81
AhmedPlaymaker 75:f00c79f79b6a 82 return success_flag;
AhmedPlaymaker 75:f00c79f79b6a 83 }
AhmedPlaymaker 75:f00c79f79b6a 84
AhmedPlaymaker 75:f00c79f79b6a 85 bool check_initialisation()
AhmedPlaymaker 75:f00c79f79b6a 86 {
AhmedPlaymaker 75:f00c79f79b6a 87
AhmedPlaymaker 75:f00c79f79b6a 88 // Change Snake to a length of 15, and x axis speed of 2.
AhmedPlaymaker 83:329da564799a 89 snake._setLength(15);
AhmedPlaymaker 83:329da564799a 90 snake._setSpeed(2);
AhmedPlaymaker 69:55e309da7efd 91
AhmedPlaymaker 68:b9cfd27987ac 92 // Set the position to WIDTH/2, HEIGHT - 3
AhmedPlaymaker 68:b9cfd27987ac 93
AhmedPlaymaker 68:b9cfd27987ac 94 Vector2D initial_pos = {WIDTH/2, HEIGHT - 3}; //Spawns player sprite near the middle of the screen.
AhmedPlaymaker 68:b9cfd27987ac 95 snake.set_pos(initial_pos);
AhmedPlaymaker 68:b9cfd27987ac 96
AhmedPlaymaker 68:b9cfd27987ac 97 // Read the position
AhmedPlaymaker 70:7caab8069b9b 98 Vector2D read_pos_1 = snake.get_pos(0);
AhmedPlaymaker 68:b9cfd27987ac 99 printf("%f, %f\n", read_pos_1.x, read_pos_1.y);
AhmedPlaymaker 76:7fa91122907f 100
AhmedPlaymaker 75:f00c79f79b6a 101 // Fail the test if the initial position is wrong
AhmedPlaymaker 75:f00c79f79b6a 102 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 103 success_flag = false;
AhmedPlaymaker 75:f00c79f79b6a 104 }
AhmedPlaymaker 75:f00c79f79b6a 105 return success_flag;
AhmedPlaymaker 75:f00c79f79b6a 106 }
AhmedPlaymaker 75:f00c79f79b6a 107
AhmedPlaymaker 75:f00c79f79b6a 108 bool check_west_movement()
AhmedPlaymaker 75:f00c79f79b6a 109 {
AhmedPlaymaker 68:b9cfd27987ac 110
AhmedPlaymaker 87:871d9fecb593 111 // Set the direction to W and set snake motion free by setting immobile_bead_n as 1;
AhmedPlaymaker 75:f00c79f79b6a 112 d = W;
AhmedPlaymaker 68:b9cfd27987ac 113
AhmedPlaymaker 70:7caab8069b9b 114 for(int i=0; i<=9; i++) {
AhmedPlaymaker 87:871d9fecb593 115 immobile_bead_n[i] = 1;
AhmedPlaymaker 68:b9cfd27987ac 116 }
AhmedPlaymaker 68:b9cfd27987ac 117
AhmedPlaymaker 68:b9cfd27987ac 118 // Update the snake position
AhmedPlaymaker 87:871d9fecb593 119 snake.update(d, immobile_bead_n);
AhmedPlaymaker 68:b9cfd27987ac 120
AhmedPlaymaker 68:b9cfd27987ac 121 // Read the position
AhmedPlaymaker 74:7b6568bc16d5 122 Vector2D read_pos_2 = snake.get_pos(0); //getting the position of the first beed
AhmedPlaymaker 68:b9cfd27987ac 123 printf("%f, %f\n", read_pos_2.x, read_pos_2.y);
AhmedPlaymaker 68:b9cfd27987ac 124
AhmedPlaymaker 75:f00c79f79b6a 125 // Fail the test if the final position after moving West is wrong
AhmedPlaymaker 75:f00c79f79b6a 126 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 127 success_flag = false;
AhmedPlaymaker 75:f00c79f79b6a 128 }
AhmedPlaymaker 75:f00c79f79b6a 129 return success_flag;
AhmedPlaymaker 75:f00c79f79b6a 130 }
AhmedPlaymaker 75:f00c79f79b6a 131
AhmedPlaymaker 75:f00c79f79b6a 132 bool check_east_movement()
AhmedPlaymaker 75:f00c79f79b6a 133 {
AhmedPlaymaker 75:f00c79f79b6a 134
AhmedPlaymaker 87:871d9fecb593 135 // Set the direction to E and set snake motion free by setting immobile_bead_n as 1;
AhmedPlaymaker 74:7b6568bc16d5 136 d = E;
AhmedPlaymaker 74:7b6568bc16d5 137
AhmedPlaymaker 74:7b6568bc16d5 138 for(int i=0; i<=9; i++) {
AhmedPlaymaker 87:871d9fecb593 139 immobile_bead_n[i] = 1;
AhmedPlaymaker 74:7b6568bc16d5 140 }
AhmedPlaymaker 74:7b6568bc16d5 141
AhmedPlaymaker 74:7b6568bc16d5 142 // Update the snake position
AhmedPlaymaker 87:871d9fecb593 143 snake.update(d, immobile_bead_n);
AhmedPlaymaker 74:7b6568bc16d5 144
AhmedPlaymaker 74:7b6568bc16d5 145 // Read the position
AhmedPlaymaker 74:7b6568bc16d5 146 Vector2D read_pos_3 = snake.get_pos(0); //getting the position of the first beed
AhmedPlaymaker 74:7b6568bc16d5 147 printf("%f, %f\n", read_pos_3.x, read_pos_3.y);
AhmedPlaymaker 74:7b6568bc16d5 148
AhmedPlaymaker 75:f00c79f79b6a 149 // Fail the test if the final position after moving East is wrong
AhmedPlaymaker 75:f00c79f79b6a 150 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 151 //and it moves back from its previous position.
AhmedPlaymaker 75:f00c79f79b6a 152 success_flag = false;
AhmedPlaymaker 75:f00c79f79b6a 153 }
AhmedPlaymaker 75:f00c79f79b6a 154 return success_flag;
AhmedPlaymaker 75:f00c79f79b6a 155 }
AhmedPlaymaker 74:7b6568bc16d5 156
AhmedPlaymaker 75:f00c79f79b6a 157 bool check_freeze_movement()
AhmedPlaymaker 75:f00c79f79b6a 158 {
AhmedPlaymaker 75:f00c79f79b6a 159
AhmedPlaymaker 87:871d9fecb593 160 // 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 161 d = W;
AhmedPlaymaker 83:329da564799a 162 snake._setLength(10);
AhmedPlaymaker 83:329da564799a 163 snake._setSpeed(10);
AhmedPlaymaker 74:7b6568bc16d5 164
AhmedPlaymaker 74:7b6568bc16d5 165 for(int i=0; i<=9; i++) {
AhmedPlaymaker 87:871d9fecb593 166 immobile_bead_n[i] = 0;
AhmedPlaymaker 74:7b6568bc16d5 167 }
AhmedPlaymaker 74:7b6568bc16d5 168
AhmedPlaymaker 74:7b6568bc16d5 169 // Update the snake position
AhmedPlaymaker 87:871d9fecb593 170 snake.update(d, immobile_bead_n);
AhmedPlaymaker 74:7b6568bc16d5 171
AhmedPlaymaker 74:7b6568bc16d5 172 // Read the position
AhmedPlaymaker 74:7b6568bc16d5 173 Vector2D read_pos_4 = snake.get_pos(0); //getting the position of the first beed
AhmedPlaymaker 74:7b6568bc16d5 174 printf("%f, %f\n", read_pos_4.x, read_pos_4.y);
AhmedPlaymaker 76:7fa91122907f 175
AhmedPlaymaker 87:871d9fecb593 176 // Fail the test if the final position after moving West but stopping motion using immobile_bead_n[i] = 0; is wrong.
AhmedPlaymaker 87:871d9fecb593 177 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 178 success_flag = false;
AhmedPlaymaker 75:f00c79f79b6a 179 }
AhmedPlaymaker 75:f00c79f79b6a 180 return success_flag;
AhmedPlaymaker 75:f00c79f79b6a 181 }
AhmedPlaymaker 74:7b6568bc16d5 182
AhmedPlaymaker 75:f00c79f79b6a 183 bool check_chain_reaction()
AhmedPlaymaker 75:f00c79f79b6a 184 {
AhmedPlaymaker 74:7b6568bc16d5 185
AhmedPlaymaker 87:871d9fecb593 186 // 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 187 d = W;
AhmedPlaymaker 83:329da564799a 188 snake._setLength(20);
AhmedPlaymaker 83:329da564799a 189 snake._setSpeed(2);
AhmedPlaymaker 74:7b6568bc16d5 190
AhmedPlaymaker 74:7b6568bc16d5 191 for(int i=0; i<=9; i++) {
AhmedPlaymaker 87:871d9fecb593 192 immobile_bead_n[i] = 1;
AhmedPlaymaker 74:7b6568bc16d5 193 }
AhmedPlaymaker 74:7b6568bc16d5 194
AhmedPlaymaker 87:871d9fecb593 195 // 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 196 snake.update(d, immobile_bead_n);
AhmedPlaymaker 87:871d9fecb593 197 snake.update(d, immobile_bead_n);
AhmedPlaymaker 87:871d9fecb593 198 snake.update(d, immobile_bead_n);
AhmedPlaymaker 74:7b6568bc16d5 199
AhmedPlaymaker 74:7b6568bc16d5 200 // Read the position
AhmedPlaymaker 74:7b6568bc16d5 201 Vector2D read_pos_5 = snake.get_pos(2); //getting the position of the third beed
AhmedPlaymaker 74:7b6568bc16d5 202 printf("%f, %f\n", read_pos_5.x, read_pos_5.y);
AhmedPlaymaker 76:7fa91122907f 203
AhmedPlaymaker 74:7b6568bc16d5 204 // Fail the test if the final position of the third beed after moving West and after 3 itterations; is wrong.
AhmedPlaymaker 87:871d9fecb593 205 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 206 success_flag = false;
AhmedPlaymaker 74:7b6568bc16d5 207 }
AhmedPlaymaker 68:b9cfd27987ac 208 return success_flag;
AhmedPlaymaker 68:b9cfd27987ac 209 }
AhmedPlaymaker 75:f00c79f79b6a 210
AhmedPlaymaker 75:f00c79f79b6a 211 bool check_block_collision_and_relative_movement()
AhmedPlaymaker 75:f00c79f79b6a 212 {
AhmedPlaymaker 75:f00c79f79b6a 213
AhmedPlaymaker 87:871d9fecb593 214 // Set the direction to E, length to 8 and set snake motion free by setting immobile_bead_n as 1;
AhmedPlaymaker 75:f00c79f79b6a 215 d = E;
AhmedPlaymaker 83:329da564799a 216 snake._setLength(8);
AhmedPlaymaker 75:f00c79f79b6a 217
AhmedPlaymaker 75:f00c79f79b6a 218 for(int i=0; i<=9; i++) {
AhmedPlaymaker 87:871d9fecb593 219 immobile_bead_n[i] = 1;
AhmedPlaymaker 75:f00c79f79b6a 220 }
AhmedPlaymaker 75:f00c79f79b6a 221
AhmedPlaymaker 75:f00c79f79b6a 222 // Update the snake position 2 times and then reduce length to 7 (this can simulate block collision of number 1).
AhmedPlaymaker 87:871d9fecb593 223 snake.update(d, immobile_bead_n);
AhmedPlaymaker 87:871d9fecb593 224 snake.update(d, immobile_bead_n);
AhmedPlaymaker 75:f00c79f79b6a 225
AhmedPlaymaker 83:329da564799a 226 snake._setLength(7);
AhmedPlaymaker 75:f00c79f79b6a 227 d = E;//now direction is set to east as it continues to move after collision and then update snake object again.
AhmedPlaymaker 87:871d9fecb593 228 snake.update(d, immobile_bead_n);
AhmedPlaymaker 75:f00c79f79b6a 229
AhmedPlaymaker 75:f00c79f79b6a 230 // Read the position
AhmedPlaymaker 75:f00c79f79b6a 231 Vector2D read_pos_6 = snake.get_pos(0); //getting the position of the first beed
AhmedPlaymaker 75:f00c79f79b6a 232 printf("%f, %f\n", read_pos_6.x, read_pos_6.y);
AhmedPlaymaker 76:7fa91122907f 233
AhmedPlaymaker 75:f00c79f79b6a 234 // 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 235 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 236 // 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 237 success_flag = false;
AhmedPlaymaker 75:f00c79f79b6a 238 }
AhmedPlaymaker 75:f00c79f79b6a 239 return success_flag;
AhmedPlaymaker 75:f00c79f79b6a 240 }
AhmedPlaymaker 75:f00c79f79b6a 241
AhmedPlaymaker 75:f00c79f79b6a 242 bool check_food_collision_and_relative_movement()
AhmedPlaymaker 75:f00c79f79b6a 243 {
AhmedPlaymaker 75:f00c79f79b6a 244
AhmedPlaymaker 87:871d9fecb593 245 // Set the direction to W, length to 7 and set snake motion free by setting immobile_bead_n as 1;
AhmedPlaymaker 75:f00c79f79b6a 246 d = W;
AhmedPlaymaker 83:329da564799a 247 snake._setLength(7);
AhmedPlaymaker 75:f00c79f79b6a 248
AhmedPlaymaker 75:f00c79f79b6a 249 for(int i=0; i<=9; i++) {
AhmedPlaymaker 87:871d9fecb593 250 immobile_bead_n[i] = 1;
AhmedPlaymaker 75:f00c79f79b6a 251 }
AhmedPlaymaker 75:f00c79f79b6a 252
AhmedPlaymaker 83:329da564799a 253 // 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 254 snake.update(d, immobile_bead_n);
AhmedPlaymaker 87:871d9fecb593 255 snake.update(d, immobile_bead_n);
AhmedPlaymaker 75:f00c79f79b6a 256
AhmedPlaymaker 83:329da564799a 257 snake._setLength(9);
AhmedPlaymaker 75:f00c79f79b6a 258 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 259 snake.update(d, immobile_bead_n);
AhmedPlaymaker 75:f00c79f79b6a 260
AhmedPlaymaker 75:f00c79f79b6a 261 // Read the position
AhmedPlaymaker 75:f00c79f79b6a 262 Vector2D read_pos_7 = snake.get_pos(0); //getting the position of the first beed
AhmedPlaymaker 75:f00c79f79b6a 263 printf("%f, %f\n", read_pos_7.x, read_pos_7.y);
AhmedPlaymaker 76:7fa91122907f 264
AhmedPlaymaker 75:f00c79f79b6a 265 // 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 266 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 267 // 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 268 success_flag = false;
AhmedPlaymaker 75:f00c79f79b6a 269 }
AhmedPlaymaker 75:f00c79f79b6a 270 return success_flag;
AhmedPlaymaker 75:f00c79f79b6a 271
AhmedPlaymaker 75:f00c79f79b6a 272 }
AhmedPlaymaker 75:f00c79f79b6a 273
AhmedPlaymaker 68:b9cfd27987ac 274 #endif