/* PROGRAM - CANNIBAL AND MISSIONARIES ----------------------------------- Written by: Anthony Rossi - abrossi@cs.millersv.edu For: CS406 - Internet Programming - Dr. Roger Webster Date: Fall 1996 This program is a java applet that is the infamous Cannibal and Missionary strategy game. The goal is to move all 3 cannibals and all 3 missionaries to the other side of the river using a 2 man boat. The only rules are: 1. The cannibals may never outnumber the missionaries, or they will eat them. 2. One person must remain with the boat to row it back. 3. Only two people will fit on the boat. You select, using the checkboxes, the people you want to send, and hit the send button. If you make a mistake, you can reset the board at any time by hitting the restart button. If you need instructions, hit the Instructions button. This code has the following classes: 1. public class scale extends Applet 2. class AboutDialog extends Frame 3. class Youwon extends Frame 4. class BadMove extends Frame 5. class Eaten extends Frame Class #1 is the main java applet class responsible for running the game. Classes #2-5 are all frames to display various messages such as: Instructions, Winning messgae, bad moves, and if missionaries have been eaten. The following files are required: RIVERD.gif DEVIL1.gif DEVIL2.gif DEVIL3.gif M1.gif M2.gif M3.gif CHURCH2.gif BOATB.gif josh2.au instructions.au eat.au siren.au newgame.au win.au cannibal.html scale.java - This File */ //------------START APPLET------------------------------------------------------------- import java.awt.*; import java.applet.*; public class scale extends Applet { public void init() { setLayout(new BorderLayout()); MediaTracker tracker = new MediaTracker(this); tony = getImage(getDocumentBase(),"RIVERD.gif"); // Image files cannibal1 = getImage(getDocumentBase(), "DEVIL1.gif"); missionary1 = getImage(getDocumentBase(), "M1.gif"); cannibal2 = getImage(getDocumentBase(), "DEVIL2.gif"); missionary2 = getImage(getDocumentBase(), "M2.gif"); cannibal3 = getImage(getDocumentBase(), "DEVIL3.gif"); missionary3 = getImage(getDocumentBase(), "M3.gif"); church = getImage(getDocumentBase(), "CHURCH2.gif"); aboat = getImage(getDocumentBase(), "BOATB.gif"); sound = getAudioClip(getDocumentBase(), "josh2.au"); inst = getAudioClip(getDocumentBase(), "instructions.au"); eatsound = getAudioClip(getDocumentBase(), "eat.au"); badsound = getAudioClip(getDocumentBase(), "siren.au"); newgame = getAudioClip(getDocumentBase(), "newgame.au"); winsound = getAudioClip(getDocumentBase(), "win.au"); tracker.addImage(tony, 0); // Adds images to mediatracker tracker.addImage(cannibal1, 0); tracker.addImage(cannibal2, 0); tracker.addImage(cannibal3, 0); tracker.addImage(missionary1, 0); tracker.addImage(missionary2, 0); tracker.addImage(missionary3, 0); tracker.addImage(church, 0); tracker.addImage(aboat, 0); try {tracker.waitForID(0);} // Waits for images to load, then display catch (InterruptedException e) {} sound.play(); Panel p= new Panel(); p.add(can = new Checkbox("C1")); // Adds checkboxes p.add(can2 = new Checkbox("C2")); p.add(can3 = new Checkbox("C3")); p.add(mis = new Checkbox("M1")); p.add(mis2 = new Checkbox("M2")); p.add(mis3 = new Checkbox("M3")); p.add(new Button("Send")); // Adds buttons p.add(new Button("Instructions!")); p.add(new Button("Restart")); add("South", p); } //---------------------------------------------------------------- public boolean action(Event evt, Object arg) { if (arg.equals("Instructions!")) //Displays help information { inst.play(); AboutDialog win = new AboutDialog(); win.pack(); win.show(); win.resize(375,325); } if (progstop==0) { if (arg.equals("Send")) //Main routine for send button { int cc1=0; int cc2=0; int cc3=0; int cm1=0; int cm2=0; int cm3=0; if (can.getState() == true) cc1=1; //Gets states of checkboxes if (can2.getState() == true) cc2=1; if (can3.getState() == true) cc3=1; if (mis.getState() == true) cm1=1; if (mis2.getState() == true) cm2=1; if (mis3.getState() == true) cm3=1; if (VerifySend(cc1,cc2,cc3,cm1,cm2,cm3)) //Verifies send { Move(cc1,cc2,cc3,cm1,cm2,cm3); //Moves images can.setState(false); //Resets checkboxes to off can2.setState(false); can3.setState(false); mis.setState(false); mis2.setState(false); mis3.setState(false); CheckWin(); //Checks to see if win if ((Eat())&&(!stop)) { //If c>m , eat m Eaten ff = new Eaten(); ff.show(); ff.resize(375,200); eatsound.play(); progstop=1; } if (boat==0) boat=1; else boat=0; } else //Bad move, display message { badsound.play(); BadMove hh = new BadMove(); hh.pack(); hh.show(); hh.resize(375,200); } } } if (arg.equals("Restart")) // Resets board to play new game { newgame.play(); can.setState(false); //Resets checkboxes can2.setState(false); can3.setState(false); mis.setState(false); mis2.setState(false); mis3.setState(false); ax=52; //Resets Image positions ay=199; bx=97; by=213; cx=147; cy=181; dx=10; dy=170; ex=54; ey=146; fx=103; fy=154; progstop=0; c1=0; c2=0; c3=0; m1=0; m2=0; m3=0; boat=0; boatx=165; stop=false; repaint(); } return true; } //---------------------------------------------------------------- public void Move(int cc1,int cc2,int cc3,int cm1,int cm2,int cm3) { //Calculates new Image positions and moves boat int pause=500; if (boat==0) boatx=345; if (boat==1) boatx=165; if (cc1 == 1) { if (c1==0) c1=1; else c1=0; if (c1==0) { ax=42; ay=199; } else { ax=460; ay=220; } } if (cc2 == 1) { if (c2==0) c2=1; else c2=0; if (c2==0) { bx=87; by=213; } else { bx=507; by=213; } } if (cc3 == 1) { if (c3==0) c3=1; else c3=0; if (c3==0) { cx=137; cy=181; } else { cx=555; cy=200; } } if (cm1 == 1) { if (m1==0) m1=1; else m1=0; if (m1==0) { dx=10; dy=170; } else { dx=394; dy=160; } } if (cm2 == 1) { if (m2==0) m2=1; else m2=0; if (m2==0) { ex=54; ey=146; } else { ex=420; ey=200; } } if (cm3 == 1) { if (m3==0) m3=1; else m3=0; if (m3==0) { fx=103; fy=154; } else { fx=463; fy=163; } } repaint(); } //---------------------------------------------------------------- public boolean Eat() //Checks to see if c>m is true --> c eats m { int ml=0; int mr=0; int cl=0; int cr=0; if (c1==0) cl++; else cr++; if (c2==0) cl++; else cr++; if (c3==0) cl++; else cr++; if (m1==0) ml++; else mr++; if (m2==0) ml++; else mr++; if (m3==0) ml++; else mr++; if ((cl > ml)||(cr > mr)) { if (ml==0) return false; else if (mr==0) return false; else return true; } else return false; } //---------------------------------------------------------------- public boolean VerifySend(int cc1,int cc2,int cc3,int cm1,int cm2,int cm3) { //Verifies that boat is in right place, //no more than 2 people go on boat, and //right people are selected. int ok = 1; int oka= 0; if ((cc1 == 0) && (cc2 == 0) && (cc3 == 0) && (cm1 == 0) && (cm2 == 0) && (cm3 == 0)) { return false; } else { if (cc1 == 1) { if (boat != c1) ok=0; oka++; } if (cc2 == 1) { if (boat != c2) ok=0; oka++; } if (cc3 == 1) { if (boat != c3) ok=0; oka++; } if (cm1 == 1) { if (boat != m1) ok=0; oka++; } if (cm2 == 1) { if (boat != m2) ok=0; oka++; } if (cm3 == 1) { if (boat != m3) ok=0; oka++; } } if ((ok == 0)||(oka>2)) return false; else return true; } //---------------------------------------------------------------- public void CheckWin() //Checks to see if everyone is on east bank for win { if ((c1==1)&&(c2==1)&&(c3==1)&&(m1==1)&&(m2==1)&&(m3==1)) { YouWon f = new YouWon(); f.pack(); f.show(); f.resize(375,200); winsound.play(); progstop=1; stop=true; } } //---------------------------------------------------------------- public int ax=52; // Image positions public int ay=199; public int bx=97; public int by=213; public int cx=147; public int cy=181; public int dx=10; public int dy=170; public int ex=54; public int ey=146; public int fx=103; public int fy=154; public int c1=0; // Misc variables public int c2=0; public int c3=0; public int m1=0; public int m2=0; public int m3=0; public int boat=0; public int boatx=165; public int progstop=0; public boolean stop=false; private Image aboat; // Images private Image tony; private Image cannibal1; private Image cannibal2; private Image cannibal3; private Image missionary1; private Image missionary2; private Image missionary3; private Image church; private Checkbox can; // Checkboxes private Checkbox mis; private Checkbox can2; private Checkbox mis2; private Checkbox can3; private Checkbox mis3; private AudioClip sound; // AudioClips private AudioClip newgame; private AudioClip inst; private AudioClip winsound; private AudioClip eatsound; private AudioClip badsound; Thread me=null; //---------------------------------------------------------------- public void paint(Graphics g) //Paints images to screen { g.drawImage(tony,0,0,this); g.drawImage(church,500,120,this); g.drawImage(cannibal1,ax,ay,this); g.drawImage(cannibal2,bx,by,this); g.drawImage(cannibal3,cx,cy,this); g.drawImage(missionary1,dx,dy,this); g.drawImage(missionary2,ex,ey,this); g.drawImage(missionary3,fx,fy,this); g.drawImage(aboat,boatx,200,this); } //---------------------------------------------------------------- public void stop() //Ends Applet Thread { if (me != null) { me.stop(); me=null; } System.exit(0); } //---------------------------------------------------------------- public void start() //Starts Applet Thread { if (me==null) { me=new Thread(); me.start(); } } //--------------------------------------------------------------- public boolean handleEvent (Event evt) { if (evt.id == Event.WINDOW_DESTROY && evt.target == this) System.exit(0); return super.handleEvent(evt); } //--------------------------------------------------------------- //public void update(Graphics g) //{ // g.drawImage(aboat,boatx,200,this); //} //-------------------------------------------------------------- } // End of applet class //---------------------------------------------------------------- //---------------------------------------------------------------- class AboutDialog extends Frame // Class to display playing instructions { public AboutDialog() { setTitle("INSTRUCTIONS!"); setLayout(new BorderLayout()); Panel p3=new Panel(); p3.setLayout(new FlowLayout(FlowLayout.CENTER)); p3.add(new Label("INSTRUCTIONS TO PLAY!")); p3.setBackground(Color.green); add("North",p3); Panel p1 = new Panel(); p1.setLayout(new FlowLayout(FlowLayout.LEFT)); p1.add(new Label("In this game, the goal is to move the cannibals and missionaries")); p1.add(new Label("to the east bank of the river without anyone being eaten. The")); p1.add(new Label("only rules are: 1) If the cannibals ever outnumber the missionaries")); p1.add(new Label("on either side of the river, they eat them. 2) No more than 2 people")); p1.add(new Label("are allowed on the boat, but one must remain to row the boat back to")); p1.add(new Label("the other bank. To select the people, you click on the checkboxes,")); p1.add(new Label("then hit the Send button to send them to the other side.")); p1.add(new Label("GOOD LUCK!")); p1.setBackground(Color.green); add("Center",p1); Panel p2 = new Panel(); p2.add(new Button("OK")); p2.setBackground(Color.green); add("South",p2); } // End Aboutdialog //---------------------------------------------------------------- public boolean handleEvent(Event e) { if (e.id == Event.WINDOW_DESTROY) dispose(); return super.handleEvent(e); } //---------------------------------------------------------------- public boolean action(Event evt, Object arg) { if (arg.equals("OK")) { dispose(); return true; } return false; } //End action //--------------------------------------------------------------- public void stop() { hide(); dispose(); System.exit(0); } } // End class AboutDialog //---------------------------------------------------------------- //---------------------------------------------------------------- class YouWon extends Frame // Class to display winning message { public YouWon() { setTitle("YOU WON!!!!!"); setLayout(new BorderLayout()); Panel p3=new Panel(); p3.setLayout(new FlowLayout(FlowLayout.CENTER)); p3.add(new Label("YOU WON THE GAME!!!")); p3.setBackground(Color.pink); add("North",p3); Panel p1 = new Panel(); p1.setLayout(new FlowLayout(FlowLayout.LEFT)); p1.add(new Label("Congratulations, you safely transported the missionaries and cannibals")); p1.add(new Label("to the other side of the river without anyone being eaten!")); p1.add(new Label("To play again, click 'OK' then 'Restart'.")); p1.setBackground(Color.pink); add("Center",p1); Panel p2 = new Panel(); p2.add(new Button("OK")); p2.setBackground(Color.pink); add("South",p2); } // End YouWon //---------------------------------------------------------------- public boolean handleEvent(Event e) { if (e.id == Event.WINDOW_DESTROY) dispose(); return super.handleEvent(e); } //---------------------------------------------------------------- public boolean action(Event evt, Object arg) { if (arg.equals("OK")) { dispose(); return true; } return false; } //End action } // End class YouWin //---------------------------------------------------------------- //---------------------------------------------------------------- class BadMove extends Frame // Class to display bad move message { public BadMove() { setTitle("BAD MOVE!!!!!"); setLayout(new BorderLayout()); Panel p3=new Panel(); p3.setLayout(new FlowLayout(FlowLayout.CENTER)); p3.add(new Label("YOU MADE AN INCORRECT MOVE!!!")); p3.setBackground(Color.yellow); add("North",p3); Panel p1 = new Panel(); p1.setLayout(new FlowLayout(FlowLayout.LEFT)); p1.add(new Label("Your move was either against the rules, you selected the wrong")); p1.add(new Label("people, or selected too many people to move. Please reselect")); p1.add(new Label("your choices and hit 'Send' to try again.")); p1.setBackground(Color.yellow); add("Center",p1); Panel p2 = new Panel(); p2.add(new Button("OK")); p2.setBackground(Color.yellow); add("South",p2); } // End BadMove //---------------------------------------------------------------- public boolean handleEvent(Event e) { if (e.id == Event.WINDOW_DESTROY) dispose(); return super.handleEvent(e); } //---------------------------------------------------------------- public boolean action(Event evt, Object arg) { if (arg.equals("OK")) { dispose(); return true; } return false; } //End action } // End class BadMove //---------------------------------------------------------------- //---------------------------------------------------------------- class Eaten extends Frame // Class to display missionaries being eaten { public Eaten() { setTitle("THEY'VE BEEN EATEN!!!"); setLayout(new BorderLayout()); Panel p3=new Panel(); p3.setLayout(new FlowLayout(FlowLayout.CENTER)); p3.add(new Label("ALL OF THE MISSIONARIES HAVE BEEN EATEN!!!")); p3.setBackground(Color.red); add("North",p3); Panel p1 = new Panel(); p1.setLayout(new FlowLayout(FlowLayout.LEFT)); p1.add(new Label("You have to be more careful next time!")); p1.add(new Label("Plan your moves ahead of time, this may help!")); p1.add(new Label("Hit 'OK' then the 'RESTART' button to play again.")); p1.setBackground(Color.red); add("Center",p1); Panel p2 = new Panel(); p2.add(new Button("OK")); p2.setBackground(Color.red); add("South",p2); } // End BadMove //---------------------------------------------------------------- public boolean handleEvent(Event e) { if (e.id == Event.WINDOW_DESTROY) dispose(); return super.handleEvent(e); } //---------------------------------------------------------------- public boolean action(Event evt, Object arg) { if (arg.equals("OK")) { dispose(); return true; } return false; } //End action } // End class BadMove //---------------------------------------------------------------- //--------------------END OF APPLET-------------------------------