Back to index

gcompris  8.2.2
Classes | Defines | Typedefs | Functions
chess_position.h File Reference
#include <gtk/gtk.h>
#include "chess_makros.h"
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  _PositionClass
struct  _Position

Defines

#define TYPE_POSITION   (position_get_type ())
#define POSITION(obj)   GTK_CHECK_CAST (obj, TYPE_POSITION, Position)
#define POSITION_CLASS(klass)   GTK_CHECK_CLASS_CAST (klass, TYPE_POSITION, PositionClass)
#define IS_POSITION(obj)   GTK_CHECK_TYPE (obj, TYPE_POSITION)

Typedefs

typedef struct _Position
typedef struct _PositionClass
typedef struct _PositionPrivate

Functions

GtkType position_get_type (void)
GtkObject * position_new (void)
GtkObject * position_new_initial (void)
Position * position_copy (Position *pos)
void position_set_initial (Position *pos)
void position_set_initial_partyend (Position *pos, int level)
void position_set_initial_movelearn (Position *pos, int level)
void position_move (Position *pos, Square from, Square to)
Square position_move_normalize (Position *pos, Square from, Square to)
void position_move_reverse_white (Position *pos, Square from, Square to)
void position_move_reverse_black (Position *pos, Square from, Square to)
void position_move_reverse (Position *pos, Square from, Square to)
gint position_move_generator (Position *pos, Square **index0, gshort *anz_s, gshort *anz_n)
gboolean position_white_king_attack (Position *pos)
gboolean position_black_king_attack (Position *pos)
gshort position_legal_move (Position *pos, Square **zl, gshort *as, gshort *an)
void position_set_white_king (Position *pos, Square square)
void position_set_black_king (Position *pos, Square square)
void position_set_color_to_move (Position *pos, short color)
short position_get_color_to_move (Position *pos)
short position_last_piece_captured (Position *pos)
void position_display (Position *pos)

Class Documentation

struct _PositionClass

Definition at line 45 of file chess_position.h.

Class Members
GtkObjectClass parent_class
struct _Position

Definition at line 49 of file chess_position.h.

Class Members
GtkObject object
PositionPrivate * priv
Piece square

Define Documentation

#define IS_POSITION (   obj)    GTK_CHECK_TYPE (obj, TYPE_POSITION)

Definition at line 39 of file chess_position.h.

#define POSITION (   obj)    GTK_CHECK_CAST (obj, TYPE_POSITION, Position)

Definition at line 37 of file chess_position.h.

#define POSITION_CLASS (   klass)    GTK_CHECK_CLASS_CAST (klass, TYPE_POSITION, PositionClass)

Definition at line 38 of file chess_position.h.

#define TYPE_POSITION   (position_get_type ())

Definition at line 36 of file chess_position.h.


Typedef Documentation

typedef struct _Position

Definition at line 41 of file chess_position.h.

typedef struct _PositionClass

Definition at line 42 of file chess_position.h.

typedef struct _PositionPrivate

Definition at line 43 of file chess_position.h.


Function Documentation

gboolean position_black_king_attack ( Position *  pos)

Definition at line 1606 of file chess_position.c.

{
       Square k = pos->priv->bk_square;
       int ret;

       g_return_val_if_fail (pos != NULL, 0);
       g_return_val_if_fail (IS_POSITION (pos), 0);

       ret = long4 (pos, k, 9, 11, -9, -11, WQ, WB);
       if (ret)
              return ret;

       ret = long4 (pos, k, 1, 10, -10, -1, WQ, WR);
       if (ret)
              return ret;

       if (short8 (pos, k, 8, 12, 19, 21, -8, -12, -19, -21, WN))
              return WN;
       if (short8 (pos, k, 9, 11, -9, -11, 1, 10, -10, -1, WK))
              return WK;

       if (pos->square[k+UL] == WP)
              return WP;
       if (pos->square[k+UR] == WP)
              return WP;

       return FALSE;
}

Here is the call graph for this function:

Here is the caller graph for this function:

Position* position_copy ( Position *  pos)

Definition at line 125 of file chess_position.c.

{
       Position *cpPos;

       cpPos = POSITION (position_new ());

       memcpy (cpPos->priv, pos->priv, sizeof (PositionPrivate));
       memcpy (cpPos->square, pos->square, sizeof (gchar) * 120);

       return cpPos;
}

Here is the call graph for this function:

void position_display ( Position *  pos)

Definition at line 383 of file chess_position.c.

{
  register Square square;
  register gshort rank;

  for (rank = 8; rank >= 1; rank--) {
    for (square = A1 + ((rank - 1) * 10);
        square <= H1 + ((rank - 1) * 10);
        square++) {

           g_warning("%c ", piece_to_ascii(pos->square [square]));
    }
    g_warning("\n");
  }
}

Here is the call graph for this function:

Here is the caller graph for this function:

short position_get_color_to_move ( Position *  pos)

Definition at line 1704 of file chess_position.c.

{
       g_return_val_if_fail (pos != NULL, NONE);
       g_return_val_if_fail (IS_POSITION (pos), NONE);

       return pos->priv->tomove;
}

Here is the caller graph for this function:

GtkType position_get_type ( void  )

Definition at line 57 of file chess_position.c.

{
       static guint position_type = 0;

       if (!position_type) {
              GtkTypeInfo position_info = {
                     "Position",
                     sizeof (Position),
                     sizeof (PositionClass),
                     (GtkClassInitFunc) class_init,
                     (GtkObjectInitFunc) init,
                     (gpointer) NULL,
                     (gpointer) NULL,
                     (GtkClassInitFunc) NULL
              };
              position_type = gtk_type_unique (gtk_object_get_type (),
                                           &position_info);
       }

       return position_type;
}

Here is the call graph for this function:

Here is the caller graph for this function:

short position_last_piece_captured ( Position *  pos)

Definition at line 1695 of file chess_position.c.

{
       g_return_val_if_fail (pos != NULL, EMPTY);
       g_return_val_if_fail (IS_POSITION (pos), EMPTY);

       return pos->priv->captured;
}

Here is the caller graph for this function:

gshort position_legal_move ( Position *  pos,
Square **  zl,
gshort *  as,
gshort *  an 
)

Definition at line 1636 of file chess_position.c.

{
       Position temp;
       Square tomove = pos->priv->tomove;
       gboolean check = FALSE;
       gshort gen_moves, a;
       Square *ap, *ap2;
       gshort anz_s, anz_n, legal_moves;
       Square zugl[AB_ZUGL];

       g_return_val_if_fail (pos != NULL, 0);
       g_return_val_if_fail (IS_POSITION (pos), 0);

        ap =  zugl + AB_ZUG_S;
        gen_moves = position_move_generator (pos, &ap, &anz_s, &anz_n);
        legal_moves = 0;
        ap2 = *zl;

        for (a = 0; a < gen_moves; a++) {
                temp = *pos;
                position_move (pos, *ap, *(ap+1));

                switch (tomove) {
                        case WHITE:
                            check = position_white_king_attack (pos);
                            break;
                        case BLACK:
                            check = position_black_king_attack (pos);
                            break;
                        default:
                            g_assert_not_reached ();
                }

                if (!check) {
                        *ap2++ = *ap++;
                        *ap2++ = *ap++;
                        legal_moves++;
                } else {
                      ap += 2;
              }
                switch (tomove) {
                        case WHITE:
                                position_move_reverse_white (pos, *(ap - 2),
                                                      *(ap - 1));
                                break;
                        case BLACK:
                                position_move_reverse_black (pos, *(ap - 2),
                                                      *(ap - 1));
                                break;
                }
                *pos = temp;
        }
        *as = legal_moves;
       *an = 0;

        return legal_moves;
}

Here is the call graph for this function:

Here is the caller graph for this function:

void position_move ( Position *  pos,
Square  from,
Square  to 
)

Definition at line 612 of file chess_position.c.

{
       switch (pos->priv->tomove) {
              case WHITE:
                     position_move_white (pos, from, to);
                     pos->priv->tomove = BLACK;
                     break;
              case BLACK:
                     position_move_black (pos, from, to);
                     pos->priv->tomove = WHITE;
                     break;
              default:
                     g_assert_not_reached ();
       }
}

Here is the call graph for this function:

Here is the caller graph for this function:

gint position_move_generator ( Position *  pos,
Square **  index0,
gshort *  anz_s,
gshort *  anz_n 
)

Definition at line 1518 of file chess_position.c.

{

       if (pos->priv->tomove == WHITE )
              return position_move_generator_white (pos, index0, anz_s, anz_n);
       else if (pos->priv->tomove == BLACK )
              return position_move_generator_black (pos, index0, anz_s, anz_n);
       else
              abort();
}

Here is the call graph for this function:

Here is the caller graph for this function:

Square position_move_normalize ( Position *  pos,
Square  from,
Square  to 
)

Definition at line 679 of file chess_position.c.

{
       gshort legal_moves, anz_n, anz_s;
       Square movelist [AB_ZUGL];
       Square *ap, *aq, ret;
       gshort i;

       ap =  movelist + AB_ZUG_S;
       legal_moves = position_legal_move (pos, &ap, &anz_s, &anz_n);

       for (aq = ap, i = 0; i < legal_moves; i++, aq += 2 ) {
              if (from == *aq) {
                     if (to == *(aq + 1))
                            return to;
                     else if (*(aq + 1) & 128) {
                                /* Promotion */
                            ret = position_move_normalize_promotion (
                                   pos, to, *(aq + 1), *(aq + 3),
                                   *(aq + 5), *(aq + 7));
                            if (ret)
                                   return ret;
                            aq += 6; /* skip 3 moves */
                     }
              }
       }

       return FALSE;
}

Here is the call graph for this function:

Here is the caller graph for this function:

void position_move_reverse ( Position *  pos,
Square  from,
Square  to 
)

Definition at line 894 of file chess_position.c.

{
       switch (pos->priv->tomove) {
              case WHITE:
                     position_move_reverse_black (pos, from, to);
                     break;
              case BLACK:
                     position_move_reverse_white (pos, from, to);
                     break;
              default:
                     g_assert_not_reached ();
       }
}

Here is the call graph for this function:

void position_move_reverse_black ( Position *  pos,
Square  from,
Square  to 
)

Definition at line 828 of file chess_position.c.

{
       int fi;

       pos->priv->tomove = BLACK;      /* change tomove */

       if (to & 128) {
                /* Promotion */
              position_move_reverse_promotion_black (pos, from, to);
              return;
       }

        fi = pos->square[to];

        /* look for castling */
        if (fi == BK) {
              pos->priv->bk_square = from;
              pos->priv->br_a_move -= 1;
              pos->priv->br_h_move -= 1;

              if (from != E8)  {  /*    no castling   */
                            pos->square[from]  = fi;
                            pos->square[to] = pos->priv->captured;
                            return;
              }
              if (abs (from - to) != 2)  {  /*    no castling  */
                            pos->square[from]  = fi;
                            pos->square[to] = pos->priv->captured;
                            return;
              }

              if (to == G8) {
                     position_move_reverse_castling_black_short (pos);
                     return;
              } else if (to == C8) {
                     position_move_reverse_castling_black_long (pos);
                     return;
              }
              abort ();
       }

       if (fi == BR && from == A8)
              pos->priv->br_a_move -= 1;
       if (fi == BR && from == H8)
              pos->priv->br_h_move -= 1;

        if (fi == BP) {
              if ((from - to) != 10) {
                     if ((from - to) !=20) {
                            if (pos->priv->captured == EMPTY) {
                                   pos->square[to+10] = WP;
                                   pos->square[to] = EMPTY;
                                   pos->square[from] = BP;
                                    return;
                            }
                     }
              }
              pos->square [from] = fi;
              pos->square [to]= pos->priv->captured;
              return;
       }
       pos->square [from]  = fi;
        pos->square [to] = pos->priv->captured;
}

Here is the call graph for this function:

Here is the caller graph for this function:

void position_move_reverse_white ( Position *  pos,
Square  from,
Square  to 
)

Definition at line 762 of file chess_position.c.

{
       Piece fi;

       pos->priv->tomove = WHITE; /* change tomove */

       if (to & 128) {
                /* Promotion */
              position_move_reverse_promotion_white (pos, from, to);
              return;
       }

        fi = pos->square[to];

        /*    look for castling   */
        if (fi == WK) {
              pos->priv->wk_square = from;
              pos->priv->wr_a_move -= 1;
              pos->priv->wr_h_move -= 1;

              if (from != E1)  {  /*    no castling   */
                            pos->square[from]  = fi;
                            pos->square[to] = pos->priv->captured;
                            return;
              }
              if (abs (from - to) != 2)  {  /*  no castling  */
                            pos->square[from]  = fi;
                            pos->square[to] = pos->priv->captured;
                            return;
              }

              if (to == G1) {
                     position_move_reverse_castling_white_short (pos);
                     return;
              } else if (to == C1) {
                     position_move_reverse_castling_white_long (pos);
                     return;
              }
              abort();
       }

       if (fi == WR && from == A1)
              pos->priv->wr_a_move -= 1;
       if (fi == WR && from == H1)
              pos->priv->wr_h_move -= 1;

        if (fi == WP) {
              if ((to - from) != 10) {
                     if ((to - from) != 20) {
                            if (pos->priv->captured == EMPTY) {
                                   pos->square [to - 10] = BP;
                                   pos->square [to] = EMPTY;
                                   pos->square [from] = WP;
                                    return;
                            }
                     }
              }
              pos->square [from] = fi;
              pos->square [to] = pos->priv->captured;
              return;
       }
        pos->square [from]  = fi;
        pos->square [to] = pos->priv->captured;
}

Here is the call graph for this function:

Here is the caller graph for this function:

GtkObject* position_new ( void  )

Definition at line 108 of file chess_position.c.

{
       return GTK_OBJECT (gtk_type_new (position_get_type ()));
}

Here is the call graph for this function:

Here is the caller graph for this function:

GtkObject* position_new_initial ( void  )

Definition at line 114 of file chess_position.c.

{
       Position *pos;

       pos = POSITION (position_new ());
       position_set_initial (pos);

       return GTK_OBJECT (pos);
}

Here is the call graph for this function:

Here is the caller graph for this function:

void position_set_black_king ( Position *  pos,
Square  square 
)

Definition at line 1722 of file chess_position.c.

{
       g_return_if_fail (pos != NULL);
       g_return_if_fail (IS_POSITION (pos));

       pos->priv->bk_square = square;
}
void position_set_color_to_move ( Position *  pos,
short  color 
)

Definition at line 1731 of file chess_position.c.

{
       g_return_if_fail (pos != NULL);
       g_return_if_fail (IS_POSITION (pos));

       pos->priv->tomove = color;
}

Here is the call graph for this function:

Here is the caller graph for this function:

void position_set_initial ( Position *  pos)

Definition at line 166 of file chess_position.c.

{
       unsigned int a;

       /* The white pieces */
       pos->square [A1] = WR;
       pos->square [B1] = WN;
       pos->square [C1] = WB;
       pos->square [D1] = WQ;
       pos->square [E1] = WK;
       pos->square [F1] = WB;
       pos->square [G1] = WN;
       pos->square [H1] = WR;

       /* The black pieces */
       pos->square [A8] = BR;
       pos->square [B8] = BN;
       pos->square [C8] = BB;
       pos->square [D8] = BQ;
       pos->square [E8] = BK;
       pos->square [F8] = BB;
       pos->square [G8] = BN;
       pos->square [H8] = BR;

       /* Pawns on the 2nd and 7th ranks */
       for (a = A2; a <= H2 ;a++)
              pos->square [a] = WP;
       for (a = A7; a <= H7 ;a++)
              pos->square [a] = BP;

       /* The rest is blank */
       for (a = A3; a <= H3 ;a++)
              pos->square [a] = EMPTY;
       for (a = A4; a <= H4 ;a++)
              pos->square [a] = EMPTY;
       for (a = A5; a <= H5 ;a++)
              pos->square [a] = EMPTY;
       for (a = A6; a <= H6 ;a++)
              pos->square [a] = EMPTY;

       /* White to move, white king on E1 and black king on E8 */
       pos->priv->wr_a_move = 0;
       pos->priv->wr_h_move = 0;
       pos->priv->wk_square = E1;
       pos->priv->br_a_move = 0;
       pos->priv->br_h_move = 0;
       pos->priv->bk_square = E8;
       pos->priv->captured = EMPTY;
       pos->priv->tomove = WHITE;
}

Here is the caller graph for this function:

void position_set_initial_movelearn ( Position *  pos,
int  level 
)

Definition at line 284 of file chess_position.c.

{
       register Square square;
       register gshort rank;

       for (rank = 1; rank <= 8; rank++) {
              for (square = A1 + ((rank - 1) * 10);
                   square <= H1 + ((rank - 1) * 10);
                   square++) {
              pos->square [square] = EMPTY;
              }
       }

       switch(level) {
       case 1:
       default:
              /* The white pieces */
              pos->square [E1] = WK;
              pos->square [B4] = WR;

              /* The black pieces */
              pos->square [A1] = BK;
              pos->square [E7] = BP;

              /* The kings */
              pos->priv->wk_square = E1;
              pos->priv->bk_square = A1;
              break;
       case 2:
              /* The white pieces */
              pos->square [E1] = WK;
              pos->square [G1] = WQ;

              /* The black pieces */
              pos->square [A1] = BK;
              pos->square [E7] = BP;

              /* The kings */
              pos->priv->wk_square = E1;
              pos->priv->bk_square = A1;
              break;
       case 3:
              /* The white pieces */
              pos->square [E1] = WK;
              pos->square [C4] = WB;
              pos->square [C5] = WB;

              /* The black pieces */
              pos->square [A1] = BK;
              pos->square [E7] = BP;

              /* The kings */
              pos->priv->wk_square = E1;
              pos->priv->bk_square = A1;
              break;
       case 4:
              /* The white pieces */
              pos->square [E1] = WK;
              pos->square [B4] = WN;
              pos->square [B5] = WN;

              /* The black pieces */
              pos->square [A1] = BK;
              pos->square [E7] = BP;

              /* The kings */
              pos->priv->wk_square = E1;
              pos->priv->bk_square = A1;
              break;
       case 5:
              /* The white pieces */
              pos->square [E1] = WK;
              pos->square [D2] = WP;
              pos->square [E2] = WP;
              pos->square [F2] = WP;
              pos->square [G2] = WP;
              pos->square [H2] = WP;

              /* The black pieces */
              pos->square [A1] = BK;
              pos->square [E7] = BP;

              /* The kings */
              pos->priv->wk_square = E1;
              pos->priv->bk_square = A1;
              break;
       }

       /* White to move */
       pos->priv->wr_a_move = 0;
       pos->priv->wr_h_move = 0;
       pos->priv->br_a_move = 0;
       pos->priv->br_h_move = 0;
       pos->priv->captured = EMPTY;
       pos->priv->tomove = WHITE;
}

Here is the caller graph for this function:

void position_set_initial_partyend ( Position *  pos,
int  level 
)

Definition at line 218 of file chess_position.c.

{
       register Square square;
       register gshort rank;

       for (rank = 1; rank <= 8; rank++) {
              for (square = A1 + ((rank - 1) * 10);
                   square <= H1 + ((rank - 1) * 10);
                   square++) {
              pos->square [square] = EMPTY;
              }
       }

       switch(level) {
       case 1:
       default:
              /* The white pieces */
              pos->square [A1] = WK;
              pos->square [G1] = WQ;
              pos->square [F1] = WQ;

              /* The black pieces */
              pos->square [E8] = BK;

              /* The kings */
              pos->priv->wk_square = A1;
              pos->priv->bk_square = E8;
              break;
       case 2:
              /* The white pieces */
              pos->square [E1] = WK;
              pos->square [F1] = WR;
              pos->square [G1] = WR;

              /* The black pieces */
              pos->square [A8] = BK;

              /* The kings */
              pos->priv->wk_square = E1;
              pos->priv->bk_square = A8;
              break;
       case 3:
              /* The white pieces */
              pos->square [E1] = WK;
              pos->square [B4] = WR;
              pos->square [B5] = WB;

              /* The black pieces */
              pos->square [A1] = BK;

              /* The kings */
              pos->priv->wk_square = E1;
              pos->priv->bk_square = A1;
              break;
       }

       /* White to move */
       pos->priv->wr_a_move = 0;
       pos->priv->wr_h_move = 0;
       pos->priv->br_a_move = 0;
       pos->priv->br_h_move = 0;
       pos->priv->captured = EMPTY;
       pos->priv->tomove = WHITE;
}

Here is the caller graph for this function:

void position_set_white_king ( Position *  pos,
Square  square 
)

Definition at line 1713 of file chess_position.c.

{
       g_return_if_fail (pos != NULL);
       g_return_if_fail (IS_POSITION (pos));

       pos->priv->wk_square = square;
}
gboolean position_white_king_attack ( Position *  pos)

Definition at line 1576 of file chess_position.c.

{
       Square k = pos->priv->wk_square;
       int ret;

       g_return_val_if_fail (pos != NULL, 0);
       g_return_val_if_fail (IS_POSITION (pos), 0);

       ret = long4 (pos, k, 9, 11, -9, -11, BQ, BB);
       if (ret)
              return ret;

       ret = long4 (pos, k, 1, 10, -10, -1, BQ, BR);
       if (ret)
              return ret;

       if (short8 (pos, k, 8, 12, 19, 21, -8, -12, -19, -21, BN))
              return BN;
       if (short8 (pos, k, 9, 11, -9, -11, 1, 10, -10, -1, BK))
              return BK;

       if (pos->square[k+OL] == BP)
              return BP;
       if (pos->square[k+OR] == BP)
              return BP;

       return FALSE;
}

Here is the call graph for this function:

Here is the caller graph for this function: