Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed
Cricket/Cricket.cpp
- Committer:
- shahidsajid
- Date:
- 2019-04-23
- Revision:
- 13:924891519a95
- Parent:
- 12:954da4f4e565
- Child:
- 14:122eaa3b7a50
File content as of revision 13:924891519a95:
#include "Cricket.h" Cricket::Cricket() { } Cricket::~Cricket() { } void Cricket::init() { bat.init(4,5); ball.init(2,3); //float f; new_round=0; direction_set=0; fieldersCount=0; init_field_counter=0; init_positions(); fielder_no=-1; check_bowled=0; outfield_fielder=-1; } void Cricket::reset(){ ball.reset(); 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){ //ball.reset(); set_field(lcd); play_game(lcd,pad); } else { play_game(lcd,pad); } } 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 loft_check=bat.get_loft_ball(); Direction dir=pad.get_direction(); if (check_bowled!=1){ check_bowled=ball.ball_start(); } else{ update_game(check_hit, loft_check, dir); } } void Cricket::update_game(int checkHit,int loft_check, Direction dir){ Vector2D pos=ball.get_pos(); set_ball_direction(dir); int fielder_check=check_fielder(ball_direction); if (checkHit ==1 && ballHit!=1){ ballHit=1; } if (fielder_check!=-1){ if(direction_set==1 && ballHit==1){ ball.update_ball_y(field[fielder_check].y); ball.update_ball_x(field[fielder_check].x); } } if (fielder_check==-1){ if(direction_set==1 && ballHit==1){ ball.update_ball_y(positions[fielder_no].y); ball.update_ball_x(positions[fielder_no].x); } } } void Cricket::draw(N5110 &lcd){ lcd.drawCircle((WIDTH/2),HEIGHT/2,23,FILL_TRANSPARENT); lcd.drawRect(37,11,12,30,FILL_TRANSPARENT); ball.draw(lcd); draw_field(lcd); bat.draw(lcd); } void Cricket::set_ball_direction(Direction dir){ 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; } } } } int Cricket::check_fielder(Direction dir){ Vector2D p; for (int i=0;i<7;i++){ if(dir == field[i].dir){ return i; } } return -1; } void Cricket::draw_field(N5110 &lcd){ lcd.drawCircle(field[0].x,field[0].y,2,FILL_TRANSPARENT); lcd.drawCircle(field[1].x,field[1].y,2,FILL_TRANSPARENT); lcd.drawCircle(field[2].x,field[2].y,2,FILL_TRANSPARENT); lcd.drawCircle(field[3].x,field[3].y,2,FILL_TRANSPARENT); } void Cricket::set_field(N5110 &lcd){ int d=0; int pos=0; int i=0; int continueCount=0; srand(time(NULL)); while (fieldersCount!=5){ d = 1+ rand() % 6; pos=rand()%2; for (int j=0;j<10;j++){ if (fieldNumbers[j]==d){ continueCount=1; } } if (continueCount==1){ continueCount=0; continue; } if (d==1){ //NW if (pos==1){ field[i].dir=NW; field[i].x=30; field[i].y=9; field[i].position=1; fieldNumbers[i]=d; i++; fieldersCount++; } else{ field[i].dir=NW; field[i].x=25; field[i].y=2; field[i].position=0; fieldNumbers[i]=d; i++; fieldersCount++; } } if (d==2){ //NE if (pos==1){ field[i].dir=NE; field[i].x=55; field[i].y=9; field[i].position=1; fieldNumbers[i]=d; i++; fieldersCount++; } else{ field[i].dir=NE; field[i].x=65; field[i].y=2; field[i].position=0; fieldNumbers[i]=d; i++; fieldersCount++; } } if (d==3){ //SW if (pos==1){ field[i].dir=SW; field[i].x=33; field[i].y=40; field[i].position=1; fieldNumbers[i]=d; i++; fieldersCount++; } else{ field[i].dir=SW; field[i].x=25; field[i].y=44; field[i].position=0; fieldNumbers[i]=d; i++; fieldersCount++; } } if (d==4){ //SE if (pos==1){ field[i].dir=SE; field[i].x=55; field[i].y=40; field[i].position=1; fieldNumbers[i]=d; i++; fieldersCount++; } else{ field[i].dir=SE; field[i].x=65; field[i].y=44; field[i].position=0; fieldNumbers[i]=d; i++; fieldersCount++; } } if (d==5){ //W if (pos==1){ field[i].dir=W; field[i].x=3; field[i].y=25; field[i].position=1; fieldNumbers[i]=d; i++; fieldersCount++; } else{ field[i].dir=W; field[i].x=25; field[i].y=25; field[i].position=0; fieldNumbers[i]=d; i++; fieldersCount++; } } if (d==6){ //W if (pos==1){ field[i].dir=E; field[i].x=80; field[i].y=25; field[i].position=1; fieldNumbers[i]=d; i++; fieldersCount++; } else{ field[i].dir=E; field[i].x=50; field[i].y=25; field[i].position=1; fieldNumbers[i]=d; i++; fieldersCount++; } } } }