ELEC2645 (2018/19) / Mbed 2 deprecated el17szs

Dependencies:   mbed

Revision:
12:954da4f4e565
Parent:
11:f481ec642cc5
Child:
13:924891519a95
diff -r f481ec642cc5 -r 954da4f4e565 Cricket/Cricket.cpp
--- a/Cricket/Cricket.cpp	Mon Apr 22 19:57:01 2019 +0000
+++ b/Cricket/Cricket.cpp	Tue Apr 23 12:46:34 2019 +0000
@@ -15,57 +15,82 @@
 {
    bat.init(4,5);
    ball.init(2,3);
-   float f;
+   //float f;
    new_round=0;
+   direction_set=0;
+   fieldersCount=0;
+   init_field_counter=0;
+   init_positions();
+   fielder_no=-1;
+   check_bowled=0;
 }
 void Cricket::game(N5110 &lcd,Gamepad &pad){
-     if (new_round==0){
+     //if (new_round==0){
         //ball.reset();
         set_field(lcd);
         play_game(lcd,pad);
-    }
-    /*
-    else
-    {
-        play_game(lcd,pad);
-    }
-    */
+    //}
+    //else
+    //{
+       // play_game(lcd,pad);
+    //s}
+
       
 }
-
+void Cricket::init_positions(){
+    set_init_positions(42,0,N,1);
+    set_init_positions(42,48,S,2);
+    set_init_positions(84,25,E,3);
+    set_init_positions(0,25,W,4);
+    set_init_positions(0,44,SW,5);
+    set_init_positions(0,2,NW,6);
+    set_init_positions(84,2,NE,7);
+    set_init_positions(84,44,SE,8);
+}
+void Cricket::set_init_positions(int x,int y, Direction direction,int no){
+    positions[init_field_counter].x=x;
+    positions[init_field_counter].y=y;
+    positions[init_field_counter].dir=direction;
+    positions[init_field_counter].no=no;
+    init_field_counter++;
+}
 void Cricket::play_game(N5110 &lcd,Gamepad &pad){
     int check_hit=bat.get_hitBall();
-    int check_bowled=ball.ball_start();
-    printf("BOWLED %i ",check_bowled);
+    Direction dir=pad.get_direction();
+    if (check_bowled!=1){
+        check_bowled=ball.ball_start();
+    }
+    else{
+        update_game(check_hit, dir);
+    }
 }
-/*
 void Cricket::update_game(int checkHit, Direction dir){
-  int check_bowled=
-  if (direction_set!=1){
-    ball_direction=dir;
-    direction_set=1;
-  }
+  Vector2D pos=ball.get_pos();
+  if (direction_set!=1){ 
+    for (int i=0;i<init_field_counter;i++){
+      if(dir==positions[i].dir){
+          printf("%i \n",positions[i].no);
+          ball_direction=dir;
+          fielder_no=i;
+          direction_set=1;
+          break; 
+      }
+    }
+}
   if (checkHit ==1 && ballHit!=1){
     ballHit=1;
   }
-  if(bowled==1 && ballHit==1){
-    printf("TEST\n");
-    for (int i=0;i<fieldersCount;i++){
-      if(ball_direction==fielder[i].dir){
-        printf("x=%i  y=%i \n",_x,_y);
-        if (!(_y == fielder[i].y )){
-          _y--;
-        }   
-        if (_x!=fielder[i].x){
-          _x++;
-        }
-        printf("HERE");        
-      }
+  if(direction_set==1 && ballHit==1){
+        printf(" ball x %f expected x %i\n",pos.x,positions[fielder_no].x);
+        printf(" ball y %f expected y %i\n",pos.y,positions[fielder_no].y);
+        ball.update_ball_y(positions[fielder_no].y);
+        ball.update_ball_x(positions[fielder_no].x);
+        
+      //ball.set_pos(pos);
+      
     }
+}
     
-  }
-}
-*/
 void Cricket::draw(N5110 &lcd){
     lcd.drawCircle((WIDTH/2),HEIGHT/2,23,FILL_TRANSPARENT);
     lcd.drawRect(37,11,12,30,FILL_TRANSPARENT);
@@ -73,8 +98,6 @@
     draw_field(lcd);
     bat.draw(lcd);
 }
-
-
 void Cricket::draw_field(N5110 &lcd){
     lcd.drawCircle(field[0].x,field[0].y,2,FILL_BLACK);
     lcd.drawCircle(field[1].x,field[1].y,2,FILL_BLACK);
@@ -83,27 +106,22 @@
 }
 void Cricket::set_field(N5110 &lcd){
     int d=0;
-    int fieldersCount=0;
     int pos=0;
     int i=0;
     int continueCount=0;
     srand(time(NULL));
     while (fieldersCount!=5){
         d = 1+ rand() % 6;
-        printf("DIRECTION %i\n",d);
         pos=rand()%2;    
         for (int j=0;j<10;j++){
            if (fieldNumbers[j]==d){
-                printf("CHECK");
                 continueCount=1;
             }
         }
         if (continueCount==1){
-            printf("IM HERE");
             continueCount=0;
             continue;
         }
-        printf("YESS");
         if (d==1){ //NW
             if (pos==1){
               field[i].dir=NW;
@@ -216,7 +234,7 @@
             if (pos==1){
                 field[i].dir=E;
                 field[i].x=80;
-                field[i].y=20;
+                field[i].y=25;
                 field[i].position=1;
                 fieldNumbers[i]=d;
                 i++;
@@ -225,7 +243,7 @@
             else{
                  field[i].dir=E;
                 field[i].x=50;
-                field[i].y=20;
+                field[i].y=25;
                 field[i].position=1;
                 fieldNumbers[i]=d;
                 i++;