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