© Corel The Practice


The sample code of this part is published under the terms of the GNU Publi license and is Open Source Software. Please read the terms of the GPL license.


First Work

Before we start to program the user interface, we first have to do some work. Because Java is an object oriented language, we have to divide our program into classes. We uses the following two classes:

ChessPartner
This class is a child of java.applet.Applet. It is the border application. Here we build the graphic user interface. Our applet will have only the button start new game.
Board
This class is a child of java.awt.Canvas. It contains the interface and the AI of our chess program. The class implements the interface Runnable because the calculations for the AI runs in his own thread.
Let's have a look on the Applet, ChessPartner:

import java.applet.*;
import java.awt.*;

/*  ChessPartner Sample Class v 1.0
    Copyright (C) 2000 Leander Eyer
    date: 10.07.2000

    This program is free software; you can redistribute
    it under the terms of the GNU Public License as published
    by the Free Software Foundation */


public class ChessPartner extends Applet {
   //The reference of the AI
   Board brain;

   //The elements of the GUI
   Button newgame = new Button ("start new game");

  //The eventhandlers
  public boolean action (Event evt, Object arg) {
     if ( ((String) arg).equals ("Neues Spiel starten"))
        brain.newgame ();
     return true;
  }

  //Initialisation of the Applet
  public void init() {
     super.init();

     //Initialize the AI
     brain = new Board (this);

     //built the GUI
     setBackground (Color.lightGray);
     setLayout (new BorderLayout (10,10));
     add ("Center", brain);
     
add ("South", newgame);
  }

  //This method makes a border with a width of 10 pixels
  public Insets insets () {
     return new Insets (10,10,10,10);
  }
}

Because we use only standart programming technices like the building of a GUI or the implementation of event handlers everyone should understand that code without further explanations. We finished our work with the class ChessPartner, during this chapter we will only change the class Board.

The basic version of the class Board follows:

import java.applet.Applet;
import java.awt.*;

/*  Board Sample Class v 0.1
    Copyright (C) 2000 Leander Eyer
    date: 10.07.2000

    This program is free software; you can redistribute
    it under the terms of the GNU Public License as published
    by the Free Software Foundation */


public class Board extends java.awt.Canvas implements java.awt.event.MouseListener, java.awt.event.MouseMotionListener, java.lang.Runnable {

   //The intern representation of the chess board
   int [] board = new int [120];

   public Board (java.applet.Applet ref) {
      super();
   }

   //The event handlers for the mouse
   public void mouseClicked(java.awt.event.MouseEvent e) { }
   public void mouseDragged(java.awt.event.MouseEvent e) { }
   public void mouseEntered(java.awt.event.MouseEvent e) { }
   public void mouseExited(java.awt.event.MouseEvent e) { }
   public void mouseMoved(java.awt.event.MouseEvent e) { }
   public void mousePressed(java.awt.event.MouseEvent e) { }
   public void mouseReleased(java.awt.event.MouseEvent e) { }

   //Prepare the AI for a new game
   public void newgame () {
      return;
   }

   //Here we paint the chess board
   public void paint (Graphics g) {
      g.setColor (Color.black);
      g.fillRect (0,0, 320,320);
   }

   //the AI thread
   public void run() { }
}

There may be one thing not clear in this class: Why do we use an array with 120 elements for the internal representation of the chessboard while a normal chess board has only 64 fields?

You will find the exact answer in the part "Implement the chess rules", here we give only a small tip: We make a border arount the chessboard. We add two rows on top and on bottom and one line left and right. So its easier to generate the chess moves.

Show applet step one
Source Code of step 1


The theory | The interface | The AI