900字范文,内容丰富有趣,生活中的好帮手!
900字范文 > 简易人机对弈算法的五子棋程序

简易人机对弈算法的五子棋程序

时间:2021-04-19 15:19:59

相关推荐

简易人机对弈算法的五子棋程序

要求的五子棋游戏应达到以下几方面的要求:

(1)运行程序后即刻出现棋盘并可以开始下棋;(2)人机对弈时,先手为黑棋,后手为白棋;人为先手,计算机为后手;(3)程序能响应鼠标点击并在相应位置画出棋子;(4)计算机具有一定的智能,能够与人进行五子棋对弈;(5)程序可以正确判断出局面的胜负情况并及时报出结果,终止棋局;(6)尽可能实现人人对战功能;(7)重新开始:在游戏途中,可以选择重新开始,中断游戏并重新开始。设计人机对弈五子棋程序,实现人与计算机进行五子棋对弈,具体任务如下:(1)棋盘布局设计(2)能进行基本的五子棋操作,有图形界面,能够用鼠标进行操作;(3)编程实现棋子的绘制;(4)增加具体的功能如重开一局等功能;(5)胜负判断的功能,程序能对下棋的结果进行判断,分出胜负,并显示获胜方;(6)运行测试程序,调试纠正运行错误。(7)游戏内容:采用无“禁手”的规则,相同颜色的棋子,五子一线为胜。2.3 工作原理(1)工作过程运行程序后,首先出现五子棋游戏窗体,用户点击游戏菜单项,出现相关子菜单,选择模式子菜单中的人机对弈便可进入人机对弈模式。然后移动鼠标,在窗体的棋盘上点击鼠标下子,即可在棋盘中相应位置显示下的对应棋子。接下来计算机会根据用户下的棋,采取穷举法选择八个方向上赢面最大的位置下子,用户与计算机谁先达到五子连成一线,谁便是获胜方。若选择模式子菜单中的人人对战便可进入人人对战模式。两个用户可同时玩游戏,同样谁先达到五子连成一线,谁便是获胜方。若双方势均力敌,可选择游戏菜单项中的棋盘子菜单,点击大号棋盘或超大号棋盘,以便避免棋盘空间不足造成无法分出胜负的情况。若觉得窗体外观不太美观,可选择外观菜单中的三个选项进行选择。为了显示程序的相关制作信息,程序提供了版本菜单进行显示。具体功能实现见第四章节运行调试及分析讨论。(2)工作原理利用图形界面编程实现,利用到的知识点包括:窗体的创建和应用界面布局;事件处理,包括按钮的动作事件处理,鼠标事件,窗体事件;接口知识:事件处理中通过接口约束事件监听者必须实现相应接口中定义的方法;流程控制语句:通过条件判断区分不同事件源,作不同操作处理。棋子状态控制:通过arrMapShow数组来判断是下什么颜色的棋子,如果是计算机下棋,则计算棋盘上某一放个八个方向上棋子的最大值,通过调用readyplay()方法判断某位置是否可下棋。3、 具体实现总共涉及五类,分别为:ChessFrame、ChessModel、ChessWindowEvent、FiveChessAppletDemo、MainPanel。引入的java包为:import java.awt.*;import java.awt.event.*; import java.applet.*; import javax.swing.*; import java.io.PrintStream; import javax.swing.JComponent; import javax.swing.JPanel;3.1 类设计(1) 类名:FiveChessAppletDemo作用:显示五子棋游戏窗体。类中含有的方法介绍:main()方法:程序运行的入口,创建了ChessFrame类的一个实例对象(cf),并启动屏幕显示显示该实例对象。(2) 类名:ChessFrame作用:创建五子棋游戏主窗体和菜单。该类继承JFrame类,实现ActionListener接口。类中含有的方法介绍:ChessFrame()构造方法:构造五子棋游戏的主窗体;makeMenu()方法:构造五子棋游戏的主菜单;makeMenuItem()方法:构造五子棋游戏的菜单项;makeRadioButtonMenuItem()方法:构造五子棋游戏的单选按钮式菜单项;mapsize()方法:获取棋盘大小;restart()方法:重新开局;actionperformed()方法:异常的捕获及不同模式、不同棋盘大小以及下拉菜单功能的实现。(3) 类名:ChessModel作用:实现了整个五子棋程序算法的核心。采用穷举法判断八个方向上存在棋子的最大值进行对弈。类中含有的主要方法介绍:ChessModel()构造方法:根据不同的棋盘模式(modeChess)来构建对应大小的棋盘;PanelInit()方法:按照棋盘模式构建棋盘大小;badxy()方法:判断下子的横向、纵向坐标是否越界;chessExist()方法:计算棋盘上某一方格上八个方向棋子的最大值,(这八个方向分别是:左、右、上、下、左上、左下、右上、右下);readyplay()方法:判断该坐标位置是否可下棋子;play()方法:在该坐标位置下棋子;computerDo()方法:用穷举法判断每一个坐标点的八个方向的的最大棋子数,最后得出棋子数最大值的坐标,下子;checkMax()方法:计算棋盘上某一方格上八个方向棋子的最大值;judgeSuccess()方法:判断胜负;showSuccess()方法:赢棋后的提示;showDefeat()方法:输棋后的提示。(4) 类名:MainPanel作用:构建一个面板,在该面板上画上棋盘;处理在该棋盘上的鼠标事件(如鼠标左键点击、鼠标右键点击、鼠标拖动等);该类继承JPanel类,实现MouseListener、MouseMotionListener接口。类中含有的主要方法介绍:ChessModel()方法:根据棋盘模式设定面板的大小;setModel()方法:根据棋盘模式设定棋盘的宽度和高度;paintComponent()方法:根据坐标计算出棋盘方格棋子的信息(如白子还是黑子),然后调用draw方法在棋盘上画出相应的棋子;draw()方法:根据提供的棋子信息(颜色、坐标)画棋子;mousePressed ()方法:响应鼠标的点击事件,根据鼠标的点击来下棋,根据下棋判断胜负、判断是否人机对弈等;mouseMoved()方法:响应鼠标的拖动事件。(5) 类名: ChessWindowEvent作用:响应退出窗口。该类继承WindowAdapter。类中含有的方法介绍:windowClosing()方法:处理窗口关闭;ChessWindowEvent ()构造方法:提供无参构造方法。3.2 对弈策略采用readyplay()方法判断该位置是否可以下棋,然后用穷举法判断每一个坐标点的八个方向,即左、右、上、下、左上、左下、右上、右下,棋子的最大值,最后得出最大值的坐标,下子。最后采用judgeCuccess()方法,判断八个方向上是否满五个同色棋子,若有,则成功,反之则失败。4、运行调试与分析讨论

图4-1 程序运行初始界面

图4-2 人机/人人对弈模式选择界面

图4-3 人机对弈界面

图4-4棋盘选择界面

#源码和注释

package 五子棋;import javafx.stage.WindowEvent;import java.awt.*;import java.awt.event.*;import java.applet.*;import javax.swing.*;import java.io.PrintStream;import java.sql.SQLOutput;import javax.swing.JComponent;import javax.swing.JPanel;/* *main 方法创建了 ChessFrame 类的一个实例对象(cf), *并启动屏幕显示显示该实例对象。 **/public class FiveChessAppletDemo {public static void main(String args[]){ChessFrame cf= new ChessFrame();cf.show();}}/* *类 ChessFrame 主要功能是创建五子棋游戏主窗体和菜单 **/class ChessFrame extends JFrame implements ActionListener{private String[] strsize={"20x15","30x20","40x30","50x50"};private String[] strmode={"人机对弈","人人对弈"};public static boolean iscomputer=true,checkcomputer=true;private int width,height;private ChessModel cm;private MainPanel mp;//构造五子棋游戏的主窗体public ChessFrame() {this.setTitle("五子棋游戏"); //菜单标题cm=new ChessModel(1); //创建棋盘,默认第一种模式mp=new MainPanel(cm); //落子用的画笔,处理鼠标点击事件Container con=this.getContentPane(); //获得当前JFramede的内容面板con.add(mp,"Center");//添加面板对象到当前的jframethis.setResizable(false); //设置当前窗口不能自由改变大小this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //关闭窗口就结束程序this.addWindowListener(new WindowAdapter(){ //匿名内部类public void windowClosing(WindowEvent e){System.exit(0);}}); //表示在窗口添加一个Windows事件消息,用于退出MapSize(20,15);//棋盘大小JMenuBar mbar=new JMenuBar(); //创建菜单栏对象this.setJMenuBar(mbar);JMenu gameMenu=new JMenu("游戏"); //创建菜单栏游戏mbar.add(makeMenu(gameMenu,new Object[] { "开局", "棋盘","模式",null,"退出" },this));JMenu lookMenu=new JMenu("视图");mbar.add(makeMenu(lookMenu,new Object[] { "Metal","Motif","Windows" },this));JMenu helpMenu=new JMenu("帮助");mbar.add(makeMenu(helpMenu,new Object[] { "关于" },this));}//构造五子棋游戏的主菜单public JMenu makeMenu(Object parent,Object items[],Object target){JMenu m=null;if(parent instanceof JMenu)m=(JMenu)parent;else if(parent instanceof String)m=new JMenu((String)parent);elsereturn null;for(int i= 0;i<items.length;i++)if(items[i]== null)m.addSeparator(); //如果为空,在菜单项添加分割线else if(items[i] == "棋盘"){JMenu jm=new JMenu("棋盘");ButtonGroup group=new ButtonGroup(); //每个JRadioButton都是独立的,需要加入到ButtonGroup对象,JRadioButtonMenuItem rmenu; //类表示可以包含在菜单中的复选框。选中菜单中的复选框可将控件的状态从打开更改为关闭或从关闭更改为打开。for(int j=0;j<strsize.length;j++){rmenu=makeRadioButtonMenuItem(strsize[j],target);if(j==0) rmenu.setSelected(true);jm.add(rmenu);group.add(rmenu);}m.add(jm);}else if(items[i]== "模式"){JMenu jm=new JMenu("模式");ButtonGroup group=new ButtonGroup();JRadioButtonMenuItem rmenu;for(int h=0;h<strmode.length;h++){ //strmode 人机对弈和人人对弈rmenu=makeRadioButtonMenuItem(strmode[h],target);if(h==0)rmenu.setSelected(true);//默认人机对弈jm.add(rmenu);group.add(rmenu);}m.add(jm);}elsem.add(makeMenuItem(items[i],target));return m;}//构造五子棋游戏的菜单项public JMenuItem makeMenuItem(Object item,Object target){JMenuItem r= null;if(item instanceof String)r=new JMenuItem((String)item);else if(item instanceof JMenuItem)r=(JMenuItem)item;elsereturn null;if(target instanceof ActionListener)r.addActionListener((ActionListener)target);return r;}//构造五子棋游戏的单选按钮式菜单项public JRadioButtonMenuItem makeRadioButtonMenuItem( Object item,Object target){JRadioButtonMenuItem r= null;if(item instanceof String)r=new JRadioButtonMenuItem((String)item);else if(item instanceof JRadioButtonMenuItem)r=(JRadioButtonMenuItem)item;elsereturn null;if(target instanceof ActionListener)r.addActionListener((ActionListener)target);return r;}public void MapSize(int w,int h){setSize(w*20+50,h*20+100);if(this.checkcomputer)this.iscomputer=true;elsethis.iscomputer=false;mp.setModel(cm); //设置棋盘大小的方法mp.repaint();//component中己有的图形发生变化后不会立刻显示,须使用repaint方法}public boolean getiscomputer(){return this.iscomputer;}//返回是否为人机对弈模式public void restart(){//重新开局的方法int modeChess=cm.getModeChess();if(modeChess<= 4&&modeChess>= 1){cm=new ChessModel(modeChess);MapSize(cm.getWidth(),cm.getHeight());}else{System.out.println("\u81EA\u5B9A\u4E49");}}public void actionPerformed(ActionEvent e){String arg=e.getActionCommand();try{ //视图风格if(arg.equals("Windows"))UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");else if(arg.equals("Motif"))UIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel");else UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");SwingUtilities.updateComponentTreeUI(this);}catch(Exception ee){}if(arg.equals("20x15")){this.width=20;this.height=15;cm=new ChessModel(1);MapSize(this.width,this.height);SwingUtilities.updateComponentTreeUI(this);}if(arg.equals("30x20")){this.width=30;this.height=20;cm=new ChessModel(2);MapSize(this.width,this.height);SwingUtilities.updateComponentTreeUI(this);}if(arg.equals("40x30")){this.width=40;this.height=30;cm=new ChessModel(3);MapSize(this.width,this.height);SwingUtilities.updateComponentTreeUI(this);}if(arg.equals("50x50")){this.width=50;this.height=50;cm=new ChessModel(4);MapSize(this.width,this.height);SwingUtilities.updateComponentTreeUI(this);}if(arg.equals("人机对弈")){this.checkcomputer=true;this.iscomputer=true;cm=new ChessModel(cm.getModeChess());MapSize(cm.getWidth(),cm.getHeight());SwingUtilities.updateComponentTreeUI(this);}if(arg.equals("人人对弈")){this.checkcomputer=false;this.iscomputer=false;cm=new ChessModel(cm.getModeChess());MapSize(cm.getWidth(),cm.getHeight());SwingUtilities.updateComponentTreeUI(this);}if(arg.equals("开局")){ restart(); }if(arg.equals("关于"))JOptionPane.showMessageDialog(this," 制作者:天天都不知道吃什么小组", "关于",0);if(arg.equals("退出")) System.exit(0);}}/* *类 ChessModel 实现了整个五子棋程序算法的核心 */class ChessModel{ //棋盘的宽度、高度、棋盘的模式(如 20×15)private int width,height,modeChess; //棋盘方格的横向、纵向坐标private int x=0,y=0; //棋盘方格的横向、纵向坐标所对应的棋子颜色,//数组arrMapShow 只有4个值:1,2,3,-5,//其中 1 代表该棋盘方格上下的棋子为黑子,//2 代表该棋盘方格上下的棋子为白子,//3 代表为该棋盘方格上没有棋子,//-5 代表该棋盘方格不能够下棋子private int[][]arrMapShow; //交换棋手的标识,棋盘方格上是否有棋子的标识符private boolean isOdd,isExist;public ChessModel() {}//该构造方法根据不同的棋盘模式(modeChess)来构建对应大小的棋盘public ChessModel(int modeChess){this.isOdd=true; //默认黑子先手if(modeChess== 1){PanelInit(20,15,modeChess);}if(modeChess== 2){PanelInit(30,20,modeChess);}if(modeChess== 3){PanelInit(40,30,modeChess);}if(modeChess== 4){PanelInit(50,50,modeChess);}}//按照棋盘模式构建棋盘大小private void PanelInit(int width, int height,int modeChess){this.width= width;this.height =height;this.modeChess=modeChess;arrMapShow=new int[width+1][height+1];for(int i= 0;i<=width; i++){for(int j=0;j <=height; j++){arrMapShow[i][j]= -5;}}}//获取是否交换棋手的标识符public boolean getisOdd(){ return this.isOdd; }//设置交换棋手的标识符public void setisOdd(boolean isodd){if(isodd)this.isOdd=true;elsethis.isOdd=false;}//获取某棋盘方格是否有棋子的标识值public boolean getisExist(){ return this.isExist; }//获取棋盘宽度public int getWidth(){ return this.width; }//获取棋盘高度public int getHeight(){ return this.height; }//获取棋盘模式public int getModeChess(){ return this.modeChess; }//获取棋盘方格上棋子的信息public int[][] getarrMapShow(){ return arrMapShow; }//判断下子的横向、纵向坐标是否越界private boolean badxy(int x,int y){if(x>= width+20|| x< 0)return true;return y>= height+20|| y<0;}//计算棋盘上某一方格上八个方向棋子的最大值, //这八个方向分别是:左、右、上、下、左上、左下、右上、右下public boolean chessExist(int i,int j){if(this.arrMapShow[i][j]==1|| this.arrMapShow[i][j]==2)//判断位置是否已经落子return true;return false;}//判断该坐标位置是否可下棋子public void readyplay(int x,int y){if(badxy(x,y)) //判断是否越界return;if(chessExist(x,y)) //判断是否已经落子return;this.arrMapShow[x][y]=3; //设置为3 ,可以落子}//在该坐标位置下棋子public void play(int x,int y){if(badxy(x,y)) //越界return;if(chessExist(x,y)){this.isExist=true; return; //落子位置已经有子}elsethis.isExist=false;if(getisOdd()){setisOdd(false);this.arrMapShow[x][y]=1;//先设置isodd 再下子}else{setisOdd(true);this.arrMapShow[x][y]=2;}}//计算机走棋 /* *说明:用穷举法判断每一个坐标点的四个方向的的最大棋子数, 最后得出棋子数最大值的坐标,下子 **/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 <=height; j++){if(!chessExist(i,j)){//如果当前位置没有落子max_white=checkMax(i,j,2);//判断白子的最大值max_black=checkMax(i,j,1);//判断黑子的最大值max_temp=Math.max(max_white,max_black);if(max_temp>max){max=max_temp; this.x=i; this.y=j; //最大值的坐标}}}}setX(this.x);setY(this.y);this.arrMapShow[this.x][this.y]=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,max_num,max_temp=0;int x_temp=x,y_temp=y;int x_temp1=x_temp,y_temp1=y_temp;//judge rightfor(int i=1;i<5;i++){x_temp1+=1;if(x_temp1>this.width)//如果到达边界,跳出循环break;if(this.arrMapShow[x_temp1][y_temp1]==black_or_white) //如果右边的下一个落子与当前字相同,num+1num++;elsebreak;}//judge leftx_temp1=x_temp;for(int i=1;i<5;i++){x_temp1-=1;if(x_temp1<0)break;if(this.arrMapShow[x_temp1][y_temp1]==black_or_white)num++;elsebreak;}if(num<5)max_temp=num;//judge upx_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(this.arrMapShow[x_temp1][y_temp1]==black_or_white)num++;elsebreak;}//judge downy_temp1=y_temp;for(int i=1;i<5;i++){y_temp1+=1;if(y_temp1>this.height)break;if(this.arrMapShow[x_temp1][y_temp1]==black_or_white)num++;elsebreak;}if(num>max_temp&&num<5)max_temp=num;//judge left_upx_temp1=x_temp;y_temp1=y_temp;num=0;for(int i=1;i<5;i++){x_temp1-=1; y_temp1-=1;if(y_temp1<0||x_temp1<0)break;if(this.arrMapShow[x_temp1][y_temp1]==black_or_white)num++;elsebreak;}//judge right_downx_temp1=x_temp;y_temp1=y_temp;for(int i=1;i<5;i++){x_temp1+=1; y_temp1+=1;if(y_temp1>this.height || x_temp1>this.width)break;if(this.arrMapShow[x_temp1][y_temp1]==black_or_white)num++;elsebreak;}if(num>max_temp&&num<5)max_temp=num;//judge right_upx_temp1=x_temp;y_temp1=y_temp;num=0;for(int i=1;i<5;i++){x_temp1+=1;y_temp1-=1;if(y_temp1<0||x_temp1>this.width)break;if(this.arrMapShow[x_temp1][y_temp1]==black_or_white)num++;elsebreak;}//judge left_downx_temp1=x_temp;y_temp1=y_temp;for(int i=1;i<5;i++){x_temp1-=1; y_temp1+=1;if(y_temp1>this.height || x_temp1<0)break;if(this.arrMapShow[x_temp1][y_temp1]==black_or_white)num++;elsebreak;}if(num>max_temp&&num<5)max_temp=num;max_num=max_temp;return max_num;}//判断胜负public boolean judgeSuccess(int x,int y,boolean isodd){int num=1;int arrvalue;int x_temp=x,y_temp=y;if(isodd) //白子落子后判断胜负arrvalue=2;else//黑子落子后判断胜负arrvalue=1;int x_temp1=x_temp,y_temp1=y_temp;//判断右边for(int i=1;i<6;i++){x_temp1+=1; if(x_temp1>this.width)break;if(this.arrMapShow[x_temp1][y_temp1]==arrvalue)num++;elsebreak;}//判断左边x_temp1=x_temp;for(int i=1;i<6;i++){x_temp1-=1;if(x_temp1<0)break;if(this.arrMapShow[x_temp1][y_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(this.arrMapShow[x_temp1][y_temp1]==arrvalue)num++;elsebreak;}//判断下方y_temp1=y_temp;for(int i=1;i<6;i++){y_temp1+=1;if(y_temp1>this.height)break;if(this.arrMapShow[x_temp1][y_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(y_temp1<0||x_temp1<0)break;if(this.arrMapShow[x_temp1][y_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(y_temp1>this.height || x_temp1>this.width)break;if(this.arrMapShow[x_temp1][y_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(y_temp1<0||x_temp1>this.width)break;if(this.arrMapShow[x_temp1][y_temp1]==arrvalue)num++;else break;}//判断左下x_temp1=x_temp;y_temp1=y_temp;for(int i=1;i<6;i++){x_temp1-=1;y_temp1+=1;if(y_temp1>this.height || x_temp1<0)break;if(this.arrMapShow[x_temp1][y_temp1]==arrvalue)num++;elsebreak;}if(num==5)return true;return false;}//赢棋后的提示,根据判断是否为人机模式,以及胜负关系,显示不同的消息public void showSuccess(JPanel jp){if(ChessFrame.iscomputer) {JOptionPane.showMessageDialog(jp, "你赢了,好厉害!", "win", JOptionPane.INFORMATION_MESSAGE);} else{if(isOdd){JOptionPane.showMessageDialog(jp, "白子赢了,好厉害!", "win", JOptionPane.INFORMATION_MESSAGE);}else{JOptionPane.showMessageDialog(jp, "黑子赢了,好厉害!", "win", JOptionPane.INFORMATION_MESSAGE);}}}//输棋后的提示public void showDefeat(JPanel jp){JOptionPane.showMessageDialog(jp,"你输了,请重新开始!","lost", JOptionPane.INFORMATION_MESSAGE);}}/* *类 MainPanel 主要完成如下功能:* 1、构建一个面板,在该面板上画上棋盘;* 2、处理在该棋盘上的鼠标事件(如鼠标左键点击、鼠标右键点击、鼠标拖动等) **/class MainPanel extends JPanel implements MouseListener,MouseMotionListener{private int width,height;//棋盘的宽度和高度private ChessModel cm;//根据棋盘模式设定面板的大小MainPanel(ChessModel mm){cm=mm;width=cm.getWidth();height=cm.getHeight();addMouseListener(this);}//根据棋盘模式设定棋盘的宽度和高度public void setModel(ChessModel mm){cm=mm;width=cm.getWidth();height =cm.getHeight();}//根据坐标计算出棋盘方格棋子的信息(如白子还是黑子), //然后调用 draw 方法在棋盘上画出相应的棋子public void paintComponent(Graphics g){super.paintComponent(g);for(int j= 0;j<=height; j++){for(int i=0;i <=width; i++){int v= cm.getarrMapShow()[i][j];draw(g,i,j,v);}}}//根据提供的棋子信息(颜色、坐标)画棋子public void draw(Graphics g,int i, int j,int v){int x=20*i+20;int y=20*j+20;//画棋盘if(i!=width&&j!=height){g.setColor(Color.white);g.drawRect(x,y,20,20);}//画黑色棋子if(v== 1){g.setColor(Color.gray);g.drawOval(x-8,y-8,16,16);g.setColor(Color.black);g.fillOval(x-8,y-8,16,16);}//画白色棋子if(v== 2){g.setColor(Color.gray);g.drawOval(x-8,y-8,16,16);g.setColor(Color.white);g.fillOval(x-8,y-8,16,16);}if(v==3){g.setColor(Color.cyan);g.drawOval(x-8,y-8,16,16);}}//响应鼠标的点击事件,根据鼠标的点击来下棋, //根据下棋判断胜负等public void mousePressed(MouseEvent evt){int x=(evt.getX()-10)/20;int y=(evt.getY()-10)/20;System.out.println(x+""+y);if(evt.getModifiers()==MouseEvent.BUTTON1_MASK){cm.play(x,y);System.out.println(cm.getisOdd()+""+cm.getarrMapShow()[x][y]);repaint();if(cm.judgeSuccess(x,y,cm.getisOdd())){//判断是否胜利并显示消息cm.showSuccess(this);evt.consume();ChessFrame.iscomputer=false;}//判断是否为人机对弈 //当机器获胜,上一个if语句无效if(ChessFrame.iscomputer&&!cm.getisExist()){//当是人机模式,且棋盘上有子时,计算机走下一步puterDo(cm.getWidth(),cm.getHeight());repaint();if(cm.judgeSuccess(cm.getX(),cm.getY(),cm.getisOdd())){cm.showDefeat(this);evt.consume();}}}}public void mouseClicked(MouseEvent evt){}public void mouseReleased(MouseEvent evt){}public void mouseEntered(MouseEvent mouseevt){}public void mouseExited(MouseEvent mouseevent){}public void mouseDragged(MouseEvent evt){}//响应鼠标的拖动事件public void mouseMoved(MouseEvent moveevt){int x=(moveevt.getX()-10)/ 20;int y=(moveevt.getY()-10)/ 20;cm.readyplay(x,y);repaint();}}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。