java五子棋的源码.doc_第1页
java五子棋的源码.doc_第2页
java五子棋的源码.doc_第3页
java五子棋的源码.doc_第4页
java五子棋的源码.doc_第5页
已阅读5页,还剩12页未读 继续免费阅读

下载本文档

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

文档简介

package com.itany.chess;import javax.swing.JOptionPane;import javax.swing.JPanel;public class ChessModel /定义棋盘的宽度 高度 棋盘模式 (如30*20)private int width,height,chessModel;/定义方格的横向和纵向坐标private int x = 0, y = 0;/定义棋盘横向和纵向坐标对应的棋子的颜色/有值1,2,3,0 分别表示黑子、白子、空白、不能放子private int arrMap;/交换棋子的标识以及是否存在棋子的标识 isOdd为true时 1 为false时2private boolean isOdd,isExist;public ChessModel()/构造方法 初始化棋盘模型this.isOdd = true;/初始化模型panelInit(30,20,chessModel);/初始化棋盘的宽度和高度public void panelInit(int width,int height,int chessModel)this.width = width;this.height = height;this.chessModel = chessModel;arrMap = new intwidth+1height+1;for(int i = 0; i = width; i+)for(int j = 0; j = height; j+)arrMapij = 0;/获取是否交换棋手的标识public boolean getIsOdd()return this.isOdd;/this表示当前对象/设置交换棋手的标识public void setIsOdd(boolean isOdd)this.isOdd = isOdd;/获取棋盘方格是否有棋子的标识public boolean getIsEixst()return this.isExist;/获取棋盘的高度public int getHeight()return this.height;/获取棋盘的宽度public int getWidth()return this.width;/获取棋盘模式public int getModelChess()return this.chessModel;/获取棋盘方格上棋子信息public int getArrMap()return this.arrMap;/判断下子的横向和纵向坐标是否越界public boolean hadXY(int x,int y)if(x width)return true;/表示横向越界else if(y height)return true;/表示纵向越界return false;/表示不越界/判断某个方格是否已有棋子(x,y是方格的坐标)public boolean chessExist(int x,int y)if(arrMapxy = 1 | arrMapxy = 2)return true;/表示在(x,y)处已经有棋子return false;/表示(x,y)处还没有棋子/准备下棋(判断是否越界,是否已经存在棋子)public void readyPlay(int x,int y)if(hadXY(x,y)return;if(chessExist(x,y)return;arrMapxy = 3;/在该坐标位置下棋public void play(int x,int y)/判断是否越界if(hadXY(x,y)return;/判断是否存在棋子if(chessExist(x,y)this.isExist = true;return;elsethis.isExist = false;/既不越界 也没有棋子 则可以在该位置下棋/还需要知道下的是白棋还是黑棋if(this.getIsOdd()/当isOdd值为true时 1this.isOdd = false;/表示交换对手arrMapxy = 1;elsethis.isOdd = true;arrMapxy = 2;/记录计算机下子后的横向坐标public void setX(int x)this.x = x;/记录计算机下子后的纵向坐标public void setY(int y)this.y = y;/获取计算机下子后的横向坐标public int getX()return this.x;/获取计算机下子后的纵向坐标public int getY()return this.y;/计算棋盘上某一方格上八个方向棋子的最大值/这八个方向分别是:上,下,左,右,左上,右上,左下,右下public int checkMax(int x,int y,int black_or_white)int num = 0;/记录某一方向棋子的数目int max_num;/最大值int max_temp = 0;/记录4条线上的最大值。int x_temp = x,y_temp = y;int x_temp1 = x_temp, y_temp1 = y_temp;/判断右边 i 1=5for(int i = 1; i 5; i+)x_temp1 += 1;/ a += 1; a = a + 1;if(x_temp1 width)break;if(arrMapx_temp1y_temp1 = black_or_white)num+;elsebreak;/判断左边x_temp1 = x_temp;for(int i = 1; i 5; i+)x_temp1 -= 1;if(x_temp1 0)break;if(arrMapx_temp1y_temp1 = black_or_white)num+;elsebreak;if(num 5)max_temp = num;/判断上方x_temp1 = x_temp;y_temp1 = y_temp;num = 0;for(int i = 1; i 5; i+)y_temp1 -= 1;if(y_temp1 0)/越界break;if(arrMapx_temp1y_temp1 = black_or_white)num+;elsebreak;/判断下方y_temp1 = y_temp;for(int i = 1; i height)break;if(arrMapx_temp1y_temp1 = black_or_white)num+;elsebreak;if(num max_temp & num 5)max_temp = num;/判断左上x_temp1 = x_temp;y_temp1 = y_temp;num = 0;for(int i = 1; i 5; i+)x_temp1 -= 1;y_temp1 -= 1;if(x_temp1 0 | y_temp1 0)break;if(arrMapx_temp1y_temp1 = black_or_white)num+;elsebreak;/判断右下x_temp1 = x_temp;y_temp1 = y_temp;for(int i = 1; i width | y_temp1 height)break;if(arrMapx_temp1y_temp1 = black_or_white)num+;elsebreak;if(num max_temp & num 5)max_temp = num;/判断右上x_temp1 = x_temp;y_temp1 = y_temp;num = 0;for(int i = 1; i width | y_temp1 0)break;if(arrMapx_temp1y_temp1 = black_or_white)num+;elsebreak;/判断左下x_temp1 = x_temp;y_temp1 = y_temp;for(int i = 1; i 5; i+)x_temp1 -= 1;y_temp1 += 1;if(x_temp1 height)break;if(arrMapx_temp1y_temp1 = black_or_white)num+;elsebreak;if(num max_temp & num 6for(int i = 1; i width)break;if(arrMapx_temp1y_temp1 = arrvalue)num+;elsebreak;/ 判断左边x_temp1 = x_temp;for(int i = 1; i 6; i+)x_temp1 -= 1;if(x_temp1 0)break;if(arrMapx_temp1y_temp1 = arrvalue)num+;elsebreak;if(num = 5)return true;/ 判断上方x_temp1 = x_temp;y_temp1 = y_temp;num = 1;for(int i = 1; i 6; i+)y_temp1 -= 1;if(y_temp1 0)break;if(arrMapx_temp1y_temp1 = arrvalue)num+;elsebreak;/ 判断下方y_temp1 = y_temp;for(int i = 1; i height)break;if(arrMapx_temp1y_temp1 = arrvalue)num+;elsebreak;if(num = 5)return true;/ 判断左上x_temp1 = x_temp;y_temp1 = y_temp;num = 1;for(int i = 1; i 6; i+)x_temp1 -= 1;y_temp1 -= 1;if(x_temp1 0 | y_temp1 0)break;if(arrMapx_temp1y_temp1 = arrvalue)num+;elsebreak;/ 判断右下x_temp1 = x_temp;y_temp1 = y_temp;for(int i = 1; i width | y_temp1 height)break;if(arrMapx_temp1y_temp1 = arrvalue)num+;elsebreak;if(num = 5)return true;/ 判断右上x_temp1 = x_temp;y_temp1 = y_temp;num = 1;for(int i = 1; i width | y_temp1 0)break;if(arrMapx_temp1y_temp1 = arrvalue)num+;elsebreak;/ 判断左下x_temp1 = x_temp;y_temp1 = y_temp;for(int i = 1; i 6; i+)x_temp1 -= 1;y_temp1 += 1;if(x_temp1 height)break;if(arrMapx_temp1y_temp1 = arrvalue)num+;elsebreak;if(num = 5)return true;return false;/计算机走棋public void computerDo(int width,int height)int max_black,max_white,max_temp,max = 0;setIsOdd(true);System.out.println(计算机走棋.);for(int i = 0; i = width; i+)for(int j = 0; j max)max = max_temp;this.x = i;this.y = j;setX(this.x);setY(this.y);this.arrMapthis.xthis.y = 2;/赢棋后的显示public void showSuccess(JPanel jp)JOptionPane.showMessageDialog(jp, 恭喜您,您赢了!,WIN,JOptionPane.INFORMATION_MESSAGE);/输棋后的提示public void showLost(JPanel jp)JOptionPane.showMessageDialog(jp, 对不起,您输了!继续加油!,LOST,JOptionPane.INFORMATION_MESSAGE);package com.itany.chess;import java.awt.Dimension;import java.awt.Toolkit;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.JFrame;import javax.swing.JMenu;import javax.swing.JMenuBar;import javax.swing.JMenuItem;import javax.swing.JOptionPane;import javax.swing.SwingUtilities;import javax.swing.UIManager;public class FiveChess extends JFrame implements ActionListener /要使棋盘最终显示在屏幕中央private Toolkit kit = Toolkit.getDefaultToolkit();private Dimension screen = kit.getScreenSize();private int screenWidth = screen.width;private int screenHeight = screen.height;public static boolean isComputer = true;public static boolean checkComputer = true;private int width = 650,height = 500;/窗口的大小private ChessModel cm;private MainPanel mp;public FiveChess(String title)super(title);this.setSize(width, height);cm = new ChessModel();mp = new MainPanel(cm);this.getContentPane().add(mp,Center);this.setResizable(false);/添加菜单并注册事件监听器/创建菜单栏对象JMenuBar jmb = new JMenuBar();/创建第一个菜单JMenu gameMenu = new JMenu(游戏);/为“游戏”菜单创建两个菜单项 并注册事件监听器JMenuItem resItem = new JMenuItem(重新开始);resItem.addActionListener(this);JMenuItem exitItem = new JMenuItem(退出);exitItem.addActionListener(this);/将菜单项添加到菜单中gameMenu.add(resItem);gameMenu.add(exitItem);/将菜单添加到菜单栏中jmb.add(gameMenu);JMenu helpMenu = new JMenu(帮助);JMenuItem aboutItem = new JMenuItem(关于我们);aboutItem.addActionListener(this);helpMenu.add(aboutItem);jmb.add(helpMenu);/为窗口设置菜单栏this.setJMenuBar(jmb);/改变外观try UIManager.setLookAndFeel(com.sun.java.swing.plaf.motif.MotifLookAndFeel);SwingUtilities.updateComponentTreeUI(this); catch (Exception e) e.printStackTrace(); this.setLocation(screenWidth/4, screenHeight/4);/事件监听器的处理方法public void actionPerformed(ActionEvent e) String s = e.getActionCommand();/获得按钮命令值if(s.equals(重新开始)restart();else if(s.equals(退出)System.exit(0);else if(s.equals(关于我们)JOptionPane.showMessageDialog(this, 这是一个神奇的五子棋-ww,FiveChess,JOptionPane.INFORMATION_MESSAGE);/重新开始 时间对应的处理方法public void restart()cm = new ChessModel();chessSize(cm.getWidth(),cm.getHeight();public void chessSize(int w, int h) setSize(w * 20 + 50, h * 20 + 100);if (!FiveChess.checkComputer) FiveChess.isComputer = false; else FiveChess.isComputer = true;mp.setModel(cm);mp.repaint();public static void main(String args) FiveChess fiveCh = new FiveChess(五子棋游戏);fiveCh.setVisible(true);fiveCh.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);package com.itany.chess;import java.awt.Color;import java.awt.Graphics;import java.awt.event.MouseEvent;import java.awt.event.MouseListener;import java.awt.event.MouseMotionListener;import javax.swing.JPanel;public class MainPanel extends JPanel implements MouseListener,MouseMotionListener private int width,height;/棋盘的宽度和高度private ChessModel cm;/棋盘模式/根据棋盘模式设定棋盘大小public MainPanel(ChessModel cm)/为属性赋值this.cm = cm;this.width = cm.getWidth();this.height = cm.getHeight();/并注册事件监听器this.addMouseListener(this);/根据棋盘模式设定棋盘的宽度和高度public void setModel(ChessModel cm)/为属性赋值this.cm = cm;this.width = cm.getWidth();this.height = cm.getHeight();/根据提供的棋子信息(颜色和坐标)画棋子 v的值为1表示是黑棋 为2时表示是白棋public void draw(Graphics gp,int i, int j, int v)int x = 20*i+20;int y = 20*j+20;/画棋盘if(i != width & j != height)gp.setColor(Color.yellow);gp.drawRect(x, y, 20, 20);/画黑色棋子if(v = 1)gp.setColor(Color.black);gp.fillOval(x-8, y-8, 16, 16);/画白色棋子if(v = 2)gp.setColor(Color.white);gp.fillOval(x-8, y-8, 16, 16);/根据坐标计算出棋盘方格棋子的信息(如白子或者黑子)/然后使用draw方法在棋盘中画出相应的棋子public void paintComponent(Graphics gp)super.paintComponent(gp);/二重for循环通过arrMap数组获得对应坐标上的值然后调用上面的draw方法画棋盘for(int i = 0; i = wid

温馨提示

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

评论

0/150

提交评论