package com.yhs02;
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.File;
@SuppressWarnings("all")
public class FiveChessFrame extends JFrame implements MouseListener, Runnable {
String blackMessage = "无限制";
String whiteMessage = "无限制";
int whiteTime = 0;
int blackTime = 0;
Thread thread = new Thread(this);
int maxTime = 0;
String nextGo = "黑子先手";
int qc = 1;
int qn = 0;
boolean startPlay = false;
int[][] SavePoint = new int[14][13];
int x = 0, y = 0;
int qx = 260, qy = 345, qw = 455, qh = 420;
int bw = 150, bh = 50, bx = 50, by = 500;
int screenWidth = Toolkit.getDefaultToolkit().getScreenSize().width;
int screenHeight = Toolkit.getDefaultToolkit().getScreenSize().height;
public FiveChessFrame() {
int frameWidth = 780;
int frameHeight = 800;
this.setTitle("五子棋");
this.setSize(frameWidth, frameHeight);
this.setLocation((screenWidth - frameWidth) / 2, (screenHeight - frameHeight) / 2);
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
this.addMouseListener(this);
}
@Override
public void paint(Graphics g) {
try {
g.drawImage(ImageIO.read(new File("d:\\wzq.png")), 3, 2, this);
} catch (Exception e) {
throw new RuntimeException(e);
}
g.setFont(new Font("华文行楷", Font.BOLD, 30));
g.drawString("游戏信息", 50, 400);
g.setFont(new Font("黑体", 0, 20));
g.drawString("黑方时间: " + blackMessage, 45, 450);
g.drawString("白方时间: " + whiteMessage, 45, 480);
g.setColor(Color.BLACK);
g.fillRect(bx, by, bw, bh);
g.setFont(new Font("华文行楷", 10, 30));
g.setColor(Color.white);
g.drawString("开始", 95, 530);
g.setColor(Color.BLUE);
g.fillRect(bx, by + 60, bw, bh);
g.setFont(new Font("华文行楷", 10, 30));
g.setColor(Color.WHITE);
g.drawString("悔棋", 95, 590);
g.setColor(Color.CYAN);
g.fillRect(bx, by + 120, bw, bh);
g.setFont(new Font("华文行楷", 10, 30));
g.setColor(Color.BLACK);
g.drawString("认输", 95, 650);
g.setColor(Color.GREEN);
g.fillRect(bx, by + 180, bw, bh);
g.setFont(new Font("华文行楷", 10, 30));
g.setColor(Color.BLACK);
g.drawString("游戏设置", 70, 710);
g.setColor(Color.black);
for (int x = 0; x <= qw; x += 35) {
g.drawLine(qx, qy + x, qw + qx, x + qy);
g.drawLine(qx + x, qy, qx + x, qh + qy);
}
for (int i = 0; i < 14; i++) {
for (int j = 0; j < 13; j++) {
if (SavePoint[i][j] == 1) {
int sx = i * 35 + qx;
int sy = j * 35 + qy;
g.setColor(Color.BLACK);
g.fillOval(sx - 10, sy - 10, 20, 20);
}
if (SavePoint[i][j] == 2) {
int sx = i * 35 + qx;
int sy = j * 35 + qy;
g.setColor(Color.WHITE);
g.fillOval(sx - 10, sy - 10, 20, 20);
g.setColor(Color.BLACK);
g.drawOval(sx - 10, sy - 10, 20, 20);
}
}
}
g.setColor(Color.BLACK);
g.setFont(new Font("宋体", 20, 30));
g.drawString(nextGo, 70, 360);
}
private boolean WinOrLose() {
boolean flag = false;
int chessColor = SavePoint[x][y];
int count = this.chessCount(1, 0, chessColor);
if (count >= 5) {
flag =true;
} else {
count = this.chessCount(1, -1, chessColor);
if (count >= 5) {
flag = true;
}else {
count = this.chessCount(1, 1, chessColor);
if (count >=5) {
flag = true;
}
}
}
return flag;
}
private int chessCount(int xChange, int yChange, int chessColor) {
int count = 1;
int tempX = xChange;
int tempY = yChange;
while (x + xChange >= 0 && x + xChange <= 12
&& y + yChange >= 0 && y + yChange <= 11
&& chessColor == SavePoint[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 <= 12
&& y - yChange >= 0 && y - yChange <= 11
&& chessColor == SavePoint[x - xChange][y - yChange]) {
count++;
if (xChange != 0) {
xChange++;
}
if (yChange != 0) {
if (yChange > 0) {
yChange++;
} else {
yChange--;
}
}
}
return count;
}
public void Initialize() {
for (int i = 0; i < 14; i++) {
for (int j = 0; j < 13; j++) {
SavePoint[i][j] = 0;
}
}
qc = 1;
nextGo = "黑子先手";
blackTime = maxTime;
whiteTime = maxTime;
if (maxTime > 0) {
blackMessage = maxTime + "秒";
whiteMessage = maxTime + "秒";
if (startPlay == true) {
thread.start();
}
}
this.repaint();
}
@Override
public void mouseClicked(MouseEvent e) {
}
@Override
public void mousePressed(MouseEvent e) {
x = e.getX();
y = e.getY();
System.out.println(x + " = " + y);
if (startPlay == true) {
if (x > qx && x < qx + qw && y > qy && y < qy + qh) {
if ((x - qx) % 35 > 17) {
x = (x - qx) / 35 + 1;
} else {
x = (x - qx) / 35;
}
if ((y - qy) % 35 > 17) {
y = (y - qy) / 35 + 1;
} else {
y = (y - qy) / 35;
}
if (SavePoint[x][y] == 0) {
SavePoint[x][y] = qc;
qn = 0;
} else {
JOptionPane.showMessageDialog(this, "当前位置有棋子");
qn = 1;
}
if (qn == 0) {
if (qc == 1) {
qc = 2;
nextGo = "轮到白子";
} else {
qc = 1;
nextGo = "轮到黑子";
}
}
this.repaint();
boolean b = this.WinOrLose ();
if (b) {
JOptionPane.showMessageDialog(this, "游戏结束" + (SavePoint[x][y] == 1 ? "黑棋win" : "白棋win"));
startPlay = false;
}
}
}
if (e.getX() > bx && e.getX() < bx + bw && e.getY() > by && e.getY() < by + bh) {
if (startPlay == false) {
startPlay = true;
JOptionPane.showMessageDialog(this, "游戏开始");
Initialize();
this.repaint();
} else {
int result = JOptionPane.showConfirmDialog(this, "是否确认重新开始游戏? ");
if (result == 0) {
JOptionPane.showMessageDialog(this, "重新开始游戏");
Initialize();
this.repaint();
}
}
}
if (e.getX() > bx && e.getX() < bx + bw && e.getY() >= by + 50 && e.getY() <= by + bh + 50) {
if (startPlay == true) {
int z = 0;
for (int i = 0; i < 14; i++) {
for (int j = 0; j < 13; i++) {
if (SavePoint[i][j] != 0) {
z++;
}
}
}
if (z != 0) {
JOptionPane.showMessageDialog(this, "不能悔棋");
} else {
JOptionPane.showMessageDialog(this, "棋盘上无棋子");
}
} else {
JOptionPane.showMessageDialog(this, "请先开始游戏");
}
}
if (e.getX() > bx && e.getX() < bx + bw && e.getY() > by + 100 && e.getY() < by + 100 + bh) {
if (startPlay == true) {
int anws = JOptionPane.showConfirmDialog(this, "是否确定认输? ");
if (anws == 0) {
if (qc == 1) {
JOptionPane.showMessageDialog(this, "黑方认输,白方获胜");
startPlay = false;
Initialize();
} else if (qc == 2) {
JOptionPane.showMessageDialog(this, "白方认输,黑方获胜");
startPlay = false;
Initialize();
}
}
} else {
JOptionPane.showMessageDialog(this, "请先开始游戏");
}
}
if (e.getX() > bx && e.getX() < bx + bw && e.getY() > by + 150 && e.getY() < by + 150 + bh) {
String input = JOptionPane.showInputDialog("请输入游戏的最大时间(单位分钟) 如果输入0表示无限制时间");
maxTime = Integer.parseInt(input) * 60;
if (maxTime < 0) {
JOptionPane.showMessageDialog(this, "输入错误,时间不可以为负数");
}
if (maxTime > 0) {
int result = JOptionPane.showConfirmDialog(this, "设置成功,是否重新开始游戏?");
if (result == 0) {
Initialize();
this.repaint();
}
}
if (maxTime == 0) {
int result = JOptionPane.showConfirmDialog(this, "无时间限制成功,是否重新开始游戏?");
if (result == 0) {
Initialize();
this.repaint();
}
}
}
}
@Override
public void mouseReleased(MouseEvent e) {
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
@Override
public void run() {
if (maxTime > 0) {
while (true) {
if (qc == 1) {
blackTime--;
if (blackTime == 0) {
JOptionPane.showMessageDialog(this, "黑方时间耗尽,结束游戏");
Initialize();
}
} else {
whiteTime--;
if (whiteTime == 0) {
JOptionPane.showMessageDialog(this, "白方时间耗尽,结束游戏");
Initialize();
}
}
blackMessage = blackTime + "秒";
whiteMessage = whiteTime + "秒";
this.repaint();
try {
thread.sleep(1000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
}
}
## 测试类
public class Test_ {
public static void main(String[] args) {
FiveChessFrame fiveChessFrame = new FiveChessFrame();
}
}