//**********************************************************

//**********************************************************

// Program: UglyBoard II

// 6th Feb 2002

// Written by Aidan Rawlinson

//**********************************************************

//**********************************************************/

// A LIST OF THE METHODS AND WHAT THEY DO:

//

// 1) public int FindNumber()

//          ...Simply gets the game number the applet will start up with.

//

// 2) public int FileCheck()

//     ...Finds how many games are on the page

//

// 3) public String ModOne(int game)

//          ...Retrieves the game

//

// 4) public static String[] ModTwo(String data)

//          ...Extracts the players names/date/venue/result

//

// 5) public static int ModThree(String game)

//          ...Works out how long the game

//

// 6) public static char[][] ModFour(String game, int gamelength)

//     ...Retrieves the moves as their written form e.g. Nbd7 (N, b, d, 7), O-O-O (O, -, O, -, O) etc...

//

// 7) public static int[][] ModFourhalf(char[][] gamemoves, int gamelength)

//     ...Converts the moves into a set of numbers which can then be 'crunched' in formula.

//

// 8) public static String[] ModFive(String game, int gamelength)

//     ...Retrieves the game commentary (e.g. Move 13, what a horrible blunder!)

//

// 9) public static char[][][] ModSix(int[][] list, int gamelength)

//     ...The heart and soul of the program.  The numbers from 7. go in and the array of boards comes out.

//

//10) public void SetLayout()

//     ...Sets up the applet.

//

//11) public void calls()

//     ...This is run everytime the board needs updating

//

//12) public void init()

//     ...This is the Method which is automatically started and everything else is run from.

//

//13) public void SetList(int noofgames)

//     ...Sets up the 'drop down' menu of games.

//

//14) public void GetImages()

//     ...Retrieves the graphics

//

//15) public boolean action(Event e, Object arg)

//16) public void actionPerformed(ActionEvent event)

//     ...These two deals with the buttons and other input methods.

//

//17) public void paint(Graphics g)

//18) public final void update(Graphics g)

//     ...These two handle the graphics.  Note that the board is created in memory first and then

//     drawn as a single image (to avoid flickering).....

/////////////////////////////////////////////////////////////////////////

 

import java.applet.Applet;

import java.awt.*;

import java.awt.event.*;

import java.io.*;

 

public class UglyII extends Applet

   implements ActionListener

   {

 

//-------Components of Applet-------------------------------------------

GridBagLayout aLayout = new GridBagLayout();

GridBagLayout bLayout = new GridBagLayout();

GridBagConstraints a = new GridBagConstraints();

GridBagConstraints bb = new GridBagConstraints();

Panel northPanel, southPanel, Panel1, Panel2, Panel3, Panel4, Panel5, Panel6;

Label event, site, round, result, date, player1, player2;

Choice games;

CheckboxGroup turn;

Checkbox turn1, turn2;

Button button1, button2, button3, button4, button5, button6;

TextField go; TextArea comment, temp;

Font f1 = new Font("Courier",0,11); 

//--------End of components---------------------------------------------

Image[] squarepics = new Image[26];

String[] picnames = {"b", "bbb", "bbw", "bkb", "bkw", "bnb", "bnw", "bqb", "bqw", "bpb", "bpw", "brb", "brw", "w", "wbb", "wbw", "wkb", "wkw", "wnb", "wnw", "wqb", "wqw", "wpb", "wpw", "wrb", "wrw"};

char[][][] boards;

char[][] movelist;

int moveply;

Image winScratch;

Graphics gScratch;

String[] gamedetails, annots;

int selectgame = -1, move = 0, gamelength = -1, checktwo = 0;

boolean flip = false;

//--------Chess Program constants---------------------------------------

    static final int M = 48;  //(number characters to correct integers)

    static final int N = 96;  //(letter character to correct integers{a - 1})

    static final char[] Legalstart = {'a','b','c','d','e','f','g','h','K','Q','B','N','R','O'};

    static final char[] Squares = {'a','b','c','d','e','f','g','h','1','2','3','4','5','6','7','8'};

    static final int[][] Compass = {{1,0},{1,1},{0,1},{-1,1},{-1,0},{-1,-1},{0,-1},{1,-1}};   // Q/K/B/R all move in compass directions {N, NE, E etc...}

    static final int[][] Horsey  = {{2,1},{1,2},{-1,2},{-2,1},{-2,-1},{-1,-2},{1,-2},{2,-1}};  // But not those dammed horseys ;)

    static final int[][] Prawns   = {{1,1},{-1,-1},{1,0},{-1,0},{1,-1},{-1,1}};    // And a table for those confusing pawns

    static final char[][] Bits = {{'P','P','P','P','P','P','P','P','K','Q','B','N','R','O'},

                   {'p','p','p','p','p','p','p','p','k','q','b','n','r','o'}};

//-------End of Constants-------------------------------------------------

 

    public int FindNumber()  //Get the variable for which game to disply.

       {

        String x = getParameter("Choice");

       int y = Integer.parseInt(x);

       return y;

      }  

 

    public int FileCheck()  //Get number of games available.

      {int x;

       String p;

       for(x = 1; x < 999; x++)

          {

           String y;

           p = "File" + x;

           y = getParameter(p);

           if(y == null)

             break;

          }

       return x-1;

      }

 

 

    public String ModOne(int game)

       {

       String File;

       String fileno = "File" + game;

        File = getParameter(fileno);

       if(File == null)

         File = getParameter("File1");

       return File;

      }  

 

    public static String[] ModTwo(String data)

      {

       char[] data2 = data.toCharArray();

       String details[] = new String[7];

       int c = 0;

       int y = 0;

       for(int x = 0; x < data2.length; x++) 

          {if(c == 7)

            break;

          if(data2[x] == '"')

             {x++;

             y = 0;

             while(data2[x] != '"')

               {x++;

                y++;

                if(x == (data2.length))

                  break;

               }

             details[c] = new String(data2,x-y,y);

             c++;

            }

          

          }

       int det0 = details[0].toCharArray().length;

       int det1 = details[1].toCharArray().length;

       int det2 = details[2].toCharArray().length;

 

       if(det0 < 20)

         {

          for(int dt0 = 0; dt0 < 20 - det0; dt0++)

          details[0] = details[0] + ' ';

         }

       else

         {

          details[0] = new String(details[0].toCharArray(),0,17) + "...";

         }

       if(det1 < 15)

          {

          for(int dt1 = 0; dt1 < 15 - det1; dt1++)

          details[1] = details[1] + ' ';

         }

       else

         {

          details[1] = new String(details[1].toCharArray(),0,12) + "...";

         }

       if(det2 < 10)

         {

          for(int dt2 = 0; dt2 < 10 - det2; dt2++)

          details[2] = details[2] + ' ';

         }

 

       return details;

      }

 

    public static int ModThree(String game)

      {char finalmove[] = new char[3];

       char[] game2 = game.toCharArray();

       int g = 0, y = 0;

             for(int x = game2.length - 1; x > 0; x--)

         {if(game2[x] == '}')

            while(game2[x] != '{')

               x--;

          if(game2[x] == '.')

            {finalmove[0] = game2[x-3];

             finalmove[1] = game2[x-2];

             finalmove[2] = game2[x-1];

             for(int z = x + 2; z < game2.length -1; z++)

               {

                if(game2[z] == '{')

                  while(game2[z] != '}')

                     z++;

                if(game2[z] == ' ')

                  {

                   z++;

                   for(int f = 0; f < 14; f++)

                      if(game2[z] == Legalstart[f])

                          {g = 1;

                           break;}

                   break;

                  }

               }

             break;

            }

         }

      if(finalmove[1] == ' ')

         y = (finalmove[2] - M);

      else

         if(finalmove[0] == ' ')

            y = ((finalmove[2] - M) + (10 * (finalmove[1] - M)));

         else

            y = ((finalmove[2] - M) + (10 * (finalmove[1] - M)) + (100*(finalmove[0] - M)));

      if(g == 1)

         y = 2 * y;

      else

         y = 2 * y - 1;

      return y;

      }

 

   public static char[][] ModFour(String game, int gamelength)

 

      {

       char[] game2 = game.toCharArray();

       char gamemoves[][] = new char[gamelength][];

       int y = 0, x = 0, a = 0, until = 0;

       

       while(until != 3)

         {until = 0;

          if(game2[y] == '1')

            until++;

           if(game2[y+1] == '.')

            until++;

          if(game2[y+2] == ' ')

            until++;

           y++;

         }

       for(x = 0; x < gamelength; x++)

         {a = 0;

          while(a == 0)

             {   if(game2[y] == '{') // Avoid annotations...

                  while(game2[y] != '}')

                     y++;

                for(int z = 0; z < 14; z++)

                  if(game2[y] == Legalstart[z])

                     a = 1;

              

                 if(a == 0)

                  y++;

                 else

                   {int b = 0;

                   while(game2[y] != ' ')

                      {if(y == game2.length-1)

                        break;

                   y++;

                      b++;}

                   gamemoves[x] = new char[b];

                   for(int c = 0; c < b; c++)

                     gamemoves[x][c] = game2[y-b+c];

                  }

            }

         }

       return gamemoves;

      }

 

   public static int[][] ModFourhalf(char[][] gamemoves, int gamelength)

 

      {

       int gamemoves2[][] = new int[gamelength][8];

       // ModFour has created char[][] gamemoves, containing the moves in char form e.g. 'e','4'

       // ModFourhalf then converts this to a int[][] array describing each move in a standardised format.

       // 0: CASTLING  1: PIECE  2: CAPTURE?  3:  +/#  4: FILE(to)  5: RANK(to)  6: FILE(from)  7: RANK(from)

       

         for(int k = 0; k < gamemoves.length; k++)

          {

          if(gamemoves[k][0] == 'O')

            if(gamemoves[k].length == 5)

               gamemoves2[k][0] = 2;         // O-O-O Castles long.

            else

               gamemoves2[k][0] = 1;     // O-O Castles short.

          else

            gamemoves2[k][0] = 0;        // Doesn't Castle.

            

          gamemoves2[k][1] = 0;          // 0: Default for pawns/castling.

          for(int j=12; j>7; j--)          // Rest of pieces in order of Legalstart e.g. K=1;

            {if(gamemoves[k][0] == Legalstart[j])

               gamemoves2[k][1] = j;

            }

 

          gamemoves2[k][2] = 0;  // Default not capture.

          for(int o = 1; o < gamemoves[k].length; o++)

            if(gamemoves[k][o] == 'x')

               gamemoves2[k][2] = 1;  // Capture

 

          gamemoves2[k][3] = 0;  // Default not check or mate.

          if(gamemoves[k][(gamemoves[k].length -1)] == '+')

            gamemoves2[k][3] = 1;     // Check

          if(gamemoves[k][(gamemoves[k].length -1)] == '#')

            gamemoves2[k][3] = 2;     // Mate

 

          int l = 0;

          if(gamemoves2[k][3] != 0)

             l = 1;

          if(gamemoves2[k][0] == 0)

             {gamemoves2[k][4] = (gamemoves[k][(gamemoves[k].length -2 - l)] - N - 1);

              gamemoves2[k][5] = (gamemoves[k][(gamemoves[k].length -1 - l)] - M - 1);}

          else

            {gamemoves2[k][4] = 0;

              gamemoves2[k][5] = 0;}

 

          int m = gamemoves[k].length;

          gamemoves2[k][6] = 9;                  // Default 'Empty' returns...

          gamemoves2[k][7] = 9; 

          if(gamemoves2[k][3] != 0)

            m--;

          while(m > 2)

            {

             for(int n = 0 ; n < Squares.length; n++)

             if(gamemoves[k][m - 3] == Squares[n])

               {if(n < 8)

                   gamemoves2[k][6] = n;       

                else

                  gamemoves2[k][7] = n - 8;

                }

             m--;

            }  

         }

 

 

       return gamemoves2;  

  

      }

 

   public static String[] ModFive(String game, int gamelength)

      {

       char[] game2 = game.toCharArray();

       String[] annots = new String[gamelength+1];

       int y = 0;

       int x = 1;

       int z = 0, until = 0, p = 0;

       

       while(until != 3)

         {until = 0;

          if(game2[y] == '1')

            until++;

           if(game2[y+1] == '.')

            until++;

          if(game2[y+2] == ' ')

            until++;

           y++;

         }

 

       y = y+2;

 

       while(y < game2.length)

          {if(game2[y] == '{')

            {while(game2[y] != '}')

               {y++;

               z++;}

             p = y;

             while(game2[p] != '.')

               {if(p+1 == game2.length)

                  {p--;

                   break;}

                p++;}

             if(game2[p+1] != '.')

                annots[x*2 - 1] = new String(game2,y-z+1,z-1);

             else

                annots[x*2 - 2] = new String(game2,y-z+1,z-1);

            }

          else

             if(game2[y] == '.' && game2[y-1] != '.' && game2[y+1] != '.')

               x++;

          y++;

          z = 0; p = 0;

         }

       return annots;

      }

 

   public static char[][][] ModSix(int[][] list, int gamelength)

      {

       char[][][] boards = new char[gamelength + 1][8][8];

       char[][] newgame = { 

                  {'R','N','B','Q','K','B','N','R'},

                  {'P','P','P','P','P','P','P','P'},

                  {'-','-','-','-','-','-','-','-'},

                  {'-','-','-','-','-','-','-','-'},

                  {'-','-','-','-','-','-','-','-'},

                  {'-','-','-','-','-','-','-','-'},

                  {'p','p','p','p','p','p','p','p'},

                  {'r','n','b','q','k','b','n','r'}

                 };

       boards[0] = newgame;

       int[][] direction = Compass;

       int range, start, tick, exit = 0, go, e = 1, f = 1;

 

       // And now for the bit which makes it all work...

     

       for(int y = 1; y < gamelength + 1; y++)

         {

           for(int z = 0; z < 8; z++) //(BOARD IS COPIED)

             boards[y][z] = (char[]) boards[y-1][z].clone();

 

          if((y-1) % 2 < 1) // WHITE(0) or BLACK(1) TURN??

               go = 0;

          else

               go = 1;

        

          // The next bit sets up all the variables required based on the piece moved.

        

          switch(list[y-1][1])

             {

              case 8:         //KING

                  range = 1;

                  direction = Compass;

                  start = 0;

                  tick = 1;

                  break;

             case 9:        //QUEEN

                  range = 7;

                  direction = Compass;

                  start = 0;

                  tick = 1;

                  break;

             case 10:        //BISHOP

                  range = 7;

                  direction = Compass;

                  start = 1;

                  tick = 2;

                  break;

             case 11:        //KNIGHT

                  range = 1;

                  direction = Horsey;

                  start = 0;

                  tick = 1;

                  break;

             case 12:        //ROOK

                  range = 7;

                  direction = Compass;

                  start = 0;

                  tick = 2;

                  break;

             default:        //PAWNS

                  direction = Prawns;

                  range = 1;

                  tick = 4;

                  if(go == 1)

                     if(list[y-1][2] == 1)

                        start = 0;

                     else

                        {start = 2;

                         range = 2;

                        }

                  else

                     if(list[y-1][2] == 1)

                        start = 1;

                     else

                        {start = 3;

                         range = 2;

                        }

                  break;

            }        

          // And now for the real equation.

          if(list[y-1][0] == 0)

            {for(int b = start; b < direction.length; b = b + tick)

               {exit = 0;

                for(int a = 0; a < range; a++)

                  if(list[y-1][5] + ((a+1)*(direction[b][0])) < 8)                    

                     if(list[y-1][5] + ((a+1)*(direction[b][0])) > -1)

                        if(list[y-1][4] + ((a+1)*(direction[b][1])) > -1)

                           if(list[y-1][4] + ((a+1)*(direction[b][1])) < 8)

                     {

                      if(list[y-1][6] == list[y-1][4] + ((a+1)*(direction[b][1])) && boards[y][(list[y-1][5]) + ((a+1)*(direction[b][0]))][ (list[y-1][4]) + ((a+1)*(direction[b][1]))] == Bits[go][(list[y-1][1])])

                         list[y-1][6] = 9;

                      if(list[y-1][7] == list[y-1][5] + ((a+1)*(direction[b][0])) && boards[y][(list[y-1][5]) + ((a+1)*(direction[b][0]))][ (list[y-1][4]) + ((a+1)*(direction[b][1]))] == Bits[go][(list[y-1][1])])

                         list[y-1][7] = 9;

                      if(list[y-1][6] == 9 && list[y-1][7] == 9)

                         if(boards[y][(list[y-1][5]) + ((a+1)*(direction[b][0]))][ (list[y-1][4]) + ((a+1)*(direction[b][1]))] == Bits[go][(list[y-1][1])])

                           {e = (list[y-1][5]) + ((a+1)*(direction[b][0]));

                            f = (list[y-1][4]) + ((a+1)*(direction[b][1]));

                            exit = 1;

                            break;

                           }

                      

                         if(boards[y][(list[y-1][5]) + ((a+1)*(direction[b][0]))][ (list[y-1][4]) + ((a+1)*(direction[b][1]))] != '-')

                            break;

                     }

                if(exit == 1)

                  break;                 

               }

             if(list[y-1][2] == 1) //En-passent rule...

                if(boards[y][(list[y-1][5])][(list[y-1][4])] == '-')         

                  if(go == 0)

                     boards[y][(list[y-1][5])-1][(list[y-1][4])] = '-';

                  else

                     boards[y][(list[y-1][5])+1][(list[y-1][4])] = '-';

              boards[y][(list[y-1][5])][(list[y-1][4])] = Bits[go][(list[y-1][1])];

             boards[y][e][f] = '-';

            }

          

          else

            {if(go == 0)

                if(list[y-1][0] == 1)

                  {boards[y][0][4] = '-'; boards[y][0][7] = '-'; boards[y][0][5] = 'R'; boards[y][0][6] = 'K';}

               else

                  {boards[y][0][4] = '-'; boards[y][0][0] = '-'; boards[y][0][3] = 'R'; boards[y][0][2] = 'K';}

             else

                if(list[y-1][0] == 1)

                  {boards[y][7][4] = '-'; boards[y][7][7] = '-'; boards[y][7][5] = 'r'; boards[y][7][6] = 'k';}

               else

                  {boards[y][7][4] = '-'; boards[y][7][0] = '-'; boards[y][7][3] = 'r'; boards[y][7][2] = 'k';}

            }

         }

       return boards;

      }

 

//---------------------------------------------------------------------------------

 

 

 

    public void SetLayout()

      {setFont(f1);

       northPanel = new Panel(); southPanel = new Panel();

       this.setLayout(new BorderLayout(0,0));

       add("North", northPanel);

       add("South", southPanel);

       northPanel.setLayout(aLayout); southPanel.setLayout(bLayout);

       a.fill = GridBagConstraints.BOTH; a.insets = new Insets(0,0,0,0);

       bb.fill = GridBagConstraints.BOTH; bb.insets = new Insets(0,0,0,0);

       turn = new CheckboxGroup();

 

       {

         a.gridx = 0; a.gridy = 0; a.gridheight = 1; a.gridwidth = 1;  

         games = new Choice(); aLayout.setConstraints(games,a); northPanel.add(games);

         games.addItem("--------------- Select --- Game----------------");

 

         a.gridx = 0; a.gridy = 1; a.gridheight = 1; a.gridwidth = 1;

         Panel1 = new Panel(); aLayout.setConstraints(Panel1,a); northPanel.add(Panel1);  

         event = new Label("World Championship"); Panel1.add(event);

         site = new Label("Lyon FRA"); Panel1.add(site);

         round = new Label("rd.13"); Panel1.add(round);

 

         a.gridx = 0; a.gridy = 2; a.gridheight = 1; a.gridwidth = 1;

         Panel2 = new Panel(); aLayout.setConstraints(Panel2,a); northPanel.add(Panel2);  

         turn1 = new Checkbox("",turn,true); Panel2.add(turn1);

         player1 = new Label("Garry Kasparov     "); Panel2.add(player1);   

         date = new Label("1990.11.24"); Panel2.add(date);  

         result = new Label("1/2-1/2"); Panel2.add(result);

 

         a.gridx = 0; a.gridy = 3; a.gridheight = 1; a.gridwidth = 1;

         temp = new TextArea(9,9); aLayout.setConstraints(temp,a); //northPanel.add(temp);

 

         bb.gridx = 0; bb.gridy = 0; bb.gridheight = 1; bb.gridwidth = 1;

         Panel3 = new Panel(); bLayout.setConstraints(Panel3,bb); southPanel.add(Panel3);

         turn2 = new Checkbox("",turn,true); Panel3.add(turn2);

         player2 = new Label("Anatoly Karpov     "); Panel3.add(player2);

         go = new TextField("FL   IP!"); Panel3.add(go);      

         button1 = new Button("<FLIP!>"); Panel3.add(button1); button1.addActionListener(this);

 

         bb.gridx = 0; bb.gridy = 1; bb.gridheight = 1; bb.gridwidth = 1;

         Panel4 = new Panel(); bLayout.setConstraints(Panel4,bb); southPanel.add(Panel4);

         button3 = new Button("<<<------"); Panel4.add(button3);

         button4 = new Button("<---------"); Panel4.add(button4);

         button5 = new Button("--------->"); Panel4.add(button5);

         button6 = new Button("------>>>"); Panel4.add(button6);

         button3.addActionListener(this);

         button4.addActionListener(this);

         button5.addActionListener(this);

         button6.addActionListener(this);

         bb.gridx = 0; bb.gridy = 2; bb.gridheight = 2; bb.gridwidth = 1;

         comment = new TextArea("Welcome!", 3, 40); bLayout.setConstraints(comment,bb); southPanel.add(comment);

       }

 

     

      }

public void SetUp()

      {

      String gamestring = ModOne(selectgame);

      gamedetails = ModTwo(gamestring);

      gamelength = ModThree(gamestring);

      movelist = ModFour(gamestring, gamelength);

      int[][] stats = ModFourhalf(movelist, gamelength);

      annots = ModFive(gamestring, gamelength);

      boards = ModSix(stats, gamelength);

      move = 0;

      event.setText(gamedetails[0]);

      site.setText(gamedetails[1]);

      round.setText("rd(" + gamedetails[3] + ")");

      date.setText(gamedetails[2]);

          int det4 = gamedetails[4].toCharArray().length;

           int det5 = gamedetails[5].toCharArray().length;

           if(det4 < 20) // name1

            {

             for(int dt4 = 0; dt4 < 20 - det4; dt4++)

             gamedetails[4] = gamedetails[4] + ' ';

            }

          else

            {

             gamedetails[4] = new String(gamedetails[4].toCharArray(),0,17) + "...";

            }

          if(det5 < 20) // name2

            {

             for(int dt5 = 0; dt5 < 20 - det5; dt5++)

             gamedetails[5] = gamedetails[5] + ' ';

            }

          else

            {

             gamedetails[5] = new String(gamedetails[5].toCharArray(),0,17) + "...";

            }

      player1.setText(gamedetails[5]);

      player2.setText(gamedetails[4]);

      result.setText(gamedetails[6]);

      flip = false;

      go.setText(" ");

      repaint();

      }

 

   public void calls()

      {

 

      if(flip)

         {if(move % 2 == 1)

            turn2.setState(true);

         else

            turn1.setState(true);}

      else

         {if(move % 2 == 1)

            turn1.setState(true);

         else

            turn2.setState(true);}

      repaint();

      if(move < 0)

         {move = 0;

         comment.setText("Start of Game");}

      if(move == boards.length)

         {move = boards.length -1;

         comment.setText("End of Game");}

      gamelength = checktwo;

 

      int moveno = (move + 1) / 2;

      if(move - 1 > -1)

         {String thismove = new String(movelist[move-1],0,movelist[move-1].length);

         if(move % 2 == 1)

            go.setText(moveno + ". " + thismove);

         else

            go.setText(moveno + "... " + thismove);}

      else

         go.setText(" ");

      if(annots[move-1] != null)

         {char[] annochars = annots[move-1].toCharArray();

          int q = 0;

          while(q < annochars.length)

            {q = q + 46;

             if(q < annochars.length -1)

                {while(annochars[q] != ' ')

                  q--;

                annochars[q] = '\n';}

            }

 

          String annot = new String(annochars,0,annochars.length);

          comment.setText(annot);     

         }

 

      }

 

   public void init()

      {

      GetImages();

      if(selectgame == -1)

         selectgame = FindNumber();

      int nogames = FileCheck();

      SetLayout();

      SetUp();

      SetList(nogames);

      //calls();

      }

   public void SetList(int noofgames)

      {

      for(int kk = 1; kk < noofgames+1; kk++)

         {String glance = ModOne(kk);

          String[] details = ModTwo(glance);

          int det4 = details[4].toCharArray().length;

           int det5 = details[5].toCharArray().length;

           if(det4 < 16) // name1

            {

             for(int dt4 = 0; dt4 < 16 - det4; dt4++)

             details[4] = details[4] + '.';

            }

          else

            {

             details[4] = new String(details[4].toCharArray(),0,15) + ".";

            }

          if(det5 < 16) // name2

            {

             for(int dt5 = 0; dt5 < 16 - det5; dt5++)

             details[5] = details[5] + '.';

            }

          else

            {

             details[5] = new String(details[5].toCharArray(),0,15) + ".";

            }

 

     

          String summary = kk + ". " + details[4] + "v " + details[5] + "(" + details[6] + ")";

          games.addItem(summary);}

      }

 

   public void GetImages()    {

      winScratch = createImage(this.size().width, this.size().height);

      gScratch = winScratch.getGraphics();

 

      for(int i = 0; i<26; i++)

         squarepics[i] = getImage(getCodeBase(),"pieces/" + picnames[i] + ".gif");

 

//     for(int j = 0; j<26; j++)

//        {

//        Image sod2 = getImage(getCodeBase(),"pieces2/" + picnames[j] + ".gif");

//        if(squarepics[j] != sod2)

//           squarepics[j] = getImage(getCodeBase(),"pieces3/" + picnames[j] + ".gif");

//        }

       }

 

   public boolean action(Event e, Object arg)

       {if(e.target instanceof Choice)

         {

          String title = (String) arg;

          char[] titles = title.toCharArray();

          int z = 0;

          while(titles[z] != '.')

            z++;

          String number  = new String(titles,0,z);

          selectgame = Integer.parseInt(number);       

          SetUp();

          return true;

         }

 

       if(e.target instanceof Checkbox)

         {

          if(turn1.getState())

            {if(flip)

                {if(move % 2 == 1)

                   move++;}

             else

               if(move % 2 == 0)

                  move++;

             calls();}

          if(turn2.getState())

            {if(flip)

               {if(move % 2 == 0)

                   move--;}

             else

               if(move % 2 == 1)

                  move--;

             calls();}

         }

     

       if(e.target instanceof TextField)

         {

          String moveno = go.getText();

          int y = Integer.parseInt(moveno);

          if(flip)

             move = (y * 2 - 1);

          else

            move = (y*2 - 2);

          if(flip)

            if(turn1.getState())

               move--;

          if(!flip)

            if(turn1.getState())

               move++;

          calls();

          

         }

       return false;

      }

  

 

   public void actionPerformed(ActionEvent event)

      {  

         if(event.getSource() == button1)

         {if(turn1.getState())

             turn2.setState(true);

          else

            turn1.setState(true);

          if(flip)

            {flip = false;

            player1.setText(gamedetails[5]);

            player2.setText(gamedetails[4]);}

 

          else

            {flip = true;

            player1.setText(gamedetails[4]);

            player2.setText(gamedetails[5]);}

          calls();

         }

         if(event.getSource() == button3)

         {

          move=-1;

          calls(); 

         }

         if(event.getSource() == button4)

         {

          move--;

          calls();

         }

         if(event.getSource() == button5)

         {

          move++;

          calls();

         }

         if(event.getSource() == button6)

         {

          move=boards.length;

          calls();

         }

       }

 

  

   public void paint(Graphics g)

      {

       int xPos = 50;

       int yPos = 85;

       Image rabbit = getImage(getCodeBase(),"butterfly.gif");

       int flip1 = 0, flip2 = 7, flip3 = 1, flip4 = -1, flip5 = flip1, flip6 = flip3, flip7 = flip2, flip8 = flip4;

       if(flip)

         {flip5 = flip1;

          flip6 = flip3;

         flip7 = flip2;

         flip8 = flip4;}

       else

         {flip5 = flip2;

         flip6 = flip4;

         flip7 = flip1;

         flip8 = flip3;}

       for(int c = flip5; c < 8 && c > -1; c = c + flip6)

            {for(int z = flip7; z < 8 && z > -1; z = z + flip8)

               {switch(boards[move][c][z])

                  {case '-':

                        if((c + z) % 2 == 1)

                        rabbit = squarepics[13];

                        else

                        rabbit = squarepics[0];

                        break;

                   case 'K':

                        if((c + z) % 2 == 1)

                        rabbit = squarepics[17];

                        else

                        rabbit = squarepics[16];

                        break;

                   case 'Q':

                        if((c + z) % 2 == 1)

                        rabbit = squarepics[21];

                        else

                        rabbit = squarepics[20];

                        break;

                   case 'B':

                        if((c + z) % 2 == 1)

                        rabbit = squarepics[15];

                        else

                        rabbit = squarepics[14];

                        break;

                   case 'N':

                        if((c + z) % 2 == 1)

                        rabbit = squarepics[19];

                        else

                        rabbit = squarepics[18];

                        break;

                   case 'R':

                        if((c + z) % 2 == 1)

                        rabbit = squarepics[25];

                        else

                        rabbit = squarepics[24];

                        break;

                   case 'P':

                        if((c + z) % 2 == 1)

                        rabbit = squarepics[23];

                        else

                        rabbit = squarepics[22];

                        break;

                   case 'k':

                        if((c + z) % 2 == 1)

                        rabbit = squarepics[4];

                        else

                        rabbit = squarepics[3];

                        break;

                   case 'q':

                        if((c + z) % 2 == 1)

                        rabbit = squarepics[8];

                        else

                        rabbit = squarepics[7];

                        break;

                   case 'b':

                        if((c + z) % 2 == 1)

                        rabbit = squarepics[2];

                        else

                        rabbit = squarepics[1];

                        break;

                   case 'n':

                        if((c + z) % 2 == 1)

                        rabbit = squarepics[6];

                        else

                        rabbit = squarepics[5];

                        break;

                   case 'r':

                        if((c + z) % 2 == 1)

                        rabbit = squarepics[12];

                        else

                        rabbit = squarepics[11];

                        break;

                   case 'p':

                        if((c + z) % 2 == 1)

                        rabbit = squarepics[10];

                        else

                        rabbit = squarepics[9];

                        break;                 

                   default:

                        rabbit = squarepics[0];

                        break;}

 

                gScratch.drawImage(rabbit,xPos,yPos,this);

                xPos = xPos + 32;

                }

             yPos = yPos + 32;

             xPos = 50;}

       g.drawImage(winScratch,0,0,this);

      }

 

   public final void update(Graphics g)

   {paint(g);}

 

 

   }