Ahmed Adamjee
/
SnakeVSBlock
Snake vs Block Game to be run upon K64F.
GameObjects/Snake/Snake-test.h@75:f00c79f79b6a, 2019-05-05 (annotated)
- Committer:
- AhmedPlaymaker
- Date:
- Sun May 05 13:04:09 2019 +0000
- Revision:
- 75:f00c79f79b6a
- Parent:
- 74:7b6568bc16d5
- Child:
- 76:7fa91122907f
Improved the snake test file to perform various sorts of tests on my snake, that can test all it's functionalities every time i change the code.
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 | 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 | 75:f00c79f79b6a | 18 | int b[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 | 75:f00c79f79b6a | 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 | 68:b9cfd27987ac | 45 | snake.init(15, 2); |
AhmedPlaymaker | 69:55e309da7efd | 46 | |
AhmedPlaymaker | 68:b9cfd27987ac | 47 | // Set the position to WIDTH/2, HEIGHT - 3 |
AhmedPlaymaker | 68:b9cfd27987ac | 48 | |
AhmedPlaymaker | 68:b9cfd27987ac | 49 | Vector2D initial_pos = {WIDTH/2, HEIGHT - 3}; //Spawns player sprite near the middle of the screen. |
AhmedPlaymaker | 68:b9cfd27987ac | 50 | snake.set_pos(initial_pos); |
AhmedPlaymaker | 68:b9cfd27987ac | 51 | |
AhmedPlaymaker | 68:b9cfd27987ac | 52 | // Read the position |
AhmedPlaymaker | 70:7caab8069b9b | 53 | Vector2D read_pos_1 = snake.get_pos(0); |
AhmedPlaymaker | 68:b9cfd27987ac | 54 | printf("%f, %f\n", read_pos_1.x, read_pos_1.y); |
AhmedPlaymaker | 75:f00c79f79b6a | 55 | |
AhmedPlaymaker | 75:f00c79f79b6a | 56 | // Fail the test if the initial position is wrong |
AhmedPlaymaker | 75:f00c79f79b6a | 57 | 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 | 58 | success_flag = false; |
AhmedPlaymaker | 75:f00c79f79b6a | 59 | } |
AhmedPlaymaker | 75:f00c79f79b6a | 60 | return success_flag; |
AhmedPlaymaker | 75:f00c79f79b6a | 61 | } |
AhmedPlaymaker | 75:f00c79f79b6a | 62 | |
AhmedPlaymaker | 75:f00c79f79b6a | 63 | bool check_west_movement() |
AhmedPlaymaker | 75:f00c79f79b6a | 64 | { |
AhmedPlaymaker | 68:b9cfd27987ac | 65 | |
AhmedPlaymaker | 68:b9cfd27987ac | 66 | // Set the direction to W and set snake motion free by setting b as 1; |
AhmedPlaymaker | 75:f00c79f79b6a | 67 | d = W; |
AhmedPlaymaker | 68:b9cfd27987ac | 68 | |
AhmedPlaymaker | 70:7caab8069b9b | 69 | for(int i=0; i<=9; i++) { |
AhmedPlaymaker | 68:b9cfd27987ac | 70 | b[i] = 1; |
AhmedPlaymaker | 68:b9cfd27987ac | 71 | } |
AhmedPlaymaker | 68:b9cfd27987ac | 72 | |
AhmedPlaymaker | 68:b9cfd27987ac | 73 | // Update the snake position |
AhmedPlaymaker | 68:b9cfd27987ac | 74 | snake.update(d, b); |
AhmedPlaymaker | 68:b9cfd27987ac | 75 | |
AhmedPlaymaker | 68:b9cfd27987ac | 76 | // Read the position |
AhmedPlaymaker | 74:7b6568bc16d5 | 77 | Vector2D read_pos_2 = snake.get_pos(0); //getting the position of the first beed |
AhmedPlaymaker | 68:b9cfd27987ac | 78 | printf("%f, %f\n", read_pos_2.x, read_pos_2.y); |
AhmedPlaymaker | 68:b9cfd27987ac | 79 | |
AhmedPlaymaker | 75:f00c79f79b6a | 80 | // Fail the test if the final position after moving West is wrong |
AhmedPlaymaker | 75:f00c79f79b6a | 81 | 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 | 82 | success_flag = false; |
AhmedPlaymaker | 75:f00c79f79b6a | 83 | } |
AhmedPlaymaker | 75:f00c79f79b6a | 84 | return success_flag; |
AhmedPlaymaker | 75:f00c79f79b6a | 85 | } |
AhmedPlaymaker | 75:f00c79f79b6a | 86 | |
AhmedPlaymaker | 75:f00c79f79b6a | 87 | bool check_east_movement() |
AhmedPlaymaker | 75:f00c79f79b6a | 88 | { |
AhmedPlaymaker | 75:f00c79f79b6a | 89 | |
AhmedPlaymaker | 74:7b6568bc16d5 | 90 | // Set the direction to E and set snake motion free by setting b as 1; |
AhmedPlaymaker | 74:7b6568bc16d5 | 91 | d = E; |
AhmedPlaymaker | 74:7b6568bc16d5 | 92 | |
AhmedPlaymaker | 74:7b6568bc16d5 | 93 | for(int i=0; i<=9; i++) { |
AhmedPlaymaker | 74:7b6568bc16d5 | 94 | b[i] = 1; |
AhmedPlaymaker | 74:7b6568bc16d5 | 95 | } |
AhmedPlaymaker | 74:7b6568bc16d5 | 96 | |
AhmedPlaymaker | 74:7b6568bc16d5 | 97 | // Update the snake position |
AhmedPlaymaker | 74:7b6568bc16d5 | 98 | snake.update(d, b); |
AhmedPlaymaker | 74:7b6568bc16d5 | 99 | |
AhmedPlaymaker | 74:7b6568bc16d5 | 100 | // Read the position |
AhmedPlaymaker | 74:7b6568bc16d5 | 101 | Vector2D read_pos_3 = snake.get_pos(0); //getting the position of the first beed |
AhmedPlaymaker | 74:7b6568bc16d5 | 102 | printf("%f, %f\n", read_pos_3.x, read_pos_3.y); |
AhmedPlaymaker | 74:7b6568bc16d5 | 103 | |
AhmedPlaymaker | 75:f00c79f79b6a | 104 | // Fail the test if the final position after moving East is wrong |
AhmedPlaymaker | 75:f00c79f79b6a | 105 | 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 | 106 | //and it moves back from its previous position. |
AhmedPlaymaker | 75:f00c79f79b6a | 107 | success_flag = false; |
AhmedPlaymaker | 75:f00c79f79b6a | 108 | } |
AhmedPlaymaker | 75:f00c79f79b6a | 109 | return success_flag; |
AhmedPlaymaker | 75:f00c79f79b6a | 110 | } |
AhmedPlaymaker | 74:7b6568bc16d5 | 111 | |
AhmedPlaymaker | 75:f00c79f79b6a | 112 | bool check_freeze_movement() |
AhmedPlaymaker | 75:f00c79f79b6a | 113 | { |
AhmedPlaymaker | 75:f00c79f79b6a | 114 | |
AhmedPlaymaker | 75:f00c79f79b6a | 115 | // Set the direction to W, length and speed both to 10 and set snake motion stopped by setting b as 0 (simulates block sides collision) ; |
AhmedPlaymaker | 74:7b6568bc16d5 | 116 | d = W; |
AhmedPlaymaker | 74:7b6568bc16d5 | 117 | snake.init(10, 10); |
AhmedPlaymaker | 74:7b6568bc16d5 | 118 | |
AhmedPlaymaker | 74:7b6568bc16d5 | 119 | for(int i=0; i<=9; i++) { |
AhmedPlaymaker | 74:7b6568bc16d5 | 120 | b[i] = 0; |
AhmedPlaymaker | 74:7b6568bc16d5 | 121 | } |
AhmedPlaymaker | 74:7b6568bc16d5 | 122 | |
AhmedPlaymaker | 74:7b6568bc16d5 | 123 | // Update the snake position |
AhmedPlaymaker | 74:7b6568bc16d5 | 124 | snake.update(d, b); |
AhmedPlaymaker | 74:7b6568bc16d5 | 125 | |
AhmedPlaymaker | 74:7b6568bc16d5 | 126 | // Read the position |
AhmedPlaymaker | 74:7b6568bc16d5 | 127 | Vector2D read_pos_4 = snake.get_pos(0); //getting the position of the first beed |
AhmedPlaymaker | 74:7b6568bc16d5 | 128 | printf("%f, %f\n", read_pos_4.x, read_pos_4.y); |
AhmedPlaymaker | 75:f00c79f79b6a | 129 | |
AhmedPlaymaker | 75:f00c79f79b6a | 130 | // Fail the test if the final position after moving West but stopping motion using b[i] = 0; is wrong. |
AhmedPlaymaker | 75:f00c79f79b6a | 131 | 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 moove. |
AhmedPlaymaker | 75:f00c79f79b6a | 132 | success_flag = false; |
AhmedPlaymaker | 75:f00c79f79b6a | 133 | } |
AhmedPlaymaker | 75:f00c79f79b6a | 134 | return success_flag; |
AhmedPlaymaker | 75:f00c79f79b6a | 135 | } |
AhmedPlaymaker | 74:7b6568bc16d5 | 136 | |
AhmedPlaymaker | 75:f00c79f79b6a | 137 | bool check_chain_reaction() |
AhmedPlaymaker | 75:f00c79f79b6a | 138 | { |
AhmedPlaymaker | 74:7b6568bc16d5 | 139 | |
AhmedPlaymaker | 74:7b6568bc16d5 | 140 | // Set the direction to W, length to 20 and speed to 2 and set snake motion free by setting b as 1; |
AhmedPlaymaker | 74:7b6568bc16d5 | 141 | d = W; |
AhmedPlaymaker | 74:7b6568bc16d5 | 142 | snake.init(20, 2); |
AhmedPlaymaker | 74:7b6568bc16d5 | 143 | |
AhmedPlaymaker | 74:7b6568bc16d5 | 144 | for(int i=0; i<=9; i++) { |
AhmedPlaymaker | 74:7b6568bc16d5 | 145 | b[i] = 1; |
AhmedPlaymaker | 74:7b6568bc16d5 | 146 | } |
AhmedPlaymaker | 74:7b6568bc16d5 | 147 | |
AhmedPlaymaker | 74:7b6568bc16d5 | 148 | // Update the snake position 3 times because i want to read the position of the third bead that only mooves with a chain reaction. |
AhmedPlaymaker | 74:7b6568bc16d5 | 149 | snake.update(d, b); |
AhmedPlaymaker | 74:7b6568bc16d5 | 150 | snake.update(d, b); |
AhmedPlaymaker | 74:7b6568bc16d5 | 151 | snake.update(d, b); |
AhmedPlaymaker | 74:7b6568bc16d5 | 152 | |
AhmedPlaymaker | 74:7b6568bc16d5 | 153 | // Read the position |
AhmedPlaymaker | 74:7b6568bc16d5 | 154 | Vector2D read_pos_5 = snake.get_pos(2); //getting the position of the third beed |
AhmedPlaymaker | 74:7b6568bc16d5 | 155 | printf("%f, %f\n", read_pos_5.x, read_pos_5.y); |
AhmedPlaymaker | 75:f00c79f79b6a | 156 | |
AhmedPlaymaker | 74:7b6568bc16d5 | 157 | // Fail the test if the final position of the third beed after moving West and after 3 itterations; is wrong. |
AhmedPlaymaker | 74:7b6568bc16d5 | 158 | 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 moove. |
AhmedPlaymaker | 74:7b6568bc16d5 | 159 | success_flag = false; |
AhmedPlaymaker | 74:7b6568bc16d5 | 160 | } |
AhmedPlaymaker | 68:b9cfd27987ac | 161 | return success_flag; |
AhmedPlaymaker | 68:b9cfd27987ac | 162 | } |
AhmedPlaymaker | 75:f00c79f79b6a | 163 | |
AhmedPlaymaker | 75:f00c79f79b6a | 164 | bool check_block_collision_and_relative_movement() |
AhmedPlaymaker | 75:f00c79f79b6a | 165 | { |
AhmedPlaymaker | 75:f00c79f79b6a | 166 | |
AhmedPlaymaker | 75:f00c79f79b6a | 167 | // Set the direction to E, length to 8 and speed to 2 and set snake motion free by setting b as 1; |
AhmedPlaymaker | 75:f00c79f79b6a | 168 | d = E; |
AhmedPlaymaker | 75:f00c79f79b6a | 169 | snake.init(8, 2); |
AhmedPlaymaker | 75:f00c79f79b6a | 170 | |
AhmedPlaymaker | 75:f00c79f79b6a | 171 | for(int i=0; i<=9; i++) { |
AhmedPlaymaker | 75:f00c79f79b6a | 172 | b[i] = 1; |
AhmedPlaymaker | 75:f00c79f79b6a | 173 | } |
AhmedPlaymaker | 75:f00c79f79b6a | 174 | |
AhmedPlaymaker | 75:f00c79f79b6a | 175 | // Update the snake position 2 times and then reduce length to 7 (this can simulate block collision of number 1). |
AhmedPlaymaker | 75:f00c79f79b6a | 176 | snake.update(d, b); |
AhmedPlaymaker | 75:f00c79f79b6a | 177 | snake.update(d, b); |
AhmedPlaymaker | 75:f00c79f79b6a | 178 | |
AhmedPlaymaker | 75:f00c79f79b6a | 179 | snake.init(7, 2); |
AhmedPlaymaker | 75:f00c79f79b6a | 180 | d = E;//now direction is set to east as it continues to move after collision and then update snake object again. |
AhmedPlaymaker | 75:f00c79f79b6a | 181 | snake.update(d, b); |
AhmedPlaymaker | 75:f00c79f79b6a | 182 | |
AhmedPlaymaker | 75:f00c79f79b6a | 183 | // Read the position |
AhmedPlaymaker | 75:f00c79f79b6a | 184 | Vector2D read_pos_6 = snake.get_pos(0); //getting the position of the first beed |
AhmedPlaymaker | 75:f00c79f79b6a | 185 | printf("%f, %f\n", read_pos_6.x, read_pos_6.y); |
AhmedPlaymaker | 75:f00c79f79b6a | 186 | |
AhmedPlaymaker | 75:f00c79f79b6a | 187 | // 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 | 188 | 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 | 75:f00c79f79b6a | 189 | // and colliding once with a block numbered 1, read_pos_6.x = ((WIDTH/2)+2) because the 7th beed from bottom mooved only 2 times in effect. |
AhmedPlaymaker | 75:f00c79f79b6a | 190 | success_flag = false; |
AhmedPlaymaker | 75:f00c79f79b6a | 191 | } |
AhmedPlaymaker | 75:f00c79f79b6a | 192 | return success_flag; |
AhmedPlaymaker | 75:f00c79f79b6a | 193 | } |
AhmedPlaymaker | 75:f00c79f79b6a | 194 | |
AhmedPlaymaker | 75:f00c79f79b6a | 195 | bool check_food_collision_and_relative_movement() |
AhmedPlaymaker | 75:f00c79f79b6a | 196 | { |
AhmedPlaymaker | 75:f00c79f79b6a | 197 | |
AhmedPlaymaker | 75:f00c79f79b6a | 198 | // Set the direction to W, length to 5 and speed to 2 and set snake motion free by setting b as 1; |
AhmedPlaymaker | 75:f00c79f79b6a | 199 | d = W; |
AhmedPlaymaker | 75:f00c79f79b6a | 200 | snake.init(7, 2); |
AhmedPlaymaker | 75:f00c79f79b6a | 201 | |
AhmedPlaymaker | 75:f00c79f79b6a | 202 | for(int i=0; i<=9; i++) { |
AhmedPlaymaker | 75:f00c79f79b6a | 203 | b[i] = 1; |
AhmedPlaymaker | 75:f00c79f79b6a | 204 | } |
AhmedPlaymaker | 75:f00c79f79b6a | 205 | |
AhmedPlaymaker | 75:f00c79f79b6a | 206 | // Update the snake position 2 times (2 game loops) and then increase length to 7 (this can simulate food collision of 2 times). |
AhmedPlaymaker | 75:f00c79f79b6a | 207 | snake.update(d, b); |
AhmedPlaymaker | 75:f00c79f79b6a | 208 | snake.update(d, b); |
AhmedPlaymaker | 75:f00c79f79b6a | 209 | |
AhmedPlaymaker | 75:f00c79f79b6a | 210 | snake.init(9, 2); |
AhmedPlaymaker | 75:f00c79f79b6a | 211 | d = W;//now direction is set to west as it continues to move after food collision and then update snake object again. |
AhmedPlaymaker | 75:f00c79f79b6a | 212 | snake.update(d, b); |
AhmedPlaymaker | 75:f00c79f79b6a | 213 | |
AhmedPlaymaker | 75:f00c79f79b6a | 214 | // Read the position |
AhmedPlaymaker | 75:f00c79f79b6a | 215 | Vector2D read_pos_7 = snake.get_pos(0); //getting the position of the first beed |
AhmedPlaymaker | 75:f00c79f79b6a | 216 | printf("%f, %f\n", read_pos_7.x, read_pos_7.y); |
AhmedPlaymaker | 75:f00c79f79b6a | 217 | |
AhmedPlaymaker | 75:f00c79f79b6a | 218 | // 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 | 219 | 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 | 75:f00c79f79b6a | 220 | // and colliding twice with food, read_pos_7.x = ((WIDTH/2)-4) because the current top beed mooved same number of times to the top beed before it ate food. |
AhmedPlaymaker | 75:f00c79f79b6a | 221 | success_flag = false; |
AhmedPlaymaker | 75:f00c79f79b6a | 222 | } |
AhmedPlaymaker | 75:f00c79f79b6a | 223 | return success_flag; |
AhmedPlaymaker | 75:f00c79f79b6a | 224 | |
AhmedPlaymaker | 75:f00c79f79b6a | 225 | } |
AhmedPlaymaker | 75:f00c79f79b6a | 226 | |
AhmedPlaymaker | 68:b9cfd27987ac | 227 | #endif |