/** the eight direction */
final static int addX[] = { -1, 0, 1, -1, 1, -1, 0, 1 };
final static int addY[] = { -1, -1, -1,0,0,1,1,1 };
/**check whether the use can place the chess */
private boolean checkPass(int x, int y, int play)
{
if( board[ x ][ y ] != 0 )
return false;
for(int i = 0; i < 8; i ++ )
{
int x1 = x + addX[ i ];
int y1 = y + addY[ i ];
if( x1 < GRIDSIZE && x1 >=0 && y1<GRIDSIZE && y1>= 0 && board[ x1 ][ y1 ] != 0 && board[ x1 ][ y1 ] != play )
{
x1= x1 + addX[ i ];
y1= y1 + addY[ i ];
while( x1>=0 && x1 < GRIDSIZE && y1>=0 && y1< GRIDSIZE )
{
if( board[ x1 ][ y1 ] == 0 )
break;
if( board[ x1 ][ y1 ] == play )
return true;
x1= x1 + addX[ i ];
y1= y1 + addY[ i ];
}
}
}
return false;
}
/** popup a dialog to show the current steps that can be choiced */
public void showNextStep( int play )
{
String msg = "";
for( int i =0 ;i <GRIDSIZE ; i++ )
for( int j= 0; j < GRIDSIZE ; j++ )
if( checkPass( i, j, play ) == true )
msg += "Right point at: ->:" + Integer.toString( i ) +"," + Integer.toString( j ) + "\n" ;
JOptionPane.showMessageDialog( null , msg );
}
/** decide the step in (x,y) so change the board, import change the affect of the new place */
public void changeBoard( int x, int y, int play )
{
board[ x ][ y ] = play;
for(int i = 0; i < 8; i ++ )
{
int x1 = x + addX[ i ];
int y1 = y + addY[ i ];
if( x1 < GRIDSIZE && x1 >=0 && y1<GRIDSIZE && y1>= 0 && board[ x1 ][ y1 ] != 0 && board[ x1 ][ y1 ] != play )
{
x1= x1 + addX[ i ];
y1= y1 + addY[ i ];
while( x1>=0 && x1 < GRIDSIZE && y1>=0 && y1< GRIDSIZE )
{
if( board[ x1 ][ y1 ] == 0 )
break;
if( board[ x1 ][ y1 ] == play )
{
do
{
x1 = x1 - addX[ i ];
y1 = y1 - addY[ i ];
board[ x1 ][ y1 ] = play;
}
while( x1 != x || y1 != y );
break;
}
x1= x1 + addX[ i ];
y1= y1 + addY[ i ];
}
}
}
}
/** change the chess arry by board */
public void changeChessByBoard()
{
for( int i =0; i < GRIDSIZE ; i++ )
for( int j=0; j < GRIDSIZE ; j++ )
{
if( board[ i ][ j ] == WHITE_PLAY )
chessArray[ i ][ j ].setCurrentType( JokeChess.WHITE_CHESS );
else if( board[ i ][ j ] == BLACK_PLAY )
chessArray[ i ][ j ].setCurrentType( JokeChess.BLACK_CHESS );
}
}
/** repaint the panel to show all the chess that should be showed */
public void showChess( )
{
for( int i =0; i < GRIDSIZE ; i++ )
for( int j=0; j < GRIDSIZE ; j++ )
{
if( board[ i ][ j ] != 0 )
chessArray[ i ][ j ].setShow( true );
}
SwingUtilities.invokeLater( new Runnable(){
public void run(){
repaint();
}
});
}
/** get the current steps count */
private int getNextStepCount( int play )
{
int count = 0;
for( int i =0 ;i <GRIDSIZE ; i++ )
for( int j= 0; j < GRIDSIZE ; j++ )
if( checkPass( i, j, play ) == true )
count ++;
return count ;
}
/** get the detail of all the steps can be placed info of point(x,y) */
private Point[] getNextStepPoint(int play)
{
Point[] step = new Point[ getNextStepCount( play ) ];
int count = 0;
for( int i =0 ;i <GRIDSIZE ; i++ )
for( int j= 0; j < GRIDSIZE ; j++ )
if( checkPass( i, j, play ) == true )
{
step[ count ] = new Point( i, j );
count++;
}
return step;
}
/** the display size of grid*/
public static int GRIDSIZE = 20;
/**the control of insert*/
private GridBagConstraints gbc;
/** store the chess condition on board*/
private int board[][];
/** stroe the whole chesses include hiden chesses*/
private JokeChess chessArray[][];
/** define the play ----just like user*/
final static int WHITE_PLAY = 1;
/** define the play ---just like computer*/
final static int BLACK_PLAY = 2;
/** parant -----playJoke is a JFrame*/
private playJoke parent;
/** temp use for decide who should move*/
// private boolean flag = true;
/**user style color*/
private int userPlay = BLACK_PLAY;
/**who first place the chess*/
private int whoFirst;
/**set user play style*/
public void setUserPlay( int style )
{
if( style == WHITE_PLAY )
userPlay = WHITE_PLAY;
else
userPlay = BLACK_PLAY;
}
/** get computer play style by user play*/
public int getComputerPlay()
{
return (userPlay == BLACK_PLAY ? WHITE_PLAY : BLACK_PLAY );
}
/**get user play style */
public int getUserPlay()
{
return userPlay ;
}
public ChessBoard(playJoke s) {
parent = s;
gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.BOTH;
loadChess();
/** place the JokeChess on the board */
public void place( Component c, int x, int y )
{
gbc.gridx = x;
gbc.gridy = y;
gbc.gridwidth = 1;
gbc.gridheight = 1;
gbc.weightx = 0;
gbc.weighty = 0;
add( c , gbc );
}
/** Check whether the game is over*/
public boolean gameOver()
{
boolean flag = true;
for( int i =0; i< GRIDSIZE ; i++ )
for( int j =0 ; j < GRIDSIZE ; j ++ )
if( board[ i ][ j ] == 0)
{
flag = false;
break;
}
return flag;
}
/** check the board to get the winner */
public int getWinner()
{
int white = 0;
int black = 0;
for( int i =0; i< GRIDSIZE ; i++ )
for( int j =0 ; j < GRIDSIZE ; j ++ )
if( board[ i ][ j ] == WHITE_PLAY )
white ++ ;
else
black ++ ;
if( white == black )
return 0;
else
return white > black? WHITE_PLAY : BLACK_PLAY;
}
/** create all the JokeChess and add it the the chess board panel */
private void loadChess()
{
parent.setStatusText("Wait loading ...........");
parent.setEnabled( false );
removeAll();
this.setLayout( new GridLayout(GRIDSIZE ,GRIDSIZE ) );
this.setPreferredSize( new Dimension( 50+ GRIDSIZE * 20, 50+ GRIDSIZE * 20 ) );
this.setBorder( new LineBorder(Color.orange, 2) );
if( chessArray != null )
chessArray = null;
chessArray = new JokeChess[ GRIDSIZE ][ GRIDSIZE ];
if( board != null )
board = null;
board = new int[GRIDSIZE][GRIDSIZE ];
int position;
for( int y =0; y < GRIDSIZE ; y ++ )
for( int x =0 ; x < GRIDSIZE ; x ++ )
{
if( x == 0 && y == 0 )
position = JokeChess.TOP_LEFT;
else if( x == 0 && y == (GRIDSIZE - 1 ) )
position = JokeChess.BOTTOM_LEFT;
else if( x == (GRIDSIZE - 1 ) && y == 0 )
position = JokeChess.TOP_RIGHT;
else if( x == (GRIDSIZE - 1 ) && y == (GRIDSIZE - 1 ) )
position = JokeChess.BOTTOM_RIGHT;
else if( x == 0 )
position = JokeChess.LEFT;
else if( y == 0 )
position = JokeChess.TOP;
else if( x == (GRIDSIZE - 1 ) )
position = JokeChess.RIGHT;
else if( y == (GRIDSIZE - 1 ) )
position = JokeChess.BOTTOM;
else
position = JokeChess.CENTER;
/* if( x ==0 && y == 0 )
position = JokeChess.TOP_LEFT;
else if( x != 0 && y == 0 && x != (GRIDSIZE - 1 ) )
position = JokeChess.LEFT;
else if( x == (GRIDSIZE - 1 ) && y == 0 )
position = JokeChess.BOTTOM_LEFT;
else if( x == (GRIDSIZE - 1 ) && y != 0 && y != (GRIDSIZE - 1 ) )
position = JokeChess.BOTTOM;
else if( x == (GRIDSIZE - 1 ) && y == (GRIDSIZE - 1 ) )
position = JokeChess.BOTTOM_RIGHT;
else if( x !=0 && x != (GRIDSIZE - 1 ) && y == (GRIDSIZE - 1 ) )
position = JokeChess.RIGHT;
else if( x == 0 && y == (GRIDSIZE - 1 ) )
position = JokeChess.TOP_RIGHT;
else if( x == 0 && y != 0 && y!= (GRIDSIZE - 1 ) )
position = JokeChess.TOP;
else
position = JokeChess.CENTER;
*/
JokeChess newChess = new JokeChess(position);
newChess.setShow( false );
newChess.setNoX( y );
newChess.setNoY( x );
board[ newChess.getNoX() ] [ newChess.getNoY() ] = 0;
chessArray[ newChess.getNoX() ] [ newChess.getNoY() ] = newChess;
place( newChess, x, y );
}
/**no use because the width and height is defiend by the chessboard
public java.awt.Dimension getPreferredSize() {
return new Dimension( 600, 400 );
}
*/