Snake vs Block Game to be run upon K64F.

Dependencies:   mbed

Committer:
AhmedPlaymaker
Date:
Wed May 08 16:39:24 2019 +0000
Revision:
87:871d9fecb593
Parent:
83:329da564799a
Child:
99:4841f326200f
I have changed some variable names so that they have more meaning, and also added some more comments

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 75:f00c79f79b6a 4 ///////////// prototypes //////////////
AhmedPlaymaker 75:f00c79f79b6a 5 bool check_initialisation();
AhmedPlaymaker 75:f00c79f79b6a 6 bool check_west_movement();
AhmedPlaymaker 75:f00c79f79b6a 7 bool check_east_movement();
AhmedPlaymaker 75:f00c79f79b6a 8 bool check_freeze_movement();
AhmedPlaymaker 75:f00c79f79b6a 9 bool check_chain_reaction();
AhmedPlaymaker 75:f00c79f79b6a 10 bool check_block_collision_and_relative_movement();
AhmedPlaymaker 75:f00c79f79b6a 11 bool check_food_collision_and_relative_movement();
AhmedPlaymaker 75:f00c79f79b6a 12
AhmedPlaymaker 75:f00c79f79b6a 13 /////////////// object ///////////////
AhmedPlaymaker 75:f00c79f79b6a 14 Snake snake;
AhmedPlaymaker 75:f00c79f79b6a 15
AhmedPlaymaker 75:f00c79f79b6a 16 //GLOBALS//
AhmedPlaymaker 75:f00c79f79b6a 17 Direction d; //represents the direction of movement of the snake.
AhmedPlaymaker 87:871d9fecb593 18 int immobile_bead_n[10]; //each element in this array represents the beed number in the snake.
AhmedPlaymaker 75:f00c79f79b6a 19 bool success_flag; //stores success / failure from a test.
AhmedPlaymaker 75:f00c79f79b6a 20
AhmedPlaymaker 68:b9cfd27987ac 21 /**
AhmedPlaymaker 75:f00c79f79b6a 22 * \brief Check that Snake object goes to correct position when moved and/ when collides.
AhmedPlaymaker 68:b9cfd27987ac 23 *
AhmedPlaymaker 68:b9cfd27987ac 24 * \returns true if all the tests passed
AhmedPlaymaker 68:b9cfd27987ac 25 */
AhmedPlaymaker 68:b9cfd27987ac 26 bool Snake_test_movement()
AhmedPlaymaker 68:b9cfd27987ac 27 {
AhmedPlaymaker 75:f00c79f79b6a 28 success_flag = true;
AhmedPlaymaker 76:7fa91122907f 29
AhmedPlaymaker 75:f00c79f79b6a 30 success_flag = check_initialisation();
AhmedPlaymaker 75:f00c79f79b6a 31 success_flag = check_west_movement();
AhmedPlaymaker 75:f00c79f79b6a 32 success_flag = check_east_movement();
AhmedPlaymaker 75:f00c79f79b6a 33 success_flag = check_freeze_movement();
AhmedPlaymaker 75:f00c79f79b6a 34 success_flag = check_chain_reaction();
AhmedPlaymaker 75:f00c79f79b6a 35 success_flag = check_block_collision_and_relative_movement();
AhmedPlaymaker 75:f00c79f79b6a 36 success_flag = check_food_collision_and_relative_movement();
AhmedPlaymaker 75:f00c79f79b6a 37
AhmedPlaymaker 75:f00c79f79b6a 38 return success_flag;
AhmedPlaymaker 75:f00c79f79b6a 39 }
AhmedPlaymaker 75:f00c79f79b6a 40
AhmedPlaymaker 75:f00c79f79b6a 41 bool check_initialisation()
AhmedPlaymaker 75:f00c79f79b6a 42 {
AhmedPlaymaker 75:f00c79f79b6a 43
AhmedPlaymaker 75:f00c79f79b6a 44 // Change Snake to a length of 15, and x axis speed of 2.
AhmedPlaymaker 83:329da564799a 45 snake._setLength(15);
AhmedPlaymaker 83:329da564799a 46 snake._setSpeed(2);
AhmedPlaymaker 69:55e309da7efd 47
AhmedPlaymaker 68:b9cfd27987ac 48 // Set the position to WIDTH/2, HEIGHT - 3
AhmedPlaymaker 68:b9cfd27987ac 49
AhmedPlaymaker 68:b9cfd27987ac 50 Vector2D initial_pos = {WIDTH/2, HEIGHT - 3}; //Spawns player sprite near the middle of the screen.
AhmedPlaymaker 68:b9cfd27987ac 51 snake.set_pos(initial_pos);
AhmedPlaymaker 68:b9cfd27987ac 52
AhmedPlaymaker 68:b9cfd27987ac 53 // Read the position
AhmedPlaymaker 70:7caab8069b9b 54 Vector2D read_pos_1 = snake.get_pos(0);
AhmedPlaymaker 68:b9cfd27987ac 55 printf("%f, %f\n", read_pos_1.x, read_pos_1.y);
AhmedPlaymaker 76:7fa91122907f 56
AhmedPlaymaker 75:f00c79f79b6a 57 // Fail the test if the initial position is wrong
AhmedPlaymaker 75:f00c79f79b6a 58 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 59 success_flag = false;
AhmedPlaymaker 75:f00c79f79b6a 60 }
AhmedPlaymaker 75:f00c79f79b6a 61 return success_flag;
AhmedPlaymaker 75:f00c79f79b6a 62 }
AhmedPlaymaker 75:f00c79f79b6a 63
AhmedPlaymaker 75:f00c79f79b6a 64 bool check_west_movement()
AhmedPlaymaker 75:f00c79f79b6a 65 {
AhmedPlaymaker 68:b9cfd27987ac 66
AhmedPlaymaker 87:871d9fecb593 67 // Set the direction to W and set snake motion free by setting immobile_bead_n as 1;
AhmedPlaymaker 75:f00c79f79b6a 68 d = W;
AhmedPlaymaker 68:b9cfd27987ac 69
AhmedPlaymaker 70:7caab8069b9b 70 for(int i=0; i<=9; i++) {
AhmedPlaymaker 87:871d9fecb593 71 immobile_bead_n[i] = 1;
AhmedPlaymaker 68:b9cfd27987ac 72 }
AhmedPlaymaker 68:b9cfd27987ac 73
AhmedPlaymaker 68:b9cfd27987ac 74 // Update the snake position
AhmedPlaymaker 87:871d9fecb593 75 snake.update(d, immobile_bead_n);
AhmedPlaymaker 68:b9cfd27987ac 76
AhmedPlaymaker 68:b9cfd27987ac 77 // Read the position
AhmedPlaymaker 74:7b6568bc16d5 78 Vector2D read_pos_2 = snake.get_pos(0); //getting the position of the first beed
AhmedPlaymaker 68:b9cfd27987ac 79 printf("%f, %f\n", read_pos_2.x, read_pos_2.y);
AhmedPlaymaker 68:b9cfd27987ac 80
AhmedPlaymaker 75:f00c79f79b6a 81 // Fail the test if the final position after moving West is wrong
AhmedPlaymaker 75:f00c79f79b6a 82 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 83 success_flag = false;
AhmedPlaymaker 75:f00c79f79b6a 84 }
AhmedPlaymaker 75:f00c79f79b6a 85 return success_flag;
AhmedPlaymaker 75:f00c79f79b6a 86 }
AhmedPlaymaker 75:f00c79f79b6a 87
AhmedPlaymaker 75:f00c79f79b6a 88 bool check_east_movement()
AhmedPlaymaker 75:f00c79f79b6a 89 {
AhmedPlaymaker 75:f00c79f79b6a 90
AhmedPlaymaker 87:871d9fecb593 91 // Set the direction to E and set snake motion free by setting immobile_bead_n as 1;
AhmedPlaymaker 74:7b6568bc16d5 92 d = E;
AhmedPlaymaker 74:7b6568bc16d5 93
AhmedPlaymaker 74:7b6568bc16d5 94 for(int i=0; i<=9; i++) {
AhmedPlaymaker 87:871d9fecb593 95 immobile_bead_n[i] = 1;
AhmedPlaymaker 74:7b6568bc16d5 96 }
AhmedPlaymaker 74:7b6568bc16d5 97
AhmedPlaymaker 74:7b6568bc16d5 98 // Update the snake position
AhmedPlaymaker 87:871d9fecb593 99 snake.update(d, immobile_bead_n);
AhmedPlaymaker 74:7b6568bc16d5 100
AhmedPlaymaker 74:7b6568bc16d5 101 // Read the position
AhmedPlaymaker 74:7b6568bc16d5 102 Vector2D read_pos_3 = snake.get_pos(0); //getting the position of the first beed
AhmedPlaymaker 74:7b6568bc16d5 103 printf("%f, %f\n", read_pos_3.x, read_pos_3.y);
AhmedPlaymaker 74:7b6568bc16d5 104
AhmedPlaymaker 75:f00c79f79b6a 105 // Fail the test if the final position after moving East is wrong
AhmedPlaymaker 75:f00c79f79b6a 106 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 107 //and it moves back from its previous position.
AhmedPlaymaker 75:f00c79f79b6a 108 success_flag = false;
AhmedPlaymaker 75:f00c79f79b6a 109 }
AhmedPlaymaker 75:f00c79f79b6a 110 return success_flag;
AhmedPlaymaker 75:f00c79f79b6a 111 }
AhmedPlaymaker 74:7b6568bc16d5 112
AhmedPlaymaker 75:f00c79f79b6a 113 bool check_freeze_movement()
AhmedPlaymaker 75:f00c79f79b6a 114 {
AhmedPlaymaker 75:f00c79f79b6a 115
AhmedPlaymaker 87:871d9fecb593 116 // 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 117 d = W;
AhmedPlaymaker 83:329da564799a 118 snake._setLength(10);
AhmedPlaymaker 83:329da564799a 119 snake._setSpeed(10);
AhmedPlaymaker 74:7b6568bc16d5 120
AhmedPlaymaker 74:7b6568bc16d5 121 for(int i=0; i<=9; i++) {
AhmedPlaymaker 87:871d9fecb593 122 immobile_bead_n[i] = 0;
AhmedPlaymaker 74:7b6568bc16d5 123 }
AhmedPlaymaker 74:7b6568bc16d5 124
AhmedPlaymaker 74:7b6568bc16d5 125 // Update the snake position
AhmedPlaymaker 87:871d9fecb593 126 snake.update(d, immobile_bead_n);
AhmedPlaymaker 74:7b6568bc16d5 127
AhmedPlaymaker 74:7b6568bc16d5 128 // Read the position
AhmedPlaymaker 74:7b6568bc16d5 129 Vector2D read_pos_4 = snake.get_pos(0); //getting the position of the first beed
AhmedPlaymaker 74:7b6568bc16d5 130 printf("%f, %f\n", read_pos_4.x, read_pos_4.y);
AhmedPlaymaker 76:7fa91122907f 131
AhmedPlaymaker 87:871d9fecb593 132 // Fail the test if the final position after moving West but stopping motion using immobile_bead_n[i] = 0; is wrong.
AhmedPlaymaker 87:871d9fecb593 133 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 134 success_flag = false;
AhmedPlaymaker 75:f00c79f79b6a 135 }
AhmedPlaymaker 75:f00c79f79b6a 136 return success_flag;
AhmedPlaymaker 75:f00c79f79b6a 137 }
AhmedPlaymaker 74:7b6568bc16d5 138
AhmedPlaymaker 75:f00c79f79b6a 139 bool check_chain_reaction()
AhmedPlaymaker 75:f00c79f79b6a 140 {
AhmedPlaymaker 74:7b6568bc16d5 141
AhmedPlaymaker 87:871d9fecb593 142 // 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 143 d = W;
AhmedPlaymaker 83:329da564799a 144 snake._setLength(20);
AhmedPlaymaker 83:329da564799a 145 snake._setSpeed(2);
AhmedPlaymaker 74:7b6568bc16d5 146
AhmedPlaymaker 74:7b6568bc16d5 147 for(int i=0; i<=9; i++) {
AhmedPlaymaker 87:871d9fecb593 148 immobile_bead_n[i] = 1;
AhmedPlaymaker 74:7b6568bc16d5 149 }
AhmedPlaymaker 74:7b6568bc16d5 150
AhmedPlaymaker 87:871d9fecb593 151 // 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 152 snake.update(d, immobile_bead_n);
AhmedPlaymaker 87:871d9fecb593 153 snake.update(d, immobile_bead_n);
AhmedPlaymaker 87:871d9fecb593 154 snake.update(d, immobile_bead_n);
AhmedPlaymaker 74:7b6568bc16d5 155
AhmedPlaymaker 74:7b6568bc16d5 156 // Read the position
AhmedPlaymaker 74:7b6568bc16d5 157 Vector2D read_pos_5 = snake.get_pos(2); //getting the position of the third beed
AhmedPlaymaker 74:7b6568bc16d5 158 printf("%f, %f\n", read_pos_5.x, read_pos_5.y);
AhmedPlaymaker 76:7fa91122907f 159
AhmedPlaymaker 74:7b6568bc16d5 160 // Fail the test if the final position of the third beed after moving West and after 3 itterations; is wrong.
AhmedPlaymaker 87:871d9fecb593 161 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 162 success_flag = false;
AhmedPlaymaker 74:7b6568bc16d5 163 }
AhmedPlaymaker 68:b9cfd27987ac 164 return success_flag;
AhmedPlaymaker 68:b9cfd27987ac 165 }
AhmedPlaymaker 75:f00c79f79b6a 166
AhmedPlaymaker 75:f00c79f79b6a 167 bool check_block_collision_and_relative_movement()
AhmedPlaymaker 75:f00c79f79b6a 168 {
AhmedPlaymaker 75:f00c79f79b6a 169
AhmedPlaymaker 87:871d9fecb593 170 // Set the direction to E, length to 8 and set snake motion free by setting immobile_bead_n as 1;
AhmedPlaymaker 75:f00c79f79b6a 171 d = E;
AhmedPlaymaker 83:329da564799a 172 snake._setLength(8);
AhmedPlaymaker 75:f00c79f79b6a 173
AhmedPlaymaker 75:f00c79f79b6a 174 for(int i=0; i<=9; i++) {
AhmedPlaymaker 87:871d9fecb593 175 immobile_bead_n[i] = 1;
AhmedPlaymaker 75:f00c79f79b6a 176 }
AhmedPlaymaker 75:f00c79f79b6a 177
AhmedPlaymaker 75:f00c79f79b6a 178 // Update the snake position 2 times and then reduce length to 7 (this can simulate block collision of number 1).
AhmedPlaymaker 87:871d9fecb593 179 snake.update(d, immobile_bead_n);
AhmedPlaymaker 87:871d9fecb593 180 snake.update(d, immobile_bead_n);
AhmedPlaymaker 75:f00c79f79b6a 181
AhmedPlaymaker 83:329da564799a 182 snake._setLength(7);
AhmedPlaymaker 75:f00c79f79b6a 183 d = E;//now direction is set to east as it continues to move after collision and then update snake object again.
AhmedPlaymaker 87:871d9fecb593 184 snake.update(d, immobile_bead_n);
AhmedPlaymaker 75:f00c79f79b6a 185
AhmedPlaymaker 75:f00c79f79b6a 186 // Read the position
AhmedPlaymaker 75:f00c79f79b6a 187 Vector2D read_pos_6 = snake.get_pos(0); //getting the position of the first beed
AhmedPlaymaker 75:f00c79f79b6a 188 printf("%f, %f\n", read_pos_6.x, read_pos_6.y);
AhmedPlaymaker 76:7fa91122907f 189
AhmedPlaymaker 75:f00c79f79b6a 190 // 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 191 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 192 // 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 193 success_flag = false;
AhmedPlaymaker 75:f00c79f79b6a 194 }
AhmedPlaymaker 75:f00c79f79b6a 195 return success_flag;
AhmedPlaymaker 75:f00c79f79b6a 196 }
AhmedPlaymaker 75:f00c79f79b6a 197
AhmedPlaymaker 75:f00c79f79b6a 198 bool check_food_collision_and_relative_movement()
AhmedPlaymaker 75:f00c79f79b6a 199 {
AhmedPlaymaker 75:f00c79f79b6a 200
AhmedPlaymaker 87:871d9fecb593 201 // Set the direction to W, length to 7 and set snake motion free by setting immobile_bead_n as 1;
AhmedPlaymaker 75:f00c79f79b6a 202 d = W;
AhmedPlaymaker 83:329da564799a 203 snake._setLength(7);
AhmedPlaymaker 75:f00c79f79b6a 204
AhmedPlaymaker 75:f00c79f79b6a 205 for(int i=0; i<=9; i++) {
AhmedPlaymaker 87:871d9fecb593 206 immobile_bead_n[i] = 1;
AhmedPlaymaker 75:f00c79f79b6a 207 }
AhmedPlaymaker 75:f00c79f79b6a 208
AhmedPlaymaker 83:329da564799a 209 // 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 210 snake.update(d, immobile_bead_n);
AhmedPlaymaker 87:871d9fecb593 211 snake.update(d, immobile_bead_n);
AhmedPlaymaker 75:f00c79f79b6a 212
AhmedPlaymaker 83:329da564799a 213 snake._setLength(9);
AhmedPlaymaker 75:f00c79f79b6a 214 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 215 snake.update(d, immobile_bead_n);
AhmedPlaymaker 75:f00c79f79b6a 216
AhmedPlaymaker 75:f00c79f79b6a 217 // Read the position
AhmedPlaymaker 75:f00c79f79b6a 218 Vector2D read_pos_7 = snake.get_pos(0); //getting the position of the first beed
AhmedPlaymaker 75:f00c79f79b6a 219 printf("%f, %f\n", read_pos_7.x, read_pos_7.y);
AhmedPlaymaker 76:7fa91122907f 220
AhmedPlaymaker 75:f00c79f79b6a 221 // 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 222 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 223 // 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 224 success_flag = false;
AhmedPlaymaker 75:f00c79f79b6a 225 }
AhmedPlaymaker 75:f00c79f79b6a 226 return success_flag;
AhmedPlaymaker 75:f00c79f79b6a 227
AhmedPlaymaker 75:f00c79f79b6a 228 }
AhmedPlaymaker 75:f00c79f79b6a 229
AhmedPlaymaker 68:b9cfd27987ac 230 #endif