import java.applet.Applet;
import java.awt.*;

public class baseball extends Applet
{
	Button PitchButton;
	Button HitButton; 
	Button NextteamButton;	
	Button NewgameButton;
	int randnum;
	int awayscore;
	int homescore;
	int inningcount = 2;
	int hscore1 = 0;
	int hscore2 = 0;
	int hscore3 = 0;
	int hscore4 = 0;
	int hscore5 = 0;
	int hscore6 = 0;
	int hscore7 = 0;
	int hscore8 = 0;
	int hscore9 = 0;
	int ascore1 = 0;
	int ascore2 = 0;
	int ascore3 = 0;
	int ascore4 = 0;
	int ascore5 = 0;
	int ascore6 = 0;
	int ascore7 = 0;
	int ascore8 = 0;
	int ascore9 = 0;
	boolean first = false; //0 represents that nobody is on, 1 represents that someone is on base.
	boolean second = false;
	boolean third = false;
	boolean home = false;
	boolean nextinning = false;
	boolean welcome = false;
	boolean hometeam = false;
	boolean awayteam = false;
	boolean over = false;
	boolean win = false;
	int strike;
	int ball;
	int out = 0;
	String temp = new String();
	String stringawayscore;
	String stringhomescore;
	String stringstrike;
	String stringball;
	String stringout;
	String stringhscore1;
	String stringhscore2;
	String stringhscore3;
	String stringhscore4;
	String stringhscore5;
	String stringhscore6;
	String stringhscore7;
	String stringhscore8;
	String stringhscore9;
	String stringascore1;
	String stringascore2;
	String stringascore3;
	String stringascore4;
	String stringascore5;
	String stringascore6;
	String stringascore7;
	String stringascore8;
	String stringascore9;

	public void init()
	{
	PitchButton = new Button ("Pitch");
	HitButton = new Button ("Hit");
	NextteamButton = new Button ("Next Team");
	NewgameButton = new Button ("New Game");
	add(PitchButton);
	add(HitButton);
	add(NextteamButton);
	add(NewgameButton);

	PitchButton.setEnabled(false);
	HitButton.setEnabled(false);
	NextteamButton.setEnabled(false);

	welcome = true;
	}

//------------------------------------------------------------userinterface
	public void paint (Graphics g) 
	{
	//drawing background
	g.setColor(Color.green);
	g.fillRect(0,0,820,520);

	//drawing larger diamond
	g.setColor(Color.black);
	int dx[] = {410,490,410,330};
	int dy[] = {460,380,300,380};
	int dpt = dx.length;
	g.drawPolygon(dx,dy,dpt);

	//drawing home plate
	int hbx[] = {410,420,420,400,400};
	int hby[] = {460,450,440,440,450};
	int hbpt = hbx.length;
	g.fillPolygon(hbx,hby,hbpt);

	//drawing firstbase
	int fbx[] = {490,480,470,480};
	int fby[] = {380,370,380,390};
	int fbpt = fbx.length;
	g.fillPolygon(fbx,fby,fbpt);

	//drawing secondbase
	int sbx[] = {410,400,410,420};
	int sby[] = {300,310,320,310};
	int sbpt = sbx.length;
	g.fillPolygon(sbx,sby,sbpt);

	//drawing thirdbase
	int tbx[] = {330,340,350,340};
	int tby[] = {380,370,380,390};
	int tbpt = tbx.length;
	g.fillPolygon(tbx,tby,tbpt);

	//drawing outfield lines and pitchers mound
	g.drawLine(490,380,590,280);
	g.drawLine(330,380,230,280);
	g.drawArc(230,130,360,300,0,180);
	g.drawLine(410,460,670,280);
	g.drawLine(410,460,150,280);
	g.drawOval(390,360,40,40);

	//drawing BatterCount
	g.drawRect(630,30,170,120);
	g.drawString("Inning",640,50);
	g.drawString("Ball(s)",640,80);
	g.drawString("Strike(s)",640,110);
	g.drawString("Out(s)",640,140);
	g.drawLine(700,60,800,60);
	g.drawLine(700,30,700,150);
	g.drawLine(700,90,800,90);
	g.drawLine(700,120,800,120);
	g.drawLine(700,150,800,150);

	//drawing Scoreboard
	g.drawRect(20,30,500,90);
	g.drawString("Inning",30,50);
	g.drawString("Away (red)",30,80);
	g.drawString("Home (blue)",30,110);
	g.drawLine(20,60,520,60);
	g.drawLine(120,60,120,120);
	g.drawLine(158,60,158,120);
	g.drawLine(196,60,196,120);
	g.drawLine(234,60,234,120);
	g.drawLine(272,60,272,120);
	g.drawLine(310,60,310,120);
	g.drawLine(348,60,348,120);
	g.drawLine(386,60,386,120);
	g.drawLine(424,60,424,120);
	g.drawLine(462,60,462,120);
	g.drawString("1",138,50);
	g.drawString("2",176,50);
	g.drawString("3",214,50);
	g.drawString("4",252,50);
	g.drawString("5",290,50);
	g.drawString("6",328,50);
	g.drawString("7",366,50);
	g.drawString("8",404,50);
	g.drawString("9",442,50);
	g.drawString("Runs",470,50);
//----------------------------------------------------welcome note
		if (welcome == true)
		{
			g.drawString("welcome to the game. you are the home team",400,470);
			welcome = false;
		}
//---------------------------------------------------inning over
		if (nextinning == true)
		{
			if (over != true)
				g.drawString("Time to switch teams",370,470);
		}
//---------------------------------------------------displaying away score
	stringawayscore = temp.valueOf(awayscore);
	g.drawString(stringawayscore,490,80);

	stringhscore1 = temp.valueOf(ascore1);
	g.drawString (stringhscore1, 138,80);

	stringhscore2 = temp.valueOf(ascore2);
	g.drawString (stringhscore2, 176,80);

	stringhscore3 = temp.valueOf(ascore3);
	g.drawString (stringhscore3, 214,80);

	stringhscore4 = temp.valueOf(ascore4);
	g.drawString (stringhscore4, 252,80);

	stringhscore5 = temp.valueOf(ascore5);
	g.drawString (stringhscore5, 290,80);

	stringhscore6 = temp.valueOf(ascore6);
	g.drawString (stringhscore6, 328,80);

	stringhscore7 = temp.valueOf(ascore7);
	g.drawString (stringhscore7, 366,80);

	stringhscore8 = temp.valueOf(ascore8);
	g.drawString (stringhscore8, 404,80);

	stringhscore9 = temp.valueOf(ascore9);
	g.drawString (stringhscore9, 442,80);
//---------------------------------------------------counting home scoring
	if (inningcount == 2)
	{
		ascore1 = awayscore - (ascore2 + ascore3 + ascore4 + ascore5 + ascore6 + ascore7 + ascore8 + ascore9);
	}
	else if (inningcount == 4)
	{
		ascore2 = awayscore - (ascore1 + ascore3 + ascore4 + ascore5 + ascore6 + ascore7 + ascore8 + ascore9);
	}
	else if (inningcount == 6)
	{
		ascore3 = awayscore - (ascore1 + ascore2 + ascore4 + ascore5 + ascore6 + ascore7 + ascore8 + ascore9);
	}
	else if (inningcount == 8)
	{
		ascore4 = awayscore - (ascore1 + ascore2 + ascore3 + ascore5 + ascore6 + ascore7 + ascore8 + ascore9);
	}
	else if (inningcount == 10)
	{
		ascore5 = awayscore - (ascore1 + ascore2 + ascore3 + ascore4 + ascore6 + ascore7 + ascore8 + ascore9);
	}
	else if (inningcount == 12)
	{
		ascore6 = awayscore - (ascore1 + ascore2 + ascore3 + ascore4 + ascore5 + ascore7 + ascore8 + ascore9);
	}
	else if (inningcount == 14)
	{
		ascore7 = awayscore - (ascore1 + ascore2 + ascore3 + ascore4 + ascore5 + ascore6 + ascore8 + ascore9);
	}
	else if (inningcount == 16)
	{
		ascore8 = awayscore - (ascore1 + ascore2 + ascore3 + ascore4 + ascore5 + ascore6 + ascore7 + ascore9);
	}
	else if (inningcount == 18)
	{
		ascore9 = awayscore - (ascore1 + ascore2 + ascore3 + ascore4 + ascore5 + ascore6 + ascore7 + ascore8);
	}
//---------------------------------------------------displaying home score
	stringhomescore = temp.valueOf(homescore);
	g.drawString(stringhomescore,490,110);

	stringhscore1 = temp.valueOf(hscore1);
	g.drawString (stringhscore1, 138,110);

	stringhscore2 = temp.valueOf(hscore2);
	g.drawString (stringhscore2, 176,110);

	stringhscore3 = temp.valueOf(hscore3);
	g.drawString (stringhscore3, 214,110);

	stringhscore4 = temp.valueOf(hscore4);
	g.drawString (stringhscore4, 252,110);

	stringhscore5 = temp.valueOf(hscore5);
	g.drawString (stringhscore5, 290,110);

	stringhscore6 = temp.valueOf(hscore6);
	g.drawString (stringhscore6, 328,110);

	stringhscore7 = temp.valueOf(hscore7);
	g.drawString (stringhscore7, 366,110);

	stringhscore8 = temp.valueOf(hscore8);
	g.drawString (stringhscore8, 404,110);

	stringhscore9 = temp.valueOf(hscore9);
	g.drawString (stringhscore9, 442,110);
//---------------------------------------------------counting home scoring
	if (inningcount == 3)
	{
		hscore1 = homescore - (hscore2 + hscore3 + hscore4 + hscore5 + hscore6 + hscore7 + hscore8 + hscore9);
	}
	else if (inningcount == 5)
	{
		hscore2 = homescore - (hscore1 + hscore3 + hscore4 + hscore5 + hscore6 + hscore7 + hscore8 + hscore9);
	}
	else if (inningcount == 7)
	{
		hscore3 = homescore - (hscore1 + hscore2 + hscore4 + hscore5 + hscore6 + hscore7 + hscore8 + hscore9);
	}
	else if (inningcount == 9)
	{
		hscore4 = homescore - (hscore1 + hscore2 + hscore3 + hscore5 + hscore6 + hscore7 + hscore8 + hscore9);
	}
	else if (inningcount == 11)
	{
		hscore5 = homescore - (hscore1 + hscore2 + hscore3 + hscore4 + hscore6 + hscore7 + hscore8 + hscore9);
	}
	else if (inningcount == 13)
	{
		hscore6 = homescore - (hscore1 + hscore2 + hscore3 + hscore4 + hscore5 + hscore7 + hscore8 + hscore9);
	}
	else if (inningcount == 15)
	{
		hscore7 = homescore - (hscore1 + hscore2 + hscore3 + hscore4 + hscore5 + hscore6 + hscore8 + hscore9);
	}
	else if (inningcount == 17)
	{
		hscore8 = homescore - (hscore1 + hscore2 + hscore3 + hscore4 + hscore5 + hscore6 + hscore7 + hscore9);
	}
	else if (inningcount == 19)
	{
		hscore9 = homescore - (hscore1 + hscore2 + hscore3 + hscore4 + hscore5 + hscore6 + hscore7 + hscore8);
	}
//---------------------------------------------------displaying batters box
	stringstrike = temp.valueOf(strike);
	g.drawString(stringstrike,770,110);	

	stringball = temp.valueOf(ball);
	g.drawString(stringball,770,80);

	stringout = temp.valueOf(out);
	g.drawString(stringout,770,140);

	stringout = temp.valueOf(inningcount/2);
	g.drawString(stringout,770,50);	

/*******************************************************
RESERVED FOR WIN AND LOSE.
*******************************************************/
	if (over == true) 
	{
		if (win == true)
		{
			g.drawString("You Win",370,470);
		}
		else
		{ 
			g.drawString("You lost", 370,470);
		}
	}



//---------------------------------------------------drawing commentary
	
		if (randnum > 0 && randnum < 6) //single
		{
			g.drawString("SINGLE!",100,260);
		}

		else if (randnum > 5 && randnum < 9) //double
		{
			g.drawString("DOUBLE!",100,260);
		}
		 
		else if (randnum > 8 && randnum < 10) //triple
		{
			g.drawString("TRIPLE!",100,260);
		}
		 
		else if (randnum > 9 && randnum < 11) //person scored
		{
			g.drawString("HOME RUN!!!", 100,260);
		}
		else if (randnum > 10 && randnum < 51) //strike
		{
			g.drawString("STTTRIKE", 100,260);
		}
		else if (randnum > 50 && randnum < 76) //ball
		{
			g.drawString("BALL", 100,250);
		}
		else if (randnum > 75 && randnum < 86)//foul ball
		{
			g.drawString("FOUL BALL", 100,260);			
		}
		else if (randnum > 85 && randnum < 101)//flyout or popout
		{
			g.drawString("High Fly Ball...", 100,245);
			g.drawString("RUNNER'S OUT", 100,260);			
		}

//------------------------------------------------------------drawing base colour
	//When buttons are pressed
		if (first == true) //person on first
		{
			if (hometeam == true)
			{
				g.setColor(Color.blue);
			}
			if (awayteam == true)
			{
				g.setColor(Color.red);
			}
			int bfbx[] = {490,480,470,480};
			int bfby[] = {380,370,380,390};
			int bfbpt = bfbx.length;
			g.fillPolygon(bfbx,bfby,bfbpt);	
		}
	
		if (second == true) //person on second
		{
			if (hometeam == true)
			{
				g.setColor(Color.blue);
			}
			if (awayteam == true)
			{
				g.setColor(Color.red);
			}
			int bsbx[] = {410,400,410,420};
			int bsby[] = {300,310,320,310};
			int bsbpt = bsbx.length;
			g.fillPolygon(bsbx,bsby,bsbpt);
		}
	
		if (third == true) //person on third
		{
			if (hometeam == true)
			{
				g.setColor(Color.blue);
			}		
			if (awayteam == true)
			{
				g.setColor(Color.red);
			}
			int btbx[] = {330,340,350,340};
			int btby[] = {380,370,380,390};
			int btbpt = btbx.length;
			g.fillPolygon(btbx,btby,btbpt);
		}

		if (home == true) //person scored
		{
			//g.setColor(Color.black);
			g.drawString("Run SCORED!!!",400,470);
		}
	}	
//------------------------------------------------------------Batting Methods
	void singleMeth()
	{
		strike = 0;
		ball = 0;
		if (third == true)
		{
			home = true;
			third = false;
			if (hometeam == true)
			{
				homescore ++;
			}
			if (awayteam == true)
			{
				awayscore ++;
			}
		}
		
		if (second == true)
		{
			third = true;
			second = false;
		}

		if (first == true)
		{
			second = true;
			first = false;
		}
		first = true;
	}

	void doubleMeth()
	{
		strike = 0;
		ball = 0;
		if (third == true)
		{
			home = true;
			third = false;
			if (hometeam == true)
			{
				homescore ++;
			}
			if (awayteam == true)
			{
				awayscore ++;
			}
		}
		
		if (second == true)
		{
			home = true;
			second = false;
			if (hometeam == true)
			{
				homescore ++;
			}
			if (awayteam == true)
			{
				awayscore ++;
			}
		}

		if (first == true)
		{
			third = true;
			first = false;			
		}
		second = true;
	}

	void tripleMeth()
	{
		strike = 0;
		ball = 0;
		if (third == true)
		{
			home = true;
			third = false;
			if (hometeam == true)
			{
				homescore ++;
			}
			if (awayteam == true)
			{
				awayscore ++;
			}
		}
		
		if (second == true)
		{
			home = true;
			second = false;
			if (hometeam == true)
			{
				homescore ++;
			}
			if (awayteam == true)
			{
				awayscore ++;
			}
		}

		if (first == true)
		{
			home = true;
			first = false;
			if (hometeam == true)
			{
				homescore ++;
			}			
			if (awayteam == true)
			{
				awayscore ++;
			}
		}
		third = true;
	}

	void homerunMeth()
	{
		strike = 0;
		ball = 0;	
		if (third == true)
		{
			home = true;
			third = false;
			if (hometeam == true)
			{
				homescore ++;
			}
			if (awayteam == true)
			{
				awayscore ++;
			}
		}
		
		if (second == true)
		{
			home = true;
			second = false;
			if (hometeam == true)
			{
				homescore ++;
			}
			if (awayteam == true)
			{
				awayscore ++;
			}
		}

		if (first == true)
		{
			first = false;
			if (hometeam == true)
			{
				homescore++;
			}
			if (awayteam == true)
			{
				awayscore++;
			}
		}

		if (hometeam == true)
		{
			homescore ++;
		}
		if (awayteam == true)
		{
			awayscore ++;
		}
		home = true;
	}

	void strikeMeth()
	{
		strike ++;
		if (strike > 2)
		{
			out ++;
			strike = 0;
			ball = 0;
		}

		if (out > 2)
		{
			nextinning = true;
			nextMeth();
		}

	}

	void ballMeth()
	{
		ball++;
		if (ball > 3)
		{
			singleMeth();
			ball = 0;
			strike = 0;
		}
	}

	void foulMeth()
	{
		if (strike < 2)
		{
			strike ++;
		}
	}

	void winMeth()
	{
		over = true;
		repaint();
	}
	
	void outMeth()
	{
		out++;
		ball = 0;
		strike = 0;
		if (out > 2)
		{
			nextinning = true;
			nextMeth();
		}
		
	}
		


//------------------------------------------------------------------------Button Methods
	void pitchMeth()
 	{
		home = false;
		hometeam = false;
		awayteam = true;
		randnum = (int) (Math.random()*101);
		if (randnum > 0 && randnum < 6) //single ===> 5% chance of hitting a single
		{
			singleMeth();				
		} 		
		else if (randnum > 5 && randnum < 9) //double ===> 3% chance of hitting a double
		{
			doubleMeth();				
		}
 		else if (randnum > 8 && randnum < 10) //triple ===> 1% chance of hitting a triple
		{	
			tripleMeth();				
		}
 		else if (randnum > 9 && randnum < 11) //homerun ===> 1% chance of hitting a homerun
		{
			homerunMeth();
		}
		else if (randnum > 10 && randnum <51 ) //strike ===> 40% chance of strike
		{
			strikeMeth();
		}
		else if (randnum > 50 && randnum < 76) //ball ===> 25% chance of ball
		{
			ballMeth();
		}
		else if (randnum > 75 && randnum < 86) //foul ball ===> 10% chance of foul
		{
			foulMeth();
		}
		else if (randnum > 85 && randnum < 101) //out ===> 15% chance of flyout or popout
		{
			outMeth();				
		}
	}

	void hitMeth()
 	{
		home = false;
		hometeam = true;
		awayteam = false;
		randnum = (int) (Math.random()*101);
		if (randnum > 0 && randnum < 6) //single ===> 5% chance of hitting a single
		{
			singleMeth();				
		} 		
		else if (randnum > 5 && randnum < 9) //double ===> 3% chance of hitting a double
		{
			doubleMeth();				
		}
 		else if (randnum > 8 && randnum < 10) //triple ===> 1% chance of hitting a triple
		{	
			tripleMeth();				
		}
 		else if (randnum > 9 && randnum < 11) //homerun ===> 1% chance of hitting a homerun
		{
			homerunMeth();
		}
		else if (randnum > 10 && randnum <51 ) //strike ===> 40% chance of strike
		{
			strikeMeth();
		}
		else if (randnum > 50 && randnum < 76) //ball ===> 25% chance of ball
		{
			ballMeth();
		}
		else if (randnum > 75 && randnum < 86) //foul ball ===> 10% chance of foul
		{
			foulMeth();
		}
			else if (randnum > 86 && randnum < 101) //out ===> 15% chance of flyout or popout
		{
			outMeth();				
		}
	}
	
	void nextMeth()
	{
		PitchButton.setEnabled(false);
		HitButton.setEnabled(false);
		NextteamButton.setEnabled(true);
		NewgameButton.setEnabled(false);
	}

	void startMeth()
 	{
		over = false;
		PitchButton.setEnabled(true);
		HitButton.setEnabled(false);
		NextteamButton.setEnabled(false);
		NewgameButton.setEnabled(false);
		welcome = false;
		randnum = 0;
		strike = 0;
		ball = 0;
		out = 0;
		home = false;
		first = false;
		second = false;
		third = false;
		nextinning = false;
		inningcount = 2;
		awayscore = 0;
		homescore = 0;
		ascore1 = 0;
		ascore2 = 0;
		ascore3 = 0;
		ascore4 = 0;
		ascore5 = 0;
		ascore6 = 0;
		ascore7 = 0;
		ascore8 = 0;
		ascore9 = 0;
		hscore1 = 0;
		hscore2 = 0;
		hscore3 = 0;
		hscore4 = 0;
		hscore5 = 0;
		hscore6 = 0;
		hscore7 = 0;
		hscore8 = 0;
		hscore9 = 0;
	}


//-------------------------------------------------------------receiving buttons

	public boolean action(Event evt,Object arg)
	{	

		if (evt.target instanceof Button)
		{

			if ((Button) (evt.target) == PitchButton)
			{
				pitchMeth();
			}
			
			else if ((Button) (evt.target) == HitButton)
			{
				hitMeth();
			}	
			
			else if ((Button) (evt.target) == NextteamButton) 
			{
				inningcount++;
				if (inningcount < 20)
				{

					welcome = false;
					randnum = 0;
					strike = 0;
					ball = 0;
					out = 0;
					home = false;
					first = false;
					second = false;
					third = false;
					nextinning = false;
					NextteamButton.setEnabled(false);
					NewgameButton.setEnabled(false);
					
					if (hometeam == true)
					{						
						HitButton.setEnabled(false);
						PitchButton.setEnabled(true);
						
					}
					else 
					{
						
						PitchButton.setEnabled(false);
						HitButton.setEnabled(true);
					}

					if (inningcount == 19) 
					{
						if (homescore > awayscore)
						{
							win = true;
							winMeth();
							HitButton.setEnabled(false);
							NextteamButton.setEnabled(false);
							NewgameButton.setEnabled(true);
						}
					}

				}
				
	
				else
				{
					if (homescore > awayscore)
					{
						win = true;
					}
					winMeth();
					NextteamButton.setEnabled(false);
					NewgameButton.setEnabled(true);
				}
			}
			else
			{
				startMeth();
				
			}
			repaint();
			return true;
		}
		return false;
	}
}
