@@ -31,9 +31,9 @@ public static String toString(short type)
* String s = Figure.toString(BLACK,type);
* returns the String-representation of a Figure
***************************** */
public static String toString(Color color, short type)
public static String toString(BlackWhite color, short type)
{
if(color == Color.BLACK)
if(color == BlackWhite.BLACK)
return toString((short)(type+BLACK_OFFSET));
return toString(type);
}
@@ -57,119 +57,113 @@ public static ArrayList<Move> getValidMoves(Board board, short col, short row)
ArrayList<Move> moves = new ArrayList<Move>();
short figureIndex = board.getFigure(col,row);
short figure_type = typeOf(figureIndex);
Color figure_color = Figure.colorOf(figureIndex);
BlackWhite figure_color = Figure.colorOf(figureIndex);
short temp_col;
short temp_row;
if (figureIndex != 0)
{
if (figure_type == PAWN)
{
if(figure_color == Color.WHITE)
temp_row = (figure_color == BlackWhite.WHITE) ? (short)(row+1) : (short)(row-1);

if(
((figure_color == BlackWhite.WHITE && temp_row <= 7)
|| (figure_color == BlackWhite.BLACK && temp_row >= 0)
)
&& board.getFigure(col,temp_row) == 0
)
{
if(figure_color == Color.WHITE)
temp_row = (short)(row+1);
// X
// o
if(
(figure_color == BlackWhite.WHITE && temp_row ==7)
|| (figure_color == BlackWhite.BLACK && temp_row ==0)
)
{
//add moves with promotion! queen, knight, bishop, rook
moves.add(new Move(figure_color,Figure.PAWN,col,row,col,temp_row,false,Figure.BISHOP));
moves.add(new Move(figure_color,Figure.PAWN,col,row,col,temp_row,false,Figure.KNIGHT));
moves.add(new Move(figure_color,Figure.PAWN,col,row,col,temp_row,false,Figure.QUEEN));
moves.add(new Move(figure_color,Figure.PAWN,col,row,col,temp_row,false,Figure.ROOK));
}
else
temp_row = (short)(row-1);
moves.add(new Move(figure_color,Figure.PAWN,col,row,col,temp_row,false,(short)0));

// X
// |
// o
if(figure_color == BlackWhite.WHITE)
temp_row = (short)(row+2);
else
temp_row = (short)(row-2);
if(
((figure_color == Color.WHITE && temp_row <= 7)
|| (figure_color == Color.BLACK && temp_row >= 0)
(
(figure_color == BlackWhite.WHITE && row==1)
|| (figure_color == BlackWhite.BLACK && row==6)
)
&& board.getFigure(col,temp_row) == 0
&& board.getFigure(col, temp_row)==0
)
moves.add(new Move(figure_color,Figure.PAWN,col,row,col,temp_row,false,(short)0));
}
// X
// o
int[][] pos;
if(figure_color == BlackWhite.WHITE)
pos = new int[][]{{-1,1},{1,1}};
else
pos = new int[][]{{-1,-1},{1,-1}};

for(int i = 0; i<2; i++)
{
temp_col = (short)(col+pos[i][0]);
temp_row = (short)(row+pos[i][1]);
if(temp_col<0 || temp_col>=8 || temp_row <0 || temp_row >=8)
continue;

if(board.getFigure(temp_col, temp_row) != 0)
{
// X
// o
if(
(figure_color == Color.WHITE && temp_row ==7)
|| (figure_color == Color.BLACK && temp_row ==0)
)
if(isValidDestination(board,figure_color,temp_col,temp_row))
{
//add moves with promotion! queen, knight, bishop, rook
moves.add(new Move(figure_color,Figure.PAWN,col,row,col,temp_row,false,Figure.BISHOP));
moves.add(new Move(figure_color,Figure.PAWN,col,row,col,temp_row,false,Figure.KNIGHT));
moves.add(new Move(figure_color,Figure.PAWN,col,row,col,temp_row,false,Figure.QUEEN));
moves.add(new Move(figure_color,Figure.PAWN,col,row,col,temp_row,false,Figure.ROOK));
}
else
moves.add(new Move(figure_color,Figure.PAWN,col,row,col,temp_row,false,(short)0));

// X
// |
// o
if(figure_color == Color.WHITE)
temp_row = (short)(row+2);
else
temp_row = (short)(row-2);
if(
(
(figure_color == Color.WHITE && row==1)
|| (figure_color == Color.BLACK && row==6)
if(
(figure_color == BlackWhite.WHITE && temp_row ==7)
|| (figure_color == BlackWhite.BLACK && temp_row ==0)
)
&& board.getFigure(col, temp_row)==0
)
moves.add(new Move(figure_color,Figure.PAWN,col,row,col,temp_row,false,(short)0));
}
// X
// o
int[][] pos;
if(figure_color == Color.WHITE)
pos = new int[][]{{-1,1},{1,1}};
else
pos = new int[][]{{-1,-1},{1,-1}};

for(int i = 0; i<2; i++)
{
temp_col = (short)(col+pos[i][0]);
temp_row = (short)(row+pos[i][1]);
if(temp_col<0 || temp_col>=8 || temp_row <0 || temp_row >=8)
continue;

if(board.getFigure(temp_col, temp_row) != 0)
{
if(isValidDestination(board,figure_color,temp_col,temp_row))
{
if(
(figure_color == Color.WHITE && temp_row ==7)
|| (figure_color == Color.BLACK && temp_row ==0)
)
{
//add moves with promotion! queen, knight, bishop, rook
moves.add(new Move(figure_color,Figure.PAWN,col,row,temp_col,temp_row,true,Figure.BISHOP));
moves.add(new Move(figure_color,Figure.PAWN,col,row,temp_col,temp_row,true,Figure.KNIGHT));
moves.add(new Move(figure_color,Figure.PAWN,col,row,temp_col,temp_row,true,Figure.QUEEN));
moves.add(new Move(figure_color,Figure.PAWN,col,row,temp_col,temp_row,true,Figure.ROOK));
}
else
moves.add(new Move(figure_color,Figure.PAWN,col,row,temp_col,temp_row,true,(short)0));
//add moves with promotion! queen, knight, bishop, rook
moves.add(new Move(figure_color,Figure.PAWN,col,row,temp_col,temp_row,true,Figure.BISHOP));
moves.add(new Move(figure_color,Figure.PAWN,col,row,temp_col,temp_row,true,Figure.KNIGHT));
moves.add(new Move(figure_color,Figure.PAWN,col,row,temp_col,temp_row,true,Figure.QUEEN));
moves.add(new Move(figure_color,Figure.PAWN,col,row,temp_col,temp_row,true,Figure.ROOK));
}

else
moves.add(new Move(figure_color,Figure.PAWN,col,row,temp_col,temp_row,true,(short)0));
}

}
else
{
//
// *
// X
// o .
if(figure_color == BlackWhite.WHITE)
temp_row = (short)(row+2);
else
temp_row = (short)(row-2);
ArrayList<Move> history = board.getHistory();
if(history.size()>0)
{
//
// *
// X
// o .
if(figure_color == Color.WHITE)
temp_row = (short)(row+2);
else
temp_row = (short)(row-2);
ArrayList<Move> history = board.getHistory();
if(history.size()>0)
{
Move lastMove = history.get(history.size()-1);
if(
lastMove.getType() == Figure.PAWN
&& lastMove.getSourceCol() == temp_col
&& lastMove.getSourceRow() == temp_row
&& lastMove.getDestCol() == temp_col
&& lastMove.getDestRow() == row
)
moves.add(new Move(figure_color,Figure.PAWN,col,row,temp_col,temp_row,true,(short)0));
}
Move lastMove = history.get(history.size()-1);
if(
lastMove.getType() == Figure.PAWN
&& lastMove.getSourceCol() == temp_col
&& lastMove.getSourceRow() == temp_row
&& lastMove.getDestCol() == temp_col
&& lastMove.getDestRow() == row
)
moves.add(new Move(figure_color,Figure.PAWN,col,row,temp_col,temp_row,true,(short)0));
}
}
}
}
}
if (figure_type == ROOK || figure_type == QUEEN)
@@ -279,22 +273,43 @@ public static ArrayList<Move> getValidMoves(Board board, short col, short row)
}

/*** Castle ***/
if(!canBeHit(board, col,row, figure_color))
if(figure_color == BlackWhite.WHITE)
{
if(figure_color == Color.WHITE)
{
if(board.getWhiteCanCastleQueenSide() && board.getFigure(1, 0) == 0 && board.getFigure(2, 0) == 0 && board.getFigure(3, 0) == 0 && Figure.typeOf(board.getFigure(0, 0)) == Figure.ROOK)
moves.add(new Move("0-0-0",board));
else if(board.getWhiteCanCastleKingSide() && board.getFigure(5, 0) == 0 && board.getFigure(6, 0) == 0 && Figure.typeOf(board.getFigure(7, 0)) == Figure.ROOK)
moves.add(new Move("0-0",board));
}
else
{
if(board.getBlackCanCastleQueenSide() && board.getFigure(1, 7) == 0 && board.getFigure(2, 7) == 0 && board.getFigure(3, 7) == 0 && Figure.typeOf(board.getFigure(0, 7)) == Figure.ROOK)
moves.add(new Move("0-0-0",board));
else if(board.getBlackCanCastleKingSide() && board.getFigure(5, 7) == 0 && board.getFigure(6, 7) == 0 && Figure.typeOf(board.getFigure(7, 7)) == Figure.ROOK)
moves.add(new Move("0-0",board));
}
if(
board.getWhiteCanCastleQueenSide()
&& board.getFigure(1, 0) == 0 && board.getFigure(2, 0) == 0
&& board.getFigure(3, 0) == 0 && Figure.typeOf(board.getFigure(0, 0)) == Figure.ROOK
&& !canBeHit(board, 4,0, figure_color) && !canBeHit(board, 3,0, figure_color)
&& !canBeHit(board, 2,0, figure_color)
)
moves.add(new Move("0-0-0",board));
if(
board.getWhiteCanCastleKingSide()
&& board.getFigure(5, 0) == 0 && board.getFigure(6, 0) == 0
&& Figure.typeOf(board.getFigure(7, 0)) == Figure.ROOK
&& !canBeHit(board, 4,0, figure_color) && !canBeHit(board, 5,0, figure_color)
&& !canBeHit(board, 6,0, figure_color)
)
moves.add(new Move("0-0",board));
}
else
{
if(
board.getBlackCanCastleQueenSide()
&& board.getFigure(1, 7) == 0 && board.getFigure(2, 7) == 0
&& board.getFigure(3, 7) == 0 && Figure.typeOf(board.getFigure(0, 7)) == Figure.ROOK
&& !canBeHit(board, 4,7, figure_color) && !canBeHit(board, 3,7, figure_color)
&& !canBeHit(board, 2,7, figure_color)
)
moves.add(new Move("0-0-0",board));
if(
board.getBlackCanCastleKingSide()
&& board.getFigure(5, 7) == 0 && board.getFigure(6, 7) == 0
&& Figure.typeOf(board.getFigure(7, 7)) == Figure.ROOK
&& !canBeHit(board, 4,7, figure_color) && !canBeHit(board, 5,7, figure_color)
&& !canBeHit(board, 6,7, figure_color)
)
moves.add(new Move("0-0",board));
}
}
}
@@ -305,7 +320,7 @@ else if(board.getBlackCanCastleKingSide() && board.getFigure(5, 7) == 0 && board
* boolean b = canBeHit(b,col,row,c)
* returns if an enemy can hit this spot
***************************** */
private static boolean canBeHit(Board b, int col, int row, Color c)
private static boolean canBeHit(Board b, int col, int row, BlackWhite c)
{
for(int i = 0; i < 8; i++)
for(int j = 0; j<8; j++)
@@ -314,7 +329,7 @@ private static boolean canBeHit(Board b, int col, int row, Color c)
if(fig == 0)
continue;

Color color = colorOf(fig);
BlackWhite color = colorOf(fig);
if(color == c)
continue;

@@ -341,7 +356,7 @@ private static boolean canBeHit(Board b, int col, int row, Color c)
* isValidDestination(b,color,0,0)
* returns if the pot is free or occupied by an enemy
***************************** */
private static boolean isValidDestination(Board board, Color color, int col, int row)
private static boolean isValidDestination(Board board, BlackWhite color, int col, int row)
{
short fig = board.getFigure(col,row);
return fig==0 || Figure.colorOf(fig)!=color;
@@ -351,11 +366,11 @@ private static boolean isValidDestination(Board board, Color color, int col, int
* Color black = colorOf(Figure.PAWN+Figure.BLACK_OFFSET);
* returns the color of a Figure
***************************** */
public static Color colorOf(short figure)
public static BlackWhite colorOf(short figure)
{
if(figure >='A'+BLACK_OFFSET)
return Color.BLACK;
return Color.WHITE;
return BlackWhite.BLACK;
return BlackWhite.WHITE;
}

/*****************************
@@ -364,7 +379,7 @@ public static Color colorOf(short figure)
***************************** */
public static short typeOf(short figure)
{
if(colorOf(figure)==Color.BLACK)
if(colorOf(figure)==BlackWhite.BLACK)
return (short)(figure - BLACK_OFFSET);
return figure;
}
@@ -373,9 +388,9 @@ public static short typeOf(short figure)
* int seven = relativeIndex(0, Color.WHITE);
* returns the type of a Figure
***************************** */
public static int relativeIndex(int i, Color c)
public static int relativeIndex(int i, BlackWhite c)
{
if(c == Color.WHITE)
if(c == BlackWhite.WHITE)
return i;

return 7-i;
@@ -23,38 +23,39 @@ public static void runningGame(Player whitePlayer, Player blackPlayer, long seed
while (!whiteMat && !blackMat && !remis && round < MAX_ROUNDS)
{
System.out.println("******** ROUND "+round+" ********");
m = whitePlayer.chooseMove(board, Color.WHITE, MAX_TIME, random);
m = whitePlayer.chooseMove(board, BlackWhite.WHITE, MAX_TIME, random);
System.out.println("WHITE: "+m.toString());
board.executeMove(m);
System.out.println(board.toString());

if(board.isMat(Color.BLACK))

if(board.isMat(BlackWhite.BLACK))
{
blackMat = true;
break;
}

m = blackPlayer.chooseMove(board, Color.BLACK, MAX_TIME, random);
m = blackPlayer.chooseMove(board, BlackWhite.BLACK, MAX_TIME, random);
System.out.println("BLACK: "+m.toString());
board.executeMove(m);
System.out.println(board.toString());

if(board.isMat(Color.WHITE))
if(board.isMat(BlackWhite.WHITE))
{
whiteMat = true;
break;
}

if(MAX_TIME>=500)
if(round>=MAX_ROUNDS)
remis = true;

round++;
}
if (whiteMat)
System.out.println("Result: BLACK wins");
if (blackMat)
else if (blackMat)
System.out.println("Result: WHITE wins");
if (remis || round == MAX_ROUNDS)
else if (remis || round == MAX_ROUNDS)
System.out.println("Result: REMIS");
}

@@ -64,11 +65,11 @@ public static void runningGame(Player whitePlayer, Player blackPlayer, long seed
***************************** */
public static void main(String[] args)
{
long seed = (new Date()).getTime();
Player whitePlayer = new HumanPlayer();
long seed = 234923454;//(new Date()).getTime();
Player whitePlayer = new HumanPlayerGUI();
Player blackPlayer = new MyPlayer();
// Player whitePlayer = new MyPlayer();
// Player blackPlayer = new HumanPlayerGUI();
//Player whitePlayer = new MyPlayer();
//Player blackPlayer = new HumanPlayerGUI();
int MAX_TIME = 500;
runningGame(whitePlayer, blackPlayer, seed, MAX_TIME);
}
@@ -19,14 +19,14 @@ public class HumanPlayer implements Player
* Move m = chooseMove(b,color,milliseconds, random);
* chooses a Move in a given time
***************************** */
public Move chooseMove(Board board, Color color, int milliSeconds, java.util.Random random)
public Move chooseMove(Board board, BlackWhite color, int milliSeconds, java.util.Random random)
{
System.out.println(">> PLEASE INPUT A COMMAND:");
String input = scanner.next();
Move out = new Move(input,board);
return out;
}
public double getFitness(Board board, Color color) {
public double getFitness(Board board, BlackWhite color) {
return 0;
}
}
@@ -0,0 +1,39 @@
package chessProgram;

import java.util.Random;

public class HumanPlayerGUI implements Player
{
@Override
public double getFitness(Board board, BlackWhite color)
{
return 0;
}

@Override
public Move chooseMove(Board board, BlackWhite color, int milliSeconds, Random random)
{
PlayerGUI theGUI = new PlayerGUI(board, color);
theGUI.setAlwaysOnTop(true);
Thread thread = new Thread(new Runnable()
{
@Override
public void run()
{
theGUI.setBounds(10, 10, 420, 180);
theGUI.show();
}
});
thread.start();
while(theGUI.move == null)
{
try
{
Thread.sleep((int)50);
}
catch(Exception l) {};
}
theGUI.dispose();
return theGUI.move;
}
}
@@ -4,7 +4,7 @@

public class Move
{
private Color color;
private BlackWhite color;
private short type;
private int sourceCol;
private int sourceRow;
@@ -39,7 +39,7 @@ public Move(String s,Board b)
type = Figure.KING;
sourceCol = 4;
color = b.currentColor();
if(color == Color.WHITE)
if(color == BlackWhite.WHITE)
{
sourceRow = 0;
destRow = 0;
@@ -102,7 +102,7 @@ public Move(String s,Board b)
/*****************************
* Copy-Constructor
***************************** */
public Move(Color color_, short type_, short sourceCol_, short sourceRow_, short destCol_, short destRow_, boolean isHit_, short newType_)
public Move(BlackWhite color_, short type_, short sourceCol_, short sourceRow_, short destCol_, short destRow_, boolean isHit_, short newType_)
{
color = color_;
type = type_;
@@ -190,7 +190,7 @@ else if(destCol == 2)
/*****************************
* Setters
*****************************/
public void setColor(Color color_)
public void setColor(BlackWhite color_)
{
color = color_;
}
@@ -203,7 +203,7 @@ public void setHit(boolean hit)
/*****************************
* Getters
***************************** */
public Color getColor()
public BlackWhite getColor()
{
return color;
}
@@ -2,11 +2,58 @@

public class MyPlayer implements Player
{
private static short[][] pawnTable = new short[][]
{
{0, 0, 0, 0, 0, 0, 0, 0},
{50, 50, 50, 50, 50, 50, 50, 50},
{10, 10, 20, 30, 30, 20, 10, 10},
{5, 5, 10, 27, 27, 10, 5, 5},
{0, 0, 0, 25, 25, 0, 0, 0},
{5, -5,-10, 0, 0,-10, -5, 5},
{5, 10, 10,-25,-25, 10, 10, 5},
{0, 0, 0, 0, 0, 0, 0, 0}
};
private static short[][] knightTable = new short[][]
{
{-50,-40,-30,-30,-30,-30,-40,-50},
{-40,-20, 0, 0, 0, 0,-20,-40},
{-30, 0, 10, 15, 15, 10, 0,-30},
{-30, 5, 15, 20, 20, 15, 5,-30},
{-30, 0, 15, 20, 20, 15, 0,-30},
{-30, 5, 10, 15, 15, 10, 5,-30},
{-40,-20, 0, 5, 5, 0,-20,-40},
{-50,-40,-20,-30,-30,-20,-40,-50}
};

private static short[][] bishopTable = new short[][]
{
{-20,-10,-10,-10,-10,-10,-10,-20},
{-10, 0, 0, 0, 0, 0, 0,-10},
{-10, 0, 5, 10, 10, 5, 0,-10},
{-10, 5, 5, 10, 10, 5, 5,-10},
{-10, 0, 10, 10, 10, 10, 0,-10},
{-10, 10, 10, 10, 10, 10, 10,-10},
{-10, 5, 0, 0, 0, 0, 5,-10},
{-20,-10,-40,-10,-10,-40,-10,-20},
};

private static short[][] kingTable = new short[][]
{
{-30, -40, -40, -50, -50, -40, -40, -30},
{-30, -40, -40, -50, -50, -40, -40, -30},
{-30, -40, -40, -50, -50, -40, -40, -30},
{-30, -40, -40, -50, -50, -40, -40, -30},
{-20, -30, -30, -40, -40, -30, -30, -20},
{-10, -20, -20, -20, -20, -20, -20, -10},
{20, 20, 0, 0, 0, 0, 20, 20},
{20, 30, 10, 0, 0, 10, 30, 20}
};

/*****************************
* Move b = getFitness(Board b, Color color);
* returns the Fitness of a given board
***************************** */
public double getFitness(Board b, Color color)
public double getFitness(Board b, BlackWhite color)
{
//idea sum up figure qualities
//pawn:1,rook: 5,bishop=knight=3.3, Queen:9,king:10000
@@ -19,25 +66,30 @@ public double getFitness(Board b, Color color)
continue;

int c = (Figure.colorOf(fig)==color) ? 1 : -1;
int isWhite = (Figure.colorOf(fig)==BlackWhite.WHITE)?1:-1;
switch(Figure.typeOf(fig))
{
case Figure.PAWN:
sum += c*1;
sum += c*100;
sum += c*pawnTable[(8-isWhite*j)%8][(8-isWhite*i)%8];
break;
case Figure.BISHOP:
sum += c*3.3;
sum += c*325;
sum += c*bishopTable[(8-isWhite*j)%8][(8-isWhite*i)%8];
break;
case Figure.KNIGHT:
sum += c*3.3;
sum += c*320;
sum += c*knightTable[(8-isWhite*j)%8][(8-isWhite*i)%8];
break;
case Figure.QUEEN:
sum += c*9;
sum += c*975;
break;
case Figure.KING:
sum += c*10000;
sum += c*40000;
sum += c*kingTable[(8-isWhite*j)%8][(8-isWhite*i)%8];
break;
case Figure.ROOK:
sum += c*5;
sum += c*500;
break;
default:
throw(new RuntimeException("getFitness is floored!"));
@@ -51,7 +103,7 @@ public double getFitness(Board b, Color color)
* Move m = chooseMove(b,color,milliseconds, random);
* chooses a Move in a given time
***************************** */
public Move chooseMove(Board board ,Color color ,int time_limit ,java.util.Random random)
public Move chooseMove(Board board ,BlackWhite color ,int time_limit ,java.util.Random random)
{
Thinker thinker = new Thinker(board,color, this, random); //interface runnable!!!!
Thread t = new Thread(thinker);
@@ -2,6 +2,6 @@

public interface Player
{
Move chooseMove(Board b,Color color,int milliseconds, java.util.Random random);
double getFitness(Board b, Color color);
Move chooseMove(Board b,BlackWhite color,int milliseconds, java.util.Random random);
double getFitness(Board b, BlackWhite color);
}
@@ -0,0 +1,190 @@
package chessProgram;

import javax.swing.JFrame;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.net.URL;
import java.util.ArrayList;

public class PlayerGUI extends JFrame
{
private static final long serialVersionUID = 1L;
private Board board;
BlackWhite myColor;
Move move = null;

final int CELLSIZE = 72;
final int RIGHTOFFSET = 8;
final int LEFTOFFSET = 8;
final int TOPOFFSET = 31;
final int BOTTOMOFFSET = 8;

//Constructor
public PlayerGUI(Board board, BlackWhite color)
{
this.board = board;
myColor = color;
this.getContentPane().setLayout(null);
this.initWindow();
}

protected void initWindow()
{
this.setTitle("Schach");
this.addWindowListener(new WindowListener()
{

@Override
public void windowActivated(WindowEvent e) {}
@Override
public void windowClosed(WindowEvent e)
{
}
@Override
public void windowClosing(WindowEvent e) {
CloseWindow();
}
@Override
public void windowDeactivated(WindowEvent e) {}
@Override
public void windowDeiconified(WindowEvent e) {}
@Override
public void windowIconified(WindowEvent e) {}
@Override
public void windowOpened(WindowEvent e) {}
});
this.addMouseListener(new MouseListener()
{
@Override
public void mouseClicked(MouseEvent arg0) {}
@Override
public void mouseEntered(MouseEvent e) {}
@Override
public void mouseExited(MouseEvent e) {}
@Override
public void mousePressed(MouseEvent e) {}
@Override
public void mouseReleased(MouseEvent e)
{
reactOnClick(e.getX(), e.getY());
}
});
}

public void paint(Graphics g)
{
setSize(CELLSIZE*8+RIGHTOFFSET+LEFTOFFSET,CELLSIZE*8+TOPOFFSET+BOTTOMOFFSET);
int w = getWidth(), h = getHeight();
g.setColor(Color.BLACK);
g.fillRect(1, 1, w, h);
g.setColor(Color.WHITE);
for(int i = 0; i < 8; i += 2)
{
for(int j = 0; j < 8; j += 2)
{
g.fillRect(LEFTOFFSET + j * CELLSIZE, TOPOFFSET + i * CELLSIZE, CELLSIZE, CELLSIZE);
g.fillRect(LEFTOFFSET + (j + 1) * CELLSIZE, TOPOFFSET + (i + 1) * CELLSIZE, CELLSIZE, CELLSIZE);
}
}
URL u = getClass().getProtectionDomain().getCodeSource().getLocation();
String directoryname = u.getPath() + "img/";
directoryname = directoryname.substring(1);
directoryname = directoryname.replace("%20", " ");
for(int row = 0; row < 8; row++)
for(int col = 0; col < 8; col++)
{
short figureIndex = board.getFigures()[col][row];
if(figureIndex == 0)
continue;

int figureType = Figure.typeOf(figureIndex);
BlackWhite figureColor = Figure.colorOf(figureIndex);
BlackWhite cellColor;
if((row+col+1)%2==0)
cellColor = BlackWhite.WHITE;
else
cellColor = BlackWhite.BLACK;
String filename = directoryname;
switch(figureType)
{
case Figure.PAWN:
filename += "pawn";
break;
case Figure.KNIGHT:
filename += "knight";
break;
case Figure.BISHOP:
filename += "bishop";
break;
case Figure.KING:
filename += "king";
break;
case Figure.QUEEN:
filename += "queen";
break;
case Figure.ROOK:
filename += "rook";
break;
}
filename += figureColor==BlackWhite.WHITE?"_white":"_black";
filename += cellColor==BlackWhite.WHITE?"_white":"_black";
filename += ".gif";
Image pic = getToolkit().getImage(filename);
g.drawImage(pic, LEFTOFFSET + col * CELLSIZE, TOPOFFSET + (7 - row) * CELLSIZE, this);
}
}

boolean notYetClicked = true;
short sourceCol;
short sourceRow;

public void reactOnClick(int x, int y)
{
short col = (short)((x - LEFTOFFSET)/CELLSIZE);
short row = (short)(7 - (y - TOPOFFSET)/CELLSIZE);
if(notYetClicked)
{
//firstClick
short figureIndex = board.getFigures()[col][row];
if(figureIndex == 0)
return;

BlackWhite figureColor = Figure.colorOf(figureIndex);
if(figureColor != myColor)
return;

sourceCol = col;
sourceRow = row;
notYetClicked = false;
System.out.println("clicked:"+col+","+row);
}
else
{
//secondClick
short figureIndex = board.getFigures()[col][row];

ArrayList<Move> validMoves = Figure.getValidMoves(board, sourceCol, sourceRow);
short newType;
if((Figure.typeOf(board.getFigures()[sourceCol][sourceRow]) == Figure.PAWN) && (row == 0 || row == 7))
newType = Figure.QUEEN;
else
newType = (short)0;
Move temp_move = new Move(myColor, Figure.typeOf(board.getFigures()[sourceCol][sourceRow]), sourceCol, sourceRow, col, row, !(figureIndex==0), newType);
if(Move.MovesListIncludesMove(validMoves, temp_move))
move = temp_move;
else
notYetClicked = true;
}
}

public void CloseWindow()
{
System.exit(-1);
}
}
@@ -8,7 +8,7 @@ public class RandomPlayer implements Player
* Move b = getFitness(Board b, Color color);
* returns the Fitness of a given board
***************************** */
public double getFitness(Board b, Color color)
public double getFitness(Board b, BlackWhite color)
{
return 0;
}
@@ -17,7 +17,7 @@ public double getFitness(Board b, Color color)
* Move m = chooseMove(b,color,milliseconds, random);
* chooses a Move in a given time
***************************** */
public Move chooseMove(Board b,Color color,int milliseconds, java.util.Random random)
public Move chooseMove(Board b,BlackWhite color,int milliseconds, java.util.Random random)
{
ArrayList<Move> moves = b.getValidMoves(color);
//break if no move is possible (throw exeption)
@@ -51,7 +51,7 @@ public static void BoardClass()
System.out.println("Board b2 = b.cloneIncompletely();");
Board b2 = b.cloneIncompletely();
BoardDebug(b2);
System.out.println("b2.isMat(Color.BLACK)="+b2.isMat(Color.BLACK));
System.out.println("b2.isMat(Color.BLACK)="+b2.isMat(BlackWhite.BLACK));

System.out.println("END testing Board...");
}
@@ -7,14 +7,14 @@ class Thinker implements Runnable

Player player;
Board b;
Color color;
BlackWhite color;
Move best_move;
java.util.Random random;

/*****************************
* Constructor
***************************** */
Thinker(Board b_,Color color_,Player player_, java.util.Random random_)
Thinker(Board b_,BlackWhite color_,Player player_, java.util.Random random_)
{
b = b_;
color = color_;
@@ -28,25 +28,29 @@ class Thinker implements Runnable
***************************** */
public void run()
{
int level = 1;
int level = 3;//1;
while(true)
{
ArrayList<Move> moves = b.getValidMoves(color);
int n = moves.size();
Move temp_best_move = null;
ArrayList<Move> temp_best_moves = new ArrayList<Move>();
double best_fitness = 0;
for(int i = 0; i< n; i++)
{
Board temp = b.cloneIncompletely();//corrent information+previous move
temp.executeMove(moves.get(i));
double q = evaluate(temp,Board.FlipColor(color), level-1); //apponant color
if(i == 0 || q>best_fitness || (q == best_fitness && random.nextInt(2)==0))
double q = evaluate(temp,Board.FlipColor(color), level-1,0); //apponant color
if(i == 0 || q>best_fitness)
{
temp_best_move = moves.get(i);
temp_best_moves = new ArrayList<Move>();
temp_best_moves.add(moves.get(i));
best_fitness = q;
}
else if(q == best_fitness)
temp_best_moves.add(moves.get(i));
}
best_move = temp_best_move;

best_move = temp_best_moves.get(random.nextInt(temp_best_moves.size()));
System.out.println("level:"+level+", fit:"+best_fitness);
level++;
}
@@ -56,7 +60,7 @@ public void run()
* double fitness = evaluate(b, color,level);
* evaluates (recursively) the state of the board
***************************** */
private double evaluate(Board b, Color color, int level)
private double evaluate(Board b, BlackWhite color, int level, double current_min)
{
if(level==0)
{
@@ -73,10 +77,13 @@ private double evaluate(Board b, Color color, int level)
{
Board temp = b.cloneIncompletely();
temp.executeMove(moves.get(i));
double d = evaluate(temp,Board.FlipColor(color), level-1);
if(i == 0)
double d = evaluate(temp,Board.FlipColor(color), level-1,min);
if(current_min>d) //alpha-beta-search
{
min = d;
else if(d < min)
break;
}
if(d < min || i == 0)
min=d;
}
return -min;
@@ -1,37 +1,15 @@
//10h
package chessProgram;


public class main {

public static void main(String[] args)
{
try
{
/*Board b = new Board();
Player p1 = new HumanPlayer();
Player p2 = new MyPlayer();//RandomPlayer();
Move m;
int time = 6000;
System.out.println(b.toString());
for(int i = 0; true; i++)
{
System.out.println("******** TURN "+i+" ********");
m = p1.chooseMove(b, Color.WHITE, time, new Random(845823893));
System.out.println("WHITE: "+m.toString());
b.executeMove(m);
System.out.println(b.toString());
if(b.isMat(Color.BLACK))
break;
m = p2.chooseMove(b, Color.BLACK, time, new Random(845823893));
System.out.println("BLACK: "+m.toString());
b.executeMove(m);
System.out.println(b.toString());
if(b.isMat(Color.WHITE))
break;
}*/
Game.main(args);
//Testing.PlayerClass();
}
catch(Exception e)
{
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.