org/homelinux/largo/games/board/Board.java
author Markus Bröker <mbroeker@largo.homelinux.org>
Fri, 17 Dec 2010 22:33:17 +0100
changeset 14 f12f77aa13b2
parent 13 f83884cc7d2f
child 15 d4b2b9a87d80
permissions -rw-r--r--
serialVersionUID starts with 1L...
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
e0dbaef72362 svn copy of the chess engine
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
     1
/**
e0dbaef72362 svn copy of the chess engine
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
     2
 *   $Id: Board.java 148 2008-04-25 22:05:48Z mbroeker $
e0dbaef72362 svn copy of the chess engine
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
     3
 *  $URL: http://localhost/svn/eclipse/Schachspiel/trunk/org/homelinux/largo/games/board/Board.java $
e0dbaef72362 svn copy of the chess engine
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
     4
 */
e0dbaef72362 svn copy of the chess engine
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
     5
e0dbaef72362 svn copy of the chess engine
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
     6
package org.homelinux.largo.games.board;
e0dbaef72362 svn copy of the chess engine
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
     7
e0dbaef72362 svn copy of the chess engine
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
     8
import java.awt.Color;
e0dbaef72362 svn copy of the chess engine
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
     9
import java.awt.Dimension;
e0dbaef72362 svn copy of the chess engine
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
    10
import java.awt.Graphics;
e0dbaef72362 svn copy of the chess engine
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
    11
import java.awt.Image;
e0dbaef72362 svn copy of the chess engine
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
    12
import java.util.Vector;
e0dbaef72362 svn copy of the chess engine
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
    13
12
d28c1e402d82 A JPanel instead of a Canvas reduces the flickering.
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 7
diff changeset
    14
import javax.swing.JPanel;
d28c1e402d82 A JPanel instead of a Canvas reduces the flickering.
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 7
diff changeset
    15
d28c1e402d82 A JPanel instead of a Canvas reduces the flickering.
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 7
diff changeset
    16
public class Board extends JPanel {
14
f12f77aa13b2 serialVersionUID starts with 1L...
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 13
diff changeset
    17
    static final long serialVersionUID = 1L;
13
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
    18
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
    19
    protected boolean UNDO_DEBUG = false;
0
e0dbaef72362 svn copy of the chess engine
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
    20
13
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
    21
    protected Piece board_pieces[];
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
    22
    private Piece pieces[];
0
e0dbaef72362 svn copy of the chess engine
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
    23
13
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
    24
    protected Vector<History> stack;
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
    25
    protected int moveNr;
0
e0dbaef72362 svn copy of the chess engine
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
    26
13
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
    27
    protected static final int EMPTY = 0;
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
    28
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
    29
    Color color_black;
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
    30
    Color color_white;
0
e0dbaef72362 svn copy of the chess engine
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
    31
13
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
    32
    int iHeight;
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
    33
    int iWidth;
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
    34
    int xPos;
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
    35
    int yPos;
0
e0dbaef72362 svn copy of the chess engine
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
    36
13
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
    37
    boolean black;
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
    38
    boolean blacksTurn;
0
e0dbaef72362 svn copy of the chess engine
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
    39
13
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
    40
    MouseListener listener;
0
e0dbaef72362 svn copy of the chess engine
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
    41
13
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
    42
    /**
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
    43
     * The init method initializes an empty board with a history.
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
    44
     */
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
    45
    public void init() {
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
    46
        int i;
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
    47
        pieces = new Piece[64];
0
e0dbaef72362 svn copy of the chess engine
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
    48
13
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
    49
        for (i = 0; i < 64; i++) {
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
    50
            pieces[i] = new Piece(null, null, EMPTY, 0);
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
    51
        }
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
    52
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
    53
        xPos = 0;
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
    54
        yPos = 0;
0
e0dbaef72362 svn copy of the chess engine
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
    55
13
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
    56
        black = false;
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
    57
        blacksTurn = false;
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
    58
        board_pieces = getPieces(pieces);
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
    59
        stack = new Vector<History>();
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
    60
        moveNr = 0;
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
    61
    }
0
e0dbaef72362 svn copy of the chess engine
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
    62
13
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
    63
    public Board(int w, int h) {
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
    64
        iWidth = w;
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
    65
        iHeight = h;
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
    66
        color_white = new Color(255, 255, 255);
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
    67
        color_black = new Color(50, 100, 200);
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
    68
        setPreferredSize(new Dimension(8 * iWidth, 8 * iHeight));
0
e0dbaef72362 svn copy of the chess engine
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
    69
13
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
    70
        init();
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
    71
        listener = new MouseListener(iWidth, iHeight);
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
    72
        addMouseListener(listener);
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
    73
    }
0
e0dbaef72362 svn copy of the chess engine
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
    74
13
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
    75
    /**
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
    76
     * getPiece returns a Piece-Object from the Board Cache. It might not be
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
    77
     * visible on the current board.
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
    78
     */
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
    79
    public Piece getPiece(int t) {
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
    80
        return pieces[t];
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
    81
    }
0
e0dbaef72362 svn copy of the chess engine
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
    82
13
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
    83
    /**
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
    84
     * setPiece sets a Piece-Object into the Board Cache and will not be
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
    85
     * visible.
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
    86
     */
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
    87
    protected void setPiece(int t, Piece p) {
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
    88
        pieces[t] = p;
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
    89
    }
0
e0dbaef72362 svn copy of the chess engine
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
    90
13
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
    91
    protected Piece[] getPieces() {
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
    92
        return getPieces(pieces);
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
    93
    }
0
e0dbaef72362 svn copy of the chess engine
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
    94
13
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
    95
    private Piece[] getPieces(Piece[] which_pieces) {
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
    96
        Piece[] p = new Piece[64];
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
    97
        int i;
0
e0dbaef72362 svn copy of the chess engine
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
    98
13
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
    99
        for (i = 0; i < 64; i++)
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   100
            p[i] = new Piece(which_pieces[i]);
0
e0dbaef72362 svn copy of the chess engine
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
   101
13
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   102
        return p;
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   103
    }
0
e0dbaef72362 svn copy of the chess engine
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
   104
13
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   105
    protected void setPieces(Piece[] p) {
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   106
        pieces = p;
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   107
    }
0
e0dbaef72362 svn copy of the chess engine
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
   108
13
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   109
    /**
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   110
     * needed in paint(Graphics g)
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   111
     */
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   112
    private Image getBoardImage(int i) {
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   113
        return board_pieces[i].image;
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   114
    }
0
e0dbaef72362 svn copy of the chess engine
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
   115
13
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   116
    /**
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   117
     * getMouseListener returns a valid MouseListener for this Board.
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   118
     */
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   119
    public MouseListener getMouseListener() {
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   120
        return listener;
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   121
    }
0
e0dbaef72362 svn copy of the chess engine
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
   122
13
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   123
    /**
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   124
     * currentMove returns the current stack.size().
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   125
     */
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   126
    public int currentMove() {
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   127
        return stack.size();
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   128
    }
0
e0dbaef72362 svn copy of the chess engine
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
   129
13
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   130
    /**
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   131
     * returns the color of piece[i].
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   132
     */
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   133
    public String getColor(int i) {
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   134
        return pieces[i].color;
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   135
    }
0
e0dbaef72362 svn copy of the chess engine
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
   136
13
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   137
    /**
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   138
     * returns the current Value of a particular piece.
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   139
     */
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   140
    public int getValue(int i) {
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   141
        return pieces[i].value;
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   142
    }
0
e0dbaef72362 svn copy of the chess engine
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
   143
13
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   144
    /**
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   145
     * Defined Integer Constants: EMTPY = 0
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   146
     */
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   147
    public int getType(int i) {
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   148
        return pieces[i].type;
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   149
    }
0
e0dbaef72362 svn copy of the chess engine
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
   150
13
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   151
    public boolean isEmpty(int i) {
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   152
        if (pieces[i].type == EMPTY)
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   153
            return true;
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   154
        return false;
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   155
    }
0
e0dbaef72362 svn copy of the chess engine
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
   156
13
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   157
    boolean BoardisEmpty(int i) {
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   158
        if (board_pieces[i].type == EMPTY)
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   159
            return true;
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   160
        return false;
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   161
    }
0
e0dbaef72362 svn copy of the chess engine
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
   162
13
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   163
    /**
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   164
     * blacksTurn = true -> Black moves blacksTurn = false -> White moves
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   165
     */
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   166
    public boolean isBlacksTurn() {
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   167
        return blacksTurn;
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   168
    }
0
e0dbaef72362 svn copy of the chess engine
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
   169
13
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   170
    public void setBlacksTurn(boolean b) {
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   171
        blacksTurn = b;
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   172
    }
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   173
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   174
    /**
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   175
     * returns true, when piece[i] is white
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   176
     */
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   177
    public boolean isWhite(int i) {
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   178
        if (isEmpty(i))
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   179
            return false;
0
e0dbaef72362 svn copy of the chess engine
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
   180
13
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   181
        if (pieces[i].color.equals("white"))
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   182
            return true;
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   183
        return false;
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   184
    }
0
e0dbaef72362 svn copy of the chess engine
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
   185
13
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   186
    /**
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   187
     * returns true, when piece[i] is black
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   188
     */
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   189
    public boolean isBlack(int i) {
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   190
        if (isEmpty(i))
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   191
            return false;
0
e0dbaef72362 svn copy of the chess engine
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
   192
13
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   193
        if (pieces[i].color.equals("black"))
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   194
            return true;
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   195
        return false;
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   196
    }
0
e0dbaef72362 svn copy of the chess engine
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
   197
13
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   198
    /**
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   199
     * returns true, when both pieces have opposite colors
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   200
     */
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   201
    public boolean isEnemy(int t, int o) {
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   202
        if (isEmpty(t))
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   203
            return false;
0
e0dbaef72362 svn copy of the chess engine
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
   204
13
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   205
        if (!pieces[t].color.equals(pieces[o].color))
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   206
            return true;
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   207
        return false;
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   208
    }
0
e0dbaef72362 svn copy of the chess engine
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
   209
13
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   210
    /**
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   211
     * returns true, when both pieces have the same color
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   212
     */
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   213
    public boolean isSamePiece(int t, int o) {
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   214
        if (isEmpty(t))
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   215
            return false;
0
e0dbaef72362 svn copy of the chess engine
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
   216
13
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   217
        if (pieces[t].color.equals(pieces[o].color))
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   218
            return true;
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   219
        return false;
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   220
    }
0
e0dbaef72362 svn copy of the chess engine
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
   221
13
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   222
    /**
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   223
     * This function checks for the color of the piece at piece[i] and returns
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   224
     * true, if the color matches the current Turn.
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   225
     */
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   226
    public boolean validTurn(int i) {
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   227
        if (isEmpty(i))
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   228
            return false;
0
e0dbaef72362 svn copy of the chess engine
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
   229
13
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   230
        if (getColor(i).equals("white") && isBlacksTurn())
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   231
            return false;
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   232
        if (getColor(i).equals("black") && !isBlacksTurn())
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   233
            return false;
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   234
        return true;
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   235
    }
0
e0dbaef72362 svn copy of the chess engine
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
   236
13
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   237
    /**
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   238
     * simulates a "valid" move or returns false. all changes are relative to
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   239
     * pieces and will not be painted. This method sets internal variables for
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   240
     * the rochade() method, which will be overidden.
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   241
     */
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   242
    public boolean simulateMove(int t, int o) {
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   243
        if (!validMove(t, o)) {
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   244
            return false;
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   245
        }
0
e0dbaef72362 svn copy of the chess engine
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
   246
13
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   247
        push(pieces[t], t, pieces[o], o);
0
e0dbaef72362 svn copy of the chess engine
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
   248
13
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   249
        pieces[t] = new Piece(pieces[o]);
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   250
        pieces[o] = new Piece(null, null, EMPTY, 0);
0
e0dbaef72362 svn copy of the chess engine
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
   251
13
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   252
        return true;
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   253
    }
0
e0dbaef72362 svn copy of the chess engine
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
   254
13
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   255
    /**
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   256
     * Moves a Piece on the Board at Point p
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   257
     */
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   258
    public boolean move(Point p) {
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   259
        int t;
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   260
        int o;
0
e0dbaef72362 svn copy of the chess engine
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
   261
13
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   262
        t = (p.endy * 8) + p.endx;
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   263
        o = (p.starty * 8) + p.startx;
0
e0dbaef72362 svn copy of the chess engine
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
   264
13
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   265
        if (t < 0 || t > 63)
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   266
            return false;
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   267
        if (o < 0 || o > 63)
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   268
            return false;
0
e0dbaef72362 svn copy of the chess engine
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
   269
13
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   270
        if (validTurn(o))
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   271
            return doMove(t, o);
0
e0dbaef72362 svn copy of the chess engine
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
   272
13
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   273
        System.out.println("It's not your turn!");
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   274
        return false;
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   275
    }
0
e0dbaef72362 svn copy of the chess engine
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
   276
13
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   277
    /**
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   278
     * Moves one half-move backward.
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   279
     */
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   280
    public void backwards() {
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   281
        History h1;
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   282
        History h2;
0
e0dbaef72362 svn copy of the chess engine
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
   283
13
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   284
        if (moveNr < 2)
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   285
            return;
0
e0dbaef72362 svn copy of the chess engine
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
   286
13
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   287
        moveNr -= 2;
0
e0dbaef72362 svn copy of the chess engine
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
   288
13
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   289
        h1 = stack.elementAt(moveNr);
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   290
        h2 = stack.elementAt(moveNr + 1);
0
e0dbaef72362 svn copy of the chess engine
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
   291
13
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   292
        board_pieces[h1.pos] = h1.piece;
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   293
        board_pieces[h2.pos] = h2.piece;
0
e0dbaef72362 svn copy of the chess engine
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
   294
13
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   295
        repaint();
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   296
    }
0
e0dbaef72362 svn copy of the chess engine
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
   297
13
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   298
    /**
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   299
     * Moves one half-move forward.
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   300
     */
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   301
    public void forward() {
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   302
        History h1;
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   303
        History h2;
0
e0dbaef72362 svn copy of the chess engine
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
   304
13
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   305
        if (moveNr > stack.size() - 2)
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   306
            return;
0
e0dbaef72362 svn copy of the chess engine
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
   307
13
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   308
        h1 = stack.elementAt(moveNr);
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   309
        h2 = stack.elementAt(moveNr + 1);
0
e0dbaef72362 svn copy of the chess engine
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
   310
13
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   311
        board_pieces[h1.pos] = h2.piece;
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   312
        board_pieces[h2.pos] = new Piece(null, null, EMPTY, 0);
0
e0dbaef72362 svn copy of the chess engine
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
   313
13
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   314
        moveNr += 2;
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   315
        repaint();
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   316
    }
0
e0dbaef72362 svn copy of the chess engine
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
   317
13
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   318
    /**
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   319
     * pushes 2 pieces onto the stack.
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   320
     */
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   321
    public void push(Piece p1, int t, Piece p2, int o) {
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   322
        stack.add(new History(p1, t, isBlacksTurn()));
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   323
        stack.add(new History(p2, o, isBlacksTurn()));
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   324
    }
0
e0dbaef72362 svn copy of the chess engine
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
   325
13
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   326
    /**
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   327
     * pop: undo the push operation.
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   328
     */
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   329
    public void undo() {
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   330
        History h1;
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   331
        History h2;
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   332
        int size = stack.size();
0
e0dbaef72362 svn copy of the chess engine
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
   333
13
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   334
        if (size < 2)
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   335
            return;
0
e0dbaef72362 svn copy of the chess engine
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
   336
13
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   337
        h1 = stack.elementAt(size - 1);
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   338
        h2 = stack.elementAt(size - 2);
0
e0dbaef72362 svn copy of the chess engine
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
   339
13
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   340
        pieces[h1.pos] = h1.piece;
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   341
        pieces[h2.pos] = h2.piece;
0
e0dbaef72362 svn copy of the chess engine
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
   342
13
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   343
        stack.setSize(size - 2);
0
e0dbaef72362 svn copy of the chess engine
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
   344
13
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   345
        /* Reset the current player */
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   346
        setBlacksTurn(h2.turn);
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   347
    }
0
e0dbaef72362 svn copy of the chess engine
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
   348
13
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   349
    /**
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   350
     * This method must be implemented in your "GameBoard" Return value: TRUE:
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   351
     * the move IS possible Return value: FALSE: the move IS NOT possible
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   352
     */
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   353
    public boolean validMove(int t, int o) {
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   354
        System.out.println("public boolean validMove(int t, int o): Implement me");
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   355
        return false;
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   356
    }
0
e0dbaef72362 svn copy of the chess engine
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
   357
13
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   358
    /**
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   359
     * This method must be implemented in your "GameBoard" Return value: TRUE:
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   360
     * the move was performed Return value: FALSE: the move was not performed
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   361
     */
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   362
    public boolean doMove(int t, int o) {
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   363
        System.out.println("public boolean doMove(int t, int o): Implement me");
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   364
        return false;
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   365
    }
0
e0dbaef72362 svn copy of the chess engine
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
   366
13
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   367
    public void update(Graphics g) {
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   368
        paint(g);
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   369
    }
0
e0dbaef72362 svn copy of the chess engine
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
   370
13
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   371
    public void paint(Graphics g) {
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   372
        for (int i = 0; i < 8; i++) {
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   373
            for (int j = 0; j < 8; j++) {
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   374
                if (black == true) {
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   375
                    g.setColor(color_black);
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   376
                    g.fillRect(xPos, yPos, iWidth, iHeight);
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   377
                    black = false;
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   378
                } else {
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   379
                    g.setColor(color_white);
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   380
                    g.fillRect(xPos, yPos, iWidth, iHeight);
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   381
                    black = true;
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   382
                }
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   383
                if (!BoardisEmpty((i * 8) + j))
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   384
                    g.drawImage(getBoardImage((i * 8) + j), xPos + 5, yPos, this);
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   385
                xPos += iWidth;
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   386
            }
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   387
            xPos = 0;
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   388
            yPos += iHeight;
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   389
            black = !black;
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   390
        }
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   391
        xPos = 0;
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   392
        yPos = 0;
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 12
diff changeset
   393
    }
0
e0dbaef72362 svn copy of the chess engine
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
   394
}