Next: Class Main Up: Sather 1.0 Tutorial Previous: About Sather 1.0

Sather Tutorial Chess

Sather Tutorial Chess is not an expert chess program. In fact, it is quite easy to win against the computer. Moreover, the implementation is very inefficient in certain parts of the code. The idea is to simply provide a context for demonstrating and explaining various features of Sather and not to show a world class chess program.

To make the best use of this tutorial, the Sather 1.0 system should be properly installed and the following files should be available online:

hello.sa
This file contains is the standard Hello World program. It does not belong to Sather Tutorial Chess but is included as an initial exercise.

Makefile
This is the Makefile for Sather Tutorial Chess.
SChess.sa
This is the main Sather file.
XInterf.sa
This is an additional Sather file. Although the code could have been in SChess.sa, it is kept in a separate file for explanatory reasons.
DefaultA.sa
If your system is not running the X11 window system, this file is used for compilation and linking.
DefaultX.sa
Otherwise, this file is used instead.
XCW.c
This C file provides the interface to the X11 window system. If you do not use X11, the Makefile will detect this and generate an executable that does not depend on or use XCW.c.
bitmaps
This directory has bitmaps for all the chess pieces which are used in XCW.c.

Hello World Program

The file hello.sa is the standard Hello World program. Sather programs usually have file names with the extension .sa. To compile it, simply enter cs hello.sa. The command for invoking the compiler is easy to remember, since cs stands for ``Compile Sather". After successful compilation you can execute it by entering a.out. If the current directory is not in your search path, enter ./a.out.

Only proceed after having successfully compiled and executed the Hello World program. If something went wrong, check your installation of the Sather 1.0 system. The file Doc/Installation might be helpful for diagnosing problems.


-- This is the standard Hello World program
-- implemented in Sather 1.0
class MAIN is
  main is
    #OUT + "Hello World\n";
  end;
end;

The first two lines of the file are comments. Comments start with two minus signs. The comment cannot be explicitly closed, they end at the end of the line. The class MAIN has a special purpose in Sather. Unless altered by compiler flags, the routine main of MAIN is started when a compiled Sather program is invoked by the user. In main there is only one statement. This statement is responsible for several things: At first #OUT creates a new object of class OUT. Class OUT is a basic class provided by Sather. In the implementation of class OUT which can be found in the library file Library/out.sa there are several routines that can be invoked on an object of that class. One of these routines has the signature


                plus(s:STR);
Make sure that you look at the library file Library/out.sa and find the routine used in the Hello World program. It is necessary for using the Sather 1.0 system that you are familiar with the libraries and the routines provided by them. The routine plus takes one string argument and ``adds" this argument to the object before returning the modified object. In line 5 of the program the routine plus is called implicitly, by the operator + which itself is syntactic sugar for the call of plus.

In Sather 1.0 a string is enclosed in double quotes ("). Similar to C, \n stands for the carriage return/line feed.

Getting Started

The other files mentioned above are needed for Sather Tutorial Chess. They could be derived from this document by extracting and concatenating the code segments explained in the remainder. Unless otherwise noted, the code segments go to the file SChess.sa.

For the presentation, code segments are numbered on the right of the code. Numbering is restarted with line 1 either when a new Sather code file is started or with the beginning of a new section.

You can create an executable Sather Tutorial Chess program by invoking the compiler. This is done by staring the execution of the Makefile:


              make

The Makefile finds out whether your system runs then X Windows. Depending on the result, the appropriate Sather code files are compiled and linked together. The executable is called


              SChess

After invoking Sather Tutorial Chess, you are the white player. The computer is responsible for the moves of black. Later, in section 3.2 we will show how this default behavior can be changed.

Class Hierarchy of Sather Tutorial Chess

Let us first discuss the basic design decisions that led to our implementation of Sather Tutorial Chess. The central object is the board. The board knows about its state, which is - roughly speaking - the set of pieces, and is capable of applying moves to itself. Moves and pieces are other types of objects. A ``moves" knows about the piece that is moved and knows both the starting and the final position of the move. Pieces and moves use position objects to represent the position on the board.

Besides those objects that are used for representing and handling the chess game, there are several helper objects that are necessary for interfacing with the user. For both players there is a player object. This player objects hides the origin of a move from the chess engine. The player object is asked to return a move. This call is either forwarded to the user or to the searching strategy of the computer player. Hence, the same chess engine can be used for all four possible pairings of human and automatic players.

Another object is used for handling the display of the chess board. If required, this interface can ask the user to enter a move in standard chess notation. The implementation provides both a plain ASCII interface and an interface to the X Window system.

The description will start with the class MAIN which contains the basic loop of the game. In section 4 we discuss the display objects. After that, section 5 deals with the players. Then the other classes are presented in the following order: move in section 6, position in section 7, board in section 8 and finally pieces in section 9.



Next: Class Main Up: Sather 1.0 Tutorial Previous: About Sather 1.0


gomes@ICSI.Berkeley.EDU
Tue May 30 21:15:13 PDT 1995