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
Diff: Cricket/Cricket.cpp
- Revision:
- 30:43aace0fdbdf
- Parent:
- 29:f7a2d2a755ec
- Child:
- 31:eefa1d23a843
--- a/Cricket/Cricket.cpp Tue May 07 18:38:54 2019 +0000 +++ b/Cricket/Cricket.cpp Wed May 08 01:20:06 2019 +0000 @@ -13,12 +13,12 @@ void Cricket::init() { - ux.init(); - bat.init(4,5); - ball.init(2,3); - scoreboard.init(); + _ux.init(); + _bat.init(4,5); + _ball.init(2,3); + _scoreboard.init(); _new_round=1; - _direction_set=0; + direction_set=0; _new_game=1; _fielders_count=0; _init_field_counter=0; @@ -33,15 +33,15 @@ _check_update=-1; } void Cricket::round_reset(){ - ball.reset(); - bat.reset(); + _ball.reset(); + _bat.reset(); _new_round=0; - _direction_set=0; + direction_set=0; _fielders_count=0; _loft_check=0; _check_hit=0; _set_hit=0; - _set_loft=0; + _set_loft= _init_field_counter=0; init_positions(); _position_no=-1; @@ -50,31 +50,33 @@ } void Cricket::game_reset(){ //pad.leds_off(); + _ux.reset(); round_reset(); _new_game=1; - scoreboard.reset(); - ball.reset_ball_count(); + _scoreboard.reset(); + _ball.reset_ball_count(); } void Cricket::intro(N5110 &lcd){ - ux.first_menu(lcd); + _ux.first_menu(lcd); } void Cricket::game(N5110 &lcd,Gamepad &pad){ if (_new_game==1){ printf("TEST 1\n"); - scoreboard.generate_target(); - ux.second_menu(lcd); - _new_game=0; + _scoreboard.generate_target(); + _ux.second_menu(lcd); + _new_game=0; } - if (_new_round==1){ + if (_new_round==1){ if (game_status(lcd)==false){ game_reset(); + _ux.second_menu(lcd); } else{ check_ball_count(lcd); pad.leds_off(); round_reset(); set_field(lcd); - ball.increment_ball_count(); + _ball.increment_ball_count(); play_game(lcd,pad); _new_round=0; } @@ -83,15 +85,15 @@ play_game(lcd,pad); } bool Cricket::game_status(N5110 &lcd){ - if (scoreboard.compare_target()==true){ + if (_scoreboard.compare_target()==true){ printf("COMING FROM HERE"); - ux.victory_menu(lcd); + _ux.victory_menu(lcd); game_reset(); return false; //} } else if(check_ball_count(lcd)==true){ - ux.game_over_menu(lcd,1); + _ux.game_over_menu(lcd,1); game_reset(); return false; } @@ -99,12 +101,11 @@ return true; } } - bool Cricket::check_ball_count(N5110 &lcd){ - int count=ball.get_ball_count(); + int count=_ball.get_ball_count(); if (count==_ball_limit){ _new_round=1; - ball.reset_ball_count(); + _ball.reset_ball_count(); _new_game=1; return true; } @@ -113,11 +114,11 @@ } } void Cricket::check_victory(N5110 &lcd){ - bool _victory_check=scoreboard.compare_target(); + bool _victory_check=_scoreboard.compare_target(); if (_victory_check==true){ - ux.victory_menu(lcd); + _ux.victory_menu(lcd); _new_game=1; - ball.reset_ball_count(); + _ball.reset_ball_count(); } } void Cricket::init_positions(){ @@ -138,102 +139,101 @@ _init_field_counter++; } void Cricket::play_game(N5110 &lcd,Gamepad &pad){ - _check_hit=bat.get_hitBall(ux); + _check_hit=_bat.get_hitBall(_ux); if (_check_hit==1){ _set_hit=1; } - _loft_check=bat.get_loft_ball(ux); + _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); + _check_bowled=_ball.ball_start(pad); } else{ - set_ball_direction(dir); update_game(_set_hit, _set_loft, dir,pad,lcd); } } -void Cricket::update_game(int checkHit,int loft_check, Direction dir,Gamepad &pad,N5110 &lcd){ - int fielder_check=check_fielder(_ball_direction); +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(direction_set==1 && checkHit==1){ if (fielder_check!=-1){ - _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){ + 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); + 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); + _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) + if (_loft_check==1) update_scoreboard(_check_update,6,pad); else - update_scoreboard(_check_update,4,pad); + update_scoreboard(_check_update,4,pad); } } -} +} void Cricket::update_scoreboard(int checkUpdate, int runs,Gamepad &pad){ if (checkUpdate==1){ - - //Hitting the ball straight would be an easy way to score runs so i have restricted it to no runs - if (_ball_direction==N){ - scoreboard.update_score(0); + if (ball_direction==N){ + _scoreboard.update_score(0); pad.led(3,1.0); wait(0.5); _new_round=1; } else{ - scoreboard.update_score(runs); + _scoreboard.update_score(runs); pad.led(3,1.0); wait(0.5); _new_round=1; - } + } } pad.leds_off(); } void Cricket::batsman_out(int option,Gamepad &pad, N5110 &lcd){ - scoreboard.reset(); + _scoreboard.reset(); pad.led(1,1.0); //pad.tone(750.0,0.5); - ball.reset_ball_count(); - ux.game_over_menu(lcd,option); + _ball.reset_ball_count(); + _ux.game_over_menu(lcd,option); game_reset(); _new_game=1; wait(2); pad.leds_off(); - ux.second_menu(lcd); -} + _ux.second_menu(lcd); +} 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); + _ball.draw(lcd); draw_field(lcd); - bat.draw(lcd); - scoreboard.draw(lcd); + _bat.draw(lcd); + _scoreboard.draw(lcd); } void Cricket::set_ball_direction(Direction dir){ - if (_direction_set!=1){ + if (direction_set!=1){ for (int i=0;i<_init_field_counter+1;i++){ if(dir==positions[i].dir){ - _ball_direction=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; + direction_set=1; + break; } } } } int Cricket::check_fielder(Direction dir){ + Vector2D p; if (dir==N){ return -1; } @@ -259,9 +259,9 @@ srand(time(NULL)); while (_fielders_count!=5){ d = 1+ rand() % 6; - pos=rand()%2; + pos=rand()%2; for (int j=0;j<10;j++){ - if (field_numbers[j]==d){ + if (fieldNumbers[j]==d){ continueCount=1; } } @@ -275,7 +275,7 @@ field[i].x=30; field[i].y=9; field[i].position=1; - field_numbers[i]=d; + fieldNumbers[i]=d; i++; _fielders_count++; } @@ -284,18 +284,20 @@ field[i].x=25; field[i].y=2; field[i].position=0; - field_numbers[i]=d; + fieldNumbers[i]=d; i++; _fielders_count++; } } + + if (d==2){ //NE if (pos==1){ field[i].dir=NE; field[i].x=55; field[i].y=9; field[i].position=1; - field_numbers[i]=d; + fieldNumbers[i]=d; i++; _fielders_count++; } @@ -304,40 +306,42 @@ field[i].x=65; field[i].y=2; field[i].position=0; - field_numbers[i]=d; + fieldNumbers[i]=d; i++; _fielders_count++; } } + + if (d==3){ //SW if (pos==1){ field[i].dir=SW; field[i].x=33; field[i].y=40; field[i].position=1; - field_numbers[i]=d; + fieldNumbers[i]=d; i++; _fielders_count++; } - + else{ field[i].dir=SW; field[i].x=25; field[i].y=44; field[i].position=0; - field_numbers[i]=d; + fieldNumbers[i]=d; i++; _fielders_count++; } } - + if (d==4){ //SE if (pos==1){ field[i].dir=SE; field[i].x=55; field[i].y=40; field[i].position=1; - field_numbers[i]=d; + fieldNumbers[i]=d; i++; _fielders_count++; } @@ -346,19 +350,19 @@ field[i].x=65; field[i].y=44; field[i].position=0; - field_numbers[i]=d; + fieldNumbers[i]=d; i++; _fielders_count++; } } - + if (d==5){ //W if (pos==1){ field[i].dir=W; field[i].x=3; field[i].y=30; field[i].position=1; - field_numbers[i]=d; + fieldNumbers[i]=d; i++; _fielders_count++; } @@ -367,18 +371,18 @@ field[i].x=25; field[i].y=30; field[i].position=0; - field_numbers[i]=d; + fieldNumbers[i]=d; i++; _fielders_count++; } - } + } if (d==6){ //W if (pos==1){ field[i].dir=E; field[i].x=80; field[i].y=30; field[i].position=1; - field_numbers[i]=d; + fieldNumbers[i]=d; i++; _fielders_count++; } @@ -387,17 +391,17 @@ field[i].x=60; field[i].y=30; field[i].position=0; - field_numbers[i]=d; + fieldNumbers[i]=d; i++; _fielders_count++; - } + } } } } void Cricket::info_screen(N5110 &lcd,int target){ lcd.clear(); - lcd.printString("YOU ARE GIVEN",2,1); - lcd.printString("10 BALLS ",3,2); + lcd.printString("YOU ARE GIVEN",2,1); + lcd.printString("10 BALLS ",3,2); lcd.refresh(); wait(0.5); char buffer[5]; @@ -409,5 +413,3 @@ lcd.refresh(); wait(3); } - -