package com.gobang;
import java.awt.*;
import java.awt.event.*;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
class chessPad extends Panel implements MouseListener,ActionListener {
private static final long serialVersionUID = 1L;
int x, y, chessColor = -1; //x,y坐标;棋子颜色,黑棋为1,白棋为-1
int point[][] = new int[17][17]; //定义二维数组,用于记录棋盘内各个坐标的值,-1 表示已有白棋, 1 表示已有黑棋
JButton button=new JButton("重新开始"); //"重新开始"按钮
JTextField text=new JTextField("白棋下子"); //提示:当前白棋/黑棋下子
chessPad() { //创建棋盘
setLayout(null); //清空布局管理器,之后添加组件
setBackground(new Color(249,214,11)); //设置棋盘背景色
addMouseListener(this); //添加鼠标监听事件
add(button);
button.setBounds(80,20,100,40); //设置按钮x,y位置和宽高
button.setBackground(new Color(249,214,11));
button.setBorder(BorderFactory.createLineBorder(new Color(249,214,11)));
button.setForeground(Color.black);
button.setFont(new Font("宋体",Font.PLAIN,24));
button.setCursor(new Cursor(Cursor.HAND_CURSOR));
button.setVisible(true);
button.addActionListener(this);
add(text);
text.setBounds(540,20,100,40); //提示样式
text.setEditable(false); //提示框不可编辑
text.setBackground(new Color(249,214,11));
text.setForeground(Color.black);
text.setBorder(BorderFactory.createLineBorder(new Color(249,214,11)));
text.setFont(new Font("宋体",Font.PLAIN,24));
text.setVisible(true);
}
public void paint(Graphics g) { //绘制棋盘点线
for(int i=80;i<=640;i=i+40){ //两种划线的方式,分用于行和列
g.drawLine(80,i,640,i); //drawLine(int x1, int y1, int x2, int y2) 起点-终点
}
for(int j=80;j<=640;j=j+40) {
g.drawLine(j,80,j,640);
}
g.fillOval(195,195,10,10); //oval表示椭圆,这里是指棋盘中的五个圆点。左上
g.fillOval(515,195,10,10); //右上
g.fillOval(355,355,10,10); //中间
g.fillOval(195,515,10,10); //坐下
g.fillOval(515,515,10,10); //右下
super.paint(g); //重绘界面保证上面的按钮和文本框可以再打开界面的时候显示出来。
}
public void mousePressed(MouseEvent e) { //当鼠标左键按下时下棋
if(e.getModifiers() == InputEvent.BUTTON1_MASK) {
int isWin;
x = (int)e.getX(); //鼠标所在位置的XY坐标
y = (int)e.getY();
// System.out.println("x = " + x + ",y = " + y);
int a = (x+20)/40, b = (y+20)/40; //确保鼠标箭头指的坐标是棋子的中心。
// System.out.println("a = " + a + ",b = " + b);
int c = a*40-15, d = b*40-15; //纠正落棋的位置,确保棋子落入的坐标刚好再横竖交叉处。
blackChess blackChess=new blackChess();
whiteChess whiteChess=new whiteChess();
if(x/40 >= 2 && y/40 >= 2 && x/40 < 16 && y/40 < 16) { //棋盘以外不能下棋子
if (point[a][b] != 0) { //如果该处已经有棋,则不能落子。
title(0);
} else {
if(chessColor == 1) {
this.add(blackChess);
blackChess.setBounds(c, d, 30, 30);
point[a][b] = 1;
//横向判断
isWin = xJudge(a, b, 1);
if (isWin >= 5) {
title(chessColor);
} else {
//纵向判断
isWin = yJudge(a, b, 1);
if (isWin >= 5) {
title(chessColor);
} else {
//从左上角到右下角的判断
isWin = xyJudge(a, b, 1);
if (isWin >= 5) {
title(chessColor);
} else {
//从左下角到右上角的判断
isWin = yxJudge(a, b, 1);
if (isWin >= 5) {
title(chessColor);
} else {
text.setText("白棋下子");
}
}
}
}
chessColor = -1;
} else if(chessColor == -1) {
this.add(whiteChess);
whiteChess.setBounds(c, d, 30, 30);
point[a][b] = -1;
//横向判断
isWin = xJudge(a, b, -1);
if (isWin >= 5) {
title(chessColor);
} else {
//纵向判断
isWin = yJudge(a, b, -1);
if (isWin >= 5) {
title(chessColor);
} else {
//从左上角到右下角的判断
isWin = xyJudge(a, b, -1);
if (isWin >= 5) {
title(chessColor);
} else {
//从左下角到右上角的判断
isWin = yxJudge(a, b, -1);
if (isWin >= 5) {
title(chessColor);
} else {
text.setText("黑棋下子");
}
}
}
}
chessColor = 1;
}
}
}
}
}
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) {
reset(); //“重新开始”按钮点击
}
public void reset() { //重置棋局
this.removeAll();
add(button);
button.setBounds(80,20,100,40); //设置按钮x,y位置和宽高
button.addActionListener(this);
add(text);
text.setBounds(540,20,100,40); //提示样式
text.setEditable(false);
//清空数组
for (int i = 2; i < 17; i++) {
for (int j = 2; j < 17; j++) {
point[i][j] = 0;
}
}
}
public void title(int chessColor) { //赢后提示信息
if (chessColor == -1) {
JOptionPane.showMessageDialog(null, "白棋胜,请重新开始!", "提示", JOptionPane.INFORMATION_MESSAGE);
reset();
text.setText("黑棋下子");
} else if (chessColor == 1) {
JOptionPane.showMessageDialog(null, "黑棋胜,请重新开始!", "提示", JOptionPane.INFORMATION_MESSAGE);
reset();
text.setText("白棋下子");
} else {
text.setText("不可下子");
}
}
public int xJudge(int a, int b, int which) { //横向判断.a x坐标;b y坐标;which 白棋还是黑棋。
int num = 1; //每次判断的时候都要重新置1
for(int
评论0
最新资源