/* OK first of all, when I wrote this, I just copied my c code and translated to java.
I had never written any java code ever before, and now, three years later, I find this
code horrible. 
But because of the many requests for the code I've decided to put out the code anyway,
but I warn you, don't code like this!

Note that this engine doesn't think ahead, it just evaluate the current position.
The best way to proceed is maybe to modify the Analyze function and use it with the 
MiniMax algorithm. I actually started coding some think-ahead but I think i'ts
commented below.

Well that's it, and good luck!

Staffan Ekvall
pnyxtr@yahoo.com

*/


import java.awt.*;
import java.awt.image.*;
import java.net.*;
import java.applet.*;
//import java.util.math;

public
class Connect5 extends Applet
	{
	char Board[][][] = new char[32][32][2];
	int PointBoard[][] = new int[32][32];
   int x, y;
   int startx = 15, endx = 15, starty = 15, endy = 15;

   int white;
   int black;
   char cleararea = 1;
   static final int PLAYING = 0;
   static final int NOTPLAYING = 1;
   static final int WAITING = 1;
   static final int YOUWON = 2;
   static final int YOULOST = 3;
   int status = NOTPLAYING;
   int picturestatus = WAITING;

   int status2[] = { 0, 0 };
   int fx, fy;

   static final char EMPTY = 0;
   static final char MARKX = 1;
   static final char MARKO = 2;
   static final char EDGE = 3;

   static final int FIVE = 80000;
   static final int FOUR = 12000;
	static final int THREE = 500;
	static final int THREEHOLE = 450;
	static final int FOURBLOCK = 450;
	static final int FOURBLOCKHOLE = 400;
	static final int TWO = 50;
	static final int TWOHOLE = 45;
	static final int THREEBLOCK = 40;
	static final int TWOBLOCK = 3;

   void
   CopyBoard(int from, int to)
   	{
      for (x = 0; x < 32; x++)
	      for (y = 0; y < 32; y++)
            Board[x][y][to] = Board[x][y][from];
      }

	int
   ChkWin(int realcheck)
		{
		int x, y, mx[] = { 1, 0, 1, -1 }, my[] = { 0, 1, 1, 1 }, i, type;
		for (x = startx - 4; x < endx + 4; x++)
			for (y = starty - 4; y < endy + 4; y++)
				{
      		if (Board[x][y][0] > 0)
		         {
      			type = Board[x][y][0];
		         if (type != EDGE)
					for (i = 0; i < 4; i++)
						if (Board[x + 1 * mx[i]][y + 1 * my[i]][0] == type  &&
						    S(x + 2 * mx[i], y + 2 * my[i]) == type  &&
						    S(x + 3 * mx[i], y + 3 * my[i]) == type  &&
						    S(x + 4 * mx[i], y + 4 * my[i]) == type)
		               {
							if (realcheck == 1)
                     	{
								status = NOTPLAYING;
      		   	      if (type == MARKX)
		      	            {
                           picturestatus = YOUWON;
		   	               }
   							else
   	   		            {
                           picturestatus = YOULOST;
	      		            }
                        }
                     return type;
            		   }
      		   }
		      }
      return 0;
		}

	boolean
	yourMove(int x, int y)
		{
		if (x < 1 || x > 30 || y < 1 || y > 30)
		   return false;
		if (Board[x][y][0] != EMPTY)
		   return false;
		MakeMark(x, y, MARKX);
		return true;
	   }

      int
      S(int x, int y)
			{
			if (x < 0) return EDGE;
			if (y < 0) return EDGE;
			if (x > 31) return EDGE;
			if (y > 31) return EDGE;
			return (Board[x][y][0]);
			}

		int
		Analyze(char type, float multiplier)
		{
		/*
		# = EMPTY
		_ = CURRENT SQUARE
		X = OWN MARKS
		O = OPP MARKS
		* == X or #
		*/

		int x, y, mx[] = { 1, -1, 0, 0, 1, -1, 1, -1 }, my[] = { 0, 0, 1, -1, 1, 1, -1, -1 }, i;
		int points = 0;
		int blockmult[][] = new int[32][32];
		for (x = 0; x < 32; x++)
			for (y = 0; y < 32; y++)
		      blockmult[x][y] = 1;

		for (x = startx - 4; x < endx + 4; x++)
			for (y = starty - 4; y < endy + 4; y++)
				{
	   	   if (S(x, y) == EMPTY)
		      for (i = 0; i < 8; i++)
		      	{
					if (S(x + 1 * mx[i], y + 1 * my[i]) == type)
   		      	{
	      			if (S(x + 2 * mx[i], y + 2 * my[i]) == type)
		               {
   				   	if (S(x + 3 * mx[i], y + 3 * my[i]) == type)
								{
		   			   	if (S(x + 4 * mx[i], y + 4 * my[i]) == type)  // _XXXX	XXXX_
									{
									points += FIVE;
		                     PointBoard[x][y] += FIVE * multiplier;
   			               continue;
                           }
		                  if (S(x + 4 * mx[i], y + 4 * my[i]) == EMPTY)
      		            	{
            		         if (S(x - 1 * mx[i], y - 1 * my[i]) == type) // X_XXX#    #XXX_X
                  		   	{
										points += FIVE;
   			                  PointBoard[x][y] += FIVE * multiplier;
            		            continue;
                  		      }
		                     if (S(x - 1 * mx[i], y - 1 * my[i]) == EMPTY) // #_XXX#   #XXX_#
      		               	{
										points += FOUR;
   	            		      PointBoard[x][y] += FOUR * multiplier;
		                        continue;
      		                  }
            		         // O_XXX#   #XXX_O
									points += FOURBLOCK;
		  	                  PointBoard[x][y] += FOURBLOCK * multiplier * blockmult[x][y];
									blockmult[x][y]++;
	         		         continue;
                  		   }
	      		         // S(x + 4 * mx[i], y + 4 * my[i]) = fi || edge
			               if (S(x - 1 * mx[i], y - 1 * my[i]) == type) // X_XXXO   OXXX_X
		      		          	{
										points += FIVE;
	            		         PointBoard[x][y] += FIVE * multiplier;
   	   		               continue;
			                     }
      		            if (S(x - 1 * mx[i], y - 1 * my[i]) == EMPTY) // #_XXXO   OXXX_#
		                   	{
									points += FOURBLOCK;
	   		               PointBoard[x][y] += FOURBLOCK * multiplier * blockmult[x][y];
									blockmult[x][y]++;
		                     continue;
		                     }
		                  // O_XXXO     OXXX_O
								continue;
      		            }
		// ---------------------------- S(x + 3 * mx[i], y + 3 * my[i]) != type
							if (S(x + 3 * mx[i], y + 3 * my[i]) == EMPTY)
		               	{
		                  if (S(x - 1 * mx[i], y - 1 * my[i]) == EMPTY) // #_XX#    #XX_#
		                  	{
									points += THREE;
		  	                  PointBoard[x][y] += THREE * multiplier;
		     	               continue;
		                     }
   		               if (S(x - 1 * mx[i], y - 1 * my[i]) == type)
	      	            	{
		                     if (S(x - 2 * mx[i], y - 2 * my[i]) == EMPTY) // #X_XX#   #XX_X#
	     		               	{
										points += FOUR;
	  			                  PointBoard[x][y] += FOUR * multiplier;
		                        continue;
      		                  }
		                     if (S(x - 2 * mx[i], y - 2 * my[i]) == type) // XX_XX#    #XX_XX   Dual
      		               	{
										points += FIVE / 2;
   			                  PointBoard[x][y] += FIVE * multiplier / 2;
		                        continue;
      		                  }
		                     // OX_XX#    #XX_XO
									points += FOURBLOCK;
  	   		               PointBoard[x][y] += FOURBLOCK * multiplier * blockmult[x][y];
									blockmult[x][y]++;
      		               continue;
		                     }
	   	               // S(x - 1 * mx[i], y - 1 * my[i]) == fi || edge
		                  if (S(x + 4 * mx[i], y + 4 * my[i]) == EMPTY) // O_XX##    ##XX_O
            		      	{
									points += THREEBLOCK;
		  	                  PointBoard[x][y] += THREEBLOCK * multiplier;
                  		   continue;
            		         }
      		            if (S(x + 4 * mx[i], y + 4 * my[i]) == type) // O_XX#X   X#XX_O
		                  	{
									points += FOURBLOCKHOLE;
  	   		               PointBoard[x][y] += FOURBLOCKHOLE * multiplier * blockmult[x][y];
									blockmult[x][y]++;
		                     continue;
      		               }
		                  // O_XX#O   O#XX_O
		                  continue;
      		            }

							// S(x + 3 * mx[i], y + 3 * my[i]) == fi || edge
		               if (S(x - 1 * mx[i], y - 1 * my[i]) == EMPTY)
               			{
            		      if (S(x - 2 * mx[i], y - 2 * my[i]) == EMPTY) // ##_XXO    OXX_##
      		            	{
									points += THREEBLOCK;
  	               		   PointBoard[x][y] += THREEBLOCK * multiplier;
            		         continue;
      		               }
		                  if (S(x - 2 * mx[i], y - 2 * my[i]) == type) // X#_XXO  OXX_#X
      		            	{
									points += FOURBLOCKHOLE;
  	               		   PointBoard[x][y] += FOURBLOCKHOLE * multiplier * blockmult[x][y];
									blockmult[x][y]++;
      		               continue;
		                     }
                  		// O#_XXO    OXX_#O
            		      continue;
      		            }
		               if (S(x - 1 * mx[i], y - 1 * my[i]) == type) // X_XXO   OXX_X
            		   	{
      		            if (S(x - 2 * mx[i], y - 2 * my[i]) == EMPTY) // #X_XXO  OXX_X#
		                  	{
									points += FOURBLOCK;
		  	                  PointBoard[x][y] += FOURBLOCK * multiplier * blockmult[x][y];
									blockmult[x][y]++;
            		         continue;
      		               }
		                  if (S(x - 2 * mx[i], y - 2 * my[i]) == type) // XX_XXO  OXX_XX   Dual
               		   	{
									points += FIVE / 2;
  			                  PointBoard[x][y] += FIVE * multiplier / 2;
	                 		   continue;
            		         }
      		            // OX_XXO    OXX_XO
		                  continue;
      		            }
		               // O_XXO    OXX_O
      		         continue;
		               }
		// --------------------- S(x + 2 * mx[i], y + 2 * my[i]) != type
						if (S(x + 2 * mx[i], y + 2 * my[i]) == EMPTY) // _X#
            		  	{
      		         if (S(x - 1 * mx[i], y - 1 * my[i]) == EMPTY) // #_X#
		                	{
                  		if (S(x - 2 * mx[i], y - 2 * my[i]) == type) // X#_X#
            		      	{
      		               if (S(x - 3 * mx[i], y - 3 * my[i]) == EMPTY) // #X#_X#
		                     	{
										points += THREEHOLE;
  		      		            PointBoard[x][y] += THREEHOLE * multiplier;
      		                  continue;
		                        }
                  		   if (S(x - 3 * mx[i], y - 3 * my[i]) == type) // XX#_X#
            		         	{
										points += FOURBLOCKHOLE;
		  		                  PointBoard[x][y] += FOURBLOCKHOLE * multiplier * blockmult[x][y];
										blockmult[x][y]++;
            		            continue;
      		                  }
									points += THREEBLOCK;
		              		   PointBoard[x][y] += THREEBLOCK * multiplier;
            		         // OX#_X#
      		               continue;
		                     }
		                  if (S(x - 2 * mx[i], y - 2 * my[i]) == EMPTY) // ##_X#
               		   	{
         		            if (S(x + 3 * mx[i], y + 3 * my[i]) == EMPTY) // ##_X##
   		                  	{
										points += TWO;
  		            		      PointBoard[x][y] += TWO * multiplier;
            		            continue;
      		                  }
		                     if (S(x + 3 * mx[i], y + 3 * my[i]) == type) // ##_X#X
                     			{
                  		      if (S(x + 4 * mx[i], y + 4 * my[i]) == EMPTY) // ##_X#X#
            		            	{
											points += THREEHOLE;
		  		   	               PointBoard[x][y] += THREEHOLE * multiplier;
                        		   continue;
                  		         }
            		            if (S(x + 4 * mx[i], y + 4 * my[i]) == type) // ##_X#XX
      		                  	{
											points += FOURBLOCKHOLE;
  		   	      		         PointBoard[x][y] += FOURBLOCKHOLE * multiplier * blockmult[x][y];
											blockmult[x][y]++;
      		                     continue;
		                           }
                     		   // ##_X#XO
										points += THREEBLOCK;
  					               PointBoard[x][y] += THREEBLOCK * multiplier;
		                        continue;
                  		      }
									// ##_X#O
									points += TWO;
			                  PointBoard[x][y] += TWO * multiplier;
	               		   continue;
         	   	         }
								// O#_X#
		                  if (S(x + 3 * mx[i], y + 3 * my[i]) == EMPTY) // O#_X##
                  			{
									points += TWO;
	   		               PointBoard[x][y] += TWO * multiplier;
		 		               continue;
               		      }
      			         if (S(x + 3 * mx[i], y + 3 * my[i]) == type)
		                  	{
                  		   if (S(x + 4 * mx[i], y + 4 * my[i]) == EMPTY) // O#_X#X#
            		         	{
										points += THREEHOLE;
		  		                  PointBoard[x][y] += THREEHOLE * multiplier;
                        		continue;
                  		      }
            		         if (S(x + 4 * mx[i], y + 4 * my[i]) == type) // O#_X#XX
      		               	{
										points += FOURBLOCKHOLE;
  		               	   	PointBoard[x][y] += FOURBLOCKHOLE * multiplier * blockmult[x][y];							blockmult[x][y]++;
										blockmult[x][y]++;
            		            continue;
      		                  }
		                     // O#_X#XO
									points += THREEBLOCK;
  	         		         PointBoard[x][y] += THREEBLOCK * multiplier;
 	   		              	continue;
		                     }
								// O#_X#O
		                  continue;
		                  }
         	      if (S(x - 1 * mx[i], y - 1 * my[i]) == type)
			              	{
		                  if (S(x - 2 * mx[i], y - 2 * my[i]) == type) // XX_X#
		                  	continue; // Already processed
		                  if (S(x - 2 * mx[i], y - 2 * my[i]) == EMPTY) // #X_X# // Dual
		                  	{
									points += THREE / 2;
		  	                  PointBoard[x][y] += THREE * multiplier / 2;
		                     continue;
		                     }
      		            if (S(x + 3 * mx[i], y + 3 * my[i]) == EMPTY) // OX_X##
		                  	{
									points += THREEBLOCK;
		  	                  PointBoard[x][y] += THREEBLOCK * multiplier;
		                     continue;
		                     }
		                  if (S(x + 3 * mx[i], y + 3 * my[i]) == type) // OX_X#X
		                  	{
									points += FOURBLOCKHOLE;
		  	                  PointBoard[x][y] += FOURBLOCKHOLE * multiplier;
		                     continue;
		                     }
								// OX_X#O
		                  continue;
		                  }
		               if (S(x + 3 * mx[i], y + 3 * my[i]) == EMPTY)
		               	{
				            if (S(x + 4 * mx[i], y + 4 * my[i]) == EMPTY) // O_X###
		                  	{
									points += TWOBLOCK;
		  		               PointBoard[x][y] += TWOBLOCK * multiplier;
      		               continue;
		                     }
				            if (S(x + 4 * mx[i], y + 4 * my[i]) == type) // O_X##X
		                  	{
									points += TWOBLOCK;
		  		               PointBoard[x][y] += TWOBLOCK * multiplier;
		                     continue;
		                     }
								// O_X##O
	      	            continue;
		                  }
		               if (S(x + 3 * mx[i], y + 3 * my[i]) == type)
		               	{
				            if (S(x + 4 * mx[i], y + 4 * my[i]) == EMPTY) // O_X#X#
		                  	{
									points += THREEBLOCK;
		  	                  PointBoard[x][y] += THREEBLOCK * multiplier;
		                     continue;
		                     }
				            if (S(x + 4 * mx[i], y + 4 * my[i]) == type) // O_X#XX
   		               	{
									points += FOURBLOCKHOLE;
  	   		               PointBoard[x][y] += FOURBLOCKHOLE * multiplier * blockmult[x][y];
									blockmult[x][y]++;
		                     continue;
      		               }
								// O_X#XO
      		            continue;
		                  }
		               // O_X#O
      		         continue;
		               }
						// S(x + 2 * mx[i], y + 2 * my[i]) == fi || edge
      		      if (S(x - 1 * mx[i], y - 1 * my[i]) == EMPTY) // #_XO
		             	{
      		         if (S(x - 2 * mx[i], y - 2 * my[i]) == type)
		               	{
            		      if (S(x - 3 * mx[i], y - 3 * my[i]) == EMPTY) // #X#_XO
      		            	{
									points += THREEBLOCK;
  	         		         PointBoard[x][y] += THREEBLOCK * multiplier;
      		               continue;
		                     }
      		            if (S(x - 3 * mx[i], y - 3 * my[i]) == type) // XX#_XO
		                  	{
									points += FOURBLOCKHOLE;
  	   		               PointBoard[x][y] += FOURBLOCKHOLE * multiplier * blockmult[x][y];
									blockmult[x][y]++;
                  		   continue;
            		         }
      		            // OX#_XO
		                  continue;
      		            }
		               if (S(x - 2 * mx[i], y - 2 * my[i]) == EMPTY)
      		           	{
		                  if (S(x - 3 * mx[i], y - 3 * my[i]) == EMPTY) // ###_XO
                  			{
									points += TWOBLOCK;
  				               PointBoard[x][y] += TWOBLOCK * multiplier;
		                     continue;
            		         }
      		            if (S(x - 3 * mx[i], y - 3 * my[i]) == type) // X##_XO
		                  	{
									points += TWOBLOCK;
  		      		         PointBoard[x][y] += TWOBLOCK * multiplier;
      		               continue;
		                     }
            		      // O##_XO
      		            continue;
		                  }
							// O#_XO
							continue;
		               }
		//		      if (S(x - 1 * mx[i], y - 1 * my[i]) == type) continue; // Already processed
		            // O_XO
						continue;
		            }
		//-------------------- S(x + 1 * mx[i], y + 1 * my[i]) != type
					if (S(x + 1 * mx[i], y + 1 * my[i]) == EMPTY && S(x + 2 * mx[i], y + 2 * my[i]) == type) // _#X
		         	{
		            if (S(x - 1 * mx[i], y - 1 * my[i]) == EMPTY && S(x + 3 * mx[i], y + 3 * my[i]) == EMPTY) // #_#X#
      		      	{
							points += TWOHOLE;
      		         PointBoard[x][y] += TWOHOLE * multiplier;
		               continue;
		               }
		            if (S(x - 1 * mx[i], y - 1 * my[i]) == type) continue; // X_#X# Already processed
		            if (S(x + 3 * mx[i], y + 3 * my[i]) == type) // _#XX
		            	{
		               if (S(x - 1 * mx[i], y - 1 * my[i]) == type) continue; // X_#XX // Already processed
		               if (S(x - 1 * mx[i], y - 1 * my[i]) == EMPTY) // #_#XX
		               	{
		                  if (S(x + 4 * mx[i], y + 4 * my[i]) == EMPTY) // #_#XX#
		                  	{
									points += THREEHOLE;
		  	                  PointBoard[x][y] += THREEHOLE * multiplier;
		                     continue;
      		               }
		                  if (S(x + 4 * mx[i], y + 4 * my[i]) == type) // #_#XXX
      		            	{
									points += FOURBLOCKHOLE;
  	   		               PointBoard[x][y] += FOURBLOCKHOLE * multiplier * blockmult[x][y];
									blockmult[x][y]++;
      		               continue;
		                     }
		                  // #_#XXO
								points += THREEBLOCK;
		                  PointBoard[x][y] += THREEBLOCK * multiplier;
		                  continue;
		                  }
		               // O_#XX
							points += THREEBLOCK;
      		         PointBoard[x][y] += THREEBLOCK * multiplier;
		               continue;
		               }
		            if (S(x - 1 * mx[i], y - 1 * my[i]) == EMPTY) // #_#XO
		            	{
							points += TWOBLOCK;
		               PointBoard[x][y] += TWOBLOCK * multiplier;
		               continue;
		               }
		            // O_#X#
						points += TWOBLOCK;
		            PointBoard[x][y] += TWOBLOCK * multiplier;
		            continue;
      		      }
					if (S(x + 1 * mx[i], y + 1 * my[i]) == EMPTY && S(x + 2 * mx[i], y + 2 * my[i]) == EMPTY && S(x + 3 * mx[i], y + 3 * my[i]) == type) // _##X
		         	{
		            if (S(x - 1 * mx[i], y - 1 * my[i]) == EMPTY && S(x + 4 * mx[i], y + 4 * my[i]) == EMPTY) // #_##X#
      			     	{
							points += TWOHOLE;
  	      	   	   PointBoard[x][y] += TWOHOLE * multiplier;
      	   	      continue;
		               }
      		      if (S(x - 1 * mx[i], y - 1 * my[i]) == type) continue; // X_##X# // Already processed
		            if (S(x + 4 * mx[i], y + 4 * my[i]) == type) // _##XX
      		      	{
		               if (S(x - 1 * mx[i], y - 1 * my[i]) == EMPTY) // #_##XX
         		      	{
								points += TWOHOLE;
   	      		      PointBoard[x][y] += TWOHOLE * multiplier;
      		            continue;
		                  }
      		         if (S(x - 1 * mx[i], y - 1 * my[i]) == type) continue; // X_##XX // Already processed
		               // O_##XX
							points += TWOBLOCK;
	   		         PointBoard[x][y] += TWOBLOCK * multiplier;
		               continue;
		               }

      		      if (S(x - 1 * mx[i], y - 1 * my[i]) == EMPTY) // #_##XO
		            	{
							points += TWOBLOCK;
		  	            PointBoard[x][y] += TWOBLOCK * multiplier;
      		         continue;
		               }
      		      // O_##X#
						points += TWOBLOCK;
		            PointBoard[x][y] += TWOBLOCK * multiplier;
         		   continue;
      	   	   }
					}
			}
		return points;
		}

	void
	MakeMark(int x, int y, char type)
		{
		if (x < startx) startx = x;
		if (x > endx) endx = x;
		if (y < starty) starty = y;
		if (y > endy) endy = y;
		if (startx < 4) startx = 4;
		if (starty < 4) starty = 4;
		if (endx > 27) endx = 27;
		if (endy > 27) endy = 27;
	   Board[x][y][0] = type;
		}

	void
	ComputerMove()
		{
		int xpoints, opoints;
		float agression;
      int status, maxstat = -300000;
		int x, y, max[] = { -300000, -300000 };
      int bestx[] = { 1, 1 }, besty[] = { 1, 1 };
      int bx = 1, by = 1, min = 300000;
      int besx = 1, besy = 1;
		int i, i2;

		for (x = 1; x < 31; x++)
			for (y = 1; y < 31; y++)
		   	PointBoard[x][y] = 0;
		opoints = Analyze(MARKO, 1);
		for (x = 1; x < 31; x++)
			for (y = 1; y < 31; y++)
		   	PointBoard[x][y] = 0;
		xpoints = Analyze(MARKX, 1);
      if (xpoints != 0)
      	{
	      agression = (float)opoints / (float)xpoints;
			agression *= 3.5;
         }
      else agression = 10;
		opoints = Analyze(MARKO, agression);

		for (x = startx - 4; x < endx + 4; x++)
  			for (y = starty - 4; y < endy + 4; y++)
		      if (PointBoard[x][y] >= max[0])
  				   	{
			         if (PointBoard[x][y] > max[0] || Math.random() < 0.5)
				 			{
				         max[0] = PointBoard[x][y];
			   	      bestx[0] = x;
			      	   besty[0] = y;
			            }
			         }

/*		for (x = startx - 4; x < endx + 4; x++)
  			for (y = starty - 4; y < endy + 4; y++)
  		   	{
				for (i = 0; i < 2; i++)
					if (PointBoard[x][y] > max[i])
					{
               for (i2 = 1; i2 > i; i2--)
               	{
						max[i2] = max[i2 - 1];
                  bestx[i2] = bestx[i2 - 1];
                  besty[i2] = besty[i2 - 1];
                  }
				   max[i] = PointBoard[x][y];
               bestx[i] = x; besty[i] = y;
               i = 2;
				   }
  		 	   }

      CopyBoard(0, 1);

		for (i = 0; i < 2; i++)
      	{
			Board[bestx[i]][besty[i]][0] = MARKO;

			for (x = 1; x < 31; x++)
				for (y = 1; y < 31; y++)
			   	PointBoard[x][y] = 0;
			xpoints = Analyze(MARKX, 1);
			for (x = 1; x < 31; x++)
				for (y = 1; y < 31; y++)
			   	PointBoard[x][y] = 0;
			opoints = Analyze(MARKO, 1);
		   agression = (float)xpoints / (float)opoints;
			agression *= 3.5;
			Analyze(MARKX, agression);

         max[0] = -300000;

	  		for (x = startx - 4; x < endx + 4; x++)
  				for (y = starty - 4; y < endy + 4; y++)
	  		   	{
					if (PointBoard[x][y] > max[0])
						{
					   max[0] = PointBoard[x][y];
	               besx = x; besy = y;
					   }
  			 	   }

			Board[besx][besy][0] = MARKX;

			for (x = 1; x < 31; x++)
				for (y = 1; y < 31; y++)
			   	PointBoard[x][y] = 0;
			opoints = Analyze(MARKO, 1) / 2;
         if (ChkWin(0) == MARKO) opoints += 150000;
			for (x = 1; x < 31; x++)
				for (y = 1; y < 31; y++)
			   	PointBoard[x][y] = 0;
			xpoints = Analyze(MARKX, 1);
         if (ChkWin(0) == MARKX) xpoints += 150000;
			status = opoints - xpoints;

         status2[i] = status;

			if (status > maxstat)
         	{
            maxstat = status;
            bx = bestx[i];
            by = besty[i];
            fx = besx; fy = besy;
            }
   	   CopyBoard(1, 0);
			}
*/
		MakeMark(bestx[0], besty[0], MARKO);
      }

   Image notImage;
   Image crossImage;
   Image Menu;
   Image YouLost;
   Image YouWon;
   Image Waiting;
   Image Playing;

    public void
    init()
    	{
		notImage = getImage(getCodeBase(), "images/not.gif");
		crossImage = getImage(getCodeBase(), "images/cross.gif");
		Menu = getImage(getCodeBase(), "images/menu.gif");
		YouLost = getImage(getCodeBase(), "images/youlost.gif");
		YouWon = getImage(getCodeBase(), "images/youwon.gif");
		Waiting = getImage(getCodeBase(), "images/waiting.gif");
		Playing = getImage(getCodeBase(), "images/playing.gif");
		setBackground(Color.white);
	   }

	public void
   NewGame()
   	{
   	for (x = 1; x < 31; x++)
	   	for (y = 1; y < 31; y++)
	       	Board[x][y][0] = EMPTY;
	   for (x = 0; x < 32; x++)
        	Board[x][0][0] = EDGE;
	   for (y = 0; y < 32; y++)
      	Board[0][y][0] = EDGE;
	   for (x = 0; x < 32; x++)
      	Board[x][31][0] = EDGE;
 	   for (y = 0; y < 32; y++)
      	Board[31][y][0] = EDGE;
		Board[15][15][0] = MARKO;

      picturestatus = PLAYING;
      cleararea = 1;
      startx = 15; starty = 15; endx = 15; endy = 15;
      repaint();
      }

	public void
	paint (Graphics g)
		{
		g.setColor(Color.black);
	   for (x = 0; x < 31; x++)
   		g.drawLine(13 * x, 0, 13 * x, 389);
	   for (y = 0; y < 31; y++)
			g.drawLine(0, 13 * y, 389, 13 * y);

	   g.drawImage(Menu, 393, 2, this);
      if (picturestatus == WAITING) g.drawImage(Waiting, 393, 240, this);
  	   if (picturestatus == PLAYING) g.drawImage(Playing, 393, 240, this);
      if (picturestatus == YOUWON) g.drawImage(YouWon, 393, 240, this);
      if (picturestatus == YOULOST) g.drawImage(YouLost, 393, 240, this);

      for (x = 1; x < 31; x++)
		    for (y = 1; y < 31; y++)
	         	{
	            if (Board[x][y][0] == MARKX) g.drawImage(crossImage, (x - 1) * 13 + 1, (y - 1) * 13 + 1, this);
	            if (Board[x][y][0] == MARKO) g.drawImage(notImage, (x - 1) * 13 + 1, (y - 1) * 13 + 1, this);
	            }
	   }

	public void
	update (Graphics g)
		{
      if (cleararea == 1)
      	{
         cleararea = 0;
			g.setColor(Color.white);
         g.fillRect(0, 0, 600, 400);
			g.setColor(Color.black);
		   for (x = 0; x < 31; x++)
	   		g.drawLine(13 * x, 0, 13 * x, 389);
		   for (y = 0; y < 31; y++)
				g.drawLine(0, 13 * y, 389, 13 * y);

	      g.drawImage(Menu, 393, 2, this);
	      if (picturestatus == WAITING) g.drawImage(Waiting, 393, 240, this);
   	   if (picturestatus == PLAYING) g.drawImage(Playing, 393, 240, this);
         }

      if (picturestatus == YOUWON) g.drawImage(YouWon, 393, 240, this);
      if (picturestatus == YOULOST) g.drawImage(YouLost, 393, 240, this);

/*		g.setColor(Color.white);
      g.fillRect(0, 0, 100, 60);
		g.setColor(Color.black);
		g.drawString("o = " + status2[0], 0, 30);
		g.drawString("x = " + status2[1], 0, 40);*/

      for (x = 1; x < 31; x++)
			for (y = 1; y < 31; y++)
	       	{
	         if (Board[x][y][0] == MARKX) g.drawImage(crossImage, (x - 1) * 13 + 1, (y - 1) * 13 + 1, this);
	         if (Board[x][y][0] == MARKO) g.drawImage(notImage, (x - 1) * 13 + 1, (y - 1) * 13 + 1, this);
	         }
	   }

	public boolean
	mouseUp(Event evt, int x, int y)
		{
		switch (status)
	     {
		  case NOTPLAYING:
          if (x > 56 + 393 && y > 114 + 2 && x < 151 + 393 && y < 162 + 2)
             {
			    play(getCodeBase(), "audio/joy.au");
				 NewGame();
	   	    repaint();
	          status = PLAYING;
             }
	       break;
	     default:

		  // Figure out the row/colum
		  Dimension d = size();
		  int c = x / 13;
		  int r = y / 13;
		  if (yourMove(c + 1, r + 1))
        		{
			   repaint();
            ChkWin(1);
				if (status == PLAYING)
   				{
            	ComputerMove();
   	         ChkWin(1);
	            }
				}
	     else play(getCodeBase(), "audio/beep.au");
	     }
		return true;
		}

	public
	String getAppletInfo()
	 	{
		return "Connect Five by Staffan Ekvall";
	   }
	}

/*  		      if (PointBoard[x][y] >= max)
  			   	{
		         if (PointBoard[x][y] > max || Math.random() < 0.5)
			 			{
			         max = PointBoard[x][y];
		   	      bestx = x;
		      	   besty = y;
		            }
		         }*/


