简易五子棋的制作
首先,设置出五子棋的基本属性,如棋盘行列,格子的大小,保存时的数组。
public interface Config {
int X0 = 20,Y0 = 20;
int rows = 15,cols = 15;
int size = 30;
int CHESS_SIZE = 14;
//定义一个数组存储棋子的位置
int Chesses[][] = new int[rows][cols];
}
然后,制作棋盘,需要重写JPanel中的paint方法。
public class ChessUI extends JFrame implements Config {
public static void main(String[] args) {
ChessUI ui = new ChessUI();
ui.initChessUI();
}
public void initChessUI() {
this.setTitle("五子棋");
this.setSize(600, 500);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel jpanel = new MyPanel();
jpanel.setBackground(new Color(128, 128, 128));
this.add(jpanel);
this.setVisible(true);
Graphics g = jpanel.getGraphics();
ChessListener clis = new ChessListener(g,jpanel);
jpanel.addMouseListener(clis);
}
class MyPanel extends JPanel {
@Override
public void paint(Graphics g) {
super.paint(g);
DrawQipan(g);
DrawQizi(g);
}
public void DrawQipan(Graphics g) {
for (int i = 0; i < rows; i++) {
g.drawLine(X0, Y0 + size * i, X0 + size * (cols - 1), Y0 + size
* i);
}
for (int j = 0; j < cols; j++) {
g.drawLine(X0 + size * j, Y0, X0 + size * j, Y0 + size
* (rows - 1));
}
}
}
设置Graphics的监听器,落下棋子即绘画出黑子或白子。
创建ChessListener类并继承MouseAdapter和Config。
public class ChessListener extends MouseAdapter implements Config,
ActionListener {
private Graphics g;
private int bool = 1;
private int r = 0, c = 0;
private boolean isover = true; //判断是否能够行棋
private JPanel jpanel;
public ChessListener(Graphics g, JPanel jpanel) {
this.g = g;
this.jpanel = jpanel;
}
添加鼠标释放的实现。
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
super.mouseReleased(e);
int x = e.getX();
int y = e.getY();
// 获得最近交叉点
r = (x - X0) / size;
c = (y - Y0) / size;
if ((x - X0) % size > size / 2) {
r++;
}
if ((y - Y0) % size > size / 2) {
c++;
}
if (isover && r >= 0 && r < size && c >= 0 && c < size
&& Chesses[r][c] == 0) {
int x1 = X0 + size * r;
int y1 = Y0 + size * c;
Chesses[r][c] = bool;
if (bool == 1) {
g.setColor(Color.BLACK);
bool = -1;
} else {
g.setColor(Color.WHITE);
bool = 1;
}
g.fillOval(x1 - CHESS_SIZE / 2, y1 - CHESS_SIZE / 2, CHESS_SIZE,
CHESS_SIZE);
}
win1(bool);
win2(bool);
win3(bool);
win4(bool);
}
判断输赢。
// 横
public void win1(int bool) {
int a = 1;
for (int i = 1; i < 5; i++) {
if (Chesses[r - i][c] == Chesses[r][c]) {
a++;
} else {
break;
}
for (int j = 1; j < 5; j++) {
if (Chesses[r + i][c] == Chesses[r][c] && r + i < cols) {
a++;
}
}
if (a >= 5) {
if (bool == 1) {
JOptionPane.showMessageDialog(null, "白字胜");
} else if (bool == -1) {
JOptionPane.showMessageDialog(null, "黑字胜");
}
isover = false;
}
}
}
public void win2(int bool) {
int a = 1;
for (int i = 1; i < 5; i++) {
if (Chesses[r][c - i] == Chesses[r][c]) {
a++;
} else {
break;
}
for (int j = 1; j < 5; j++) {
if (Chesses[r][c + i] == Chesses[r][c] && c + i < rows) {
a++;
}
}
if (a >= 5) {
if (bool == 1) {
JOptionPane.showMessageDialog(null, "白字胜");
} else if (bool == -1) {
JOptionPane.showMessageDialog(null, "黑字胜");
}
isover = false;
}
}
}
public void win3(int bool) {
int a = 1;
for (int i = 1; i < 5; i++) {
if (Chesses[r - i][c + i] == Chesses[r][c] && c + i < rows) {
a++;
} else {
break;
}
for (int j = 1; j < 5; j++) {
if (Chesses[r + i][c - i] == Chesses[r][c] && r + i < cols) {
a++;
}
}
if (a >= 5) {
if (bool == 1) {
JOptionPane.showMessageDialog(null, "白字胜");
} else if (bool == -1) {
JOptionPane.showMessageDialog(null, "黑字胜");
}
isover = false;
}
}
}
public void win4(int bool) {
int a = 1;
for (int i = 1; i < 5; i++) {
if (Chesses[r - i][c - i] == Chesses[r][c]) {
a++;
} else {
break;
}
for (int j = 1; j < 5; j++) {
if (Chesses[r + i][c + i] == Chesses[r][c] && r + i < cols
&& r + i < rows) {
a++;
}
}
if (a >= 5) {
if (bool == 1) {
JOptionPane.showMessageDialog(null, "白字胜");
} else if (bool == -1) {
JOptionPane.showMessageDialog(null, "黑字胜");
}
isover = false;
}
}
}
这时已经可以人人对战了。
添加重置按钮,清空棋局。
需要将jpanel的布局设置为null,然后设置JButton的位置。
jpanel.setLayout(null);
JButton jb = new JButton("刷新");
jb.setBounds(450, 350, 80, 50);
jpanel.add(jb);
添加jb的动作映射。添加到ChessListener中。
jb.addActionListener(clis);
需要将jpanel传入到Chesslistener里重载paint方法。
public void actionPerformed(ActionEvent e) {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
Chesses[j][i] = 0;
}
}
jpanel.paint(g);
isover = true;
}