五子棋游戏软件设计

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.*;
import java.util.*;
import javax.swing.*;
class ChessFrame extends JFrame {
public static boolean computer_Start = false;
public ChessFrame() {
super.setTitle("五子棋");
super.setSize(800, 800);
super.setBackground(Color.WHITE);
JMenu menuFile = new JMenu("菜单");
JMenu newItem = new JMenu("新游戏");
JMenuItem newItem_1 = new JMenuItem("先手");
JMenuItem newItem_2 = new JMenuItem("后手");
newItem.add(newItem_1);
newItem.add(newItem_2);
JMenuItem openItem = new JMenuItem("打开");
JMenuItem closeItem = new JMenuItem("保存");
JMenuItem exitItem = new JMenuItem("退出");
newItem_1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) {
System.out.println("=== 选择了“新游戏==先手”菜单项");
newGame();
ImagePanel.flag_NewGame = true;
}
});
newItem_2.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) {
System.out.println("=== 选择了“新游戏==后手”菜单项");
newGame();
computer_Start = true;
}
});
openItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) {
System.out.println("=== 选择了“打开”菜单项");
}
});
closeItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) {
System.out.println("=== 选择了“保存”菜单项");
}
});
exitItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) {
System.out.println("=== 选择了“退出”菜单项");
System.exit(0);
}
});
menuFile.add(newItem);
menuFile.add(openItem);
menuFile.add(closeItem);
menuFile.add(exitItem);
JMenuBar menuBar = new JMenuBar();
menuBar.add(menuFile);
super.setJMenuBar(menuBar);
ImagePanel imagePanel = new ImagePanel();
super.add(imagePanel);
super.setVisible(true);
super.setResizable(false);
super.setLocationRelativeTo(getOwner());
}
public void newGame() {
super.dispose();
FiveChess.start();
}
}
class ImagePanel extends JPanel implements MouseListener {
public static boolean flag_NewGame = false;
private boolean flag_BLACK = true;
private boolean flag_WHITE = false;
private int[][] win_Count = new int[2][572];
private boolean[][][] white_Win = new boolean[15][15][572];
private boolean[][][] black_Win = new boolean[15][15][572];
private int[][] chess = new int[15][15];
public LinkedList<A> list = new LinkedList<A>();
int count = 0;
public ImagePanel() {
// 横
for (int i = 0; i < 15; i++) {
for (int j = 0; j < 11; j++) {
for (int k = 0; k < 5; k++) {
black_Win[j + k][i][count] = true;
white_Win[j + k][i][count] = true;
}
count++;
}
}
// 竖
for (int i = 0; i < 15; i++) {
for (int j = 0; j < 11; j++) {
for (int k = 0; k < 5; k++) {
black_Win[i][j + k][count] = true;
white_Win[i][j + k][count] = true;
}
count++;
}
}
// 右斜
for (int i = 0; i < 11; i++) {
for (int j = 0; j < 11; j++) {
for (int k = 0; k < 5; k++) {
black_Win[j + k][i + k][count] = true;
white_Win[j + k][i + k][count] = true;
}
count++;
}
}
// 左斜
for (int i = 0; i < 11; i++) {
for (int j = 14; j >= 4; j--) {
for (int k = 0; k < 5; k++) {
black_Win[j - k][i + k][count] = true;
white_Win[j + -k][i + k][count] = true;
}
count++;
}
}
System.out.println("count----" + count);
for (int m = 0; m < 2; m++) {
for (int n = 0; n < 572; n++) {
win_Count[m][n] = 0;
}
}
super.addMouseListener(this);
}
public void paint_BLACk(Graphics g, int xx, int yy) { Graphics2D g2 = (Graphics2D) g;
g2.setColor(Color.BLACK);
g2.setStroke(new BasicStroke(2));
g2.fillArc(xx - 14, yy - 14, 30, 30, 0, 360);
}
public void paint_WHITE(Graphics g, int xx, int yy) { Graphics2D g2 = (Graphics2D) g;
g2.setColor(Color.WHITE);
g2.setStroke(new BasicStroke(2));
g2.fillArc(xx - 14, yy - 14, 30, 30, 0, 360);
}
public boolean isWin(int x, int y, int temp) {//X为横坐标,Y为纵坐标,temp为当前颜色
for (int i = 0; i < 11; i++) {//横
if (chess[i][y] == temp && chess[i + 1][y] == temp && chess[i + 2][y] == temp && chess[i + 3][y] == temp
&& chess[i + 4][y] == temp) {
return true;
}
}
for (int j = 0; j < 11; j++) {//竖
if (chess[x][j] == temp && chess[x][j + 1] == temp
&& chess[x][j + 2] == temp && chess[x][j + 3] == temp
&& chess[x][j + 4] == temp) {
return true;
}
}
for (int m = 4; m < 15; m++) {//左斜
for (int n = 0; n < 11; n++) {
if (chess[m][n] == temp && chess[m - 1][n + 1] == temp
&& chess[m - 2][n + 2] == temp
&& chess[m - 3][n + 3] == temp
&& chess[m - 4][n + 4] == temp) {
return true;
}
}
}
for (int p = 0; p < 11; p++) {//右斜
for (int q = 0; q < 11; q++) {
if (chess[p][q] == temp && chess[p + 1][q + 1] == temp
&& chess[p + 2][q + 2] == temp
&& chess[p + 3][q + 3] == temp
&& chess[p + 4][q + 4] == temp) {
return true;
}
}
}
return false;
}
public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
super.paintComponent(g2);
//String picPath = "image/qipan.jpg";
String picPath = "d:" + File.separator +"qipan.jpg" ;
Icon img = new ImageIcon(picPath);
img.paintIcon(this, g, 121, 121);
if (puter_Start) {
this.paint_WHITE(this.getGraphics(), 7 * 35 + 140, 7 * 35 + 140);
this.white_Opera(7, 7);
puter_Start = false;
flag_NewGame = true;
}
if (list != null) {
Iterator<A> it = list.iterator();
while (it.hasNext()) {
A a = (A) it.next();
if (a.get_a_Color() == 1) {
paint_BLACk(g, a.get_a_X() * 35 + 140,
a.get_a_Y() * 35 + 140);
} else if (a.get_a_Color() == 2) {
paint_WHITE(g, a.get_a_X() * 35 + 140,
a.get_a_Y() * 35 + 140);
}
}
}
}
class A {
private int a_X;
private int a_Y;
private int a_Color;
public A(int x, int y, int z) {
this.a_X = x;
this.a_Y = y;
this.a_Color = z;
}
public int get_a_X() {
return this.a_X;
}
public int get_a_Y() {
return this.a_Y;
}
public int get_a_Color() {
return this.a_Color;
}
}
public void mouseClicked(MouseEvent e) { if (flag_NewGame) {
int c = e.getButton();
String mouseInfo = null;
if (c == MouseEvent.BUTTON1) {
mouseInfo = "左键";
Point p = e.getPoint();
System.out.println(p.toString());
int x = (int) p.getX();
int y = (int) p.getY();
int xx = 0;
int yy = 0;
if (x >= 140 && x <= 630 && y >= 140 && y <= 630) {
int quotient_X = (x - 140) / 35;
int compliment_X = (x - 140) % 35;
int quotient_Y = (y - 140) / 35;
int compliment_Y = (y - 140) % 35;
if (compliment_X <= 17) {
xx = (quotient_X) * 35 + 140;
} else if (compliment_X > 17) {
quotient_X = quotient_X + 1;
xx = quotient_X * 35 + 140;
}
if (compliment_Y <= 17) {
yy = (quotient_Y) * 35 + 140;
} else if (compliment_Y > 17) {
quotient_Y = quotient_Y + 1;
yy = quotient_Y * 35 + 140;
}
System.out.println(xx + "---" + yy);
if (flag_BLACK && chess[quotient_X][quotient_Y] == 0) {
System.out.println(quotient_X + "---" + quotient_Y);
this.paint_BLACk(this.getGraphics(), xx, yy);
chess[quotient_X][quotient_Y] = 1;
list.add(new A(quotient_X, quotient_Y, 1));
for (int i = 0; i < 572; i++) {
if
(black_Win[quotient_X][quotient_Y][i]
&& win_Count[0][i] != -1) {
win_Count[0][i]++;
}
if
(white_Win[quotient_X][quotient_Y][i]) {
white_Win[quotient_X][quotient_Y][i] = false;
win_Count[1][i] = -1;
}
}
int temp = 1;
if (isWin(quotient_X, quotient_Y, temp)) {
JOptionPane.showMessageDialog(this, "黑棋赢");
Graphics g = this.getGraphics();
g.setColor(Color.RED);
g.setFont(new Font("华文行楷", 0,
20));
g.drawString("很帅的黑棋赢了", 300,
80);
flag_WHITE = false;
flag_BLACK = false;
flag_NewGame = false;
} else {
computer_chess();
}
} else if (flag_WHITE && chess[quotient_X][quotient_Y] == 0) {
} else {
JOptionPane.showMessageDialog(this, "该位置不能下子");
}
}
}
if (c == MouseEvent.BUTTON3) {
mouseInfo = "右键";
}
if (c == MouseEvent.BUTTON2) {
mouseInfo = "滚轴";
}
}
}
private void computer_chess() {
int[][] black_Score = new int[15][15];
int[][] white_Score = new int[15][15];
int white_Temp_Score = 0;
int black_Temp_Score = 0;
int white_x = 0;
int white_y = 0;
int black_x = 0;
int black_y = 0;
int x;
int y;
for (int i = 0; i < 15; i++) {
for (int j = 0; j < 15; j++) {
white_Score[i][j] = 0;
if (chess[i][j] == 0) {
for (int k = 0; k < 572; k++) {
if (white_Win[i][j][k]) {
switch (win_Count[1][k]) {
case 1:
white_Score[i][j] += 5;
break;
case 2:
white_Score[i][j] += 50;
break;
case 3:
white_Score[i][j] += 180;
break;
case 4:
white_Score[i][j] += 400;
break;
}
}
}
}
black_Score[i][j] = 0;
if (chess[i][j] == 0) {
for (int k = 0; k < 572; k++) { if (black_Win[i][j][k]) {
switch (win_Count[0][k]) {
case 1:
black_Score[i][j] += 6;
break;
case 2:
black_Score[i][j] += 52;
break;
case 3:
black_Score[i][j] += 181;
case 4:
black_Score[i][j] += 410;
break;
}
}
}
}
}
}
for (int i = 0; i < 15; i++) {
for (int j = 0; j < 15; j++) {
if (chess[i][j] == 0) {
if (white_Score[i][j] >= white_Temp_Score) {
white_x = i;
white_y = j;
white_Temp_Score = white_Score[i][j];
}
if (black_Score[i][j] >= black_Temp_Score) {
black_y = j;
black_Temp_Score = black_Score[i][j];
}
}
}
}
if (white_Temp_Score >= black_Temp_Score) {
x = white_x;
y = white_y;
} else {
x = black_x;
y = black_y;
}
paint_WHITE(this.getGraphics(), x * 35 + 140, y * 35 + 140);
white_Opera(x, y);
}
private void white_Opera(int x, int y) {
chess[x][y] = 2;
list.add(new A(x, y, 2));
for (int i = 0; i < 572; i++) {
if (white_Win[x][y][i] && win_Count[1][i] != -1) {
win_Count[1][i]++;
}
if (black_Win[x][y][i]) {
black_Win[x][y][i] = false;
win_Count[0][i] = -1;
}
}
int temp = 2;
if (isWin(x, y, temp)) {
JOptionPane.showMessageDialog(this, "白棋赢");
Graphics g = this.getGraphics();
g.setColor(Color.RED);
g.setFont(new Font("华文行楷", 0, 20));
g.drawString("很帅的白棋赢了", 300, 80);
flag_WHITE = false;
flag_BLACK = false;
flag_NewGame = false;
}
}
@Override
public void mouseEntered(MouseEvent e) { // TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent e) { // TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent e) { // TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
}
public class FiveChess{
public static void main(String[] args) {
start();
}
public static void start() {
ChessFrame cf = new ChessFrame();
cf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}。

相关文档
最新文档