import java.awt.*;
import java.applet.*;
import java.lang.*;
import java.net.*;
import java.io.*;


public class gomoku extends Applet {

	static final String VERSION = "1.00";			// Version

        static final int YSHIFT = 51;

        static final Color BLACK = Color.black;                  //
	static final Color BLACK_S = Color.darkGray;		//
	static final Color WHITE = Color.white;			// Colors
	static final Color WHITE_S = Color.lightGray;		//
        static final Color BOARD = new Color(0, 255, 0);        //

	static final int NOMOVE = 0;					//
	static final int REALMOVE = 1;				// Type of move
	static final int PSEUDOMOVE = 2;				//

	static final int Empty = 0;					//
	static final int User = 1;					// Board's owners
	static final int Computer = 2;				//

	boolean UserMove = true;
	boolean GameOver = false;
	boolean CopyWinOn = false;
	boolean StillInitiated = false;				// This solve a little
										// problem on reinit.
int vari2[]={ 3,0,2,0,-1,-1,-2,-2,
              0,3,0,2,-1,-1,-2,-2,
              0,-3,0,-2,-1,-1,-2,-2,
             -3,0,-2,0,-1,-1,-2,-2,
             -2,0,-1,0,0,1,0,2,
	     -2,0,-1,0,-1,1,-2,2,
	     -2,0,-1,0,-1,-1,-2,-2,
	     -2,0,-1,0,0,-1,0,-2,
	     -2,0,-1,0,1,-1,2,-2,
	     -2,0,-1,0,1,1,2,2,
	     0,-1,0,-2,1,0,2,0,
	     0,-1,0,-2,1,-1,2,-2,
	     0,-1,0,-2,1,1,2,2,
	     0,-1,0,-2,-1,1,-2,2,
	     0,-1,0,-2,-1,-1,-2,-2,
	     1,0,2,0,1,-1,2,-2,
	     1,0,2,0,1,1,2,2,
	     1,0,2,0,0,1,0,2,
	     1,0,2,0,-1,1,-2,2,
	     1,0,2,0,-1,-1,-2,-2,
	     0,1,0,2,1,-1,2,-2,
	     0,1,0,2,1,1,2,2,
	     0,1,0,2,-1,1,-2,2,
	     0,1,0,2,-1,-1,-2,-2,
	     -1,-1,-2,-2,1,-1,2,-2,
	     -1,-1,-2,-2,-1,1,-2,2,
	     -1,1,-2,2,1,1,2,2,
	     1,-1,2,-2,1,1,2,2
};

int go[][];
int Score[][];
int BMove[];
               String         WinMsg_W = "HO VINTO !";
               String               WinMsg_T = "PATTA !";
               String         WinMsg_L = "HAI VINTO !";
               String         BtnNew = "Nuova Partita";
               String         BtnCopy = "Istruzioni";
               String         MyMove = "Tocca a me";
               String         YourMove = "Tocca a te";
               String         PressWin = "Click del mouse per tornare alla scacchiera";
               

	public void DrawBoard(Graphics g) {
			// draw upper & lower side of board (321x33)
                g.setColor(new Color(255, 0, 0));
                g.fillRect(0, 0, 350, 30);
                g.fillRect(0, 380, 350, 30);
			// draw board (321x321)
		g.setColor(BOARD);
                g.fillRect(0, 30, 350, 350);
			// board lines
		g.setColor(BLACK);
                for (int i=0; i<=14; i++) {
                        g.drawLine((22*i)+21,51,(22*i)+21,359);                               // horizontal
                        g.drawLine(21,YSHIFT+(22*(i)),329,YSHIFT+(22*(i)));      // vertical
		}
	}
	// End of DrawBoard


	public void DrawPiece(int Who, int Col, int Row) {
		Graphics g = getGraphics();
                int pCol = (22*(Col)+6);
                int pRow = YSHIFT+(22*(Row)-14);
		Color pColor,
		      pShadow;

		if (Who == User) {
			pColor = BLACK;
			pShadow = BLACK_S;
		} else {
			pColor = WHITE;
			pShadow = WHITE_S;
		}
                go[Col][Row] = Who;
		g.setColor(pShadow);
                g.fillOval(pCol+6, pRow+6, 19, 19);
		g.setColor(pColor);
                g.fillOval(pCol+5, pRow+5, 19, 19);
	}


	public boolean IsBoardComplete() {
                for (int i=0; i<15; i++) {
                        for (int j=0; j<15; j++) {
                                if (go[i][j] == Empty) {
					return false;
				}
			}
		}
		return true;
	}
	// End of IsBoardComplete


public void varie3(int i,int j, int chi) {
if(i+3<=14&&i-2>=0){
   if ((go[i][j] == 0)&&(go[i+1][j] == chi)&&
   (go[i+2][j] == chi)&&(go[i-1][j] == chi)&&
   (go[i-2][j] == 0)&&(go[i+3][j] == 0))
                                   Score[i][j]+=(100*(chi*chi));
                                   
}
if(j+3<=14&&j-2>=0){
if ((go[i][j] == 0)&&(go[i][j+1] == chi)&&
   (go[i][j+2] == chi)&&(go[i][j-1] == chi)&&
   (go[i][j-2] == 0)&&(go[i][j+3] == 0))
                                   
			Score[i][j]+=(100*(chi*chi));
                                   
}
if(i+3<=14&&i-2>=0&&j-2>=0&&j+3<=14){
if ((go[i][j] == 0)&&(go[i+1][j+1] == chi)&&
   (go[i+2][j+2] == chi)&&(go[i-1][j-1] == chi)&&
   (go[i-2][j-2] == 0)&&(go[i+3][j+3] == 0))
                                   
			Score[i][j]+=(100*(chi*chi));
                                   
}
if(i+2<=14&&i-3>=0&&j+3<=14&&j-2>=0){
if ((go[i][j] == 0)&&(go[i-1][j+1] == chi)&&
   (go[i-2][j+2] == chi)&&(go[i+1][j-1] == chi)&&
   (go[i+2][j-2] == 0)&&(go[i-3][j+3] == 0))
                                   
			Score[i][j]+=(100*(chi*chi));
                                   
}
if(i+2<=14&&i-3>=0){
   if ((go[i][j] == 0)&&(go[i+1][j] == chi)&&
   (go[i+2][j] == 0)&&(go[i-1][j] == chi)&&
   (go[i-2][j] == chi)&&(go[i-3][j] == 0))
                                   
				Score[i][j]+=(100*(chi*chi));
                                   
}
if(j+2<=14&&j-3>=0 ){
if ((go[i][j] == 0)&&(go[i][j+1] == chi)&&
   (go[i][j+2] == 0)&&(go[i][j-1] == chi)&&
   (go[i][j-2] == chi)&&(go[i][j-3] == 0))
                                   
			Score[i][j]+=(100*(chi*chi));
                                   
}
if(i+2<=14&&i-3>=0&&j-3>=0&&j+2<=14){
if ((go[i][j] == 0)&&(go[i+1][j+1] == chi)&&
   (go[i+2][j+2] == 0)&&(go[i-1][j-1] == chi)&&
   (go[i-2][j-2] == chi)&&(go[i-3][j-3] == 0))
                                   
			Score[i][j]+=(100*(chi*chi));
                                   
}
if(i+3<=14&&i-2>=0&&j+2<=14&&j-3>=0){
if ((go[i][j] == 0)&&(go[i-1][j+1] == chi)&&
   (go[i-2][j+2] == 0)&&(go[i+1][j-1] == chi)&&
   (go[i+2][j-2] == chi)&&(go[i+3][j-3] == 0))
                                   
			Score[i][j]+=(100*(chi*chi));
                                   
}
		}


        public int[] BestMove()
         {
int retval[];
    retval=new int[3];
                retval[0] = -998;
                retval[1] = 0;  // colonna
                retval[2] = 0;  // riga

              int i,j,C,R,x;
              boolean esci;

 for (i=0; i<15; i++) { for (j=0; j<15; j++) Score[i][j]=-999;}

		for (i=0; i<15; i++) {
			for (j=0; j<15; j++) {

                          for (x=1;x<3;x++){
                             duec(i,j,x);
                             varie2(i,j,x);
                             tre(i,j,x);
                             trec(i,j,x);
                            quattro(i,j,x);
                             varie1(i,j,x);
                             varie3(i,j,x);
                              }

			     }
			}

                 for (C=0; C<15; C++) {
                        for (R=0; R<15; R++) {
                                if (Score[C][R] > retval[0]) {
                                        retval[1] = C;
                                        retval[2] = R;
                                        retval[0] = Score[C][R];
                               }
                       }
               }

                if (retval[0]<-997)
                {
                  esci=false;
                  for (C=9; C>1; C--) {
                        for (R=9; R>1; R--) {
                              if (go[C][R]==0)
                                {
                                retval[1]=C;
                                retval[2]=R;
                                esci=true;
                                break;
                               }
                             }
                          if (esci) break;
                          }
                }

           //     retval[1]+=2;
           //     retval[2]+=2;

        return retval;

	}

        public void vincitore(int vint) {
		Graphics g = getGraphics();
		g.setColor(Color.lightGray);
		g.fill3DRect(20,53,281,281, true);
		g.setColor(Color.black);
		g.setFont(new Font("Helvetica", Font.BOLD, 24));
                String TheMsg = "Go Moku (v." + VERSION +")";
                g.drawString(TheMsg, ((int)((321-(g.getFontMetrics(g.getFont()).stringWidth(TheMsg)))/2))+1, 90);

                g.setFont(new Font("Helvetica", Font.BOLD, 14));
                TheMsg = "Congratulazioni hai vinto";
                if(vint==2) TheMsg="Mi dispiace hai perso";
                if(vint==3) TheMsg="La partita e' finita in parita'";
                g.drawString(TheMsg, ((int)((321-(g.getFontMetrics(g.getFont()).stringWidth(TheMsg)))/2))+1, 200);
		g.setFont(new Font("System", Font.BOLD, 12));
		g.drawString(PressWin, ((int)((321-(g.getFontMetrics(g.getFont()).stringWidth(PressWin)))/2))+1, 320);
		CopyWinOn = true;
	}



	public void ShowAbout() {
		Graphics g = getGraphics();
		g.setColor(Color.lightGray);
		g.fill3DRect(20,53,281,281, true);
		g.setColor(Color.black);
		g.setFont(new Font("Helvetica", Font.BOLD, 24));
                String TheMsg = "Go Moku (v." + VERSION +")";
                g.drawString(TheMsg, ((int)((321-(g.getFontMetrics(g.getFont()).stringWidth(TheMsg)))/2))+1, 90);

		g.setFont(new Font("Helvetica", Font.BOLD, 12));
                TheMsg = "Lo scopo del gioco e' molto semplice";
                g.drawString(TheMsg, ((int)((321-(g.getFontMetrics(g.getFont()).stringWidth(TheMsg)))/2))+1, 120);

                TheMsg = "per vincere bisogna allineare 5 pedine";
                g.drawString(TheMsg, ((int)((321-(g.getFontMetrics(g.getFont()).stringWidth(TheMsg)))/2))+1, 145);

                TheMsg = "in orizzontale, verticale o diagonale";
                g.drawString(TheMsg, ((int)((321-(g.getFontMetrics(g.getFont()).stringWidth(TheMsg)))/2))+1, 170);

                TheMsg = "Buon divertimento da";
                g.drawString(TheMsg, ((int)((321-(g.getFontMetrics(g.getFont()).stringWidth(TheMsg)))/2))+1, 210);

                TheMsg = "Antonino Iacono";
                g.drawString(TheMsg, ((int)((321-(g.getFontMetrics(g.getFont()).stringWidth(TheMsg)))/2))+1, 235);

                TheMsg = "aiacono@tiscalinet.it";
                g.drawString(TheMsg, ((int)((321-(g.getFontMetrics(g.getFont()).stringWidth(TheMsg)))/2))+1, 260);
		g.setFont(new Font("System", Font.BOLD, 12));
		g.drawString(PressWin, ((int)((321-(g.getFontMetrics(g.getFont()).stringWidth(PressWin)))/2))+1, 320);
		CopyWinOn = true;
	}
	// End of ShowAbout


	//
	// paint
	//
	public void paint(Graphics g) {
		DrawBoard(g);
		if (!CopyWinOn) {
                        for (int i=0; i<15; i++) {
                                for (int j=0; j<15; j++) {
                                        if (go[i][j] != Empty) {
                                                DrawPiece(go[i][j], i, j);
					}
				}
			}
		} else {
			ShowAbout();
		}
	}
	// End of paint


	//
	// init
	//
	public void init() {
                resize(350,410);
                if (!StillInitiated) {
			setFont(new Font("System", Font.PLAIN, 12));
			add(new Button(BtnNew));
			add(new Button(BtnCopy));
			StillInitiated = true;
		}
                BMove = new int[3];
                Score = new int[15][15];
                go = new int[15][15];
                for (int i=0; i<15; i++) {
                        for (int j=0; j<15; j++) {
                                go[i][j]=0;
                        }
		}
                go[7][7] = User;
                go[7][8] = Computer;
                go[8][7] = Computer;
                go[8][8] = User;
		UserMove = true;
		repaint();
	}
	// End of init


	//
	// handleEvent
	//
	public boolean handleEvent(Event evt) {
		int TheCol,
		    TheRow;
		int BX,
		    BY;

                boolean retval1 = false;

			// Restart
		if (BtnNew.equals(evt.arg) && (!CopyWinOn)) {
			init();
			return true;
		}
			// Show Copyright
		if (BtnCopy.equals(evt.arg) && (!CopyWinOn)) {
			ShowAbout();
			return true;
		}
			// Click with copyright message on
		if ((CopyWinOn) && (evt.id == Event.MOUSE_UP)) {
			CopyWinOn = false;
			repaint();
			return true;
		}
			// Process a normal click in the board
                BX = evt.x-10;
                BY = evt.y - YSHIFT + 10;
                if ((BY >= -10) && (BY <= 320) && (BX >= -10) && (BX <= 320) && 
		    (evt.id == Event.MOUSE_UP) && (UserMove)) {
                        TheCol = (int)((BX/22));
                        TheRow = (int)((BY/22));
                                if(go[TheCol][TheRow]==0)
                                {
                                retval1 = MakeMove(User, TheCol, TheRow);

                                while (retval1 && (!UserMove)) {
                                        BMove = BestMove();
                                        retval1 = MakeMove(Computer, BMove[1], BMove[2]);
				}
                                if (!retval1) {
					EndGame();
                                 }
                                 }
			return true;
		}
		return false;
	}
	// End of handleEvent


public void quattro(int i,int j,int chi)
{
if(i+4 <= 14){
   if ((go[i][j] == chi)&&(go[i+1][j] == chi)&&
   (go[i+2][j] == chi)&&(go[i+3][j] == chi)&&
   (go[i+4][j] == 0))
                                   
				  Score[i+4][j]+=(1000*(chi*chi));
                                   
}
if(j+4 <= 14){
if ((go[i][j] == chi)&&(go[i][j+1] == chi)&&
   (go[i][j+2] == chi)&&(go[i][j+3] == chi)&&
   (go[i][j+4] == 0))
                                   
                                 Score[i][j+4]+=(1000*(chi*chi));          
}
if(i+4 <= 14 && j+4<=14){
if ((go[i][j] == chi)&&(go[i+1][j+1] == chi)&&
   (go[i+2][j+2] == chi)&&(go[i+3][j+3] == chi)&&
   (go[i+4][j+4] == 0))
                                   
				Score[i+4][j+4]+=(1000*(chi*chi));   }
                         
if(i-4 >= 0 && j+4<=14){
if ((go[i][j] == chi)&&(go[i-1][j+1] == chi)&&
   (go[i-2][j+2] == chi)&&(go[i-3][j+3] == chi)&&
   (go[i-4][j+4] == 0))
                                   
			Score[i-4][j+4]+=(1000*(chi*chi));
                                   
}
if(i-1 >= 0 && i+3<=14){
if ((go[i][j] == chi)&&(go[i+1][j] == chi)&&
   (go[i+2][j] == chi)&&(go[i+3][j] == chi)&&
   (go[i-1][j] == 0))
                                   
				Score[i-1][j]+=(1000*(chi*chi));
                                   
}
if(j-1 >= 0 && j+3<=14){
if ((go[i][j] == chi)&&(go[i][j+1] == chi)&&
   (go[i][j+2] == chi)&&(go[i][j+3] == chi)&&
   (go[i][j-1] == 0))
                                   
				 Score[i][j-1]+=(1000*(chi*chi));
                                   
}
if(i-1 >= 0 && j-1>=0 && j+3<=14 && i+3<=14){
if ((go[i][j] == chi)&&(go[i+1][j+1] == chi)&&
   (go[i+2][j+2] == chi)&&(go[i+3][j+3] == chi)&&
   (go[i-1][j-1] == 0))
                   
			Score[i-1][j-1]+=(1000*(chi*chi));
                                   
}
if(i+1 <= 14 && j-1>=0 && i-3>=0 && j+3<=14){
if ((go[i][j] == chi)&&(go[i-1][j+1] == chi)&&
   (go[i-2][j+2] == chi)&&(go[i-3][j+3] == chi)&&
   (go[i+1][j-1] == 0))
                                   
				Score[i+1][j-1]+=(1000*(chi*chi));
                                   
}
                                }


public void varie1(int i,int j, int chi) {

if(i-2>=0&&i+2<=14){
   if ((go[i][j] == 0)&&(go[i+1][j] == chi)&&
   (go[i+2][j] == chi)&&(go[i-1][j] == chi)&&
   (go[i-2][j] == chi))
                                   
				Score[i][j]+=(1000*(chi*chi));
                                   
}
if(j-2>=0&&j+2<=14){
if ((go[i][j] == 0)&&(go[i][j+1] == chi)&&
   (go[i][j+2] == chi)&&(go[i][j-1] == chi)&&
   (go[i][j-2] == chi))
                                   
			Score[i][j]+=(1000*(chi*chi));
                                   
}
if(i-2>=0&&i+2<=14&&j-2>=0&&j+2<=14){
if ((go[i][j] == 0)&&(go[i+1][j+1] == chi)&&
   (go[i+2][j+2] == chi)&&(go[i-1][j-1] == chi)&&
   (go[i-2][j-2] == chi))
                                   
			Score[i][j]+=(1000*(chi*chi));
                                   
}
if(i-2>=0&&i+2<=14&j-2>=0&&j+2<=14){
if ((go[i][j] == 0)&&(go[i-1][j+1] == chi)&&
   (go[i-2][j+2] == chi)&&(go[i+1][j-1] == chi)&&
   (go[i+2][j-2] == chi))
                                   
			Score[i][j]+=(1000*(chi*chi));
                                   
}
if(i+3<=14&&i-1>=0){
   if ((go[i][j] == 0)&&(go[i+1][j] == chi)&&
   (go[i+2][j] == chi)&&(go[i+3][j] == chi)&&
   (go[i-1][j] == chi))
                                   
				Score[i][j]+=(1000*(chi*chi));
                                   
}
if(j-1>=0&&j+3<=14){
if ((go[i][j] == 0)&&(go[i][j+1] == chi)&&
   (go[i][j+2] == chi)&&(go[i][j+3] == chi)&&
   (go[i][j-1] == chi))
                                   
			Score[i][j]+=(1000*(chi*chi));
                                   
}
if(i+3<=14&&i-1>=0&&j+3<=14&&j-1>=0){
if ((go[i][j] == 0)&&(go[i+1][j+1] == chi)&&
   (go[i+2][j+2] == chi)&&(go[i+3][j+3] == chi)&&
   (go[i-1][j-1] == chi))
                                   
			Score[i][j]+=(1000*(chi*chi));
                                   
}
if(i-3>=0&&i+1<=14&&j+3<=14&&j-1>=0){
if ((go[i][j] == 0)&&(go[i-1][j+1] == chi)&&
   (go[i-2][j+2] == chi)&&(go[i-3][j+3] == chi)&&
   (go[i+1][j-1] == chi))
                                   
			Score[i][j]+=(1000*(chi*chi));
                                   
}
if(i-3>=0&&i+1<=14){
   if ((go[i][j] == 0)&&(go[i+1][j] == chi)&&
   (go[i-1][j] == chi)&&(go[i-2][j] == chi)&&
   (go[i-3][j] == chi))
                                   
				Score[i][j]+=(1000*(chi*chi));
                                   
}
if(j-3>=0&&j+1<=14){
if ((go[i][j] == 0)&&(go[i][j+1] == chi)&&
   (go[i][j-1] == chi)&&(go[i][j-2] == chi)&&
   (go[i][j-3] == chi))
                                   
			Score[i][j]+=(1000*(chi*chi));
                                   
}
if(i-3>=0&&i+1<=14&&j-3>=0&&j+1<=14){
if ((go[i][j] == 0)&&(go[i+1][j+1] == chi)&&
   (go[i-1][j-1] == chi)&&(go[i-2][j-2] == chi)&&
   (go[i-3][j-3] == chi))
                                   
			Score[i][j]+=(1000*(chi*chi));
                                   
}
if(i+3<=14&&i-1>=0&&j+1<=14&&j-3>=0){
if ((go[i][j] == 0)&&(go[i-1][j+1] == chi)&&
   (go[i+1][j-1] == chi)&&(go[i+2][j-2] == chi)&&
   (go[i+3][j-3] == chi))
                                   
			Score[i][j]+=(1000*(chi*chi));
                                   
	}
}
public void duec(int i,int j, int chi) {

if(i-3>=0&&i+1<=14){
   if ((go[i][j] == 0)&&(go[i+1][j] == 0)&&
   (go[i-1][j] == chi)&&(go[i-2][j] == chi)&&
   (go[i-3][j] == 0))                                
                               Score[i][j]+=(2*(chi*chi));
         }
if(j+1<=14&&j-3>=0){
if ((go[i][j] == 0)&&(go[i][j+1] == 0)&&
   (go[i][j-1] == chi)&&(go[i][j-2] == chi)&&
   (go[i][j-3] == 0))
			Score[i][j]+=(2*(chi*chi));
}
if(j-3>=0&&j+1<=14&&i-3>=0&&i+1<=14){

if ((go[i][j] == 0)&&(go[i+1][j+1] == 0)&&
   (go[i-1][j-1] == chi)&&(go[i-2][j-2] == chi)&&
   (go[i-3][j-3] == 0))
                        Score[i][j]+=(2*(chi*chi));
                                   
   }
if (j-3>=0&&j+1<=14&&i-1>=0&&i+3<=14){
if ((go[i][j] == 0)&&(go[i-1][j+1] == 0)&&
   (go[i+1][j-1] == chi)&&(go[i+2][j-2] == chi)&&
   (go[i+3][j-3] == 0))
			Score[i][j]+=(2*(chi*chi));
	}
}

public void varie2(int i,int j,int chi) {

   int g;

   for (g=0;g<217;g+=8)   {
   if(i+vari2[g]>=0&&i+vari2[g]<=14&&j+vari2[g+1]>=0&&j+vari2[g+1]<=14&&i+vari2[g+2]>=0&&i+vari2[g+2]<=14&&j+vari2[g+3]>=0&&j+vari2[g+3]<=14&&i+vari2[g+4]>=0&&i+vari2[g+4]<=14&&j+vari2[g+5]>=0&&j+vari2[g+5]<=14&&i+vari2[g+6]>=0&&i+vari2[g+6]<=14&&j+vari2[g+7]>=0&&j+vari2[g+7]<=14){
   if ((go[i][j] == 0)&&(go[i+vari2[g]][j+vari2[g+1]] == chi)&&
   (go[i+vari2[g+2]][j+vari2[g+3]] == chi)&&(go[i+vari2[g+4]][j+vari2[g+5]] == chi)&&
   (go[i+vari2[g+6]][j+vari2[g+7]] == chi))
		   Score[i][j]+=(20*(chi*chi));
                                   }
                                   }
				   }

public void tre(int i,int j,int chi) {
 if(i+3 <= 14 && i-1>=0){
	if ((go[i][j] == chi)&&(go[i+1][j] == chi)&&
	   (go[i+2][j] == chi)&&(go[i+3][j] == 0)&&
           (go[i-1][j] == 0))
			    {
                     if (go[i+4][j]==1 && i+4<=14)
				   Score[i+3][j]+=(1000*(chi*chi));
                                  
		     else
                     
				   Score[i-1][j]+=(100*(chi*chi));
                         
		     }
}
if(j+3 <= 14 && j-1>=0){
 if ((go[i][j] == chi)&&(go[i][j+1] == chi)&&
   (go[i][j+2] == chi)&&(go[i][j+3] == 0)&&
   (go[i][j-1] == 0))
		    {
                    if(j+4<=14){
                    if (go[i][j+4]==1 && j+4<=14)
		      Score[i][j+3]+=(1000*(chi*chi));
                   
		   else
                   
		      Score[i][j-1]+=(100*(chi*chi));
                      
                   }
                   }
}
if(i+3 <= 14 && j+3<=14 && i-1>=0 && j-1>=0){
  if ((go[i][j] == chi)&&(go[i+1][j+1] == chi)&&
   (go[i+2][j+2] == chi)&&(go[i+3][j+3] == 0)&&
   (go[i-1][j-1] == 0))
	   {
           if(i+4<=14 && j+4<=14){
           if (go[i+4][j+4]==1 )
		   Score[i+3][j+3]+=(1000*(chi*chi));
                                   
	   else
                   
		      Score[i-1][j-1]+=(100*(chi*chi));
                   
                   }
                   }
}
if(i-3 >= 0 && j+3<=14 && i+1<=14 && j-1>=0){
if ((go[i][j] == chi)&&(go[i-1][j+1] == chi)&&
   (go[i-2][j+2] == chi)&&(go[i-3][j+3] == 0)&&
   (go[i+1][j-1] == 0))
	   {
             if(i-4>=0 && j+4<=14){
             if (go[i-4][j+4]==1)
		 Score[i-3][j+3]+=(1000*(chi*chi));
                
		   else
                
		Score[i+1][j-1]+=(100*(chi*chi));
                 
               }
	   }
	}
}

public void trec(int i,int j,int chi) {
if(i+3<=14){
	if ((go[i][j] == chi)&&(go[i+1][j] == chi)&&
	   (go[i+2][j] == chi)&&(go[i+3][j] == 0)
           )
				   Score[i+3][j]+=(5*(chi*chi));
}
if(j+3<=14){
 if ((go[i][j] == chi)&&(go[i][j+1] == chi)&&
   (go[i][j+2] == chi)&&(go[i][j+3] == 0)
   )
		      Score[i][j+3]+=(5*(chi*chi));
}
if(i+3 <= 14 && j+3<=14 ){
  if ((go[i][j] == chi)&&(go[i+1][j+1] == chi)&&
   (go[i+2][j+2] == chi)&&(go[i+3][j+3] == 0))
		   Score[i+3][j+3]+=(5*(chi*chi));
}
if(i-3 >= 0 && j+3<=14 ){
if ((go[i][j] == chi)&&(go[i-1][j+1] == chi)&&
   (go[i-2][j+2] == chi)&&(go[i-3][j+3] == 0)
   )
		 Score[i-3][j+3]+=(5*(chi*chi));
}
}

        boolean vinto(int vint) {
	     int i,j;
		for (i=0; i<15; i++) {
			for (j=0; j<15; j++) {
                                if(i+4<=14){
                                   if ((go[i][j] == vint)&&
                                   (go[i+1][j] == vint)&&
                                   (go[i+2][j] == vint)&&
                                   (go[i+3][j] == vint)&&
                                   (go[i+4][j] == vint))
                                   return true;
                                   }
                                if(j+4<=14){
                                   if ((go[i][j] == vint)&&
                                   (go[i][j+1] == vint)&&
                                   (go[i][j+2] == vint)&&
                                   (go[i][j+3] == vint)&&
                                   (go[i][j+4] == vint))
                                   return true;
                                   }
                                 if(i-4>=0&&j+4<=14){
                                   if ((go[i][j] == vint)&&
                                   (go[i-1][j+1] == vint)&&
                                   (go[i-2][j+2] == vint)&&
                                   (go[i-3][j+3] == vint)&&
                                   (go[i-4][j+4] == vint))
                                   return true;
                                   }
                                  if(i+4<=14&&j+4<=14){
                                   if ((go[i][j] == vint)&&
                                   (go[i+1][j+1] == vint)&&
                                   (go[i+2][j+2] == vint)&&
                                   (go[i+3][j+3] == vint)&&
                                   (go[i+4][j+4] == vint))
                                   return true;
                                   }
                        }
		}
           return false;
	}

	public boolean MakeMove(int Who, int C, int R) {

                    DrawPiece(Who,C,R);

                if (IsBoardComplete() || vinto(1) || vinto(2))  {
                        return false;
		}
                UserMove = !UserMove;
        	return true;
	}

public   void EndGame() {
		Graphics g = getGraphics();

		int CompPieces = 0;
		int UserPieces = 0;

		int StrWidth,c,r;
                String TheMsg="";
		if (IsBoardComplete())
                {
                vincitore(3);
                TheMsg = "PARITA";
		   }
                if (vinto(2))
                 {
                 TheMsg = "HO VINTO";
                vincitore(2);
              } else if (vinto(1)) {
                 TheMsg = "HAI VINTO";
                vincitore(1);
                }
                g.setFont(new Font("Helvetica", Font.BOLD, 14));
                g.drawString(TheMsg, ((int)((321-(g.getFontMetrics(g.getFont()).stringWidth(TheMsg)))/2))+1, 400);
		}
	}
