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-30
- Revision:
- 22:69d7fe739872
- Parent:
- 21:a0904159e183
- Child:
- 23:a16c97a59a41
File content as of revision 22:69d7fe739872:
#include "Cricket.h" Cricket::Cricket() { } Cricket::~Cricket() { } void Cricket::init() { ux.init(); bat.init(4,5); ball.init(2,3); scoreboard.init(); _new_round=1; direction_set=0; _new_game=1; fieldersCount=0; init_field_counter=0; init_positions(); set_hit=0; set_loft=0; _position_no=-1; check_bowled=0; _ball_limit=3; check_hit=0; loft_check=0; outfield_fielder=-1; check_update=-1; } void Cricket::round_reset(){ ball.reset(); bat.reset(); ux.reset(); _new_round=0; direction_set=0; fieldersCount=0; loft_check=0; check_hit=0; set_hit=0; set_loft=0; ballHit=0; init_field_counter=0; init_positions(); _position_no=-1; check_bowled=0; outfield_fielder=-1; check_update=-1; } void Cricket::game_reset(){ round_reset(); _new_game=1; scoreboard.reset(); ball.reset_ball_count(); } void Cricket::intro(N5110 &lcd){ ux.first_menu(lcd); } void Cricket::game(N5110 &lcd,Gamepad &pad){ if (_new_game==1){ new_game(lcd,pad); } if (_new_round==1){ new_round(lcd,pad); } else{ play_game(lcd,pad); } } void Cricket::new_game(N5110 &lcd, Gamepad &pad){ _new_game=0; scoreboard.generate_target(); ux.second_menu(lcd); ux.info_screen(lcd,scoreboard.get_target()); play_game(lcd,pad); } void Cricket::new_round(N5110 &lcd, Gamepad &pad){ if (scoreboard.compare_target()==true){ ux.victory_menu(lcd); game_reset(); } else if(check_ball_count(lcd)==true){ ux.game_over_menu(lcd,1); game_reset(); } else{ check_ball_count(lcd); pad.leds_off(); round_reset(); set_field(lcd); ball.increment_ball_count(); play_game(lcd,pad); _new_round=0; } } bool Cricket::check_ball_count(N5110 &lcd){ int count=ball.get_ball_count(); if (count==_ball_limit){ _new_round=1; ball.reset_ball_count(); _new_game=1; return true; } else{ return false; } } void Cricket::check_victory(N5110 &lcd){ bool _victory_check=scoreboard.compare_target(); if (_victory_check==true){ ux.victory_menu(lcd); _new_game=1; ball.reset_ball_count(); } } void Cricket::init_positions(){ set_init_positions(42,15,N,1); set_init_positions(84,30,E,2); set_init_positions(0,30,W,3); set_init_positions(2,44,SW,4); set_init_positions(0,2,NW,5); set_init_positions(65,2,NE,6); set_init_positions(60,44,SE,7); } 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){ check_hit=bat.get_hitBall(ux); if (check_hit==1){ set_hit=1; } loft_check=bat.get_loft_ball(ux); if (loft_check==1){ set_loft=1; } Direction dir=pad.get_direction(); if (check_bowled!=1){ check_bowled=ball.ball_start(pad); } else{ update_game(set_hit, set_loft, dir,pad,lcd); } } void Cricket::update_game(int checkHit,int loft_check, Direction dir,Gamepad &pad,N5110 &lcd){ set_ball_direction(dir); int fielder_check=check_fielder(ball_direction); if (checkHit!=1) batsman_out(2,pad,lcd); if(direction_set==1 && checkHit==1){ if (fielder_check!=-1){ int check_update=ball.update_ball(field[fielder_check].x,field[fielder_check].y); printf("Location x %i y %i\n",field[fielder_check].x,field[fielder_check].y); if (loft_check==1){ if (check_update==1) batsman_out(3,pad,lcd); } else update_scoreboard(check_update,field[fielder_check].position+1,pad); } if (fielder_check==-1){ check_update=ball.update_ball(positions[_position_no].x,positions[_position_no].y); printf("location x %i y %i\n update %i\n",positions[_position_no].x,positions[_position_no].y,check_update); if (loft_check==1) update_scoreboard(check_update,6,pad); else update_scoreboard(check_update,4,pad); } } } void Cricket::update_scoreboard(int checkUpdate, int runs,Gamepad &pad){ if (checkUpdate==1){ if (ball_direction==N){ scoreboard.update_score(0); pad.led(3,1.0); wait(0.5); _new_round=1; } else{ scoreboard.update_score(runs); pad.led(3,1.0); wait(0.5); _new_round=1; } } } void Cricket::batsman_out(int option,Gamepad &pad, N5110 &lcd){ scoreboard.reset(); pad.led(1,1.0); //pad.tone(750.0,0.5); ball.reset_ball_count(); ux.game_over_menu(lcd,option); _new_game=1; wait(2); } 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); scoreboard.draw(lcd); } void Cricket::set_ball_direction(Direction dir){ if (direction_set!=1){ for (int i=0;i<init_field_counter+1;i++){ if(dir==positions[i].dir){ ball_direction=dir; printf(" BALL DIRECTION %i x %i y %i \n",positions[i].no,positions[i].x,positions[i].y); _position_no=i; direction_set=1; break; } } } } int Cricket::check_fielder(Direction dir){ Vector2D p; if (dir==N){ return -1; } for (int i=0;i<8;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); lcd.drawCircle(field[4].x,field[4].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=30; field[i].position=1; fieldNumbers[i]=d; i++; fieldersCount++; } else{ field[i].dir=W; field[i].x=25; field[i].y=30; 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=30; field[i].position=1; fieldNumbers[i]=d; i++; fieldersCount++; } else{ field[i].dir=E; field[i].x=60; field[i].y=30; field[i].position=0; fieldNumbers[i]=d; i++; fieldersCount++; } } } }