Snake vs Block Game to be run upon K64F.

Dependencies:   mbed

Revision:
75:f00c79f79b6a
Parent:
74:7b6568bc16d5
Child:
76:7fa91122907f
--- a/GameObjects/Snake/Snake-test.h	Sun May 05 11:46:08 2019 +0000
+++ b/GameObjects/Snake/Snake-test.h	Sun May 05 13:04:09 2019 +0000
@@ -1,15 +1,47 @@
 #ifndef SNAKE_TEST_H
 #define SNAKE_TEST_H
 
+///////////// prototypes //////////////
+bool check_initialisation();
+bool check_west_movement();
+bool check_east_movement();
+bool check_freeze_movement();
+bool check_chain_reaction();
+bool check_block_collision_and_relative_movement();
+bool check_food_collision_and_relative_movement();
+
+/////////////// object ///////////////
+Snake snake;
+
+//GLOBALS//
+Direction d; //represents the direction of movement of the snake.
+int b[10];  //each element in this array represents the beed number in the snake.
+bool success_flag; //stores success / failure from a test.
+
 /**
- * \brief Check that Snake object goes to correct position when moved
+ * \brief Check that Snake object goes to correct position when moved and/ when collides.
  *
  * \returns true if all the tests passed
  */
 bool Snake_test_movement()
 {
-    // Create Snake object with a length of 15, and x axis speed of 2.
-    Snake snake;
+    success_flag = true;
+    
+    success_flag = check_initialisation();
+    success_flag = check_west_movement();
+    success_flag = check_east_movement();
+    success_flag = check_freeze_movement();
+    success_flag = check_chain_reaction();
+    success_flag = check_block_collision_and_relative_movement();
+    success_flag = check_food_collision_and_relative_movement();
+
+    return success_flag;
+}
+
+bool check_initialisation()
+{
+
+    // Change Snake to a length of 15, and x axis speed of 2.
     snake.init(15, 2);
 
     // Set the position to WIDTH/2, HEIGHT - 3
@@ -20,11 +52,20 @@
     // Read the position
     Vector2D read_pos_1 = snake.get_pos(0);
     printf("%f, %f\n", read_pos_1.x, read_pos_1.y);
+    
+    // Fail the test if the initial position is wrong
+    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.
+        success_flag = false;
+    }
+    return success_flag;
+}
+
+bool check_west_movement()
+{
 
     // Set the direction to W and set snake motion free by setting b as 1;
-    Direction d = W;
+    d = W;
 
-    int b[10];  //each element in this array represents the beed number in the snake.
     for(int i=0; i<=9; i++)  {
         b[i] = 1;
     }
@@ -36,6 +77,16 @@
     Vector2D read_pos_2 = snake.get_pos(0); //getting the position of the first beed
     printf("%f, %f\n", read_pos_2.x, read_pos_2.y);
 
+    // Fail the test if the final position after moving West is wrong
+    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.
+        success_flag = false;
+    }
+    return success_flag;
+}
+
+bool check_east_movement()
+{
+
     // Set the direction to E and set snake motion free by setting b as 1;
     d = E;
 
@@ -50,8 +101,18 @@
     Vector2D read_pos_3 = snake.get_pos(0); //getting the position of the first beed
     printf("%f, %f\n", read_pos_3.x, read_pos_3.y);
 
+    // Fail the test if the final position after moving East is wrong
+    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
+        //and it moves back from its previous position.
+        success_flag = false;
+    }
+    return success_flag;
+}
 
-    // Set the direction to W, length and speed both to 10 and set snake motion stopped by setting b as 0;
+bool check_freeze_movement()
+{
+
+    // 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) ;
     d = W;
     snake.init(10, 10);
 
@@ -65,7 +126,16 @@
     // Read the position
     Vector2D read_pos_4 = snake.get_pos(0); //getting the position of the first beed
     printf("%f, %f\n", read_pos_4.x, read_pos_4.y);
+    
+    // Fail the test if the final position after moving West but stopping motion using b[i] = 0; is wrong.
+    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.
+        success_flag = false;
+    }
+    return success_flag;
+}
 
+bool check_chain_reaction()
+{
 
     // Set the direction to W, length to 20 and speed to 2 and set snake motion free by setting b as 1;
     d = W;
@@ -83,36 +153,75 @@
     // Read the position
     Vector2D read_pos_5 = snake.get_pos(2); //getting the position of the third beed
     printf("%f, %f\n", read_pos_5.x, read_pos_5.y);
-
-
-    // Now check that all the positions are as expected
-    bool success_flag = true;
-
-    // Fail the test if the initial position is wrong
-    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.
-        success_flag = false;
-    }
-
-    // Fail the test if the final position after moving West is wrong
-    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.
-        success_flag = false;
-    }
-
-    // Fail the test if the final position after moving East is wrong
-    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
-        //and it moves back from its previous position.
-        success_flag = false;
-    }
-
-    // Fail the test if the final position after moving West but stopping motion using b[i] = 0; is wrong.
-    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.
-        success_flag = false;
-    }
-
+    
     // Fail the test if the final position of the third beed after moving West and after 3 itterations; is wrong.
     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.
         success_flag = false;
     }
     return success_flag;
 }
+
+bool check_block_collision_and_relative_movement()
+{
+
+    // Set the direction to E, length to 8 and speed to 2 and set snake motion free by setting b as 1;
+    d = E;
+    snake.init(8, 2);
+
+    for(int i=0; i<=9; i++)  {
+        b[i] = 1;
+    }
+
+    // Update the snake position 2 times and then reduce length to 7 (this can simulate block collision of number 1).
+    snake.update(d, b);
+    snake.update(d, b);
+
+    snake.init(7, 2);
+    d = E;//now direction is set to east as it continues to move after collision and then update snake object again.
+    snake.update(d, b);
+
+    // Read the position
+    Vector2D read_pos_6 = snake.get_pos(0); //getting the position of the first beed
+    printf("%f, %f\n", read_pos_6.x, read_pos_6.y);
+    
+    // Fail the test if the final position of the first beed after moving East 3 times and colliding with block numbered 1; is wrong.
+    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
+        // 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.
+        success_flag = false;
+    }
+    return success_flag;
+}
+
+bool check_food_collision_and_relative_movement()
+{
+
+    // Set the direction to W, length to 5 and speed to 2 and set snake motion free by setting b as 1;
+    d = W;
+    snake.init(7, 2);
+
+    for(int i=0; i<=9; i++)  {
+        b[i] = 1;
+    }
+
+    // Update the snake position 2 times (2 game  loops) and then increase length to 7 (this can simulate food collision of 2 times).
+    snake.update(d, b);
+    snake.update(d, b);
+
+    snake.init(9, 2);
+    d = W;//now direction is set to west as it continues to move after food collision and then update snake object again.
+    snake.update(d, b);
+
+    // Read the position
+    Vector2D read_pos_7 = snake.get_pos(0); //getting the position of the first beed
+    printf("%f, %f\n", read_pos_7.x, read_pos_7.y);
+    
+    // Fail the test if the final position of the first beed after moving East 3 times and colliding with block numbered 1; is wrong.
+    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
+        // 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.
+        success_flag = false;
+    }
+    return success_flag;
+
+}
+
 #endif
\ No newline at end of file