/*
Applet zum "Ziegenproblem" - J. Giesen 4.1.1998"
*/

import java.awt.*;
import java.applet.*;
import java.util.Date;
import java.util.Random;
import java.lang.Math;

public class CarApplet20 extends Applet {

	int x0;
	int y0;
	int h=150;
	int b=100;
	int x1=30;
	int x2=180;
	int x3=320;
	int oben, unten;
	int wahl;
	int zaehler;
	int OK;	
	int AnzahlSpiele;
	int gewonnen;	
	int umwahl;
	int ohneUmwahlGewonnen, mitUmwahlGewonnen;
	int p, q;
	int ersteWahl;
	
	
	boolean hit;
	//Rectangle rect;
	//MediaTracker tracker;
	//Graphics g;
	//Image picture;
			
	StringBuffer buffer;
	Date dat;
	Random r;
	int zufall;
	int moderator;
	Image auto, ziege;
	String bildname;
	int x[] = new int[4];
	int z[] = new int[4];	
	
	public void init() {		
		oben=80;
		unten=oben+h;
		
		x[0]=0;		
		x[1]=30;
		x[2]=180;
		x[3]=320;
		
		wahl=0;
		Font f = new Font("Chicago", Font.PLAIN, 12);
    	setFont(f);
    	buffer = new StringBuffer();
    	
		moderator = 0;
		zaehler=0;
		AnzahlSpiele=1;
		gewonnen=0;
		mitUmwahlGewonnen=0;
		ohneUmwahlGewonnen=0;
			
		bildname = getParameter("auto");
		auto = getImage(getDocumentBase(),bildname);					
		
		bildname = getParameter("ziege");
		ziege = getImage(getDocumentBase(),bildname);		
																
		add(new Button("new game"));
		add(new Button("clear"));
		
		dat = new Date();
		r = new Random(dat.getTime());
		zufall =  r.nextInt( ) % 3; 
		zufall = Math.abs(zufall);
		zufall = zufall +1;
		wahl=0;
		moderator=0;
		OK=1;
		z[0]=1;
		z[1]=0;
		z[2]=0;
		z[3]=0;
		z[zufall]=1;
		ersteWahl=0;
						
		resize(size().width,size().height);
		//repaint();
	}
	
 	void AB(int a, int b) {
 		int x;
 		dat = new Date();
		r = new Random(dat.getTime());
		x =  r.nextInt( ) % 2; 
		x = Math.abs(x);
		if (x==0) {p=a;q=b;}
		if (x==1) {p=b;q=a;}
 		
 	} 
//----------------------------------------------	
	public void paint( Graphics g ) {
	
		if (OK==0) {
		
			g.clearRect(0, 0, size().width, size().height);
			
			g.drawRect(1, 1, size().width-2, size().height-2);														
			buffer = new StringBuffer(AnzahlSpiele + " games: won "+gewonnen);	
			g.drawString(buffer.toString(),50, unten+50);
			buffer = new StringBuffer(ohneUmwahlGewonnen + " won without changing");	
			g.drawString(buffer.toString(),50, unten+65);
			buffer = new StringBuffer(mitUmwahlGewonnen + " won with changing");	
			g.drawString(buffer.toString(),50, unten+80);
			g.setColor(Color.red);
			buffer = new StringBuffer("click the button 'new game'");
			g.drawString(buffer.toString(), 170, unten+100);						
		}
		
		if (OK==1) {
										
			g.clearRect(0, 0, size().width, size().height);
		
			//3 Rahmen zeichnen
			g.setColor(Color.black);
			g.drawRect(1, 1, size().width-2, size().height-2);
			g.drawRect(x1, oben, b, h);
			g.drawRect(x2, oben, b, h);
			g.drawRect(x3,oben, b, h);
			g.setColor(Color.red);
			g.drawString("1",80,60);
			g.drawString("2",230,60);
			g.drawString("3",370,60);
			
			//3 schwarze Fuellungen zeichnen
			if (zaehler==0) {
				g.setColor(Color.black);		
				g.fillRect(x1+2, oben+2, b-3, h-3);
				g.fillRect(x2+2, oben+2, b-3, h-3);
				g.fillRect(x3+2, oben+2, b-3, h-3);			
				g.setColor(Color.red);				
				g.drawString("Select a door !",170,unten+30);
			}
			
			//Wahl und Modeator zeichnen		
													
			if (zaehler==1) {
			
				g.setColor(Color.green);			
				g.fillRect(x[wahl]+2, oben+2, b-3, h-3);	
			
				g.setColor(Color.green);
				g.drawString("confirm ",160,unten+30);
				g.setColor(Color.black);
				g.drawString("or change",220,unten+30);				
				
				buffer = new StringBuffer(AnzahlSpiele + ". game");	
				g.drawString(buffer.toString(),50, unten+50);													
			
				g.fillRect(x[umwahl]+2, oben+2, b-3, h-3);			
				g.clearRect(x[moderator]+2, oben+2, b-3, h-3);
				g.setColor(Color.red);
				g.drawRect(x[moderator], oben, b, h);
				ziege = getImage(getDocumentBase(),"goatImage.gif");
				g.setColor(Color.black);							
				g.drawImage(ziege,x[moderator]+20,oben+70,this);														
			}	
				
			//2. Wahl
			if (zaehler==2) {
								
				g.setColor(Color.red);								
								
				if (wahl==zufall) g.drawString("Won !",x[wahl]+30,unten+25);
				else g.drawString("Lost !",x[wahl]+30,unten+25);					

				g.drawRect(x[moderator], oben, b, h);	
				
				g.clearRect(x[zufall]+2, oben+2, b-3, h-3);
				g.setColor(Color.black);
				
				auto = getImage(getDocumentBase(),"carImage.gif");
				g.drawImage(auto,x[zufall]+10,oben+70,this);
					
				ziege = getImage(getDocumentBase(),"goatImage.gif");
				for (int i=1; i<4; i++)
					if (i!=zufall) g.drawImage(ziege,x[i]+20,oben+70,this);
																					
				g.setColor(Color.green);
				g.drawRect(x[ersteWahl], oben, b, h);
				g.drawRect(x[wahl], oben, b, h);
				g.drawRect(x[wahl]+1, oben+1, b-2, h-2);
				g.drawRect(x[wahl]+2, oben+2, b-4, h-4);					
													
				g.setColor(Color.black);								
				buffer = new StringBuffer(AnzahlSpiele + " Games: won "+gewonnen);	
				g.drawString(buffer.toString(),50, unten+50);
				buffer = new StringBuffer(ohneUmwahlGewonnen + " won without changing");	
				g.drawString(buffer.toString(),50, unten+65);
				buffer = new StringBuffer(mitUmwahlGewonnen + " won with changing");	
				g.drawString(buffer.toString(),50, unten+80);
				g.setColor(Color.red);
				buffer = new StringBuffer("Click the button 'new game'");
				g.drawString(buffer.toString(), 170, unten+100);
				System.out.println(AnzahlSpiele+" games  1="+z[1]+"  2="+z[2]+"  3="+z[3]);
			}
							
		}					
	}
//-------------------------------------------------
	public boolean mouseDown(Event event, int x, int y) {
											
		// Verarbeitung des 1. Klicks: Registrierung der 1. Wahl
		// und Festlegung der vom Moderator zu oeffnenden Tür
		if (zaehler==0) {	
			if ((x>x1) && (x<x1+b) && (y<unten) && (y>oben)) wahl=1;
			if ((x>x2) && (x<x2+b) && (y<unten) && (y>oben)) wahl=2;
			if ((x>x3) && (x<x3+b) && (y<unten) && (y>oben)) wahl=3;
				
			if (wahl==1) {			
				if (zufall==1) {AB(2,3);moderator=p;umwahl=q;ohneUmwahlGewonnen=ohneUmwahlGewonnen+1;}
				if (zufall==2) {moderator=3;umwahl=2;mitUmwahlGewonnen=mitUmwahlGewonnen+1;}
				if (zufall==3) {moderator=2;umwahl=3;mitUmwahlGewonnen=mitUmwahlGewonnen+1;}
				zaehler=zaehler+1;
				ersteWahl=1;
				repaint();			
				return true;			
			}					
			if (wahl==2) {			
				if (zufall==1) {moderator=3;umwahl=1;mitUmwahlGewonnen=mitUmwahlGewonnen+1;}
				if (zufall==2) {AB(1,3);moderator=p;umwahl=q;ohneUmwahlGewonnen=ohneUmwahlGewonnen+1;}
				if (zufall==3) {moderator=1;umwahl=3;mitUmwahlGewonnen=mitUmwahlGewonnen+1;}
				zaehler=zaehler+1;
				ersteWahl=2;
				repaint();			
				return true;			
			}				
			if (wahl==3) {			
				if (zufall==1) {moderator=2;umwahl=1;mitUmwahlGewonnen=mitUmwahlGewonnen+1;}
				if (zufall==2) {moderator=1;umwahl=2;mitUmwahlGewonnen=mitUmwahlGewonnen+1;}
				if (zufall==3) {AB(1,2);moderator=p;umwahl=q;ohneUmwahlGewonnen=ohneUmwahlGewonnen+1;}
				zaehler=zaehler+1;
				ersteWahl=3;
				repaint();			
				return true;			
			}										
		}
		
		
		if (zaehler==1) {
			hit=false;			
			if ((x>x1) && (x<x1+b) && (y<unten) && (y>oben)) {hit=true;wahl=1; if (wahl!=moderator) {zaehler=zaehler+1; repaint();}}
			if ((x>x2) && (x<x2+b) && (y<unten) && (y>oben)) {hit=true;wahl=2; if (wahl!=moderator) {zaehler=zaehler+1; repaint();}}
			if ((x>x3) && (x<x3+b) && (y<unten) && (y>oben)) {hit=true;wahl=3; if (wahl!=moderator) {zaehler=zaehler+1; repaint();}}
			if (hit==true) {
				if (wahl==zufall) gewonnen=gewonnen+1;
				return true;
				}							
		}
				
		if (zaehler==2) {	
			if ((x>x1) && (x<x1+b) && (y<unten) && (y>oben)) {zaehler=zaehler+1;OK=0;;repaint();}
			if ((x>x2) && (x<x2+b) && (y<unten) && (y>oben)) {zaehler=zaehler+1;OK=0;;repaint();}
			if ((x>x3) && (x<x3+b) && (y<unten) && (y>oben)) {zaehler=zaehler+1;OK=0;;repaint();}						
			return true;		
		}
		
		
		return true;					
	}
//------------------------------------------------
	public boolean action(Event event, Object eventObject) {
		
		if (eventObject.equals("new game")) {
			
			//System.out.println("zaehler= "+zaehler);
			if (zaehler>1) {
					
				dat = new Date();
				r = new Random(dat.getTime());
				zufall =  r.nextInt( ) % 3; 
				zufall = Math.abs(zufall);
				zufall = zufall +1;
				wahl=0;
				moderator=0;			
				OK=1;			
				zaehler=0;				
				umwahl=0;
				ersteWahl=0;				
				AnzahlSpiele=AnzahlSpiele+1;
				z[0]=z[0]+1;
				z[zufall]=z[zufall]+1;								
				repaint();			
			}						
		}
				
		if (eventObject.equals("clear")) {
		
			Frame parentFrame = new Frame("Dialog");
						 		
			Dialog okDialog = new OKDialog(parentFrame,"Reset all ?");
			parentFrame.resize(okDialog.size().width,okDialog.size().height);
			okDialog.show();
									
			dat = new Date();
			r = new Random(dat.getTime());
			zufall =  r.nextInt( ) % 3; 
			zufall = Math.abs(zufall);
			zufall = zufall +1;
			wahl=0;
			moderator=0;			
			OK=1;
			AnzahlSpiele=1;
			gewonnen=0;
			mitUmwahlGewonnen=0;
			ohneUmwahlGewonnen=0;
			zaehler=0;
			ersteWahl=0;
			z[0]=1;
			z[1]=0;
			z[2]=0;
			z[3]=0;
			z[zufall]=1;				
			repaint();
			
			return true;//+++ 26.3.98}
		}
		repaint();
		return true;		
	}
	
}

//----------------
// Example 21.6 ­ OK Dialog class 
// 
// OKDialog ­ Custom dialog that presents a message and waits for 
// you to click on the OK button. 
// 
// Example use: 
// Dialog ok = new OKDialog(parentFrame, "Click OK to continue"); 
// ok.show(); // Other input will be blocked until OK is 
// As a shortcut, you can use the static createOKDialog that will 
// create its own frame and activate itself: 
// OKDialog.createOKDialog("Click OK to continue"); 
// 

public class OKDialog extends Dialog 
{ 
protected Button okButton; 
protected static Frame createdFrame; 

public OKDialog(Frame parent, String message) 
{ 
super(parent, true); // Must call the parent's constructor 

// This Dialog box uses the GridBagLayout to provide a pretty good layout. 

GridBagLayout gridbag = new GridBagLayout(); 
GridBagConstraints constraints = new GridBagConstraints();

// Create the OK button and the message to display 
okButton = new Button("OK"); 
Label messageLabel = new Label(message); 

setLayout(gridbag); 

// The message should not fill, it should be centered within this area, with 
// some extra padding. The gridwidth of REMAINDER means this is the only 
// thing on its row, and the gridheight of RELATIVE means there should only 
// be one thing below it. 
constraints.fill = GridBagConstraints.NONE; 
constraints.anchor = GridBagConstraints.CENTER; 
constraints.ipadx = 20; 
constraints.ipady = 20; 
constraints.weightx = 1.0; 
constraints.weighty = 1.0; 
constraints.gridwidth = GridBagConstraints.REMAINDER; 
constraints.gridheight = GridBagConstraints.RELATIVE; 
gridbag.setConstraints(messageLabel, constraints); 
add(messageLabel); 

// The button has no padding, no weight, taked up minimal width, and 
// Is the last thing in its column. 

constraints.ipadx = 0; 
constraints.ipady = 0; 
constraints.weightx = 0.0; 
constraints.weighty = 0.0; 
constraints.gridwidth = 1; 
constraints.gridheight = GridBagConstraints.REMAINDER; 

gridbag.setConstraints(okButton, constraints); 
add(okButton); 

// Pack is a special window method that makes the window take up the 
// space necessary to contain its components. 

pack(); 

} 

// The action method just waits for the OK button to be clicked and 
// when it is it hides the dialog, causing the show() method to 
// back to whoever activated this dialog. 

public boolean action(Event evt, Object whichAction) 
{ 
if (evt.target == okButton) 
{ 
hide(); 
if (createdFrame != null) 
{ 
createdFrame.hide(); 
} 
} 
return true; 
} 

// Shortcut to create a frame automatically, the frame is a static 
// so all dialogs in an applet or application can use the same frame. 

public static void createOKDialog(String dialogString) 
{ 
// If the frame hasn't been created yet, create it 
if (createdFrame == null) 
{ 
createdFrame = new Frame("Dialog"); 
} 
// Create the dialog now 
///OKDialog okDialog = new OKDialog(createdFrame,"OK"); 
 
///createdFrame.resize(okDialog.size().width,okDialog.size().height);
// Shrink the frame to just fit the dialog 

// Show the dialog 
///okDialog.show(); 

} 
} 
