import java.awt.*; import java.awt.event.*; import java.awt.image.*; import java.net.*; import java.applet.*; import java.lang.Math; public class chess extends Applet implements MouseListener { Image imagePieces[]; static final int Black = 0; static final int White = 1; static final int Empty = -1; static final int PawnB = 0; static final int RookeB = 1; static final int KnightB = 2; static final int BishopB = 3; static final int QueenB = 4; static final int KingB = 5; static final int PawnW = 6; static final int RookeW = 7; static final int KnightW = 8; static final int BishopW = 9; static final int QueenW = 10; static final int KingW = 11; int anBoard[][]; int nActivePiece[] = { -1, -1 }; int nPlayer = White; public void init() { imagePieces = new Image[12]; imagePieces[ PawnB ] = getImage( getCodeBase(), "images/pawnb.gif" ); imagePieces[ RookeB ] = getImage( getCodeBase(), "images/rookeb.gif" ); imagePieces[ KnightB ] = getImage( getCodeBase(), "images/knightb.gif" ); imagePieces[ BishopB ] = getImage( getCodeBase(), "images/bishopb.gif" ); imagePieces[ QueenB ] = getImage( getCodeBase(), "images/queenb.gif" ); imagePieces[ KingB ] = getImage( getCodeBase(), "images/kingb.gif" ); imagePieces[ PawnW ] = getImage( getCodeBase(), "images/pawnw.gif" ); imagePieces[ RookeW ] = getImage( getCodeBase(), "images/rookew.gif" ); imagePieces[ KnightW ] = getImage( getCodeBase(), "images/knightw.gif" ); imagePieces[ BishopW ] = getImage( getCodeBase(), "images/bishopw.gif" ); imagePieces[ QueenW ] = getImage( getCodeBase(), "images/queenw.gif" ); imagePieces[ KingW ] = getImage( getCodeBase(), "images/kingw.gif" ); // Clear the board anBoard = new int[8][8]; for ( int i = 0; i < 8; ++i ) for ( int j = 0; j < 8; ++j ) anBoard[i][j] = Empty; anBoard[0][0] = RookeB; anBoard[0][1] = KnightB; anBoard[0][2] = BishopB; anBoard[0][3] = QueenB; anBoard[0][4] = KingB; anBoard[0][5] = BishopB; anBoard[0][6] = KnightB; anBoard[0][7] = RookeB; anBoard[1][0] = PawnB; anBoard[1][1] = PawnB; anBoard[1][2] = PawnB; anBoard[1][3] = PawnB; anBoard[1][4] = PawnB; anBoard[1][5] = PawnB; anBoard[1][6] = PawnB; anBoard[1][7] = PawnB; anBoard[7][0] = RookeW; anBoard[7][1] = KnightW; anBoard[7][2] = BishopW; anBoard[7][3] = QueenW; anBoard[7][4] = KingW; anBoard[7][5] = BishopW; anBoard[7][6] = KnightW; anBoard[7][7] = RookeW; anBoard[6][0] = PawnW; anBoard[6][1] = PawnW; anBoard[6][2] = PawnW; anBoard[6][3] = PawnW; anBoard[6][4] = PawnW; anBoard[6][5] = PawnW; anBoard[6][6] = PawnW; anBoard[6][7] = PawnW; addMouseListener( this ); } public void destroy() { removeMouseListener( this ); } public void paint( Graphics g ) { Dimension d = getSize(); Image offScrImage = createImage( d.width, d.height ); Graphics og = offScrImage.getGraphics(); int xoff = d.width / 8; int yoff = d.height / 8; /* Draw grid of black/white squares, oriented according to whether this player is white or black. */ for ( int c = 0; c < 8; ++c ) { for ( int r = 0; r < 8; ++r ) { if ( (c + r) % 2 == 0 ) { og.setColor( Color.black ); } else { og.setColor( Color.white ); } og.fillRect( xoff * c, yoff * r, xoff, yoff ); /* Draw each image according to map */ int nPiece = anBoard[ r ][ c ]; if ( nPiece >= 0 ) { og.drawImage( imagePieces[ nPiece ], c * xoff, r * yoff, this ); } } } /* Draw rect around active piece */ if ( nActivePiece[0] >= 0 ) { if ( nPlayer == White ) og.setColor( Color.red ); else og.setColor( Color.blue ); og.drawRect( xoff * nActivePiece[0], yoff * nActivePiece[1], xoff, yoff ); og.drawRect( xoff * nActivePiece[0] - 1, yoff * nActivePiece[1] - 1, xoff + 2, yoff + 2 ); } g.drawImage( offScrImage, 0, 0, this ); } boolean checkMoveLat( int c1, int r1, int c2, int r2 ) { boolean bOK = false; int nPiece = anBoard[ r1 ][ c1 ]; int nPType = nPiece % 6; int nDestPiece = anBoard[ r2 ][ c2 ]; int nDestPType = nDestPiece % 6; if ( c1 == c2 && r1 != r2 ) { int rd; if ( r1 < r2 ) rd = 1; else rd = -1; bOK = true; for ( int r = r1 + rd; r != r2; r += rd ) { if ( anBoard[ r ][ c1 ] != -1 ) { bOK = false; break; } } } else if ( r1 == r2 && c1 != c2 ) { int cd; if ( c1 < c2 ) cd = 1; else cd = -1; bOK = true; for ( int c = c1 + cd; c != c2; c += cd ) { if ( anBoard[ r1 ][ c ] != -1 ) { bOK = false; break; } } } return bOK; } boolean checkMoveDiag( int c1, int r1, int c2, int r2 ) { boolean bOK = false; int nPiece = anBoard[ r1 ][ c1 ]; int nPType = nPiece % 6; int nDestPiece = anBoard[ r2 ][ c2 ]; int nDestPType = nDestPiece % 6; if ( (c1 + r1) % 2 == 0 && (c2 + r2) % 2 == 0 ) { if ( c1 + r1 == c2 + r2 ) { bOK = true; } else if ( c1 - r1 == c2 - r2 ) { bOK = true; } } else if ( (c1 + r1) % 2 == 1 && (c2 + r2) % 2 == 1 ) { if ( c1 + r1 == c2 + r2 ) { bOK = true; } else if ( c1 - r1 == c2 - r2 ) { bOK = true; } } if ( bOK ) { int cd; if ( c1 < c2 ) cd = 1; else cd = -1; int rd; if ( r1 < r2 ) rd = 1; else rd = -1; int r = r1 + rd; int c = c1 + cd; for ( ; c != c2; r += rd, c += cd ) { if ( anBoard[ r ][ c ] != -1 ) { bOK = false; break; } } } return bOK; } boolean checkMove( int c1, int r1, int c2, int r2 ) { if ( c1 == c2 && r1 == r2 ) return false; boolean bOK = false; int nPiece = anBoard[ r1 ][ c1 ]; int nPType = nPiece % 6; int nDestPiece = anBoard[ r2 ][ c2 ]; int nDestPType = nDestPiece % 6; switch ( nPType ) { case PawnB: if ( nPiece < 6 ) { if ( c1 == c2 ) { if ( r1 == r2 - 1 ) { if ( nDestPiece == -1 ) bOK = true; } else if ( r1 == r2 - 2 && r1 == 1 ) { if ( anBoard[ r2 - 1 ][ c1 ] == -1 ) bOK = true; } } else if ( c1 == c2 + 1 || c1 == c2 - 1 ) { if ( r1 == r2 - 1 ) { if ( nDestPiece != -1 && nDestPiece > 5 ) bOK = true; } } } else { if ( c1 == c2 ) { if ( r1 == r2 + 1 ) { if ( nDestPiece == -1 ) bOK = true; } else if ( r1 == r2 + 2 && r1 == 6 ) { if ( anBoard[ r2 + 1 ][ c1 ] == -1 ) bOK = true; } } else if ( c1 == c2 + 1 || c1 == c2 - 1 ) { if ( r1 == r2 + 1 ) { if ( nDestPiece != -1 && nDestPiece < 6 ) bOK = true; } } } break; case RookeB: bOK = checkMoveLat( c1, r1, c2, r2 ); break; case KnightB: if ( r1 == r2 - 2 || r1 == r2 + 2 ) { if ( c1 == c2 - 1 || c1 == c2 + 1 ) { bOK = true; } } else if ( c1 == c2 - 2 || c1 == c2 + 2 ) { if ( r1 == r2 - 1 || r1 == r2 + 1 ) { bOK = true; } } break; case BishopB: bOK = checkMoveDiag( c1, r1, c2, r2 ); break; case QueenB: bOK = checkMoveLat( c1, r1, c2, r2 ); if ( bOK == false ) bOK = checkMoveDiag( c1, r1, c2, r2 ); break; case KingB: if ( Math.abs( r1 - r2 ) < 2 && Math.abs( c1 - c2 ) < 2 && (Math.abs( r1 - r2 ) != 0 || Math.abs( c1 - c2 ) != 0) ) bOK = true; break; default: break; } if ( bOK == true ) { if ( nPiece < 6 && (nDestPiece == -1 || nDestPiece > 5) ) bOK = true; else if ( nPiece > 5 && (nDestPiece == -1 || nDestPiece < 6) ) bOK = true; else bOK = false; } return bOK; } public void mouseReleased( MouseEvent e ) { } public void mousePressed( MouseEvent e ) { } public void mouseClicked( MouseEvent e ) { if ( nActivePiece[0] >= 0 ) { int x = e.getX(); int y = e.getY(); // Figure out the row/column Dimension d = getSize(); int c = (x * 8) / d.width; int r = (y * 8) / d.height; if ( c < 0 || c > 7 ) return; if ( r < 0 || r > 7 ) return; if ( checkMove( nActivePiece[0], nActivePiece[1], c, r ) ) { anBoard[ r ][ c ] = anBoard[ nActivePiece[1] ][ nActivePiece[0] ]; anBoard[ nActivePiece[1] ][ nActivePiece[0] ] = -1; nPlayer = (nPlayer + 1) % 2; } nActivePiece[0] = -1; nActivePiece[1] = -1; } else { int x = e.getX(); int y = e.getY(); // Figure out the row/column Dimension d = getSize(); int c = (x * 8) / d.width; int r = (y * 8) / d.height; if ( c < 0 || c > 7 ) return; if ( r < 0 || r > 7 ) return; // Check to see if there's a piece there. // If so, set active piece to it. if ( anBoard[ r ][ c ] != -1 ) { // Check to see who's turn it is if ( (anBoard[ r ][ c ] < 6 && nPlayer == 0) || (anBoard[ r ][ c ] > 5 && nPlayer == 1) ) { nActivePiece[0] = c; nActivePiece[1] = r; } } } repaint(); } public void mouseEntered( MouseEvent e ) { } public void mouseExited( MouseEvent e ) { } }