1  import java.applet.*;
  2  import java.awt.*;
  3  import java.lang.*;
  4  import java.io.*;
  5  
  6  public class Games extends Applet {
  7  
  8     public TheBoard board;
  9     public MyPanel mPanel;
 10     int px = 0, py = 0, re = 0, A31 = 0, ex = 0, why = 0, zee = 0, num = 0;
 11     int A[] = {2,4,5,7,1,3,10,2,6,7,5,8,1,3,4,9,11,8,10,6,9};
 12     public int A4[] = new int[21];
 13     public int invalid[] = new int[21];
 14     int done  = 21;	 
 15     public int s = 210;
 16     public int invalid_count, A4_count, handfull;
 17     public int hand1, hand2, hand3;
 18     public boolean A1, A2, A3;
 19  
 20     public void init() {
 21  	Setup();
 22  	setBackground(Color.pink);
 23  	setLayout(new GridLayout(2,1));
 24          board = new TheBoard(this);
 25  	mPanel = new MyPanel(this);
 26          add(board);
 27         	}
 28  
 29     public void paint(Graphics g) {
 30  	board.paint(g);
 31          
 32  	}
 33  
 34     public void update() {
 35     	board.update();
 36      	}
 37      
 38     public boolean mouseDown(Event evt, int x, int y) {
 39  	px = x; py = y;
 40  	mPanel.l.setText("Mouse down at " + x + ", " + y);
 41  
 42  	// mouse on the deck
 43  	if ( A1 && ( x >= 370 ) && ( x <= 430 ) && ( y >= 110) && ( y <= 200))
 44  		{
 45  		re = InArea1();
 46  	 	if ( hand1 == 0 )
 47  			hand1 = re;
 48  		else if ( hand2 == 0 )
 49  			hand2 = re;
 50  		else if ( hand3 == 0)
 51  			hand3 = re;
 52  		update();
 53  		A2 = true;
 54  		}
 55  
 56  	// mouse on the hand
 57  	if ( A2 && ( x >= 30 ) && ( x <= 150 ) && ( y >= 330 ) && ( y <=380 ))
 58  		{ 
 59  		A1 = false;
 60  		A2 = false;
 61  		A3 = true;
 62  		ex = getValue(px,py);
 63  		if ( ex == 11 )
 64  			mPanel.l.setText("Wild Card");
 65  		else
 66  			mPanel.l.setText("hand is " + ex);
 67  		if ( ex == 0 )
 68  			// mouse on the frame
 69  			{
 70  			A2 = true;
 71  			A3 = false;
 72  			}
 73  		}
 74  
 75  	// mouse on the board
 76  	if ( A3 && ( A31 < 2 ) &&( x >= 50 ) && ( x <= 300 ) 
 77  	     && ( y >= 100 ) && ( y <= 300))
 78  		{
 79  		num = getValue(px,py);
 80  		if ( validNum(num) )
 81  			{
 82  			invalid[invalid_count++] = num;
 83  			if ( A31 == 0 )
 84  				{zee = num;mPanel.l.setText("zee = "+zee);}
 85  			else {why = num;mPanel.l.setText("why = "+why);}
 86  			// draw cross here
 87  			update();
 88  			A31++;
 89  			if ( A31 >= 2)
 90  				{
 91  				A3 = false;
 92  				A31 = 0;
 93  				
 94  				if ( calculate(ex,why,zee) )
 95  					{
 96  					InArea4(ex);
 97  					mPanel.l.setText("that right!");
 98  					clearHand(ex);
 99  					score(why,zee);
100  					}	
101  				else {
102  					signal();
103  					//clear  why and zee
104  					invalid_count= invalid_count - 2;
105  					//update();	
106  					A2 = true;
107  				     }
108  				if ( handfull < 3 )
109  					A1 = true;
110  				if ( handfull != 0 )
111  					A2 = true;
112  				}
113  			}
114  		}
115  	return true;
116  	}
117  
118     public void Setup() {
119  	//reset all the variable to the initial condition
120  
121     	s = 210;
122  	done = 21;
123     	invalid_count = 0;
124     	A4_count = 0;
125     	handfull = 0;
126    	hand1 = 0; hand2 = 0; hand3 = 0;
127     	A1 = true; A2 = false; A3 = false;
128  	for (int i = 0; i < 21; i++)
129  		{
130  		invalid[i] = -1;
131  		A4[i] = -1;
132  		}
133  
134  	}
135  	
136     public int InArea1() {
137  	//get a card from the deck
138  
139  	if ( done == 0 )
140  		{
141  		mPanel.l.setText("deck empty");
142  		return (-1);
143  		}
144  
145  	//now, deck still has some cards
146  	if ( handfull >=  3 )
147  		{
148                  handfull = 4; 
149  		mPanel.l.setText("hand is full");
150  	        return (0);
151  		}
152  
153  	//now, hand still has some room
154  	handfull = handfull + 1;
155  	done = done - 1;
156  	if ( A[done] == 11 )
157  		{
158  		mPanel.l.setText("will card !");
159  		}
160  	else mPanel.l.setText("card" + A[done]);
161  	return (A[done]);
162      	}
163  
164     public int getValue(int x, int y) {
165  	//return a value from xy area
166  
167      	if ((x >= 50) && (x <= 95) && (y >= 100) && (y <= 145))
168             	return (1); 
169          if ((x >= 100) && (x <= 145) && (y >= 100) && (y <= 145))
170             	return (2); 
171          if ((x >= 150) && (x <= 195) && (y >= 100) && (y <= 145))
172             	return (3); 
173          if ((x >= 200) && (x <= 245) && (y >= 100) && (y <= 145))
174             	return (4); 
175          if ((x >= 250) && (x <=295) && (y >= 100) && (y <= 145))
176             	return (5); 
177          if ((x >= 50) && (x <= 95) && (y >= 150) && (y <= 195))
178             	return (6); 
179          if ((x >= 100) && (x <= 145) && (y >= 150) && (y <= 195))
180             	return (7); 
181          if ((x >= 150) && (x <= 195) && (y >= 150) && (y <= 195))
182             	return (8); 
183          if ((x >= 200) && (x <= 245) && (y >= 150) && (y <= 195))
184             	return (9); 
185          if ((x >= 250) && (x <=295) && (y >= 150) && (y <= 195))
186             	return (10); 
187          if ((x >= 50) && (x <= 95) && (y >= 200) && (y <= 245))
188             	return (11); 
189          if ((x >= 100) && (x <= 145) && (y >= 200) && (y <= 245))
190             	return (12); 
191          if ((x >= 150) && (x <= 195) && (y >= 200) && (y <= 245))
192             	return (13); 
193          if ((x >= 200) && (x <= 245) && (y >= 200) && (y <= 245))
194             	return (14); 
195          if ((x >= 250) && (x <=295) && (y >= 200) && (y <= 245))
196             	return (15); 
197          if ((x >= 50) && (x <= 95) && (y >= 250) && (y <= 295))
198             	return (16); 
199          if ((x >= 100) && (x <= 145) && (y >= 250) && (y <= 295))
200             	return (17); 
201          if ((x >= 150) && (x <= 195) && (y >= 250) && (y <= 295))
202             	return (18); 
203          if ((x >= 200) && (x <= 245) && (y >= 250) && (y <= 295))
204             	return (19); 
205          if ((x >= 250) && (x <=295) && (y >= 250) && (y <= 295))
206             	return (20);
207  
208          if ((x >= 30) && (x <= 70) && (y >= 330) && (y <= 380))
209             	return (hand1); 
210          if ((x >= 75) && (x <= 115) && (y >= 330) && (y <= 380))
211             	return (hand2); 
212          if ((x >= 120) && (x <= 160) && (y >= 330) && (y <= 380))
213             	return (hand3);
214  
215  	return (0); 
216  
217  	}
218      
219     public boolean calculate(int x, int y, int z) {
220  	//x = |z - y|
221  
222  	if ( x == 11 )
223  		return (true);
224          if (x == (Math.abs(z-y)) )
225              return (true); 
226          else return (false); 
227  
228  	}
229  
230     public boolean validNum(int x) {
231  	//return true if  x is not already used
232  
233  	for (int i = 0; i < invalid_count; i++)
234  		{
235  		if (invalid[i] == x)
236  			{
237  			mPanel.l.setText("card " + x + " is used");
238  			return false;
239  			}
240  		} 
241  	mPanel.l.setText("invalid count is " + invalid_count);
242  	return true;
243  
244  	}
245  
246     public void InArea4(int x) {
247  	//draw x in area 4
248  	
249  	A4[A4_count++] = x;
250  	update();
251  	
252  	}
253  
254     public void clearHand(int x) {
255  	//remove a card from the hand
256  
257  	if (hand1 == x)
258  		hand1 = 0;
259  	if (hand2 == x)
260  		hand2 = 0;
261  	if (hand3 == x)
262  		hand3 = 0;
263  	handfull = handfull - 1;
264  	update();
265  	
266  	}
267  
268     public void score(int y, int z) {
269  	//keep game score(s)
270  
271  	s = s - z - y;
272  
273  	}
274  
275     public void signal() {
276  	//produce error signal
277  	
278  	mPanel.l.setText("error!...please try again");
279  	}
280  
281  }
282  
283  
284  //***************************TheBoard*****************************
285  
286  class TheBoard extends Applet {
287      
288     Games parent;
289     int x, y, dx, dy, px, py;
290  
291     TheBoard(Games parent) {
292  
293  	this.parent = parent;
294  	}  
295   
296     public void paint(Graphics g) {
297  	
298  	drawBoard(g);
299        	drawStr(g);
300  	drawHand(g);
301  	if ( parent.handfull != 0 )
302  		fillHand(g);
303  	if ( parent.invalid_count != 0 )
304  		drawCross(g); 
305  	if ( parent.A4_count != 0 )
306  		drawA4(g);
307       	}
308  
309     public void update() {
310  
311  	repaint();	
312  	}
313  
314     public void drawBoard(Graphics g) {
315  
316  	int x = 50, y = 100, dx = 250, dy = 200;
317  	g.setColor(Color.yellow);
318  	g.fillRect(x,y,dx,dy);
319  	g.setColor(Color.black);
320  	for (int i = 0; i < 5; i++)
321  		{
322  		g.drawLine(x,y,x+dx,y);
323  		y = y + 50; 
324  		}
325  	y = 100;
326  	for (int j = 0; j < 6; j++)
327  		{
328  		g.drawLine(x,y,x,y+dy);
329  		x = x + 50;
330  		}
331  	}
332  
333     public void drawHand(Graphics g) {
334  	
335  	g.setColor(Color.green);
336        	g.fillRoundRect(30,330,40,50,15,15);
337        	g.fillRoundRect(75,330,40,50,15,15);
338        	g.fillRoundRect(120,330,40,50,15,15);
339  	g.setColor(Color.black);
340        	g.fillRoundRect(375,105,60,90,15,15);
341  	g.setColor(Color.white);
342        	g.fillRoundRect(370,110,60,90,15,15);
343  
344  	}
345  
346     public void drawStr(Graphics g) {
347        
348  	Font afont = new Font("Courier",Font.BOLD,40); 
349        	g.setFont(afont); 
350        	g.setColor(Color.black);
351         	g.drawString("1",63,133); 	g.drawString("2",113,133); 
352        	g.drawString("3",163,133); 	g.drawString("4",213,133); 
353        	g.drawString("5",263,133);	g.drawString("6",63,183);
354        	g.drawString("7",113,183);	g.drawString("8",163,183);
355        	g.drawString("9",213,183);	g.drawString("10",253,183);
356        	g.drawString("11",53,233); 	g.drawString("12",103,233); 
357        	g.drawString("13",153,233); 	g.drawString("14",203,233); 
358        	g.drawString("15",253,233);	g.drawString("16",53,283);
359        	g.drawString("17",103,283);	g.drawString("18",153,283);
360        	g.drawString("19",203,283);	g.drawString("20",253,283);
361       	}
362  
363     public void fillHand(Graphics g) {
364  	        
365        	Font font1 = new Font("Courier",Font.BOLD,32); 
366        	g.setFont(font1); 
367  	g.setColor(Color.gray);
368  	if ( parent.hand1 != 0 )
369  		if ( parent.hand1 == 11 )
370  			g.drawString("W",33,363);
371  		else
372  			g.drawString(String.valueOf(parent.hand1),33,363);
373  	if ( parent.hand2 != 0 )
374  		if ( parent.hand2 == 11 )
375  			g.drawString("W",75,363);
376  		else
377  			g.drawString(String.valueOf(parent.hand2),75,363);
378  	if ( parent.hand3 != 0 )
379  		if ( parent.hand3 == 11 )
380  			g.drawString("W",123,363);
381  		else
382  			g.drawString(String.valueOf(parent.hand3),123,363);
383  	}
384  
385     public void drawCross(Graphics g) {
386  
387  	g.setColor(Color.red);
388  	for (int i = 0; i < parent.invalid_count; i++)
389  		{
390  		if ( parent.invalid[i] == 1 )
391  			fillCross(g,75,125);
392  		if ( parent.invalid[i] == 2 )
393  			fillCross(g,125,125);
394  		if ( parent.invalid[i] == 3 )
395  			fillCross(g,175,125);
396  		if ( parent.invalid[i] == 4 )
397  			fillCross(g,225,125);
398  		if ( parent.invalid[i] == 5 )
399  			fillCross(g,275,125);
400  		if ( parent.invalid[i] == 6 )
401  			fillCross(g,75,175);
402  		if ( parent.invalid[i] == 7 )
403  			fillCross(g,125,175);
404  		if ( parent.invalid[i] == 8 )
405  			fillCross(g,175,175);
406  		if ( parent.invalid[i] == 9 )
407  			fillCross(g,225,175);
408  		if ( parent.invalid[i] == 10 )
409  			fillCross(g,275,175);
410  		if ( parent.invalid[i] == 11 )
411  			fillCross(g,75,225);
412  		if ( parent.invalid[i] == 12 )
413  			fillCross(g,125,225);
414  		if ( parent.invalid[i] == 13 )
415  			fillCross(g,175,225);
416  		if ( parent.invalid[i] == 14 )
417  			fillCross(g,225,225);
418  		if ( parent.invalid[i] == 15 )
419  			fillCross(g,275,225);
420  		if ( parent.invalid[i] == 16 )
421  			fillCross(g,75,275);
422  		if ( parent.invalid[i] == 17 )
423  			fillCross(g,125,275);
424  		if ( parent.invalid[i] == 18 )
425  			fillCross(g,175,275);
426  		if ( parent.invalid[i] == 19 )
427  			fillCross(g,225,275);
428  		if ( parent.invalid[i] == 20 )
429  			fillCross(g,275,275);
430  		
431  		}
432  	}
433  
434     public void drawA4(Graphics g) {	
435  
436  	x = 10; y = 2;
437          px = x ; py = y + 25;
438  	dx = 30; dy = 35;
439        	Font font2 = new Font("Courier",Font.BOLD,28); 
440        	g.setFont(font2); 
441          for (int i = 0; i < parent.A4_count; i++)
442  		{ 
443  		if ( i < 10 )
444  			{
445  			g.setColor(Color.red);
446  			g.drawRoundRect(x,y,dx,dy,10,10);
447  			g.setColor(Color.magenta);
448  			g.fillRoundRect(x,y,dx,dy,10,10);
449  			g.setColor(Color.blue);
450  			if (parent.A4[i] > 9)
451  				g.drawString(String.valueOf
452  						(parent.A4[i]),px,py);
453  			else
454  				g.drawString(String.valueOf
455  						(parent.A4[i]),px+7,py);
456  				
457  			x = x + dx + 5; 
458     			px = x;
459  			}
460  		}
461  	x = 10; y = 42; py = y + 25;
462          for (int i = 0; i < parent.A4_count; i++)
463  		{ 
464  		if ( i >= 10 )
465  			{
466  			g.setColor(Color.magenta);
467  			g.fillRoundRect(x,y,dx,dy,10,10);
468  			g.setColor(Color.blue);
469  			g.drawString(String.valueOf(parent.A4[i]),px,py);
470  			x = x + dx + 5; 
471     			px = x;
472  			}
473  		}
474  
475  	}	
476  
477     public void fillCross(Graphics g, int x, int y) {
478  	
479  	int X[] = {x-25,x-20,x,x+20,x+25,x+5,x+25,x+20,x,x-20,x-25,x-5};
480  	int Y[] = {y-20,y-25,y-5,y-25,y-20,y,y+20,y+25,y+5,y+25,y+20,y};
481  	g.setColor(Color.red);
482  	g.fillPolygon(X,Y,12);
483  	
484  	}
485  }
486  
487  
488  //****************************MyPanel******************************
489  
490  class MyPanel extends Frame {
491  	
492  	Games parent;
493   	Label l; 
494  	int px, py;
495  	boolean read = true;
496  	int count = 0;
497  	String str = new String();
498  	Button score, New, done, hall;
499  	public int Sc[] = new int[10];
500  	public String Name[] = new String[10];  
501  
502  
503     MyPanel(Games parent) {
504          this.parent = parent;
505  
506  	resize(400,150);
507  	setTitle("Substraction Game: Main Menu");
508  	setLayout(new BorderLayout());
509  	setBackground(Color.pink);
510  
511  	l = new Label("loading...");
512  	l.setFont(new Font("TimesRoman", Font.ITALIC, 24));
513  	add("North",l);
514  	
515  	score = new Button("SCORE");
516  	add("West",score);
517  	
518  	done = new Button("DONE");
519  	add("East",done);	
520  
521  	hall = new Button("Hall of Fame");
522  	add("South",hall);
523  
524  	New = new Button("NEW");
525  	add("Center",New);	
526  
527  	show();
528  	}
529  
530     public boolean action (Event evt, Object arg) {
531  
532  	int i = 100 - parent.s;
533  	if ( evt.target instanceof Button )
534  		{
535  		if (evt.target == score) 
536  			l.setText("the score now is " + i);
537  		if (evt.target == New) 
538  			{
539  			parent.Setup();
540  			parent.update();
541  			}
542  		if (evt.target == done) 
543  			{
544  			l.setText("your score is " + i);
545  			parent.A1 = false; 
546  			parent.A2 = false;
547  			parent.A3 = false;
548  			checkScore(i);
549  			} 
550  		//if (evt.target == hall)
551  			//viewHall();
552  		}
553  	return true;
554  	} 
555  
556     public void checkScore(int x) {
557  	//enter user name if x is top 10
558  
559  	}
560  
561  }