我用JAVA写了一个黑白棋的单机版,不是很智能,打不过手机上的那个游戏
但是很稳定,不会出错
我刚学的,不知道有没有兴趣
但是很稳定,不会出错
我刚学的,不知道有没有兴趣
解决方案 »
- log4j的配置
- jsp上传文件
- <many-to-one ...access="property"../>的access的问题
- struts.i18n.encoding 与 filter
- Hibernate 3.2 Hello world的问题(Could not execute JDBC batch update)
- Spring Struts Hibernate 的思想是什么?
- 第一个hibernate测试项目报错,100分
- action怎样给s:iterator中的s:checkbox预赋值?
- 学JAVA用Jbuilder如何
- 求助:计算一个超长字符串中的子串个数
- 有关ejb的事务管理的
- 如何让用户提交表单获得空间?
4个文件,其中还要resource目录下的一个splash.jpg文件
用Forte for Java 编译可以的
// playJoke.java
/**
*
* @author Administrator
* @version
*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;public class playJoke extends JFrame {
private JWindow splashScreen = null;
private JLabel status = null;
private ChessBoard chessBoard = null;
private JSplitPane split = null;
private ControlPanel rightPanel = null;
private JMenuBar menuBar = null;
/** Creates new playJoke */
public playJoke() {
super("Play Chess");
init();
pack();
this.setResizable( false );
} private void init()
{
buildSplashScreen();
addWindowListener( new WindowAdapter(){
public void windowClosing( WindowEvent e )
{
System.exit( 0 );
}
});
createMenu();
status = new JLabel();
status.setText( "Loading ......" );
getContentPane().add( status, BorderLayout.SOUTH );
split = new JSplitPane();
split.setDividerSize( 5 );
chessBoard = new ChessBoard(this);
split.setLeftComponent( chessBoard );
rightPanel = new ControlPanel( this, chessBoard );
split.setRightComponent( rightPanel );
getContentPane().add( split, BorderLayout.CENTER );
this.setLocation( 200, 200 );
splashScreen.setVisible( false );
status.setText( "Loading OK!" );
}
public static void main( String[] args )
{
new playJoke().setVisible( true );
}
/**no use because the width and height is defiend by the chessboard
public java.awt.Dimension getPreferredSize() {
return new Dimension( 600, 400 );
}
*/
private void buildSplashScreen()
{
splashScreen = new JWindow();
JLabel splashLabel = new JLabel( new ImageIcon( getClass().getResource( "/resource/Splash.jpg" ) ) );
splashScreen.getContentPane().add( splashLabel, BorderLayout.CENTER );
splashScreen.pack();
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension thisSize = splashScreen.getSize();
splashScreen.setLocation( screenSize.width/2 - thisSize.width/2,
screenSize.height/2 - thisSize.height/2);
splashScreen.setVisible( true );
}
private void createMenu()
{
}
private class MouseAttention extends MouseAdapter
{
public void mousePressed( MouseEvent event )
{
}
}
public void setStatusText( String msg )
{
status.setText( msg );
}
}
/*
* ChessBoard.java
*
* Created on GRIDSIZE 03年3月5日, 上午8:54
*/
/**
*
* @author Administrator
* @version
*/
import javax.swing.*;
import java.awt.*;
import javax.swing.border.*;
import java.awt.event.*;
import javax.swing.text.*;
public class ChessBoard extends JPanel{ /** Creates new ChessBoard */
/** 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(); addMouseListener( new MouseAdapter(){
public void mousePressed( MouseEvent event )
{
if( userMove( event.getX(), event.getY(), getUserPlay() ) == true )
{
parent.setStatusText("Computer move....");
computerMove( getComputerPlay() );
}
}
});
} /** 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 );
}
board[ (GRIDSIZE / 2 ) ] [ (GRIDSIZE / 2 ) ] = BLACK_PLAY ;
board[ (GRIDSIZE / 2 ) ] [ (GRIDSIZE / 2 -1 ) ] = BLACK_PLAY;
chessArray[ (GRIDSIZE / 2 ) ][ (GRIDSIZE / 2 ) ].setCurrentType( JokeChess.BLACK_CHESS );
chessArray[ (GRIDSIZE / 2 ) ][ (GRIDSIZE / 2 - 1 ) ].setCurrentType( JokeChess.BLACK_CHESS );
chessArray[ (GRIDSIZE / 2 ) ][ (GRIDSIZE / 2 ) ].setShow( true );
chessArray[ (GRIDSIZE / 2 ) ][ (GRIDSIZE / 2 - 1 ) ].setShow( true );
board[ (GRIDSIZE / 2 - 1 ) ] [ (GRIDSIZE / 2 ) ] = WHITE_PLAY;
board[ (GRIDSIZE / 2 - 1 ) ] [ (GRIDSIZE / 2 - 1 ) ] = WHITE_PLAY;
chessArray[ (GRIDSIZE / 2 - 1 ) ][ (GRIDSIZE / 2 ) ].setCurrentType( JokeChess.WHITE_CHESS);
chessArray[ (GRIDSIZE / 2 - 1 ) ][ (GRIDSIZE / 2 - 1 ) ].setCurrentType( JokeChess.WHITE_CHESS );
chessArray[ (GRIDSIZE / 2 - 1 ) ][ (GRIDSIZE / 2 ) ].setShow( true );
chessArray[ (GRIDSIZE / 2 - 1 ) ][ (GRIDSIZE / 2 - 1 ) ].setShow( true );
repaint();
parent.setEnabled( true );
parent.setStatusText("Load Ok!");
}
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;
}