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