/*
William Brownlow Final project
4-25-98 This is a simulator much like SimCity but with a beehive instead
It should require some skill to play but its not that hard if one
has any experience with bees. Enjoy. WGBV
*/
import java.awt.event.*;
import java.awt.*;
import java.applet.Applet;
public class BeeHive extends Applet implements Runnable, ActionListener
{
Panel buttonPanel;
Panel textPanel;
double RobbedHoney = 0.0;
TextField ytdProducedHoney;
TextField currentHoneyInHive;
TextField currentBeesInHive;
TextField currentBroodInHive;
TextField currentDisease;
public int build[]; //hold equipment for hive
/* 1 = Base Board //0
2 = Hive Body Max = 2 //1,2
3 = Excluder //3
4 = Super Max = 5 //4,5,6,7,8
5 = Hive Cover //9
*/
Button Speed;
Button CheckAll;
Button CheckHoney;
Button RobAll;
Button CheckBees;
Button Medicate;
Thread runthread;
Hive myHive;
int speedValue = 0; // 0 is stop, 1 is slow, 2 medium , 3 is fast
String speedLabel[];
String diseaseLabel[];
int diseaseValue = 0; // 0 is none, 1 is Varo Mites, 2 is Tracheal Mites,
// 3 is Ants, 4 is Bear (Game Over)
double honeyProduced = 0.0;
boolean runVal = true;
public void init()
{
speedLabel = new String[4];
speedLabel[0] = " Stop ";
speedLabel[1] = " Slow ";
speedLabel[2] = " Medium ";
speedLabel[3] = " Fast ";
build = new int[10];
for (int i = 0; i < 10 ; i ++)
build[i] = 0;
myHive = new Hive(2, 4);
buttonPanel = new Panel();
textPanel = new Panel();
Speed = new Button(speedLabel[0]);
Speed.addActionListener(this);
CheckAll = new Button("Check All");
CheckAll.addActionListener(this);
CheckHoney = new Button("Check Supers");
CheckHoney.addActionListener(this);
RobAll = new Button("Rob Supers");
RobAll.addActionListener(this);
CheckBees = new Button("Check Bees");
CheckBees.addActionListener(this);
Medicate = new Button("Medicate");
Medicate.addActionListener(this);
buttonPanel.setLayout( new GridLayout( 2, 4) ) ;
buttonPanel.add(Speed);
buttonPanel.add(CheckAll);
buttonPanel.add(CheckHoney);
buttonPanel.add(RobAll);
buttonPanel.add(CheckBees);
buttonPanel.add(Medicate);
setLayout( new BorderLayout() );
add( buttonPanel, BorderLayout.SOUTH );
ytdProducedHoney = new TextField("Honey Produced");
ytdProducedHoney.setEditable(false);
currentHoneyInHive = new TextField("Current Honey in Hive");
currentHoneyInHive.setEditable(false);
currentBroodInHive = new TextField("Current Brood in Hive");
currentBroodInHive.setEditable(false);
currentDisease = new TextField("Current Diseases (if any)");
currentDisease.setEditable(false);
currentBeesInHive = new TextField("Current Bees in Hive");
currentBeesInHive.setEditable(false);
textPanel.setLayout( new GridLayout(5,1));
textPanel.add(ytdProducedHoney);
textPanel.add(currentHoneyInHive);
textPanel.add(currentBeesInHive);
textPanel.add(currentBroodInHive);
textPanel.add(currentDisease);
add( textPanel, BorderLayout.EAST);
diseaseLabel = new String[5];
diseaseLabel[0] = "No Diseases";
diseaseLabel[1] = "Varo Mites";
diseaseLabel[2] = "Tracheal Mites";
diseaseLabel[3] = "Ants";
diseaseLabel[4] = "Brown Bear";
setSize(600,380);
setBackground(Color.green);
repaint();
}//ends init on the main applet
public void paint( Graphics g )
{
int x, i, m, n, wid, hei;
m = 20;
n = 20;
wid = 200;
hei = 10;
double CheckedHoney = 0.0;
for( i = 0; i < 10 ; i++)
{
if ( build[i] == 4)
CheckedHoney += myHive.CHECK(i);
}
currentHoneyInHive.setText("Honey in Hive " + ( (int) CheckedHoney));
if (diseaseValue != 0)
currentDisease.setText("Disease: " + diseaseLabel[diseaseValue]);
// else
// currentDisease.setText("No Diseases present" );
int CheckedBees = 0;
CheckedBees += myHive.h1.getBees();
CheckedBees += myHive.h2.getBees();
CheckedBees += myHive.s1.getBees();
CheckedBees += myHive.s2.getBees();
CheckedBees += myHive.s3.getBees();
CheckedBees += myHive.s4.getBees();
CheckedBees += myHive.s5.getBees();
currentBeesInHive.setText("Bees In Hive " + CheckedBees);
double totalB= 0.0;
totalB = myHive.h1.getBrood() + myHive.h2.getBrood();
currentBroodInHive.setText("" + ((int) totalB) + " Pounds of Brood");
for( i = 9; i >=0 ; i--)
{
if ( build[i] == 5 )
{
hei = 20;
g.drawRect( (m-10) , n , (wid+20) , hei );
n = (n+hei);
}
else if ( build[i] == 4 )
{
hei = 35;
g.drawRect( m , n , wid , hei );
n = (n+hei);
x = (i-3);
switch (x)
{
case 1:
if (myHive.s1.isFullHoney())
{
g.drawString("Is Full " , (m+wid+5) , (n-hei)+8);
// g.fillRect( m , (n - hei), wid ,hei);
}
else
{
// g.fillRect(m , n - hei + (35 * (int) (myHive.s1.getHoney()/ 30.0)), wid , n - hei + (35 * (int) (myHive.s1.getHoney()/ 30.0)) );
g.drawString("" + ((int) myHive.s1.getHoney()) + "Pounds of Honey" , (m+wid+5) , (n-hei)+8);
}
break;
case 2:
if (myHive.s2.isFullHoney())
{
// g.fillRect( m , (n - hei), wid ,hei);
g.drawString("Is Full " , (m+wid+5) , (n-hei)+8);
}
else
{
// g.fillRect(m , n - hei + (35 * (int) (myHive.s2.getHoney()/ 30.0)), wid , n - hei + (35 * (int) (myHive.s2.getHoney()/ 30.0)) );
g.drawString("" + ((int) myHive.s2.getHoney() ) + "Pounds of Honey", (m+wid+5) , (n-hei)+8);
}
break;
case 3:
if (myHive.s3.isFullHoney())
{
// g.fillRect( m , (n - hei), wid ,hei);
g.drawString("Is Full " , (m+wid+5) , (n-hei)+8);
}
else
{
// g.fillRect(m , n - hei + (35 * (int) (myHive.s3.getHoney()/ 30.0)), wid , n - hei + (35 * (int) (myHive.s3.getHoney()/ 30.0)) );
g.drawString("" + ((int) myHive.s3.getHoney()) + "Pounds of Honey", (m+wid+5) , (n-hei)+8);
}
break;
case 4:
if (myHive.s4.isFullHoney())
{
g.drawString("Is Full " , (m+wid+5) , (n-hei)+8);
// g.fillRect( m , (n - hei), wid ,hei);
}
else
{
// g.fillRect(m , n - hei + (35 * (int) (myHive.s4.getHoney()/ 30.0)), wid , n - hei + (35 * (int) (myHive.s4.getHoney()/ 30.0)) );
g.drawString("" + ((int) myHive.s4.getHoney()) + "Pounds of Honey", (m+wid+5) , (n-hei)+8);
}
break;
case 5:
if (myHive.s5.isFullHoney())
{
g.drawString("Is Full " , (m+wid+5) , (n-hei)+8);
// g.fillRect( m , (n - hei), wid ,hei);
}
else
{
// g.fillRect(m , n - hei + (35 * (int) (myHive.s5.getHoney()/ 30.0)), wid , n - hei + (35 * (int) (myHive.s5.getHoney()/ 30.0)) );
g.drawString("" + ((int) myHive.s5.getHoney()) + "Pounds of Honey", (m+wid+5) , (n-hei)+8);
}
break;
default:
break;
}//ends switch statement
}
else if ( build[i] == 3)
{
hei = 5;
g.fillRect( m , n , wid , hei );
n = (n+hei);
}
else if ( build[i] == 2)
{
hei = 55;
g.drawRect( m , n , wid , hei );
n = (n+hei);
switch (i)
{
case 1:
if (myHive.h1.isFullBrood())
g.drawString("Is Full " , (m+wid+5) , (n-hei)+8);
else
g.drawString(((int) myHive.h1.getBrood()) + " Brood" + " Queen? " + myHive.h1.isQueen() , (m+wid+5) , (n-hei)+8);
break;
case 2:
if (myHive.h2.isFullBrood())
g.drawString("Is Full " , (m+wid+5) , (n-hei)+8);
else
g.drawString(((int) myHive.h2.getBrood()) + " Brood" + " Queen? " + myHive.h2.isQueen() , (m+wid+5) , (n-hei)+8);
break;
default:
break;
}//ends switch statement
}
else if ( build[i] == 1 )
{
hei = 10;
g.fillRect(m-5, n , wid + 10 , hei);
n = (n+hei);
}
}// ends for loop
}//ends Paint()
public void actionPerformed( ActionEvent e )
{
int i;
if (runVal)
{
if ( e.getSource() == CheckBees)
{
myHive.h1.numberBees -= 500;
if (myHive.h1.numberBees < myHive.h1.MaxBees)
myHive.h1.fullBees = false;
int CheckedBees = 0;
CheckedBees += myHive.h1.getBees();
CheckedBees += myHive.h2.getBees();
CheckedBees += myHive.s1.getBees();
CheckedBees += myHive.s2.getBees();
CheckedBees += myHive.s3.getBees();
CheckedBees += myHive.s4.getBees();
CheckedBees += myHive.s5.getBees();
currentBeesInHive.setText("Bees In Hive " + CheckedBees);
showStatus("Bees In Hive " + CheckedBees);
} // ends SwitchSupers processor
if ( e.getSource() == Medicate)
{
myHive.h1.numberBees -= 500;
if (myHive.h1.numberBees < myHive.h1.MaxBees)
myHive.h1.fullBees = false;
if (diseaseValue == 4)
{
runVal = false;
ytdProducedHoney.setText(" A BEAR ");
currentHoneyInHive.setText(" HAS KNOCKED ");
currentBeesInHive.setText(" OVER ");
currentBroodInHive.setText(" YOUR BEE HIVE ");
currentDisease.setText("A Bear");
showStatus("Sorry, a bear knocked over your Bee Hive. Please play again. ");
}
else
{
currentDisease.setText( "" + diseaseLabel[diseaseValue] + " have been exterminated");
diseaseValue = 0;
showStatus("The Bees have been medicated");
}
} // ends Medicate processor
if ( e.getSource() == CheckAll)
{
repaint();
} // end process CheckAll event
else if (e.getSource() == CheckHoney)
{
double CheckedHoney = 0.0;
for( i = 0; i < 10 ; i++)
{
if ( build[i] == 4)
CheckedHoney += myHive.CHECK(i);
}
currentHoneyInHive.setText("Honey in Hive " + ( (int) CheckedHoney));
showStatus("Honey in Hive " + CheckedHoney);
} // end process CheckHoney event
else if (e.getSource() == RobAll)
{
myHive.h1.numberBees -= 500;
if (myHive.h1.numberBees < myHive.h1.MaxBees)
myHive.h1.fullBees = false;
RobbedHoney = 0.0;
for( i = 0; i < 10 ; i++)
{
if ( build[i] == 4)
RobbedHoney += myHive.ROB(i);
}
honeyProduced += RobbedHoney;
ytdProducedHoney.setText(((int) honeyProduced ) + " Pounds of Honey Produced");
} // end process RobAll event
else if (e.getSource() == Speed)
{
int hold = speedValue;
if ( hold == 3 )
hold = 0;
else if (hold == 1)
hold++;
else
hold++;
Speed.setLabel(speedLabel[hold]);
speedValue = hold;
} // end process Speed event
} // ends if (runVal)
}//ends actionPerformed
int randomDisease()
{
int retval = 0;
int randomValue = (int) ( 1 + Math.random() * 75);
if (randomValue == 1)
retval = 1;
else if (randomValue == 2)
retval = 2;
else if (randomValue == 3)
retval = 3;
else if (randomValue == 4)
retval = 4;
else
retval = 0;
return retval;
}
public void start()
{
runthread = new Thread( this, "RunThread" );
runthread.start();
}
public void stop()
{
runthread.stop();
}
public void run()
{
while( runVal )
{
int SV;
SV = speedValue;
int SVCoord = 0;
Thread RT;
switch(SV)
{
case 0:
while (speedValue == 0)
{
try {
Thread.sleep(1000);
}
catch ( InterruptedException e ) {
e.printStackTrace();
}
}
break;
case 1:
SVCoord = 5;
break;
case 2:
SVCoord = 3;
break;
case 3:
SVCoord = 1;
break;
default:
SVCoord = 3;
break;
}
if (diseaseValue == 0)
diseaseValue = randomDisease();
try {
Thread.sleep( 600 * SVCoord ); //sleeps ten seconds for each speed
}
catch ( InterruptedException e1 ) {
e1.printStackTrace();
}
//Calculates the current Bee Population and Brood and Honey for each box
double leftoverHoney = 0.0;
double leftoverBrood = 0.0;
int leftoverBees = 0;
if (diseaseValue == 0)
{
repaint();
if (build[1] == 2)
{
if(myHive.h1.isQueen())
leftoverBrood = myHive.h1.incrementBrood( (Math.random() * 5));
else
myHive.h1.incrementBrood(-5.0); //loses pound of bees in queen isn't there
if(!myHive.h1.isFullBees())
leftoverBees = myHive.h1.incrementBees( (int) ( (Math.random() * 40 * myHive.h1.getBrood()) - 100));
if (!myHive.h1.isFullHoney())
leftoverHoney = myHive.h1.incrementHoney((double) Math.random() * myHive.h1.getBees() / 500.0 );
}
if (build[2] ==2)
{
if(myHive.h2.isQueen())
leftoverBrood = myHive.h2.incrementBrood( (Math.random() * 5));
else
myHive.h2.incrementBrood(-5.0); //loses pound of bees in queen isn't there
if(!myHive.h2.isFullBees())
leftoverBees = myHive.h2.incrementBees( (int) ( (Math.random() * 40 * myHive.h2.getBrood()) - 100));
if (!myHive.h2.isFullHoney())
leftoverHoney = myHive.h2.incrementHoney((double) Math.random() * myHive.h2.getBees() / 5000.0 );
}
double totalBrood = 0.0;
totalBrood = myHive.h1.getBrood() + myHive.h2.getBrood();
if (build[4] ==4)
{
if(!myHive.s1.isFullBees())
leftoverBees = myHive.s1.incrementBees( (int)(totalBrood * Math.random() * 50) + leftoverBees);
if (!myHive.s1.isFullHoney())
leftoverHoney=myHive.s1.incrementHoney((double)(Math.random()*myHive.s1.getBees()/5000.0)+leftoverHoney);
}
if (build[5] ==4)
{
if(!myHive.s2.isFullBees())
leftoverBees = myHive.s2.incrementBees( (int)(totalBrood * Math.random() * 40) + leftoverBees );
if (!myHive.s2.isFullHoney())
leftoverHoney=myHive.s2.incrementHoney((double)(Math.random()*myHive.s2.getBees()/5000.0)+leftoverHoney);
}
if (build[6] ==4)
{
if(!myHive.s3.isFullBees())
leftoverBees = myHive.s3.incrementBees( (int)(totalBrood * Math.random() * 30) + leftoverBees );
if (!myHive.s3.isFullHoney())
leftoverHoney=myHive.s3.incrementHoney((double)(Math.random()*myHive.s3.getBees()/5000.0)+leftoverHoney);
}
if (build[7] ==4)
{
if(!myHive.s4.isFullBees())
leftoverBees = myHive.s4.incrementBees( (int)(totalBrood * Math.random() * 20) + leftoverBees );
if (!myHive.s4.isFullHoney())
leftoverHoney=myHive.s4.incrementHoney((double)(Math.random()*myHive.s4.getBees()/5000.0)+leftoverHoney);
}
if (build[8] ==4)
{
if(!myHive.s5.isFullBees())
leftoverBees = myHive.s5.incrementBees( leftoverBees );
if (!myHive.s5.isFullHoney())
leftoverHoney=myHive.s5.incrementHoney((double)(Math.random()*myHive.s5.getBees()/5000.0)+leftoverHoney);
}
}
}
} //end of run
//*********************HIVE CLASS***********HIVE CLASS********************
class Hive
{
hivebody h1;
hivebody h2;
hivesuper s1;
hivesuper s2;
hivesuper s3;
hivesuper s4;
hivesuper s5;
int x, y;
//*********HIVE CONSTRUCTOR************//
public Hive(int x, int y) //x denotes hive bodies, y denotes supers
{
this.x = x;
this.y = y;
for (int dz = 0 ; dz < 10; dz++)
{
build[dz] = 0;
}
System.out.println("In Hive Constructor");
if (x < 1)
x=1;
if (x > 2)
x=2;
if (y < 1)
y=1;
if (y > 5)
y=5;
build[0] = 1; //puts in Baseboard
build[1] = 2; //hivebody
build[2] = 2; // hive body II
build[3] = 3; // excluder
build[4] = 4; // super
for(int i = 5; i < y + 4; i++)
build[i] = 4;
build[9] = 5; // hive cover
int pos = 1;
h1 = new hivebody(1);
h1.QueenHere = true;
h1.incrementBrood(5.0);
h1.incrementBees(4000);
h2 = new hivebody(2);
s1 = new hivesuper(4);
s2 = new hivesuper(5);
s3 = new hivesuper(6);
s4 = new hivesuper(7);
s5 = new hivesuper(8);
}//ends Hive constructor
double ROB(int pos)
{
double robbedHoney = 0.0;
int x;
if (build[pos] == 4)
{
x = (pos-3);
switch (x)
{
case(1):
robbedHoney = myHive.s1.incrementHoney(-1.0);
break;
case(2):
robbedHoney = myHive.s2.incrementHoney(-1.0);
break;
case(3):
robbedHoney = myHive.s3.incrementHoney(-1.0);
break;
case(4):
robbedHoney = myHive.s4.incrementHoney(-1.0);
break;
case(5):
robbedHoney = myHive.s5.incrementHoney(-1.0);
break;
default:
break;
}//ends switch statement
}//ends correct ROB if statement
else
{
showStatus("Can't rob that");
}//ends else statment
return robbedHoney;
}//ends ROB
//************CHECK**************
public double CHECK(int pos)
{
double CheckedHoney = 0.0;
int x;
if (build[pos] == 4)
{
x = (pos-3);
switch (x)
{
case(1):
CheckedHoney = myHive.s1.getHoney();
break;
case(2):
CheckedHoney = myHive.s2.getHoney();
break;
case(3):
CheckedHoney = myHive.s3.getHoney();
break;
case(4):
CheckedHoney = myHive.s4.getHoney();
break;
case(5):
CheckedHoney = myHive.s5.getHoney();
break;
default:
break;
}//ends switch statement
}//ends correct CHECK if statement
else
{
System.out.println("Can't Check that");
}//ends else statment
return CheckedHoney;
}//ends CHECK
//**************CLASS HIVEBODY******************
class hivebody
{
double MaxHoney = 10.0;
double MaxBrood = 40.0;
int MaxBees = 15000;
boolean QueenHere = false;
boolean fullHoney = false;
boolean fullBrood = false;
boolean fullBees = false;
double PoundsHoney = 0.0;
double PoundsBrood = 0.0;
int numberBees = 0;
boolean isQueen() { return QueenHere; }
boolean isFullHoney() { return fullHoney; }
boolean isFullBrood() { return fullBrood; }
boolean isFullBees() { return fullBees; }
double getHoney() { return PoundsHoney; }
double getBrood() { return PoundsBrood; }
int getBees() { return numberBees; }
int position; //keeps track of location in the build array
public hivebody(int pos)
{
position = pos;
}// ends constructor for hivebody
double incrementHoney( double h )
{
double leftover = 0.0;
if ( (h >= 0.0) && (!isFullHoney()) )
{
if ( (PoundsHoney + h) > MaxHoney )
{
fullHoney = true;
leftover = (PoundsHoney + h) - MaxHoney;
}
else
{
PoundsHoney += h;
}
}
else if (isFullHoney())
leftover = h;
else if (h < 0.0)
{
leftover = PoundsHoney;
PoundsHoney = 0.0;
}
return leftover;
}// ends incrementHoney
double incrementBrood( double b )
{
double leftover = 0.0;
if ( (PoundsBrood + b) > MaxBrood )
{
fullBrood = true;
leftover = (PoundsBrood + b) - MaxBrood;
if (h1.QueenHere)
{
myHive.h2.QueenHere = true;
myHive.h1.QueenHere = false;
}
else
{
myHive.h2.QueenHere = false;
myHive.h1.QueenHere = true;
}
}
else if (b < 0.0)
{
PoundsBrood += b;
fullBrood = false;
if (PoundsBrood < 0.0)
PoundsBrood = 0.0;
}
else if (b >= 0.0)
{
PoundsBrood += b;
}
return leftover;
}// ends incrementBrood
public int incrementBees( int b )
{
int leftover = 0;
if ( (numberBees + b) > MaxBees )
{
fullBees = true;
leftover = (numberBees + b) - MaxBees;
}
else
{
numberBees += b;
if (numberBees < 0.0)
{
leftover = numberBees;
numberBees = 0;
}
}
return leftover;
}// ends incrementBees
}// ends class hivebody
//***************CLASS HIVESUPER*************
class hivesuper
{
double MaxHoney = 30.0;
int MaxBees = 8000;
boolean fullBees = false;
boolean fullHoney = false;
double PoundsHoney = 0.0;
int numberBees = 0;
public boolean isFullHoney() { return fullHoney; }
public boolean isFullBees() { return fullBees; }
public double getHoney() { return PoundsHoney; }
public int getBees() { return numberBees; }
int position; //keeps track of location in the build array
public hivesuper(int pos)
{
position = pos;
}// ends constructor for hivesuper
public double incrementHoney( double h )
{
double leftover = 0.0;
if ( (h >= 0.0) && (!isFullHoney()) )
{
if ( (PoundsHoney + h) > MaxHoney )
{
fullHoney = true;
leftover = (PoundsHoney + h) - MaxHoney;
}
else
{
PoundsHoney += h;
}
}
else if (h < 0.0)
{
leftover = PoundsHoney;
PoundsHoney = 0.0;
fullHoney = false;
}
else if (isFullHoney())
leftover = h;
return leftover;
}// ends incrementHoney
public int incrementBees( int b )
{
int leftover = 0;
if ( (numberBees + b) > MaxBees )
{
fullBees = true;
leftover = (numberBees + b) - MaxBees;
}
else
{
numberBees += b;
if (numberBees < 0.0)
{
leftover = numberBees;
numberBees = 0;
}
}
return leftover;
}// ends incrementBees
}// ends class hivesuper
//***************FUNCTION ROB**************
}// ends class Hive
}//ends BeeHive class