JAVA课程设计-五子棋游戏.doc

上传人:11****ws 文档编号:3179722 上传时间:2019-05-24 格式:DOC 页数:13 大小:100KB
下载 相关 举报
JAVA课程设计-五子棋游戏.doc_第1页
第1页 / 共13页
JAVA课程设计-五子棋游戏.doc_第2页
第2页 / 共13页
JAVA课程设计-五子棋游戏.doc_第3页
第3页 / 共13页
JAVA课程设计-五子棋游戏.doc_第4页
第4页 / 共13页
JAVA课程设计-五子棋游戏.doc_第5页
第5页 / 共13页
点击查看更多>>
资源描述

1、package chess_game;import java.awt.Color;import java.io.Serializable;public class Point implements Serializableprivate int x;private int y;private Color color;public Point()public Point(int x,int y,Color color)this.x = x;this.y = y;this.color = color;public int getX()return x;public int getY()return

2、 y;public Color getColor()return color;package chess_game;public class IsWin public static boolean winLR(int x,int y) /判断左右方向是否存在连续五子int countnum=0;/用来记录一方连续棋子个数boolean flag = false;for(int i=y;i=0;i-) /先向左找if(ChessBoard.boardxi=ChessBoard.boardxy)countnum+;else break;for(int i=y+1;i=5)flag=true; /左

3、右方向存在连续五子return flag; /flag 为 true 时,表示已判断出输赢public static boolean winUD(int x,int y) /判断上下方向是否存在连续五子int countnum=0;boolean flag = false;for(int i=x;i=0;i-) /先向上找if(ChessBoard.boardiy=ChessBoard.boardxy)countnum+;elsebreak;for(int i=x+1;i=5)flag=true; /上下方向存在连续五子return flag;public static boolean win

4、RD(int x,int y) /判断右对角线是否存在连续五子int countnum=0;boolean flag = false;for(int i=x,j=y;i=0i-,j-) /先向左上方向寻找if(ChessBoard.boardij=ChessBoard.boardxy)countnum+;elsebreak;for(int i=x+1,j=y+1;i=5)flag=true; /右对角线方向存在连续五子return flag;public static boolean winLU(int x,int y) /判断左对角线是否存在连续五子int countnum=0;boolea

5、n flag = false;for(int i=x,j=y;i=0;i+,j-) /先向右上方向寻找if(ChessBoard.boardij=ChessBoard.boardxy)countnum+;elsebreak;for(int i=x-1,j=y+1;i=0 /左对角线方向存在连续五子return flag;public static boolean isWin(int x,int y) /调用之前的四个方向的函数,有一个方向寻在连续五子,即停止return winLR(x,y)|winUD(x,y)|winRD(x,y)|winLU(x,y);package chess_game

6、;import java.awt.BasicStroke;import java.awt.Color;import java.awt.Graphics;import java.awt.Graphics2D;import java.awt.Shape;import java.awt.Stroke;import java.awt.event.MouseAdapter;import java.awt.event.MouseEvent;import java.awt.geom.Ellipse2D;import java.util.ArrayList;import java.util.List;impo

7、rt javax.swing.JFrame;import javax.swing.JPanel;public class ChessBoard extends JPanelpublic static final int upSpace = 40; /距离上面的间距public static final int leftSpace = 40;/距离左边的间距public static final int rowSpace = 40; /行间距public static final int colSpace = 40; /列间距public static final int rowNumber =

8、 13;/13*20 方格public static final int colNumber = 20;public static final int board = new intcolNumber+1rowNumber+1;public static final int r = 15;public static boolean flag = false;public Point point = new Point(rowNumber+1)*(colNumber+1);List lists = new ArrayList();/用于存放点过的点类的集合public int pointCoun

9、t =0;public void paint(Graphics g)super.paint(g);int m=0,n=0;Graphics2D g1 = (Graphics2D)g; /用画一个填充圆Graphics g2 = (Graphics2D)g; /用于给最后一个落子加上圆圈Stroke s = new BasicStroke(2.0f);/设置画笔粗细,参数为 float 类型g1.setStroke(s);for(int i = 0;i=1)g2.setColor(Color.RED);g2.drawOval(leftSpace-r+pointpointCount-1.getX(

10、)*colSpace,upSpace-r+pointpointCount-1.getY()*rowSpace,2*r,2*r);package chess_game;import java.awt.BorderLayout;import java.awt.Color;import java.awt.FlowLayout;import java.awt.Graphics2D;import java.awt.GridLayout;import java.awt.LayoutManager;import java.awt.Shape;import java.awt.event.ActionEvent

11、;import java.awt.event.ActionListener;import java.awt.event.MouseAdapter;import java.awt.event.MouseEvent;import java.awt.event.MouseListener;import java.awt.geom.Ellipse2D;import java.io.BufferedReader;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import ja

12、va.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.io.ObjectInputStream;import java.io.ObjectOutputStream;import java.io.OutputStream;import java.io.OutputStreamWriter;import java.io.PrintWriter;import javax.swing.ImageIcon;impor

13、t javax.swing.JButton;import javax.swing.JComboBox;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JMenu;import javax.swing.JMenuBar;import javax.swing.JMenuItem;import javax.swing.JOptionPane;import javax.swing.JPanel;import javax.swing.JTextField;public class ChessFrame exte

14、nds JFramepublic JPanel mainPanel; /主面板public JPanel northPanel,northPanel1,northPanel3;public JPanel southPanel,southPanel1,southPanel2;public ChessBoard boardPanel; public JLabel tipsLlabel;public JMenuBar menubar;public JMenu menu;public JMenuItem exitMenu;public JMenuItem saveMenu;public JMenuIt

15、em readMenu;public JButton start,lastStep,nextStep,reStart,exit,regret;public boolean isBlack = true;public boolean iswin = false;public boolean flag = false;/开始游戏后,flag 赋值为 trueFile file;public void createBar()/创建菜单menubar = new JMenuBar();menu = new JMenu(“菜单“);exitMenu = new JMenuItem(“退出“);saveM

16、enu = new JMenuItem(“保存游戏“);readMenu = new JMenuItem(“读取游戏“);saveMenu.addActionListener(new MyClick();readMenu.addActionListener(new MyClick();exitMenu.addActionListener(new MyClick();menubar.add(menu);menu.add(saveMenu);menu.add(readMenu);menu.add(exitMenu);public void createLabel()/创建各个标签tipsLlabe

17、l = new JLabel(“开始游戏 或 读取游戏,请点击下面的按钮“);start = new JButton(“开 始“);lastStep = new JButton(“上一步“);nextStep = new JButton(“下一步“);reStart = new JButton(“重新开始“);exit = new JButton(“退出“);regret = new JButton(“悔棋“);public void createPanel()/创建面板createBar();createLabel();mainPanel = new JPanel(new BorderLay

18、out();boardPanel = new ChessBoard();boardPanel.setBackground(new Color(204,153,0);northPanel = new JPanel(new GridLayout(2,1);southPanel = new JPanel(new BorderLayout();southPanel1 = new JPanel(new FlowLayout();southPanel2 = new JPanel();northPanel1 = new JPanel(new BorderLayout();northPanel3 = new

19、JPanel(new FlowLayout();northPanel.add(northPanel1);northPanel.add(northPanel3);northPanel1.add(menubar,BorderLayout.WEST);northPanel3.add(tipsLlabel);southPanel1.add(start);southPanel1.add(exit);southPanel2.add(regret);southPanel2.add(reStart);southPanel.add(southPanel1,BorderLayout.CENTER);southPa

20、nel.add(southPanel2,BorderLayout.EAST);mainPanel.add(boardPanel,BorderLayout.CENTER);mainPanel.add(northPanel,BorderLayout.NORTH);mainPanel.add(southPanel, BorderLayout.SOUTH);this.setContentPane(mainPanel);public void innit() throws IOException/初始化createPanel();this.setTitle(“五子棋游戏“);this.setSize(9

21、50, 730);this.setVisible(true);file = new File(“E:GameSave“);File file2 = new File(file,“listsSize.txt“);file.mkdirs();file2.createNewFile();public void save() throws IOExceptionfor(int i=0;iboardPanel.pointCount;i+)boardPanel.lists.add(boardPanel.pointi);OutputStream output = new FileOutputStream(“

22、E:GameSavelistsSize.txt“);/用于存在 lists 的长度output.write(boardPanel.lists.size();FileOutputStream out = new FileOutputStream(“E:GameSaveGameSave.txt“);ObjectOutputStream objout = new ObjectOutputStream(out);for(int i=0;iboardPanel.lists.size();i+)objout.writeObject(boardPanel.lists.get(i);objout.flush(

23、);public void load() throws IOException, ClassNotFoundExceptionboardPanel.pointCount=0;for(int a=0;aboardPanel.board.length;a+)for(int b=0;bboardPanel.boarda.length;b+)boardPanel.boardab = 0;FileInputStream in = new FileInputStream(“E:GameSaveGameSave.txt“);ObjectInputStream objin = new ObjectInputS

24、tream(in);Point p = new Point();InputStream input = new FileInputStream(“E:GameSavelistsSize.txt“);int size = input.read();for(int i=0;isize;i+)p=(Point)objin.readObject();int x =p.getX();int y =p.getY();boardPanel.boardxy=(p.getColor().equals(Color.BLACK)?1:2;isBlack = !p.getColor().equals(Color.BL

25、ACK);tipsLlabel.setText(isBlack?“请黑方落子“:“请白方落子“);boardPanel.repaint();class MyClick implements ActionListener/通过 ActionListener 的子类来添加菜单的鼠标事件Overridepublic void actionPerformed(ActionEvent e) if(e.getSource()=exitMenu)System.exit(0);else if(flag catch (IOException e1) / TODO Auto-generated catch blo

26、cke1.printStackTrace();JOptionPane.showMessageDialog(ChessFrame.this, “保存游戏在E:GameSaveGameSave.txt 文件成功!“);else if(flag catch (IOException e1) / TODO Auto-generated catch blocke1.printStackTrace(); catch (ClassNotFoundException e1) / TODO Auto-generated catch blocke1.printStackTrace();JOptionPane.sh

27、owMessageDialog(ChessFrame.this, “成功读取上次保存的游戏!“);public void addButtonClick() /添加按钮的鼠标事件start.addMouseListener(new MouseAdapter() /添加开始按钮的鼠标事件public void mouseClicked(MouseEvent e) super.mouseClicked(e);/if(!flag)if(e.getButton()=MouseEvent.BUTTON1)tipsLlabel.setText(“游戏开始,请黑方落子 “);start.setEnabled(

28、false);flag = true;/ );exit.addMouseListener(new MouseAdapter() /添加退出的鼠标事件Overridepublic void mouseClicked(MouseEvent e) super.mouseClicked(e);if(e.getButton()=MouseEvent.BUTTON1)System.exit(0););reStart.addMouseListener(new MouseAdapter() /添加重新开始的鼠标事件Overridepublic void mouseClicked(MouseEvent e) super.mouseClicked(e);if(flag&(e.getButton()=MouseEvent.BUTTON1)

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 教育教学资料库 > 精品笔记

Copyright © 2018-2021 Wenke99.com All rights reserved

工信部备案号浙ICP备20026746号-2  

公安局备案号:浙公网安备33038302330469号

本站为C2C交文档易平台,即用户上传的文档直接卖给下载用户,本站只是网络服务中间平台,所有原创文档下载所得归上传人所有,若您发现上传作品侵犯了您的权利,请立刻联系网站客服并提供证据,平台将在3个工作日内予以改正。