package XXXX;
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import static javax.swing.JOptionPane.*;
class fiveChessJFrame extends JFrame implements MouseListener {
public int x = 0, y = 0;
private int[][] allChess = new int[19][19];
private boolean isBlack = true;
private boolean canPlay = true;
private String messageWin = "";
private String message = "黑色先行!";
fiveChessJFrame() {
myPanel fiveChess = new myPanel();
this.setSize(460, 540);
this.setLocation(30, 300);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
this.setTitle("五子棋游戏");
this.setVisible(true);
this.add(fiveChess);
addMouseListener(this);
}
class myPanel extends JPanel {
public void paintComponent(Graphics graphics) {
//这是设置通知颜色;
graphics.setColor(Color.BLACK);
graphics.setFont(new Font("黑体",
Font.BOLD, 20));
graphics.drawString("游戏信息:" + message, 120, 40);
graphics.setFont(new Font("宋体",
Font.PLAIN, 28));
graphics.setColor(Color.RED);
graphics.drawString(messageWin, 120, 470);
graphics.setColor(Color.BLACK);
//每一格是20*20小方格。第一个点是40,70,最后一个点是400,430。
//一共有18*18个方格、19+19个线,所以一共有19*19的点。
for (int i = 0; i < 19; i++) {
graphics.drawLine(40, 70 + 20 * i, 400, 70 + 20 * i);
graphics.drawLine(40 + 20 * i, 70, 40 + 20 * i, 430);
}
graphics.fillOval(97, 127, 6, 6);
graphics.fillOval(337, 127, 6, 6);
graphics.fillOval(337, 367, 6, 6);
graphics.fillOval(97, 367, 6, 6);
graphics.fillOval(337, 247, 6, 6);
graphics.fillOval(217, 127, 6, 6);
graphics.fillOval(97, 247, 6, 6);
graphics.fillOval(217, 367, 6, 6);
graphics.fillOval(217, 247, 6, 6);
//allChess[][]== 0 空; allChess[][] ==1:黑色; allChess[][]== 2 白色;
for (int i = 0; i < 19; i++) {
for (int j = 0; j < 19; j++) {
if (allChess[i][j] == 1) {
graphics.setColor(Color.BLACK);
graphics.fillOval(i * 20 + 40 - 7, j * 20 + 70 - 7, 14, 14);
} else if (allChess[i][j] == 2) {
//填充白色;
graphics.setColor(Color.white);
graphics.fillOval(i * 20 + 40 - 7, j * 20 + 70 - 7, 14, 14);
//黑色描边;
graphics.setColor(Color.BLACK);
graphics.drawOval(i * 20 + 40 - 7, j * 20 + 70 - 7, 14, 14);
}
}
}
}
}
public void mousePressed(MouseEvent e) {
x = e.getX();
y = e.getY();
if (canPlay) {
if (x >= 30 && x <= 430 && y >= 90 && y <= 490) {
double x_around = Math.round((double) x / 10) * 10;
double y_around = Math.round((double) y / 10) * 10;
//打印像素和鼠标像素横向有一个10的误差,纵向有一个40的误差
int xPonit = ((int) x_around - 40 - 10) / 20;
int yPonit = ((int) y_around - 70 - 40) / 20;
if (allChess[xPonit][yPonit] == 0) {
if (isBlack) {
allChess[xPonit][yPonit] = 1;
isBlack = false;
message = "轮到白方!";
} else {
allChess[xPonit][yPonit] = 2;
isBlack = true;
message = "轮到黑方!";
}
if (checkIfWin(xPonit, yPonit) == 1) {
this.messageWin = "黑色胜利啦!";
canPlay = false;
} else if (checkIfWin(xPonit, yPonit) == 2) {
this.messageWin = "白色胜利啦!";
canPlay = false;
}
} else {
showMessageDialog(this, "当前位置有棋子了,请重新落子!");
}
this.repaint();
}
} else {
showMessageDialog(this, "游戏已经结束!");
if (JOptionPane.showConfirmDialog(this, "重新开始吗?") == YES_OPTION) {
showMessageDialog(this, "开始游戏!");
allChess = new int[19][19];
message = "黑色先行!";
messageWin = "";
isBlack = true;
canPlay = true;
this.repaint();
} else {
showMessageDialog(this, "关闭游戏!");
this.setVisible(false);
}
}
}
private int checkIfWin(int x, int y) {
//1黑色胜利,2白色胜利,0没有胜利者;
int[] aroundColorHeng = new int[9];
int[] aroundColorShu = new int[9];
int[] aroundColorPie = new int[9];
int[] aroundColorNa = new int[9];
for (int i = 0; i < 9; i++) {
if (x - 4 + i >= 0 && x - 4 + i < 19) {
aroundColorHeng[i] = allChess[x - 4 + i][y];
} else aroundColorHeng[i] = 0;
if (y - 4 + i >= 0 && y - 4 + i < 19) {
aroundColorShu[i] = allChess[x][y - 4 + i];
} else aroundColorShu[i] = 0;
if (x - 4 + i >= 0 && x - 4 + i < 19 && y - 4 + i >= 0 && y - 4 + i < 19) {
aroundColorPie[i] = allChess[x - 4 + i][y - 4 + i];
} else aroundColorPie[i] = 0;
if (x + 4 - i >= 0 && x + 4 - i < 19 && y - 4 + i >= 0 && y - 4 + i < 19) {
aroundColorNa[i] = allChess[x + 4 - i][y - 4 + i];
} else aroundColorNa[i] = 0;
}
int countNumHeng = 1, countNumShu = 1, countNumPie = 1, countNumNa = 1;
if (allChess[x][y] == 1) {
for (int i = 0; i < 8; i++) {
if (aroundColorHeng[i] == 1 && aroundColorHeng[i + 1] == 1) {
countNumHeng++;
} else countNumHeng = 1;
if (aroundColorShu[i] == 1 && aroundColorShu[i + 1] == 1) {
countNumShu++;
} else countNumShu = 1;
if (aroundColorPie[i] == 1 && aroundColorPie[i + 1] == 1) {
countNumPie++;
} else countNumPie = 1;
if (aroundColorNa[i] == 1 && aroundColorNa[i + 1] == 1) {
countNumNa++;
} else countNumNa = 1;
if (countNumNa == 5 || countNumPie == 5 || countNumShu == 5 || countNumHeng == 5) return 1;
}
} else if (allChess[x][y] == 2) {
for (int i = 0; i < 8; i++) {
if (aroundColorHeng[i] == 2 && aroundColorHeng[i + 1] == 2) {
countNumHeng++;
} else countNumHeng = 1;
if (aroundColorShu[i] == 2 && aroundColorShu[i + 1] == 2) {
countNumShu++;
} else countNumShu = 1;
if (aroundColorPie[i] == 2 && aroundColorPie[i + 1] == 2) {
countNumPie++;
} else countNumPie = 1;
if (aroundColorNa[i] == 2 && aroundColorNa[i + 1] == 2) {
countNumNa++;
} else countNumNa = 1;
if (countNumNa == 5 || countNumPie == 5 || countNumShu == 5 || countNumHeng == 5) return 2;
}
}
return 0;
}
public void mouseClicked(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
}
public class testProject {
public static void main(String args[]) {
//设置一个新的框。
fiveChessJFrame myChessJFrame = new fiveChessJFrame();
if (JOptionPane.showConfirmDialog(myChessJFrame,
"现在开始游戏吗?") == YES_OPTION) {
showMessageDialog(myChessJFrame,
"开始游戏!");
} else {
showMessageDialog(myChessJFrame,
"关闭游戏!");
myChessJFrame.setVisible(false);
}
}
}
关于五子棋的java源码
Posted aimxzj
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了关于五子棋的java源码相关的知识,希望对你有一定的参考价值。
以上是关于关于五子棋的java源码的主要内容,如果未能解决你的问题,请参考以下文章