50,530
社区成员
发帖
与我相关
我的任务
分享
class ChessPad extends Panel implements MouseListener, ActionListener {
boolean isBlack = true;
Button reset = new Button("重新开局");
TextField text_1 = new TextField("请黑棋下子"), text_2 = new TextField();
boolean[][] mapB = new boolean[18][18]; //是否有白子
boolean[][] mapW = new boolean[18][18];
ChessPad() {
setSize(440, 440);
setLayout(null);
setBackground(Color.orange);
addMouseListener(this);
add(reset);
reset.setBounds(10, 5, 60, 26);
reset.addActionListener(this);
add(text_1);
text_1.setBounds(90, 5, 90, 24);
add(text_2);
text_2.setBounds(290, 5, 90, 24);
text_1.setEditable(false);
text_2.setEditable(false);
}
public void paint(Graphics g) {
for (int i = 40; i <= 380; i = i + 20) {
g.drawLine(40, i, 400, i);
}
g.drawLine(40, 400, 400, 400);
for (int j = 40; j <= 380; j = j + 20) {
g.drawLine(j, 40, j, 400);
}
g.drawLine(400, 40, 400, 400);
g.fillOval(337, 97, 6, 6);
g.fillOval(97, 97, 6, 6);
g.fillOval(97, 337, 6, 6);
g.fillOval(337, 337, 6, 6);
g.fillOval(217, 217, 6, 6);
}
public void mousePressed(MouseEvent e) {
if (e.getModifiers() == InputEvent.BUTTON1_MASK) {
int x = (int) e.getX();
int y = (int) e.getY();
ChessPoint_black chesspoint_black = new ChessPoint_black(this);
ChessPoint_white chesspoint_white = new ChessPoint_white(this);
int cx = (x+10)/20-2;
int cy = (y+10)/20-2;
if (cx>-1 && cy>-1 && cx<18 && cy<18) {
if (isBlack) {
this.add(chesspoint_black);
mapB[cx][cy] = true;
chesspoint_black.setBounds(cx * 20 + 30, cy * 20 + 30, 20, 20);
boolean winB = checkWin(mapB, cx, cy);
if (winB) System.out.println("Black Win");
isBlack = !isBlack;
text_2.setText("请白棋下子");
text_1.setText("");
} else{
this.add(chesspoint_white);
chesspoint_white.setBounds(cx * 20 + 30, cy * 20 + 30, 20, 20);
mapW[cx][cy] = true;
boolean winW = checkWin(mapW, cx, cy);
if (winW) System.out.println("White Win");
isBlack = !isBlack;
text_1.setText("请黑棋下子");
text_2.setText("");
}
}
}
}
private boolean checkWin(boolean[][] map, int x, int y) {
int[] deltaX = {1, 1, 1, 0 };
int[] deltaY = {0, 1,-1, 1 };
for(int i=0; i<4; i++){
int cx = x;
int cy = y;
int count = 0;
while(cx>-1 && cy>-1 && cx<18 && cy<18 && map[cx][cy]){
cx -= deltaX[i];
cy -= deltaY[i];
count++;
}
cx = x;
cy = y;
while(cx>-1 && cy>-1 && cx<18 && cy<18 && map[cx][cy]){
count ++;
cx += deltaX[i];
cy += deltaY[i];
}
if (count>5) return true;
}
return false;
}
public void mouseReleased(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void mouseClicked(MouseEvent e) {
}
public void actionPerformed(ActionEvent e) {
this.removeAll();
isBlack = true;
add(reset);
reset.setBounds(10, 5, 60, 26);
add(text_1);
text_1.setBounds(90, 5, 90, 24);
text_2.setText("");
text_1.setText("请黑棋下子");
add(text_2);
text_2.setBounds(290, 5, 90, 24);
}
}
class ChessPoint_black extends Canvas implements MouseListener {
ChessPad chesspad = null;
ChessPoint_black(ChessPad p) {
setSize(20, 20);
chesspad = p;
addMouseListener(this);
}
public void paint(Graphics g) {
g.setColor(Color.black);
g.fillOval(0, 0, 20, 20);
}
public void mousePressed(MouseEvent e) {
if (e.getModifiers() == InputEvent.BUTTON3_MASK) {
chesspad.remove(this);
chesspad.isBlack = true;
chesspad.text_2.setText("");
chesspad.text_1.setText("请黑棋下子");
}
}
public void mouseReleased(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void mouseClicked(MouseEvent e) {
if (e.getClickCount() >= 2)
chesspad.remove(this);
}
}
class ChessPoint_white extends Canvas implements MouseListener {
ChessPad chesspad = null;
ChessPoint_white(ChessPad p) {
setSize(20, 20);
addMouseListener(this);
chesspad = p;
}
public void paint(Graphics g) {
g.setColor(Color.white);
g.fillOval(0, 0, 20, 20);
}
public void mousePressed(MouseEvent e) {
if (e.getModifiers() == InputEvent.BUTTON3_MASK) {
chesspad.remove(this);
chesspad.isBlack = false;
chesspad.text_2.setText("请白棋下子");
chesspad.text_1.setText("");
}
}
public void mouseReleased(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void mouseClicked(MouseEvent e) {
if (e.getClickCount() >= 2)
chesspad.remove(this);
}
}
public class Chess extends Frame {
ChessPad chesspad = new ChessPad();
Chess() {
setVisible(true);
setLayout(null);
Label label = new Label("单击左键下棋子,双击吃棋子,用右键单击棋子悔棋", Label.CENTER);
add(label);
label.setBounds(70, 55, 440, 26);
label.setBackground(Color.orange);
add(chesspad);
chesspad.setBounds(70, 90, 440, 440);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
pack();
setSize(600, 500);
}
public static void main(String args[]) {
Chess chess = new Chess();
}
}
private boolean checkWin(){
int color=markChess[x][y];
boolean flag=false;
if(checkCount(1,0,color)>=5){
flag=true;
} else if(checkCount(0,1,color)>=5){
flag=true;
} else if(checkCount(1,-1,color)>=5){
flag=true;
} else if(checkCount(1,1,color)>=5){
flag=true;
}
return flag;
}
//返回相连棋子的个数
private int checkCount(int xChange,int yChange,int color){
int count=1;
int tempX=xChange;
int tempY=yChange;
while(x+xChange>=0&&x+xChange<=18&&y+yChange>=0&&y+yChange<=18
&& color==markChess[x+xChange][y+yChange]){
count++;
if(xChange!=0){
xChange++;
}
if(yChange!=0){
if(yChange>0){
yChange++;
}
else yChange--;
}
}
xChange=tempX;
yChange=tempY;
while(x-xChange>=0&&x-xChange<=18&&y-yChange>=0&&y-yChange<=18
&&color==markChess[x-xChange][y-yChange]){
count++;
if(xChange!=0){
xChange++;
}
if(yChange!=0){
if(yChange>0){
yChange++;
}
else yChange--;
}
}
return count;
}