ELEC2645 (2018/19) / Mbed 2 deprecated el17szs

Dependencies:   mbed

Committer:
shahidsajid
Date:
Mon Apr 22 17:34:11 2019 +0000
Revision:
10:6c6e09023942
Child:
11:f481ec642cc5
Created a new folder called Cricket to modularize the code for efficiency

Who changed what in which revision?

UserRevisionLine numberNew contents of line
shahidsajid 10:6c6e09023942 1 #include "Cricket.h"
shahidsajid 10:6c6e09023942 2
shahidsajid 10:6c6e09023942 3
shahidsajid 10:6c6e09023942 4 Cricket::Cricket()
shahidsajid 10:6c6e09023942 5 {
shahidsajid 10:6c6e09023942 6
shahidsajid 10:6c6e09023942 7 }
shahidsajid 10:6c6e09023942 8
shahidsajid 10:6c6e09023942 9 Cricket::~Cricket()
shahidsajid 10:6c6e09023942 10 {
shahidsajid 10:6c6e09023942 11
shahidsajid 10:6c6e09023942 12 }
shahidsajid 10:6c6e09023942 13
shahidsajid 10:6c6e09023942 14 void Cricket::init()
shahidsajid 10:6c6e09023942 15 {
shahidsajid 10:6c6e09023942 16 bat.init(4,5);
shahidsajid 10:6c6e09023942 17 ball.init(2,3);
shahidsajid 10:6c6e09023942 18 float f;
shahidsajid 10:6c6e09023942 19 new_round=0;
shahidsajid 10:6c6e09023942 20 }
shahidsajid 10:6c6e09023942 21 void Cricket::game(N5110 &lcd){
shahidsajid 10:6c6e09023942 22 if (new_round==0){
shahidsajid 10:6c6e09023942 23 //ball.reset();
shahidsajid 10:6c6e09023942 24 set_field(lcd);
shahidsajid 10:6c6e09023942 25 //play_game(lcd);
shahidsajid 10:6c6e09023942 26 }
shahidsajid 10:6c6e09023942 27 /*
shahidsajid 10:6c6e09023942 28 else
shahidsajid 10:6c6e09023942 29 {
shahidsajid 10:6c6e09023942 30 play_game(lcd);
shahidsajid 10:6c6e09023942 31 }
shahidsajid 10:6c6e09023942 32 */
shahidsajid 10:6c6e09023942 33
shahidsajid 10:6c6e09023942 34 }
shahidsajid 10:6c6e09023942 35 /*
shahidsajid 10:6c6e09023942 36 void Cricket::play_game(N5110 &lcd){
shahidsajid 10:6c6e09023942 37 int check_hit=bat.get_hitBall();
shahidsajid 10:6c6e09023942 38 int check_bowled=ball.start(lcd,check_hit,pad.get_direction());
shahidsajid 10:6c6e09023942 39 }
shahidsajid 10:6c6e09023942 40 void Cricket::update_game(int checkHit, Direction dir){
shahidsajid 10:6c6e09023942 41 int check_bowled=
shahidsajid 10:6c6e09023942 42 if (direction_set!=1){
shahidsajid 10:6c6e09023942 43 ball_direction=dir;
shahidsajid 10:6c6e09023942 44 direction_set=1;
shahidsajid 10:6c6e09023942 45 }
shahidsajid 10:6c6e09023942 46 if (checkHit ==1 && ballHit!=1){
shahidsajid 10:6c6e09023942 47 ballHit=1;
shahidsajid 10:6c6e09023942 48 }
shahidsajid 10:6c6e09023942 49 if(bowled==1 && ballHit==1){
shahidsajid 10:6c6e09023942 50 printf("TEST\n");
shahidsajid 10:6c6e09023942 51 for (int i=0;i<fieldersCount;i++){
shahidsajid 10:6c6e09023942 52 if(ball_direction==fielder[i].dir){
shahidsajid 10:6c6e09023942 53 printf("x=%i y=%i \n",_x,_y);
shahidsajid 10:6c6e09023942 54 if (!(_y == fielder[i].y )){
shahidsajid 10:6c6e09023942 55 _y--;
shahidsajid 10:6c6e09023942 56 }
shahidsajid 10:6c6e09023942 57 if (_x!=fielder[i].x){
shahidsajid 10:6c6e09023942 58 _x++;
shahidsajid 10:6c6e09023942 59 }
shahidsajid 10:6c6e09023942 60 printf("HERE");
shahidsajid 10:6c6e09023942 61 }
shahidsajid 10:6c6e09023942 62 }
shahidsajid 10:6c6e09023942 63
shahidsajid 10:6c6e09023942 64 }
shahidsajid 10:6c6e09023942 65 }
shahidsajid 10:6c6e09023942 66 */
shahidsajid 10:6c6e09023942 67 void Cricket::draw(N5110 &lcd){
shahidsajid 10:6c6e09023942 68 lcd.drawCircle((WIDTH/2),HEIGHT/2,23,FILL_TRANSPARENT);
shahidsajid 10:6c6e09023942 69 lcd.drawRect(37,11,12,30,FILL_TRANSPARENT);
shahidsajid 10:6c6e09023942 70 ball.draw(lcd);
shahidsajid 10:6c6e09023942 71 draw_field(lcd);
shahidsajid 10:6c6e09023942 72 bat.draw(lcd);
shahidsajid 10:6c6e09023942 73 }
shahidsajid 10:6c6e09023942 74
shahidsajid 10:6c6e09023942 75
shahidsajid 10:6c6e09023942 76 void Cricket::draw_field(N5110 &lcd){
shahidsajid 10:6c6e09023942 77 lcd.drawCircle(field[0].x,field[0].y,2,FILL_BLACK);
shahidsajid 10:6c6e09023942 78 lcd.drawCircle(field[1].x,field[1].y,2,FILL_BLACK);
shahidsajid 10:6c6e09023942 79 lcd.drawCircle(field[2].x,field[2].y,2,FILL_BLACK);
shahidsajid 10:6c6e09023942 80 lcd.drawCircle(field[3].x,field[3].y,2,FILL_BLACK);
shahidsajid 10:6c6e09023942 81 }
shahidsajid 10:6c6e09023942 82 void Cricket::set_field(N5110 &lcd){
shahidsajid 10:6c6e09023942 83 int d=0;
shahidsajid 10:6c6e09023942 84 int fieldersCount=0;
shahidsajid 10:6c6e09023942 85 int pos=0;
shahidsajid 10:6c6e09023942 86 int i=0;
shahidsajid 10:6c6e09023942 87 int continueCount=0;
shahidsajid 10:6c6e09023942 88 srand(time(NULL));
shahidsajid 10:6c6e09023942 89 while (fieldersCount!=5){
shahidsajid 10:6c6e09023942 90 d = 1+ rand() % 6;
shahidsajid 10:6c6e09023942 91 printf("DIRECTION %i\n",d);
shahidsajid 10:6c6e09023942 92 pos=rand()%2;
shahidsajid 10:6c6e09023942 93 for (int j=0;j<10;j++){
shahidsajid 10:6c6e09023942 94 if (fieldNumbers[j]==d){
shahidsajid 10:6c6e09023942 95 printf("CHECK");
shahidsajid 10:6c6e09023942 96 continueCount=1;
shahidsajid 10:6c6e09023942 97 }
shahidsajid 10:6c6e09023942 98 }
shahidsajid 10:6c6e09023942 99 if (continueCount==1){
shahidsajid 10:6c6e09023942 100 printf("IM HERE");
shahidsajid 10:6c6e09023942 101 continueCount=0;
shahidsajid 10:6c6e09023942 102 continue;
shahidsajid 10:6c6e09023942 103 }
shahidsajid 10:6c6e09023942 104 printf("YESS");
shahidsajid 10:6c6e09023942 105 if (d==1){ //NW
shahidsajid 10:6c6e09023942 106 if (pos==1){
shahidsajid 10:6c6e09023942 107 field[i].dir=NW;
shahidsajid 10:6c6e09023942 108 field[i].x=30;
shahidsajid 10:6c6e09023942 109 field[i].y=9;
shahidsajid 10:6c6e09023942 110 field[i].position=1;
shahidsajid 10:6c6e09023942 111 fieldNumbers[i]=d;
shahidsajid 10:6c6e09023942 112 i++;
shahidsajid 10:6c6e09023942 113 fieldersCount++;
shahidsajid 10:6c6e09023942 114 }
shahidsajid 10:6c6e09023942 115 else{
shahidsajid 10:6c6e09023942 116 field[i].dir=NW;
shahidsajid 10:6c6e09023942 117 field[i].x=25;
shahidsajid 10:6c6e09023942 118 field[i].y=2;
shahidsajid 10:6c6e09023942 119 field[i].position=0;
shahidsajid 10:6c6e09023942 120 fieldNumbers[i]=d;
shahidsajid 10:6c6e09023942 121 i++;
shahidsajid 10:6c6e09023942 122 fieldersCount++;
shahidsajid 10:6c6e09023942 123 }
shahidsajid 10:6c6e09023942 124 }
shahidsajid 10:6c6e09023942 125
shahidsajid 10:6c6e09023942 126
shahidsajid 10:6c6e09023942 127 if (d==2){ //NE
shahidsajid 10:6c6e09023942 128 if (pos==1){
shahidsajid 10:6c6e09023942 129 field[i].dir=NE;
shahidsajid 10:6c6e09023942 130 field[i].x=55;
shahidsajid 10:6c6e09023942 131 field[i].y=9;
shahidsajid 10:6c6e09023942 132 field[i].position=1;
shahidsajid 10:6c6e09023942 133 fieldNumbers[i]=d;
shahidsajid 10:6c6e09023942 134 i++;
shahidsajid 10:6c6e09023942 135 fieldersCount++;
shahidsajid 10:6c6e09023942 136 }
shahidsajid 10:6c6e09023942 137 else{
shahidsajid 10:6c6e09023942 138 field[i].dir=NE;
shahidsajid 10:6c6e09023942 139 field[i].x=65;
shahidsajid 10:6c6e09023942 140 field[i].y=2;
shahidsajid 10:6c6e09023942 141 field[i].position=0;
shahidsajid 10:6c6e09023942 142 fieldNumbers[i]=d;
shahidsajid 10:6c6e09023942 143 i++;
shahidsajid 10:6c6e09023942 144 fieldersCount++;
shahidsajid 10:6c6e09023942 145 }
shahidsajid 10:6c6e09023942 146 }
shahidsajid 10:6c6e09023942 147
shahidsajid 10:6c6e09023942 148
shahidsajid 10:6c6e09023942 149 if (d==3){ //SW
shahidsajid 10:6c6e09023942 150 if (pos==1){
shahidsajid 10:6c6e09023942 151 field[i].dir=SW;
shahidsajid 10:6c6e09023942 152 field[i].x=33;
shahidsajid 10:6c6e09023942 153 field[i].y=40;
shahidsajid 10:6c6e09023942 154 field[i].position=1;
shahidsajid 10:6c6e09023942 155 fieldNumbers[i]=d;
shahidsajid 10:6c6e09023942 156 i++;
shahidsajid 10:6c6e09023942 157 fieldersCount++;
shahidsajid 10:6c6e09023942 158 }
shahidsajid 10:6c6e09023942 159
shahidsajid 10:6c6e09023942 160 else{
shahidsajid 10:6c6e09023942 161 field[i].dir=SW;
shahidsajid 10:6c6e09023942 162 field[i].x=25;
shahidsajid 10:6c6e09023942 163 field[i].y=44;
shahidsajid 10:6c6e09023942 164 field[i].position=0;
shahidsajid 10:6c6e09023942 165 fieldNumbers[i]=d;
shahidsajid 10:6c6e09023942 166 i++;
shahidsajid 10:6c6e09023942 167 fieldersCount++;
shahidsajid 10:6c6e09023942 168 }
shahidsajid 10:6c6e09023942 169 }
shahidsajid 10:6c6e09023942 170
shahidsajid 10:6c6e09023942 171 if (d==4){ //SE
shahidsajid 10:6c6e09023942 172 if (pos==1){
shahidsajid 10:6c6e09023942 173 field[i].dir=SE;
shahidsajid 10:6c6e09023942 174 field[i].x=55;
shahidsajid 10:6c6e09023942 175 field[i].y=40;
shahidsajid 10:6c6e09023942 176 field[i].position=1;
shahidsajid 10:6c6e09023942 177 fieldNumbers[i]=d;
shahidsajid 10:6c6e09023942 178 i++;
shahidsajid 10:6c6e09023942 179 fieldersCount++;
shahidsajid 10:6c6e09023942 180 }
shahidsajid 10:6c6e09023942 181 else{
shahidsajid 10:6c6e09023942 182 field[i].dir=SE;
shahidsajid 10:6c6e09023942 183 field[i].x=65;
shahidsajid 10:6c6e09023942 184 field[i].y=44;
shahidsajid 10:6c6e09023942 185 field[i].position=0;
shahidsajid 10:6c6e09023942 186 fieldNumbers[i]=d;
shahidsajid 10:6c6e09023942 187 i++;
shahidsajid 10:6c6e09023942 188 fieldersCount++;
shahidsajid 10:6c6e09023942 189 }
shahidsajid 10:6c6e09023942 190 }
shahidsajid 10:6c6e09023942 191
shahidsajid 10:6c6e09023942 192 if (d==5){ //W
shahidsajid 10:6c6e09023942 193 if (pos==1){
shahidsajid 10:6c6e09023942 194 field[i].dir=W;
shahidsajid 10:6c6e09023942 195 field[i].x=3;
shahidsajid 10:6c6e09023942 196 field[i].y=25;
shahidsajid 10:6c6e09023942 197 field[i].position=1;
shahidsajid 10:6c6e09023942 198 fieldNumbers[i]=d;
shahidsajid 10:6c6e09023942 199 i++;
shahidsajid 10:6c6e09023942 200 fieldersCount++;
shahidsajid 10:6c6e09023942 201 }
shahidsajid 10:6c6e09023942 202 else{
shahidsajid 10:6c6e09023942 203 field[i].dir=W;
shahidsajid 10:6c6e09023942 204 field[i].x=25;
shahidsajid 10:6c6e09023942 205 field[i].y=25;
shahidsajid 10:6c6e09023942 206 field[i].position=0;
shahidsajid 10:6c6e09023942 207 fieldNumbers[i]=d;
shahidsajid 10:6c6e09023942 208 i++;
shahidsajid 10:6c6e09023942 209 fieldersCount++;
shahidsajid 10:6c6e09023942 210 }
shahidsajid 10:6c6e09023942 211 }
shahidsajid 10:6c6e09023942 212
shahidsajid 10:6c6e09023942 213 if (d==6){ //W
shahidsajid 10:6c6e09023942 214 if (pos==1){
shahidsajid 10:6c6e09023942 215 field[i].dir=E;
shahidsajid 10:6c6e09023942 216 field[i].x=80;
shahidsajid 10:6c6e09023942 217 field[i].y=20;
shahidsajid 10:6c6e09023942 218 field[i].position=1;
shahidsajid 10:6c6e09023942 219 fieldNumbers[i]=d;
shahidsajid 10:6c6e09023942 220 i++;
shahidsajid 10:6c6e09023942 221 fieldersCount++;
shahidsajid 10:6c6e09023942 222 }
shahidsajid 10:6c6e09023942 223 else{
shahidsajid 10:6c6e09023942 224 field[i].dir=E;
shahidsajid 10:6c6e09023942 225 field[i].x=50;
shahidsajid 10:6c6e09023942 226 field[i].y=20;
shahidsajid 10:6c6e09023942 227 field[i].position=1;
shahidsajid 10:6c6e09023942 228 fieldNumbers[i]=d;
shahidsajid 10:6c6e09023942 229 i++;
shahidsajid 10:6c6e09023942 230 fieldersCount++;
shahidsajid 10:6c6e09023942 231 }
shahidsajid 10:6c6e09023942 232
shahidsajid 10:6c6e09023942 233 }
shahidsajid 10:6c6e09023942 234
shahidsajid 10:6c6e09023942 235 }
shahidsajid 10:6c6e09023942 236
shahidsajid 10:6c6e09023942 237
shahidsajid 10:6c6e09023942 238 }