// This is a baseball simulation just to show array usage
// this is version I
// John Sklar for CEd 516 - Spring, 2001
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class Baseballapplet extends Applet implements ActionListener
{
// initialize the variables
Font VerybigFont = new Font("Helvetica",Font.BOLD,30);
Font bigFont = new Font("Helvetica",Font.BOLD,24);
Font midFont = new Font("Helvetica",Font.BOLD,18);
Button aButton = new Button("Play Ball");
Label Teams = new Label ("Brewers vs. Cubs");
Label header = new Label(" ");
Label brewers = new Label(" ");
Label cubbies = new Label(" ");
Panel top,Amiddle,Bmiddle,Cmiddle,bottom;
public void init()
{
// set all the fonts, add the components
setSize(600,200);
setLayout(new GridLayout(5,1));
top = new Panel();
top.setBackground(Color.white);
top.setForeground(Color.red);
Amiddle = new Panel();
Amiddle.setBackground(Color.black);
Amiddle.setForeground(Color.white);
Bmiddle = new Panel();
Bmiddle.setBackground(Color.white);
Bmiddle.setForeground(Color.black);
Cmiddle = new Panel();
Cmiddle.setBackground(Color.white);
Cmiddle.setForeground(Color.black);
bottom = new Panel();
bottom.setBackground(Color.white);
bottom.setForeground(Color.black);
// set all the fonts
aButton.setFont(bigFont);
header.setFont(bigFont);
brewers.setFont(midFont);
cubbies.setFont(midFont);
// add the panels
add(top);
add(Amiddle);
add(Bmiddle);
add(Cmiddle);
add(bottom);
//put stuff in the panels
Teams.setFont(VerybigFont);
top.add(Teams);
Amiddle.add(header);
Bmiddle.add(brewers);
Cmiddle.add(cubbies);
bottom.add(aButton);
// set some stuff up.
aButton.addActionListener(this);
}
public void start()
{
header.setText(" Box Score: ");
}
public void actionPerformed(ActionEvent e)
{
String boxscore[] = new String[2];
String[] teams = {" Brewers:"," Cubbies:"};
int i,temp,innings_played=0;
int[] scores = new int[2];
int[][] the_game = new int[50][2];
if (aButton.getLabel()=="Play Ball"){
for( i = 0;i<9 || scores[0] == scores[1] ;i++){
for(int j = 0 ; j < 2; ++j){
temp = (int) (-(5+(Math.random()*10))+Math.random()*(Math.random()*18));
the_game[i][j] = temp < 0 ? 0 : temp;
scores[j] += the_game[i][j];
}
innings_played++;
}
if(innings_played > 15){
aButton.setLabel("Play Ball");
brewers.setText("Game called because of curfew!");
}
else{
for(int j = 0 ; j < 2; ++j)
boxscore[j] = teams[j] + " ";
for(int j = 0 ; j < 2; ++j){
for( i = 0;i<innings_played;i++)
boxscore[j] += the_game[i][j] + ( the_game[i][j]<10 ? " " : " ");
}
for(int j = 0 ; j < 2; ++j)
boxscore[j] += " Total: " + scores[j] ;
if(scores[0]>scores[1])
header.setText("Brew Crew wins in " + innings_played + " innings!");
else
header.setText("Cubbies win in " + innings_played + " innings!");
brewers.setText(boxscore[0]);
cubbies.setText(boxscore[1]);
aButton.setLabel(" Again? ");
}
}
else{
aButton.setLabel("Play Ball");
header.setText(" Box Score: ");
brewers.setText(" ");
cubbies.setText(" ");
}
}
}