Snake vs Block Game to be run upon K64F.

Dependencies:   mbed

Committer:
AhmedPlaymaker
Date:
Sun May 05 11:46:08 2019 +0000
Revision:
74:7b6568bc16d5
Parent:
70:7caab8069b9b
Child:
75:f00c79f79b6a
edited the test file so that it tests more functionalities of the snake

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 68:b9cfd27987ac 4 /**
AhmedPlaymaker 68:b9cfd27987ac 5 * \brief Check that Snake object goes to correct position when moved
AhmedPlaymaker 68:b9cfd27987ac 6 *
AhmedPlaymaker 68:b9cfd27987ac 7 * \returns true if all the tests passed
AhmedPlaymaker 68:b9cfd27987ac 8 */
AhmedPlaymaker 68:b9cfd27987ac 9 bool Snake_test_movement()
AhmedPlaymaker 68:b9cfd27987ac 10 {
AhmedPlaymaker 68:b9cfd27987ac 11 // Create Snake object with a length of 15, and x axis speed of 2.
AhmedPlaymaker 68:b9cfd27987ac 12 Snake snake;
AhmedPlaymaker 68:b9cfd27987ac 13 snake.init(15, 2);
AhmedPlaymaker 69:55e309da7efd 14
AhmedPlaymaker 68:b9cfd27987ac 15 // Set the position to WIDTH/2, HEIGHT - 3
AhmedPlaymaker 68:b9cfd27987ac 16
AhmedPlaymaker 68:b9cfd27987ac 17 Vector2D initial_pos = {WIDTH/2, HEIGHT - 3}; //Spawns player sprite near the middle of the screen.
AhmedPlaymaker 68:b9cfd27987ac 18 snake.set_pos(initial_pos);
AhmedPlaymaker 68:b9cfd27987ac 19
AhmedPlaymaker 68:b9cfd27987ac 20 // Read the position
AhmedPlaymaker 70:7caab8069b9b 21 Vector2D read_pos_1 = snake.get_pos(0);
AhmedPlaymaker 68:b9cfd27987ac 22 printf("%f, %f\n", read_pos_1.x, read_pos_1.y);
AhmedPlaymaker 68:b9cfd27987ac 23
AhmedPlaymaker 68:b9cfd27987ac 24 // Set the direction to W and set snake motion free by setting b as 1;
AhmedPlaymaker 68:b9cfd27987ac 25 Direction d = W;
AhmedPlaymaker 68:b9cfd27987ac 26
AhmedPlaymaker 70:7caab8069b9b 27 int b[10]; //each element in this array represents the beed number in the snake.
AhmedPlaymaker 70:7caab8069b9b 28 for(int i=0; i<=9; i++) {
AhmedPlaymaker 68:b9cfd27987ac 29 b[i] = 1;
AhmedPlaymaker 68:b9cfd27987ac 30 }
AhmedPlaymaker 68:b9cfd27987ac 31
AhmedPlaymaker 68:b9cfd27987ac 32 // Update the snake position
AhmedPlaymaker 68:b9cfd27987ac 33 snake.update(d, b);
AhmedPlaymaker 68:b9cfd27987ac 34
AhmedPlaymaker 68:b9cfd27987ac 35 // Read the position
AhmedPlaymaker 74:7b6568bc16d5 36 Vector2D read_pos_2 = snake.get_pos(0); //getting the position of the first beed
AhmedPlaymaker 68:b9cfd27987ac 37 printf("%f, %f\n", read_pos_2.x, read_pos_2.y);
AhmedPlaymaker 68:b9cfd27987ac 38
AhmedPlaymaker 74:7b6568bc16d5 39 // Set the direction to E and set snake motion free by setting b as 1;
AhmedPlaymaker 74:7b6568bc16d5 40 d = E;
AhmedPlaymaker 74:7b6568bc16d5 41
AhmedPlaymaker 74:7b6568bc16d5 42 for(int i=0; i<=9; i++) {
AhmedPlaymaker 74:7b6568bc16d5 43 b[i] = 1;
AhmedPlaymaker 74:7b6568bc16d5 44 }
AhmedPlaymaker 74:7b6568bc16d5 45
AhmedPlaymaker 74:7b6568bc16d5 46 // Update the snake position
AhmedPlaymaker 74:7b6568bc16d5 47 snake.update(d, b);
AhmedPlaymaker 74:7b6568bc16d5 48
AhmedPlaymaker 74:7b6568bc16d5 49 // Read the position
AhmedPlaymaker 74:7b6568bc16d5 50 Vector2D read_pos_3 = snake.get_pos(0); //getting the position of the first beed
AhmedPlaymaker 74:7b6568bc16d5 51 printf("%f, %f\n", read_pos_3.x, read_pos_3.y);
AhmedPlaymaker 74:7b6568bc16d5 52
AhmedPlaymaker 74:7b6568bc16d5 53
AhmedPlaymaker 74:7b6568bc16d5 54 // Set the direction to W, length and speed both to 10 and set snake motion stopped by setting b as 0;
AhmedPlaymaker 74:7b6568bc16d5 55 d = W;
AhmedPlaymaker 74:7b6568bc16d5 56 snake.init(10, 10);
AhmedPlaymaker 74:7b6568bc16d5 57
AhmedPlaymaker 74:7b6568bc16d5 58 for(int i=0; i<=9; i++) {
AhmedPlaymaker 74:7b6568bc16d5 59 b[i] = 0;
AhmedPlaymaker 74:7b6568bc16d5 60 }
AhmedPlaymaker 74:7b6568bc16d5 61
AhmedPlaymaker 74:7b6568bc16d5 62 // Update the snake position
AhmedPlaymaker 74:7b6568bc16d5 63 snake.update(d, b);
AhmedPlaymaker 74:7b6568bc16d5 64
AhmedPlaymaker 74:7b6568bc16d5 65 // Read the position
AhmedPlaymaker 74:7b6568bc16d5 66 Vector2D read_pos_4 = snake.get_pos(0); //getting the position of the first beed
AhmedPlaymaker 74:7b6568bc16d5 67 printf("%f, %f\n", read_pos_4.x, read_pos_4.y);
AhmedPlaymaker 74:7b6568bc16d5 68
AhmedPlaymaker 74:7b6568bc16d5 69
AhmedPlaymaker 74:7b6568bc16d5 70 // 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 71 d = W;
AhmedPlaymaker 74:7b6568bc16d5 72 snake.init(20, 2);
AhmedPlaymaker 74:7b6568bc16d5 73
AhmedPlaymaker 74:7b6568bc16d5 74 for(int i=0; i<=9; i++) {
AhmedPlaymaker 74:7b6568bc16d5 75 b[i] = 1;
AhmedPlaymaker 74:7b6568bc16d5 76 }
AhmedPlaymaker 74:7b6568bc16d5 77
AhmedPlaymaker 74:7b6568bc16d5 78 // 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 79 snake.update(d, b);
AhmedPlaymaker 74:7b6568bc16d5 80 snake.update(d, b);
AhmedPlaymaker 74:7b6568bc16d5 81 snake.update(d, b);
AhmedPlaymaker 74:7b6568bc16d5 82
AhmedPlaymaker 74:7b6568bc16d5 83 // Read the position
AhmedPlaymaker 74:7b6568bc16d5 84 Vector2D read_pos_5 = snake.get_pos(2); //getting the position of the third beed
AhmedPlaymaker 74:7b6568bc16d5 85 printf("%f, %f\n", read_pos_5.x, read_pos_5.y);
AhmedPlaymaker 74:7b6568bc16d5 86
AhmedPlaymaker 74:7b6568bc16d5 87
AhmedPlaymaker 74:7b6568bc16d5 88 // Now check that all the positions are as expected
AhmedPlaymaker 68:b9cfd27987ac 89 bool success_flag = true;
AhmedPlaymaker 68:b9cfd27987ac 90
AhmedPlaymaker 68:b9cfd27987ac 91 // Fail the test if the initial position is wrong
AhmedPlaymaker 74:7b6568bc16d5 92 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 74:7b6568bc16d5 93 success_flag = false;
AhmedPlaymaker 74:7b6568bc16d5 94 }
AhmedPlaymaker 74:7b6568bc16d5 95
AhmedPlaymaker 74:7b6568bc16d5 96 // Fail the test if the final position after moving West is wrong
AhmedPlaymaker 74:7b6568bc16d5 97 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 68:b9cfd27987ac 98 success_flag = false;
AhmedPlaymaker 68:b9cfd27987ac 99 }
AhmedPlaymaker 68:b9cfd27987ac 100
AhmedPlaymaker 74:7b6568bc16d5 101 // Fail the test if the final position after moving East is wrong
AhmedPlaymaker 74:7b6568bc16d5 102 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 74:7b6568bc16d5 103 //and it moves back from its previous position.
AhmedPlaymaker 68:b9cfd27987ac 104 success_flag = false;
AhmedPlaymaker 68:b9cfd27987ac 105 }
AhmedPlaymaker 68:b9cfd27987ac 106
AhmedPlaymaker 74:7b6568bc16d5 107 // Fail the test if the final position after moving West but stopping motion using b[i] = 0; is wrong.
AhmedPlaymaker 74:7b6568bc16d5 108 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 74:7b6568bc16d5 109 success_flag = false;
AhmedPlaymaker 74:7b6568bc16d5 110 }
AhmedPlaymaker 74:7b6568bc16d5 111
AhmedPlaymaker 74:7b6568bc16d5 112 // Fail the test if the final position of the third beed after moving West and after 3 itterations; is wrong.
AhmedPlaymaker 74:7b6568bc16d5 113 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 114 success_flag = false;
AhmedPlaymaker 74:7b6568bc16d5 115 }
AhmedPlaymaker 68:b9cfd27987ac 116 return success_flag;
AhmedPlaymaker 68:b9cfd27987ac 117 }
AhmedPlaymaker 68:b9cfd27987ac 118 #endif