五子棋游戏软件设计.doc_第1页
五子棋游戏软件设计.doc_第2页
五子棋游戏软件设计.doc_第3页
五子棋游戏软件设计.doc_第4页
五子棋游戏软件设计.doc_第5页
已阅读5页,还剩19页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

五子棋游戏软件设计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 int2572;private boolean white_Win = new boolean1515572;private boolean black_Win = new boolean1515572;private int chess = new int1515;public LinkedList list = new LinkedList();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_Winj + kicount = true;white_Winj + kicount = true;count+;/ 竖for (int i = 0; i 15; i+) for (int j = 0; j 11; j+) for (int k = 0; k 5; k+) black_Winij + kcount = true;white_Winij + kcount = true;count+;/ 右斜for (int i = 0; i 11; i+) for (int j = 0; j 11; j+) for (int k = 0; k 5; k+) black_Winj + ki + kcount = true;white_Winj + ki + kcount = true;count+;/ 左斜for (int i = 0; i = 4; j-) for (int k = 0; k 5; k+) black_Winj - ki + kcount = true;white_Winj + -ki + kcount = true;count+;System.out.println(count- + count);for (int m = 0; m 2; m+) for (int n = 0; n 572; n+) win_Countmn = 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 (chessiy = temp & chessi + 1y = temp& chessi + 2y = temp & chessi + 3y = temp& chessi + 4y = temp) return true;for (int j = 0; j 11; j+) /竖if (chessxj = temp & chessxj + 1 = temp& chessxj + 2 = temp & chessxj + 3 = temp& chessxj + 4 = temp) return true;for (int m = 4; m 15; m+) /左斜for (int n = 0; n 11; n+) if (chessmn = temp & chessm - 1n + 1 = temp& chessm - 2n + 2 = temp& chessm - 3n + 3 = temp& chessm - 4n + 4 = temp) return true;for (int p = 0; p 11; p+) /右斜for (int q = 0; q 11; q+) if (chesspq = temp & chessp + 1q + 1 = temp& chessp + 2q + 2 = temp& chessp + 3q + 3 = temp& chessp + 4q + 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 (ChessFputer_Start) this.paint_WHITE(this.getGraphics(), 7 * 35 + 140, 7 * 35 + 140);this.white_Opera(7, 7);ChessFputer_Start = false;flag_NewGame = true;if (list != null) Iterator 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 = 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) quotient_X = quotient_X + 1;xx = quotient_X * 35 + 140;if (compliment_Y 17) quotient_Y = quotient_Y + 1;yy = quotient_Y * 35 + 140;System.out.println(xx + - + yy);if (flag_BLACK & chessquotient_Xquotient_Y = 0) System.out.println(quotient_X + - + quotient_Y);this.paint_BLACk(this.getGraphics(), xx, yy);chessquotient_Xquotient_Y = 1;list.add(new A(quotient_X, quotient_Y, 1);for (int i = 0; i 572; i+) if (black_Winquotient_Xquotient_Yi& win_Count0i != -1) win_Count0i+;if (white_Winquotient_Xquotient_Yi) white_Winquotient_Xquotient_Yi = false;win_Count1i = -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 & chessquotient_Xquotient_Y = 0) else JOptionPane.showMessageDialog(this, 该位置不能下子);if (c = MouseEvent.BUTTON3) mouseInfo = 右键;if (c = MouseEvent.BUTTON2) mouseInfo = 滚轴;private void computer_chess() int black_Score = new int1515;int white_Score = new int1515;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_Scoreij = 0;if (chessij = 0) for (int k = 0; k 572; k+) if (white_Winijk) switch (win_Count1k) case 1:white_Scoreij += 5;break;case 2:white_Scoreij += 50;break;case 3:white_Scoreij += 180;break;case 4:white_Scoreij += 400;break;black_Scoreij = 0;if (chessij = 0) for (int k = 0; k 572; k+) if (black_Winijk) switch (win_Count0k) case 1:black_Scoreij += 6;break;case 2:black_Scoreij += 52;break;case 3:black_Scoreij += 181;break;case 4:black_Scoreij += 410;break;for (int i = 0; i 15; i+) for (int j = 0; j = white_Temp_Score) white_x = i;white_y = j;white_Temp_Score = white_Scoreij;if (black_Scoreij = black_Temp_Score) black_x = i;black_y = j;black_Temp_Score = black_Scoreij;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) chessxy = 2;list.add(new A(x, y, 2);for (int i = 0; i 572; i+) if (w

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论