Java实现,没什么好说。
Chess.java
/**
*
*/
/**
* @author Administrator
*
*/
@SuppressWarnings("serial")
public class Chess extends Frame {
/**
* @param args
*/
ChessPad chesspad = new ChessPad();
Chess() {
setVisible(true);
setLayout(null);
Label label = new Label("单击左键下棋子,双击吃棋子,用右键单击棋子悔棋",Label.CENTER);
add(label);
//label.setBounds(95, 60, 400, 15);
label.setBackground(Color.orange);
add(chesspad);
chesspad.setBounds(95, 90, 400, 400);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(NORMAL);
}
}
);
pack();
setSize(600, 550);
}
public static void main(String[] args) {
new Chess();
}
}
ChessPad.java
@SuppressWarnings("serial")
public class ChessPad extends Panel implements MouseListener, ActionListener {
int x=-1,y=-1,color=1;
Button button = new Button("重新开局");
TextField text = new TextField("请白棋下子");
ChessField cf = new ChessField(this);
ChessPad() {
setSize(440, 440);
setLayout(null);
setBackground(Color.orange);
addMouseListener(this);
add(button);
button.setBounds(10, 5, 60, 26);
button.addActionListener(this);
add(text);
text.setBounds(180, 5, 90, 24);
text.setEditable(false);
}
public void paint(Graphics g) {
for(int i=40;i<=360;i+=40) {
g.drawLine(40, i, 360, i);
}
for(int j=40;j<=360;j+=40) {
g.drawLine(j, 40, j, 360);
}
g.drawLine(38, 38, 38, 362);
g.drawLine(38, 38, 362, 38);
g.drawLine(38, 362, 362, 362);
g.drawLine(362, 38, 362, 362);
cf.repaint(g);
}
@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent e) {
if(e.getModifiers()==InputEvent.BUTTON1_MASK) {
x = (int)e.getX();
y = (int)e.getY();
//System.out.println(x);
//System.out.println(y);
int a = x/40-1,
b = y/40-1;
if(x<40||y<40||x>360||y>360||x%40==0||y%40==0){
// do nothing
}
else {
if(color == -1) {//-1 表示轮到黑棋
if(cf.positionOK(a, b, color))cf.setChessPad(a, b, color);
else return;
color *= -1;
text.setText("请白棋下子");
}
else if(color == 1) {//1 表示轮到白棋
if(cf.positionOK(a, b, color))cf.setChessPad(a, b, color);
else return;
color *= -1;
text.setText("请黑棋下子");
}
repaint();
}
}
}
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void actionPerformed(ActionEvent e) {
this.removeAll();
cf = new ChessField(this);
color = 1;
add(button);
button.setBounds(10, 5, 60, 26);
add(text);
text.setBounds(180, 5, 90, 24);
text.setText("请白棋下子");
}
}
ChessPoint_black.java
/**
*
*/
/**
* @author Administrator
*
*/
@SuppressWarnings("serial")
public class ChessPoint_black extends Canvas implements MouseListener {
ChessPad chesspad = null;
/**
*
*/
public ChessPoint_black() {
// TODO Auto-generated constructor stub
}
/**
* @param config
*/
public ChessPoint_black(ChessPad p) {
setSize(38, 38);
chesspad = p;
addMouseListener(this);
}
public void paint(Graphics g) {
g.setColor(Color.black);
g.fillOval(0, 0, 38, 38);
}
/* (non-Javadoc)
* @see java.awt.event.MouseListener#mouseClicked(java.awt.event.MouseEvent)
*/
@Override
public void mouseClicked(MouseEvent e) {
}
/* (non-Javadoc)
* @see java.awt.event.MouseListener#mouseEntered(java.awt.event.MouseEvent)
*/
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
/* (non-Javadoc)
* @see java.awt.event.MouseListener#mouseExited(java.awt.event.MouseEvent)
*/
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
/* (non-Javadoc)
* @see java.awt.event.MouseListener#mousePressed(java.awt.event.MouseEvent)
*/
@Override
public void mousePressed(MouseEvent e) {
/*if(e.getModifiers()==InputEvent.BUTTON3_MASK) {
chesspad.remove(this);
chesspad.color = 1;
chesspad.text.setText("请黑棋下子");
}*/
}
/* (non-Javadoc)
* @see java.awt.event.MouseListener#mouseReleased(java.awt.event.MouseEvent)
*/
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
}
ChessPoint_white.java
@SuppressWarnings("serial")
public class ChessPoint_white extends Canvas implements MouseListener {
ChessPad chesspad = null;
public ChessPoint_white() {
// TODO Auto-generated constructor stub
}
public ChessPoint_white(ChessPad p) {
setSize(38,38);
addMouseListener(this);
chesspad = p;
}
public void paint(Graphics g) {
g.setColor(Color.white);
g.fillOval(0, 0, 38, 38);
}
@Override
public void mouseClicked(MouseEvent e) {
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent e) {
/*if(e.getModifiers()==InputEvent.BUTTON3_MASK) {
chesspad.remove(this);
chesspad.color = -1;
chesspad.text.setText("请白棋下子");
}*/
}
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
}
ChessField.java
public class ChessField {
int chesspad[][] = new int[8][8];
ChessPoint_black black[][] = new ChessPoint_black[8][8];
ChessPoint_white white[][] = new ChessPoint_white[8][8];
public ChessField(ChessPad cp) {
for(int i=0;i<8;i++) {
for(int j=0;j<8;j++) {
black[i][j] = new ChessPoint_black();
white[i][j] = new ChessPoint_white();
black[i][j].setVisible(false);
black[i][j].setBounds(40*i+41, 40*j+41, 38, 38);
cp.add(black[i][j]);
white[i][j].setVisible(false);
white[i][j].setBounds(40*i+41, 40*j+41, 38, 38);
cp.add(white[i][j]);
}
}
chesspad[3][3] = 1;
chesspad[3][4] = -1;
chesspad[4][3] = -1;
chesspad[4][4] = 1;
}
public void repaint(Graphics g) {
for(int i=0;i<8;i++) {
for(int j=0;j<8;j++) {
if(chesspad[i][j] == -1) {
black[i][j].setVisible(true);
white[i][j].setVisible(false);
}
else if(chesspad[i][j] == 1) {
white[i][j].setVisible(true);
black[i][j].setVisible(false);
}
}
}
}
public void setChessPad(int x,int y,int color) {
int u=-1,v=-1;
// 该棋子的右边
for(int i=x+1;i<7;i++) {
if(chesspad[i][y] == color) {
u = i;
break;
}
}
if(u != -1) {
for(int i=x+1;i<u;i++) {
chesspad[i][y] = color;
}
}
// 该棋子的左边
u=-1;
for(int i=x-1;i>=0;i--) {
if(chesspad[i][y] == color) {
u = i;
break;
}
}
if(u != -1) {
for(int i=x-1;i>u;i--) {
chesspad[i][y] = color;
}
}
// 该棋子的下边
v=-1;
for(int i=y+1;i<7;i++) {
if(chesspad[x][i] == color) {
v = i;
break;
}
}
if(v != -1) {
for(int i=y+1;i<v;i++) {
chesspad[x][i] = color;
}
}
// 该棋子的上边
v=-1;
for(int i=y-1;i>=0;i--) {
if(chesspad[x][i] == color) {
v = i;
break;
}
}
if(v != -1) {
for(int i=y-1;i>v;i--) {
chesspad[x][i] = color;
}
}
// 该棋子的右下方
u=-1;v=-1;
for(int i=x+1,j=y+1;i<7&&j<7;i++,j++) {
if(chesspad[i][j] == color) {
u=i;v=j;
break;
}
}
if(u!=-1&&v!=-1) {
for(int i=x+1,j=y+1;i<u&&j<v;i++,j++) {
chesspad[i][j] = color;
}
}
// 该棋子的左上方
u=-1;v=-1;
for(int i=x-1,j=y-1;i>=0&&j>=0;i--,j--) {
if(chesspad[i][j] == color) {
u=i;v=j;
break;
}
}
if(u!=-1&&v!=-1) {
for(int i=x-1,j=y-1;i>u&&j>v;i--,j--) {
chesspad[i][j] = color;
}
}
// 该棋子的右上方
u=-1;v=-1;
for(int i=x+1,j=y-1;i<7&&j>=0;i++,j--) {
if(chesspad[i][j] == color) {
u=i;v=j;
break;
}
}
if(u!=-1&&v!=-1) {
for(int i=x+1,j=y-1;i<u&&j>v;i++,j--) {
chesspad[i][j] = color;
}
}
// 该棋子的左下方
u=-1;v=-1;
for(int i=x-1,j=y+1;i>=0&&j<7;i--,j++) {
if(chesspad[i][j] == color) {
u=i;v=j;
break;
}
}
if(u!=-1&&v!=-1) {
for(int i=x-1,j=y+1;i>u&&j<v;i--,j++) {
chesspad[i][j] = color;
}
}
chesspad[x][y] = color;
}
public boolean positionOK(int x,int y,int color) {
int u=-1,v=-1;
boolean flag = false;
// 该棋子的右边
for(int i=x+1;i<7;i++) {
if(chesspad[i][y] == color) {
u = i;
break;
}
}
if(u != -1) {
for(int i=x+1;i<u;i++) {
flag = true;
}
}
// 该棋子的左边
u=-1;
for(int i=x-1;i>=0;i--) {
if(chesspad[i][y] == color) {
u = i;
break;
}
}
if(u != -1) {
for(int i=x-1;i>u;i--) {
flag = true;
}
}
// 该棋子的下边
v=-1;
for(int i=y+1;i<7;i++) {
if(chesspad[x][i] == color) {
v = i;
break;
}
}
if(v != -1) {
for(int i=y+1;i<v;i++) {
flag = true;
}
}
// 该棋子的上边
v=-1;
for(int i=y-1;i>=0;i--) {
if(chesspad[x][i] == color) {
v = i;
break;
}
}
if(v != -1) {
for(int i=y-1;i>v;i--) {
flag = true;
}
}
// 该棋子的右下方
u=-1;v=-1;
for(int i=x+1,j=y+1;i<7&&j<7;i++,j++) {
if(chesspad[i][j] == color) {
u=i;v=j;
break;
}
}
if(u!=-1&&v!=-1) {
for(int i=x+1,j=y+1;i<u&&j<v;i++,j++) {
flag = true;
}
}
// 该棋子的左上方
u=-1;v=-1;
for(int i=x-1,j=y-1;i>=0&&j>=0;i--,j--) {
if(chesspad[i][j] == color) {
u=i;v=j;
break;
}
}
if(u!=-1&&v!=-1) {
for(int i=x-1,j=y-1;i>u&&j>v;i--,j--) {
flag = true;
}
}
// 该棋子的右上方
u=-1;v=-1;
for(int i=x+1,j=y-1;i<7&&j>=0;i++,j--) {
if(chesspad[i][j] == color) {
u=i;v=j;
break;
}
}
if(u!=-1&&v!=-1) {
for(int i=x+1,j=y-1;i<u&&j>v;i++,j--) {
flag = true;
}
}
// 该棋子的左下方
u=-1;v=-1;
for(int i=x-1,j=y+1;i>=0&&j<7;i--,j++) {
if(chesspad[i][j] == color) {
u=i;v=j;
break;
}
}
if(u!=-1&&v!=-1) {
for(int i=x-1,j=y+1;i>u&&j<v;i--,j++) {
flag = true;
}
}
return flag;
}
}