//
/* ----------------------------------------------------------------------------------------------
    card.java	数当てカード								ver. 1.10 (JDK 1.02)
    														ueyama@infonet.co.jp  1999.04.18
    														update: 2008.08.06
---------------------------------------------------------------------------------------------- */
import java.applet.Applet;
import java.awt.*;


public class card extends Applet
{
	int		N=0;														// 正解の値
	int		Q=0;														// 選択カード数
	int		Card[][][]={{{57,59,61,63},{49,51,53,55},{41,43,45,47},{33,35,37,39},
						 {25,27,29,31},{17,19,21,23},{ 9,11,13,15},{ 1, 3, 5, 7}},
						{{58,59,62,63},{50,51,54,55},{42,43,46,47},{34,35,38,39},
						 {26,27,30,31},{18,19,22,23},{10,11,14,15},{ 2, 3, 6, 7}},
						{{60,61,62,63},{52,53,54,55},{44,45,46,47},{36,37,38,39},
						 {28,29,30,31},{20,21,22,23},{12,13,14,15},{ 4, 5, 6, 7}},
						{{60,61,62,63},{56,57,58,59},{44,45,46,47},{40,41,42,43},
						 {28,29,30,31},{24,25,26,27},{12,13,14,15},{ 8, 9,10,11}},
						{{60,61,62,63},{56,57,58,59},{52,53,54,55},{48,49,50,51},
						 {28,29,30,31},{24,25,26,27},{20,21,22,23},{16,17,18,19}},
						{{60,61,62,63},{56,57,58,59},{52,53,54,55},{48,49,50,51},
						 {44,45,46,47},{40,41,42,43},{36,37,38,39},{32,33,34,35}}};
	boolean	Cs[]={false,false,false,false,false,false,false,false};		// true: 選択されたカード
	boolean	A=false;
	Image	Img;
	
	public void init()
	{
		String	bg=getParameter("bgcolor");								// 背景色の読み込み
		int bgc=Integer.valueOf(bg,16).intValue();
		setBackground(new Color(bgc));
		MediaTracker mt=new MediaTracker(this);
		Img=getImage(getCodeBase(), getParameter("figure"));			// gif ファイルの読み込み
		mt.addImage(Img, 0);
		try
		{
			mt.waitForID(0);
		}
		catch(InterruptedException e){};
	}
	
	public boolean inside(int x, int a, int b)
	{
		return (x<=Math.max(a,b) && x>=Math.min(a,b)) ? true:false;
	}
	
	public void dsp_b(int x, int y, int m, int n, int c)
	{
		int w=0, h=0, ox=0, oy=0;
		switch(c)
		{
			case 0: w=13; h=16; ox=  0; oy= 0;	break;					// 数字の表示
			case 1: w=26; h=23; ox=130; oy= 0;	break;					// ボタンの表示
		}
		Graphics gx=getGraphics();
		gx.clipRect(x,y,w,h);
		gx.clearRect(x,y,w,h);
		gx.drawImage(Img,x-(ox+m*w),y-(oy+n*h),this);
		gx.dispose();
	}
	
	public void dsp_c(int n, int c)										// カード(数字)の表示
	{
		int i,j,k,x=n%3 *167,y=n/3 *208;
		for(j=0; j<8; j++)
		{
			for(i=0; i<4; i++)
			{
				k=Card[n][j][i];
				if(k>9) dsp_b(x+i*39+9, y+j*24+8, k/10, (A && k==N)?2:c, 0);
				dsp_b(x+i*39+13+9, y+j*24+8, k%10, (A && k==N)?2:c, 0);
			}
		}
	}
	
	public void paint(Graphics g)
	{
		int i;
		g.clearRect(0,0,size().width,size().height);
		for(i=0; i<6; i++) g.drawRect(i%3 *167,i/3 *208,159,200);		// カードの枠の表示
		for(i=0; i<6; i++) dsp_c(i,(Cs[i])?1:0);						// カードの数字の表示
		dsp_b(500,385,(N>0)?0:2,0,1);									// クリアボタンの表示
		dsp_b(530,385,(Q>1)?0:2,1,1);									// ?ボタンの表示
	}
	
	public boolean mouseDown(Event e, int mx, int my)
	{
		int m,n;
		if(mx%167<159 && my%208<200 && mx<493)							// カードをクリック
		{
			if(A)
			{
				N=0; Q=0;
				for(m=0; m<6; m++) {Cs[m]=false; dsp_c(m,0);}			// カードのクリアと表示
			}
			m=my/208*3 + mx/167;										// クリックされたカード番号
			Cs[m]=!Cs[m];
			dsp_c(m,(Cs[m])?1:0);										// カードの表示
			A=false; Q++;
		}
		if(inside(mx,500,526) && inside(my,385,408) && N>0)				// クリアボタン
		{
			A=false; N=0; Q=0;
			dsp_b(500,385,1,0,1);
			for(m=0; m<6; m++) {Cs[m]=false; dsp_c(m,0);}				// カードのクリアと表示
		}
		if(inside(mx,530,556) && inside(my,385,408) && Q>0)				// ?ボタン
		{
			A=true;
			dsp_b(530,385,1,1,1);
			for(m=0; m<6; m++) dsp_c(m,(Cs[m])?1:0);					// カードの表示
		}
		for(N=0,n=1,m=0; m<6; m++,n*=2) if(Cs[m]) N+=n;
		return true;
	}
	
	public boolean mouseUp(Event e, int mx, int my)
	{
		dsp_b(500,385,(N>0)?0:2,0,1);									// クリアボタンの表示
		dsp_b(530,385,(Q>0)?0:2,1,1);									// ?ボタンの表示
		return true;
	}
}
//
戻る