java网络五子棋

合集下载

Java五子棋游戏源代码(人机对战)

Java五子棋游戏源代码(人机对战)

//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;/**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"};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();con.add(mp,"Center");this.setResizable(false);this.addWindowListener(new ChessWindowEvent());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();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++){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();}public boolean getiscomputer(){return this.iscomputer;}public void restart(){int modeChess = cm.getModeChess();if(modeChess <= 3 && 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");elseUIManager.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("人机对弈")){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只有3个值: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);}}//按照棋盘模式构建棋盘大小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;}//在该坐标位置下棋子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;}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++;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;elsearrvalue=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++){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;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++;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<0)break;if(this.arrMapShow[x_temp1][y_temp1]==arrvalue)num++;elsebreak;}if(num==5)return true;return false;}//赢棋后的提示public void showSuccess(JPanel jp){JOptionPane.showMessageDialog(jp,"你赢了,好厉害!","win",RMATION_MESSAGE);}//输棋后的提示public void showDefeat(JPanel jp){JOptionPane.showMessageDialog(jp,"你输了,请重新开始!","lost",RMATION_MESSAGE);}}/**类MainPanel主要完成如下功能:*1、构建一个面板,在该面板上画上棋盘;*2、处理在该棋盘上的鼠标事件(如鼠标左键点击、鼠标右键点击、鼠标拖动等)**/class MainPanel extends JPanelimplements 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(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();}}class ChessWindowEvent extends WindowAdapter{ public void windowClosing(WindowEvent e){ System.exit(0);}ChessWindowEvent(){}}。

基于JAVA的五子棋游戏系统设计与实现

基于JAVA的五子棋游戏系统设计与实现

基于JAVA的五子棋游戏系统设计与实现引言五子棋是一种古老而又受欢迎的棋类游戏,在目前的计算机科学中,由于其算法的多样性和难度的适中,成为了很多程序员学习和思考的对象。

在本篇文章中,我们将介绍基于JAVA的五子棋游戏系统的设计与实现,包括系统的整体结构、主要功能模块、技术选型和实现细节等方面。

一、系统整体结构五子棋游戏系统的整体结构,可以分为三个层次:显示层、逻辑层和数据层。

其中,显示层负责绘制游戏界面和处理用户交互事件;逻辑层实现游戏规则和策略,负责判断胜负并给出提示;数据层则维护游戏状态和棋局数据。

如图所示,五子棋游戏系统的整体结构如下:二、主要功能模块1.游戏开始和重置当用户点击“开始游戏”按钮时,系统会开始初始化游戏状态和棋局数据,同时展示游戏界面。

当用户在游戏中点击“重新开始”按钮时,系统会清空棋盘数据并重新初始化游戏状态。

2.用户交互和落子用户可以通过鼠标点击来操作棋盘,并在可落子点上落下自己的棋子。

落子后,系统需要检测当前状态下是否已经有五子连珠的情况出现。

3.胜负判断和提示当一方落下五子连珠时,系统会弹出胜利提示,并停止游戏。

同时,游戏界面上会显示当前胜方的姓名和胜利棋局的情况。

4.游戏设置系统提供了一些游戏设置的选项,例如棋盘大小、棋子颜色、先手后手等。

用户可以自由设置游戏参数并开始游戏。

5.游戏记录和回放系统支持对游戏过程的记录和回放功能,用户可以查看任意一局棋局的走法、时间、胜负等情况。

6.本地游戏和联网对战用户可以选择本地游戏(双人游戏)和联网对战两种模式。

在联网对战中,两位玩家可以通过互联网进行远程对战。

三、技术选型1.编程语言:JAVA由于JAVA是一种跨平台的编程语言,在开发五子棋游戏系统时,可以保证相对的兼容性和应用范围。

同时,JAVA还具有良好的面向对象编程特性,代码复用性高,易于维护。

2.图形界面:SwingSwing是JAVA自带的图形界面库,可以用来实现各种复杂的图形界面。

五子棋设计与实现完整版

五子棋设计与实现完整版

哈尔滨商业大学毕业设计(论文)五子棋的设计与实现学生姓名周玉春指导教师李恩林专业计算机科学与技术学院计算机与信息工程2010年06月02日Graduation Project (Thesis)Harbin University of CommerceThe Design and Implementationof GobangStudent Zhou YuchunSupervisor Li EnlinSpecialty Computer Science and Technology School Computer and Information Engineering2010 - 06 - 02毕业设计(论文)任务书姓名:周玉春学院:计算机与信息工程班级:2006级4班专业:计算机科学与技术毕业设计(论文)题目:五子棋的设计与实现立题目的和意义:本程序旨在用JA V A实现一个基本于C/S模式的五子棋网络对战游戏。

玩家作为客户端通过服务器端与其它玩家进行对战,聊天等功能。

通过实现本程序加深对计算机网络编程的了解。

技术要求与工作计划:开发环境:Intel core 2 duo 1.8GHz 1G内存160硬盘;Microsoft® Windows™XP Professional;JDK 1.50;Eclipse 3.3.2运行环境:IntelPentium® 2及以上处理器,32M以上内存,4G以上硬盘;Microsoft® Windows™ 9X/NT/XP 操作系统;800*600或以上的屏幕分辨率工作计划:第一阶段需求分析第二阶段系统设计第三阶段系统实现第四阶段论文初稿,程序调试第五阶段论文终稿,程序修订第六阶段准备论文答辩时间安排:1月--2月收集论文资料,确定毕业设计题目3月--4月实习和调研,编写开题报告4月--5月程序设计和调试5月--6月论文初稿6月--答辩论文终稿及答辩指导教师要求:(签字)年月日教研室主任意见:(签字)年月日院长意见:(签字)年月日毕业设计(论文)审阅评语一、指导教师评语:指导老师签字:年月日毕业设计(论文)审阅评语二、评阅人评语:评阅人签字:年月日毕业设计(论文)答辩评语三、答辩委员会评语:四、毕业设计(论文)成绩:专业答辩组负责人签字:年月日五、答辩委员会主任签章答辩委员会主任单位:(签章)答辩委员会主任职称:答辩委员会主任签字:年月日摘要随着互联网迅速的发展,网络游戏已经成为人们普遍生活中不可或缺的一部分,它不仅能使人娱乐,也能够开发人的智力,就像本文所主要讲的五子棋游戏一样能挖掘人们聪明的才干与脑袋的机灵程度。

Java五子棋游戏的设计论文

Java五子棋游戏的设计论文

基于的五子棋游戏的设计摘要五子棋作为一个棋类竞技运动,在民间十分流行,为了熟悉五子棋规则与技巧,以与研究简单的人工智能,决定用开发五子棋游戏。

主要完成了人机对战和玩家之间联网对战2个功能。

网络连接部分为编程应用,客户端和服务器端的交互用定义,有很好的可扩展性,客户端负责界面维护和收集用户输入的信息,与错误处理。

服务器维护在线用户的基本信息和任意两个对战用户的棋盘信息,动态维护用户列表。

在人机对弈中通过深度搜索和估值模块,来提高电脑棋手的智能。

分析估值模块中的影响精准性的几个要素,以与提出若干提高精准性的办法,以与对它们搜索的节点数进行比较,在这些算法的基础上分析一些提高电脑方案,如递归算法、电脑学习等。

算法的研究有助于理解程序结构,增强逻辑思维能力,在其他人工智能方面也有很大的参考作用。

关键词:深度搜索;估值;电脑;五子棋;算法a , , , , I . , , a , , . , . , . , a , , , , . , , .: ; ; ; ;目录1 引言.............................................................. 错误!未指定书签。

1.1 选题背景...................................................... 错误!未指定书签。

1.2 本课题研究的意义.............................................. 错误!未指定书签。

1.3 本课题的研究方法.............................................. 错误!未指定书签。

2 课题相关基础...................................................... 错误!未指定书签。

2.1 五子棋........................................................ 错误!未指定书签。

双人五子棋的Java源代码

双人五子棋的Java源代码

第一个文件:import javax.swing.*;import java.awt.event.*;import java.awt.*;/*五子棋-主框架类, 程序启动类*/public class StartChessJFrame extends JFrame {private ChessBoard chessBoard;//对战面板private JPanel toolbar;//工具条面板private JButton startButton, backButton, exitButton;//重新开始按钮,悔棋按钮,和退出按钮private JMenuBar menuBar;//菜单栏private JMenu sysMenu;//系统菜单private JMenuItem startMenuItem, exitMenuItem, backMenuItem;//重新开始,退出,和悔棋菜单项public StartChessJFrame() {setTitle("单机版五子棋");//设置标题chessBoard = new ChessBoard();//初始化面板对象// 创建和添加菜单menuBar = new JMenuBar();//初始化菜单栏sysMenu = new JMenu("系统");//初始化菜单startMenuItem = new JMenuItem("重新开始");exitMenuItem = new JMenuItem("退出");backMenuItem = new JMenuItem("悔棋");//初始化菜单项sysMenu.add(startMenuItem);//将三个菜单项添加到菜单上sysMenu.add(backMenuItem);sysMenu.add(exitMenuItem);MyItemListener lis = new MyItemListener();//初始化按钮事件监听器内部类this.startMenuItem.addActionListener(lis);//将三个菜单项注册到事件监听器上backMenuItem.addActionListener(lis);exitMenuItem.addActionListener(lis);menuBar.add(sysMenu);//将系统菜单添加到菜单栏上setJMenuBar(menuBar);// 将menuBar设置为菜单栏toolbar = new JPanel();//工具面板栏实例化startButton = new JButton("重新开始");//三个按钮初始化backButton = new JButton("悔棋");exitButton = new JButton("退出");toolbar.setLayout(new FlowLayout(FlowLayout.LEFT));//将工具面板按钮用FlowLayout布局toolbar.add(startButton);//将三个按钮添加到工具面板上toolbar.add(backButton);toolbar.add(exitButton);startButton.addActionListener(lis);//将三个按钮注册监听事件backButton.addActionListener(lis);exitButton.addActionListener(lis);add(toolbar, BorderLayout.SOUTH);//将工具面板布局到界面"南"方也就是下面add(chessBoard);//将面板对象添加到窗体上setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//设置界面关闭事件//setSize(800,800);pack(); // 自适应大小}private class MyItemListener implements ActionListener {//事件监听器内部类public void actionPerformed(ActionEvent e) {Object obj = e.getSource(); // 取得事件源if (obj == StartChessJFrame.this.startMenuItem || obj == startButton) {// 重新开始// JFiveFrame.this内部类引用外部类System.out.println("重新开始...");chessBoard.restartGame();} else if (obj == exitMenuItem || obj == exitButton) {System.exit(0); // 结束应用程序} else if (obj == backMenuItem || obj == backButton) { // 悔棋System.out.println("悔棋...");chessBoard.goback();}}}public static void main(String[] args) {StartChessJFrame f = new StartChessJFrame(); // 创建主框架f.setVisible(true); // 显示主框架}}第二个文件:import java.awt.Color;/*五子棋的棋子设计。

五子棋游戏代码

五子棋游戏代码

1、游戏实现效果图:2、游戏代码(含详细注解):import java.awt.*;import java.awt.event.*;import java.awt.Color;import static java.awt.BorderLayout.*;public class Gobang00{Frame f = new Frame("五子棋游戏");MyCanvas border = new MyCanvas();int color = 0;// 旗子的颜色标识0:白子1:黑子boolean isStart = false;// 游戏开始标志int bodyArray[][] = new int[16][16]; // 设置棋盘棋子状态0 无子1 白子2 黑子Button b1 = new Button("游戏开始");Button b2 = new Button("重置游戏");//Label WinMess = new Label(" ");//用于输出获胜信息TextField WinMess = new TextField(40);Checkbox[] ckbHB = new Checkbox[2];//用于判断白子先下,还是黑子先下CheckboxGroup ckgHB = new CheckboxGroup();public void init(){border.setPreferredSize(new Dimension(320,310));WinMess.setBounds(100, 340, 300, 30);f.add(border);//设置游戏状态,是开始游戏,还是重置游戏Panel p1 = new Panel();//用于放置b1和b2按钮,及选择框p1.add(b1);p1.add(b2);b1.addActionListener(new gameStateListener());b2.addActionListener(new gameStateListener());//设置谁先下棋// Panel p2 = new Panel();//用于放置选择组建ckbHB[0] = new Checkbox("白子先", ckgHB, false); ckbHB[1] = new Checkbox("黑子先", ckgHB, false); p1.add(ckbHB[0]);p1.add(ckbHB[1]);ckbHB[0].addItemListener(new itemListener()); ckbHB[1].addItemListener(new itemListener());//进入下棋状态,为窗口和board添加事件监听器f.addMouseListener(new mouseProcessor()); border.addMouseListener(new mouseProcessor()); Panel p2 = new Panel();//用于放置输赢信息显示框p2.add(WinMess);gameInit();f.add(p1,BorderLayout.SOUTH);f.add(p2,BorderLayout.NORTH);f.pack();f.setVisible(true);//设置窗体关闭事件f.addWindowListener(new WindowAdapter(){public void windowClosing(WindowEvent e){System.exit(0);}});}public static void main(String[] args){new Gobang00().init();}//定义事件监听器类,用于判断是开始游戏,还是重置游戏class gameStateListener implements ActionListener{public void actionPerformed(ActionEvent e){if (e.getSource() == b1){gameStart();}else{reStart();}}}public void gameStart() // 游戏开始{isStart = true;enableGame(false);b2.setEnabled(true);}public void enableGame(boolean e) // 设置组件状态{b1.setEnabled(e);//setEnabled(boolean) - 类ponent 中的方法:根据参数b 的值启用或禁用此组件。

Java程序设计报告

Java程序设计报告

楚雄师范学院2015年春季期末Java程序设计报告项目名称:基于Java平台开发的五子棋程序设计学院:物理与电子科学学院专业:电子信息科学与技术班级:2 0 1 3 级电信一班组员姓名:杨邦桂许勇董俊宏课程教师:程满目录引言 (3)第一章设计目的和要求 (3)第二章JAVA语言概述 (3)2.1 JAVA简介 (3)2.1.1 JAVA的基本特点 (3)2.2 JAVA工具JDK (4)第三章程序的设计思路和算法 (4)3.1 人机博弈的要点 (4)3.2 五子棋特点及规则 (4)3.3 设计思路 (5)3.3.1 (5)第四章测试及运行效果 (5)4.1棋盘系统主界面 (5)4.2下期博弈过程界面 (6)4.3黑方赢的结果 (6)4.4白方赢的结果 (7)第五章设计体会与总结 (8)附录 (9)源程序代码及简要说明: (9)引言随着计算机技术的不断发展,网络技术的普及范围越来越广,网络能够提供的服务多样、便捷,已经成为人们生产生活中不可缺少的重要组成部分。

如今网络休闲游戏发展迅速,它凭借健康、方便、互动性强、益智等诸多优点,成为大部分现代人休闲娱乐的首选。

网络五子棋游戏是使用Java语言开发的一款游戏。

它使用SOCKET建立连接,多线程处理数据,以及可嵌入网络浏览器的APPLET作为客户端,这些特点使这款游戏无论是服务器还是客户端的实现都相对容易。

通过对该软件的编写,还可以巩固学生对以上各种知识的掌握和理解。

第一章设计目的和要求1.1 实现一个简单的五子棋游戏程序,包括如下两个界面: (1)对弈及角色(黑方先落棋)。

(2)在游戏界面,有游戏栏(开局、悔棋、退出),帮助栏;显示区;棋盘区。

1.2在实际系统中使用、实现人工智能的相关算法1.3进一步加深对人工智能算法的理解第二章JAVA语言概述2.1 JAVA简介JAVA是Sun Microsystem公司开发的编程语言,是一个简单,面向对象,分布式,解释性,强壮,安全,与系统无关,可移植,高性能,多线程和动态的语言。

Java五子棋毕业设计论文

Java五子棋毕业设计论文
(2)棋局回放:用户根据自己的需求回放自己以前下过的棋局。
(3)玩五子棋:用户在点击playgame后会进入游戏界面玩游戏。
(4)悔棋功能:在下棋过程中,用户根据自己的需求,选择悔棋。
(5)更改密码:用户可以根据自己的需求更改自己的密码。
(6)管理普通用户:可以对普通用户进行添加和删除。
3.1.3 系统用例图:
2.1 五子棋系统设计思路
五子棋系统基本设计思路是把系统按照实现模块进行分解,利用java进行编程。
2.2 经济可行性分析
主要是对项目的经济效益进行评价,本系统开发费用对于学员在经济上是可以接受的,并且本系统实施后可以让用户很好的对五子棋进行更加深入的研究,有助于开发用户智力。
2.3技术上的可行性
技术上的可行性分析主要技术条件是否顺利完成开发工作,硬、软件是否满足开发者的需要等。该系统采用的简单的二层结构,数据库选用MySQL,它相对于其他的数据库来说,安装简单,灵活性、安全性和易用性为数据库编程提供了良好的条件。因此,系统的软件开发平台已成熟可行。硬件方面,科技飞速发展的今天,硬件更新的速度越来越快,容量越来越大,可靠性越来越高,价格越来越低,棋硬件平台完全能满足此系统的需求。
3.1 用户需求分析
根据老师提出的要求,我们把系统的用户分为两类:普通用户、系统管理员用户,下面是我们对两种用户的需求进行的分析:
3.1.1 普通用户主要需要:
根据分析,普通用户具有的功能:
(1)用户登录:用户根据自己的密码登录本系统。
(2)棋局回放:用户根据自己的需求回放自己以前下过的棋局。
(3)玩五子棋:用户在点击playgame后会进入游戏界面玩游戏。
关键词:人工智能; 计算人机对弈;五子棋;JSP
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

java网络五子棋下面的源代码分为4个文件;chessClient.java:客户端主程序。

chessInterface.java:客户端的界面。

chessPad.java:棋盘的绘制。

chessServer.java:服务器端。

可同时容纳50个人同时在线下棋,聊天。

没有加上详细注释,不过绝对可以运行,j2sdk1.4下通过。

/********************************************************************************************* 1.chessClient.java**********************************************************************************************/import java.awt.*;import java.awt.event.*;import java.io.*;import .*;import java.util.*;class clientThread extends Thread{chessClient chessclient;clientThread(chessClient chessclient){this.chessclient=chessclient;}public void acceptMessage(String recMessage){if(recMessage.startsWith("/userlist ")){StringTokenizer userToken=new StringTokenizer(recMessage," ");int userNumber=0;erList.removeAll();erChoice.removeAll();erChoice.addItem("所有人");while(userToken.hasMoreTokens()){String user=(String)userToken.nextToken(" ");if(userNumber>0 && !user.startsWith("[inchess]")){erList.add(user);erChoice.addItem(user);}userNumber++;}erChoice.select("所有人");}else if(recMessage.startsWith("/yourname ")){chessclient.chessClientName=recMessage.substring(10);chessclient.setTitle("Java五子棋客户端"+"用户名:"+chessclient.chessClientName);}else if(recMessage.equals("/reject")){try{chessclient.chesspad.statusText.setText("不能加入游戏");chessclient.controlpad.cancelGameButton.setEnabled(false);chessclient.controlpad.joinGameButton.setEnabled(true);chessclient.controlpad.creatGameButton.setEnabled(true);}catch(Exception ef){chessclient.chatpad.chatLineArea.setText("chessclient.chesspad.chessSocket.close无法关闭");}chessclient.controlpad.joinGameButton.setEnabled(true);}else if(recMessage.startsWith("/peer ")){chessclient.chesspad.chessPeerName=recMessage.substring(6);if(chessclient.isServer){chessclient.chesspad.chessColor=1;chessclient.chesspad.isMouseEnabled=true;chessclient.chesspad.statusText.setText("请黑棋下子");}else if(chessclient.isClient){chessclient.chesspad.chessColor=-1;chessclient.chesspad.statusText.setText("已加入游戏,等待对方下子...");}}else if(recMessage.equals("/youwin")){chessclient.isOnChess=false;chessclient.chesspad.chessVictory(chessclient.chesspad.chessColor); chessclient.chesspad.statusText.setText("对方退出,请点放弃游戏退出连接"); chessclient.chesspad.isMouseEnabled=false;}else if(recMessage.equals("/OK")){chessclient.chesspad.statusText.setText("创建游戏成功,等待别人加入...");}else if(recMessage.equals("/error")){chessclient.chatpad.chatLineArea.append("传输错误:请退出程序,重新加入\n"); }else{chessclient.chatpad.chatLineArea.append(recMessage+"\n");chessclient.chatpad.chatLineArea.setCaretPosition(chessclient.chatpad.chatLineArea.getText().length());}}public void run(){String message="";try{while(true){message=chessclient.in.readUTF();acceptMessage(message);}}catch(IOException es){}}}public class chessClient extends Frame implements ActionListener,KeyListener {userPad userpad=new userPad();chatPad chatpad=new chatPad();controlPad controlpad=new controlPad();chessPad chesspad=new chessPad();inputPad inputpad=new inputPad();Socket chatSocket;DataInputStream in;DataOutputStream out;String chessClientName=null;String host=null;int port=4331;boolean isOnChat=false; //在聊天?boolean isOnChess=false; //在下棋?boolean isGameConnected=false; //下棋的客户端连接?boolean isServer=false; //如果是下棋的主机boolean isClient=false; //如果是下棋的客户端Panel southPanel=new Panel();Panel northPanel=new Panel();Panel centerPanel=new Panel();Panel westPanel=new Panel();Panel eastPanel=new Panel();chessClient(){super("Java五子棋客户端");setLayout(new BorderLayout());host=controlpad.inputIP.getText();westPanel.setLayout(new BorderLayout()); westPanel.add(userpad,BorderLayout.NORTH); westPanel.add(chatpad,BorderLayout.CENTER); westPanel.setBackground(Color.pink);inputpad.inputWords.addKeyListener(this); chesspad.host=controlpad.inputIP.getText();centerPanel.add(chesspad,BorderLayout.CENTER); centerPanel.add(inputpad,BorderLayout.SOUTH); centerPanel.setBackground(Color.pink);controlpad.connectButton.addActionListener(this); controlpad.creatGameButton.addActionListener(this); controlpad.joinGameButton.addActionListener(this); controlpad.cancelGameButton.addActionListener(this); controlpad.exitGameButton.addActionListener(this);controlpad.creatGameButton.setEnabled(false); controlpad.joinGameButton.setEnabled(false); controlpad.cancelGameButton.setEnabled(false);southPanel.add(controlpad,BorderLayout.CENTER); southPanel.setBackground(Color.pink);addWindowListener(new WindowAdapter(){public void windowClosing(WindowEvent e){if(isOnChat){try{chatSocket.close();}catch(Exception ed){}}if(isOnChess || isGameConnected){try{chesspad.chessSocket.close();}catch(Exception ee){}}System.exit(0);}public void windowActivated(WindowEvent ea){}});add(westPanel,BorderLayout.WEST);add(centerPanel,BorderLayout.CENTER);add(southPanel,BorderLayout.SOUTH);pack();setSize(670,548);setVisible(true);setResizable(false);validate();}public boolean connectServer(String serverIP,int serverPort) throws Exception {try{chatSocket=new Socket(serverIP,serverPort);in=new DataInputStream(chatSocket.getInputStream());out=new DataOutputStream(chatSocket.getOutputStream());clientThread clientthread=new clientThread(this);clientthread.start();isOnChat=true;return true;}catch(IOException ex){chatpad.chatLineArea.setText("chessClient:connectServer:无法连接,建议重新启动程序\n");}return false;}public void actionPerformed(ActionEvent e){if(e.getSource()==controlpad.connectButton){host=chesspad.host=controlpad.inputIP.getText();try{if(connectServer(host,port)){chatpad.chatLineArea.setText("");controlpad.connectButton.setEnabled(false);controlpad.creatGameButton.setEnabled(true);controlpad.joinGameButton.setEnabled(true);chesspad.statusText.setText("连接成功,请创建游戏或加入游戏");}}catch(Exception ei){chatpad.chatLineArea.setText("controlpad.connectButton:无法连接,建议重新启动程序\n");}}if(e.getSource()==controlpad.exitGameButton){if(isOnChat){try{chatSocket.close();}catch(Exception ed){}}if(isOnChess || isGameConnected){try{chesspad.chessSocket.close();}catch(Exception ee){}}System.exit(0);}if(e.getSource()==controlpad.joinGameButton){String selectedUser=erList.getSelectedItem();if(selectedUser==null || selectedUser.startsWith("[inchess]") || selectedUser.equals(chessClientName)){chesspad.statusText.setText("必须先选定一个有效用户");}else{try{if(!isGameConnected){if(chesspad.connectServer(chesspad.host,chesspad.port)) {isGameConnected=true;isOnChess=true;isClient=true;controlpad.creatGameButton.setEnabled(false); controlpad.joinGameButton.setEnabled(false); controlpad.cancelGameButton.setEnabled(true);chesspad.chessthread.sendMessage("/joingame "+erList.getSelectedItem()+" "+chessClientName);}}else{isOnChess=true;isClient=true;controlpad.creatGameButton.setEnabled(false);controlpad.joinGameButton.setEnabled(false);controlpad.cancelGameButton.setEnabled(true);chesspad.chessthread.sendMessage("/joingame "+erList.getSelectedItem()+" "+chessClientName);}}catch(Exception ee){isGameConnected=false;isOnChess=false;isClient=false;controlpad.creatGameButton.setEnabled(true);controlpad.joinGameButton.setEnabled(true);controlpad.cancelGameButton.setEnabled(false);chatpad.chatLineArea.setText("chesspad.connectServer无法连接\n"+ee);}}}if(e.getSource()==controlpad.creatGameButton){try{if(!isGameConnected){if(chesspad.connectServer(chesspad.host,chesspad.port)){isGameConnected=true;isOnChess=true;isServer=true;controlpad.creatGameButton.setEnabled(false);controlpad.joinGameButton.setEnabled(false);controlpad.cancelGameButton.setEnabled(true);chesspad.chessthread.sendMessage("/creatgame "+"[inchess]"+chessClientName); }}else{isOnChess=true;isServer=true;controlpad.creatGameButton.setEnabled(false);controlpad.joinGameButton.setEnabled(false);controlpad.cancelGameButton.setEnabled(true);chesspad.chessthread.sendMessage("/creatgame "+"[inchess]"+chessClientName); }}catch(Exception ec){isGameConnected=false;isOnChess=false;isServer=false;controlpad.creatGameButton.setEnabled(true);controlpad.joinGameButton.setEnabled(true);controlpad.cancelGameButton.setEnabled(false);ec.printStackTrace();chatpad.chatLineArea.setText("chesspad.connectServer无法连接\n"+ec);}}if(e.getSource()==controlpad.cancelGameButton){if(isOnChess){chesspad.chessthread.sendMessage("/giveup "+chessClientName);chesspad.chessVictory(-1*chesspad.chessColor);controlpad.creatGameButton.setEnabled(true);controlpad.joinGameButton.setEnabled(true);controlpad.cancelGameButton.setEnabled(false);chesspad.statusText.setText("请建立游戏或者加入游戏");}if(!isOnChess){controlpad.creatGameButton.setEnabled(true);controlpad.joinGameButton.setEnabled(true);controlpad.cancelGameButton.setEnabled(false);chesspad.statusText.setText("请建立游戏或者加入游戏");}isClient=isServer=false;}}public void keyPressed(KeyEvent e){TextField inputWords=(TextField)e.getSource();if(e.getKeyCode()==KeyEvent.VK_ENTER){if(erChoice.getSelectedItem().equals("所有人")){try{out.writeUTF(inputWords.getText());inputWords.setText("");}catch(Exception ea){chatpad.chatLineArea.setText("chessClient:KeyPressed无法连接,建议重新连接\n"); erList.removeAll();erChoice.removeAll();inputWords.setText("");controlpad.connectButton.setEnabled(true);}}else{try{out.writeUTF("/"+erChoice.getSelectedItem()+" "+inputWords.getText()); inputWords.setText("");}catch(Exception ea)chatpad.chatLineArea.setText("chessClient:KeyPressed无法连接,建议重新连接\n"); erList.removeAll();erChoice.removeAll();inputWords.setText("");controlpad.connectButton.setEnabled(true);}}}}public void keyTyped(KeyEvent e){}public void keyReleased(KeyEvent e){}public static void main(String args[]){chessClient chessClient=new chessClient();}}/******************************************************************************************下面是:chessInteface.java******************************************************************************************/import java.awt.*;import java.awt.event.*;import java.io.*;import .*;class userPad extends PanelList userList=new List(10);userPad(){setLayout(new BorderLayout());for(int i=0;i<50;i++){userList.add(i+"."+"没有用户");}add(userList,BorderLayout.CENTER);}}class chatPad extends Panel{TextArea chatLineArea=newTextArea("",18,30,TextArea.SCROLLBARS_VERTICAL_ONLY);chatPad(){setLayout(new BorderLayout());add(chatLineArea,BorderLayout.CENTER);}}class controlPad extends Panel{Label IPlabel=new Label("IP",Label.LEFT);TextField inputIP=new TextField("localhost",10);Button connectButton=new Button("连接主机");Button creatGameButton=new Button("建立游戏");Button joinGameButton=new Button("加入游戏");Button cancelGameButton=new Button("放弃游戏");Button exitGameButton=new Button("关闭程序");controlPad(){setLayout(new FlowLayout(FlowLayout.LEFT));setBackground(Color.pink);add(IPlabel);add(inputIP);add(connectButton);add(creatGameButton);add(joinGameButton);add(cancelGameButton);add(exitGameButton);}}class inputPad extends Panel{TextField inputWords=new TextField("",40);Choice userChoice=new Choice();inputPad(){setLayout(new FlowLayout(FlowLayout.LEFT));for(int i=0;i<50;i++){userChoice.addItem(i+"."+"没有用户");}userChoice.setSize(60,24);add(userChoice);add(inputWords);}}/********************************************************************************************** 下面是:chessPad.java**********************************************************************************************/ import java.awt.*;import java.awt.event.*;import java.io.*;import .*;import java.util.*;class chessThread extends Thread{chessPad chesspad;chessThread(chessPad chesspad){this.chesspad=chesspad;}public void sendMessage(String sndMessage){try{chesspad.outData.writeUTF(sndMessage);}catch(Exception ea){System.out.println("chessThread.sendMessage:"+ea);}}public void acceptMessage(String recMessage){if(recMessage.startsWith("/chess ")){StringTokenizer userToken=new StringTokenizer(recMessage," "); String chessToken;String[] chessOpt={"-1","-1","0"};int chessOptNum=0;while(userToken.hasMoreTokens()){chessToken=(String)userToken.nextToken(" ");if(chessOptNum>=1 && chessOptNum<=3){chessOpt[chessOptNum-1]=chessToken;chessOptNum++;}ChessPaint(Integer.parseInt(chessOpt[0]),Integer.parseInt(chessOpt[1]),Int eger.parseInt(chessOpt[2]));}else if(recMessage.startsWith("/yourname ")){chesspad.chessSelfName=recMessage.substring(10);}else if(recMessage.equals("/error")){chesspad.statusText.setText("错误:没有这个用户,请退出程序,重新加入");}else{//System.out.println(recMessage);}}public void run(){String message="";try{while(true){message=chesspad.inData.readUTF();acceptMessage(message);}}catch(IOException es){}}}class chessPad extends Panel implements MouseListener,ActionListener{int chessPoint_x=-1,chessPoint_y=-1,chessColor=1;int chessBlack_x[]=new int[200];int chessBlack_y[]=new int[200];int chessWhite_x[]=new int[200];int chessWhite_y[]=new int[200];int chessBlackCount=0,chessWhiteCount=0;int chessBlackWin=0,chessWhiteWin=0;boolean isMouseEnabled=false,isWin=false,isInGame=false;TextField statusText=new TextField("请先连接服务器");Socket chessSocket;DataInputStream inData;DataOutputStream outData;String chessSelfName=null;String chessPeerName=null;String host=null;int port=4331;chessThread chessthread=new chessThread(this);chessPad(){setSize(440,440);setLayout(null);setBackground(Color.pink);addMouseListener(this);add(statusText);statusText.setBounds(40,5,360,24);statusText.setEditable(false);}public boolean connectServer(String ServerIP,int ServerPort) throws Exception {try{chessSocket=new Socket(ServerIP,ServerPort);inData=new DataInputStream(chessSocket.getInputStream());outData=new DataOutputStream(chessSocket.getOutputStream());chessthread.start();return true;}catch(IOException ex){statusText.setText("chessPad:connectServer:无法连接\n");}return false;}public void chessVictory(int chessColorWin){this.removeAll();for(int i=0;i<=chessBlackCount;i++){chessBlack_x[i]=0;chessBlack_y[i]=0;}for(int i=0;i<=chessWhiteCount;i++){chessWhite_x[i]=0;chessWhite_y[i]=0;}chessBlackCount=0;chessWhiteCount=0;add(statusText);statusText.setBounds(40,5,360,24);if(chessColorWin==1){ chessBlackWin++;statusText.setText("黑棋胜,黑:白为"+chessBlackWin+":"+chessWhiteWin+",重新开局,等待白棋下子...");}else if(chessColorWin==-1){chessWhiteWin++;statusText.setText("白棋胜,黑:白为"+chessBlackWin+":"+chessWhiteWin+",重新开局,等待黑棋下子...");}}public void getLocation(int a,int b,int color){if(color==1){chessBlack_x[chessBlackCount]=a*20;chessBlack_y[chessBlackCount]=b*20;chessBlackCount++;}else if(color==-1){chessWhite_x[chessWhiteCount]=a*20;chessWhite_y[chessWhiteCount]=b*20;chessWhiteCount++;}}public boolean checkWin(int a,int b,int checkColor){int step=1,chessLink=1,chessLinkTest=1,chessCompare=0;if(checkColor==1){chessLink=1;for(step=1;step<=4;step++){for(chessCompare=0;chessCompare<=chessBlackCount;chessCompare++) {if(((a+step)*20==chessBlack_x[chessCompare]) &&((b*20)==chessBlack_y[chessCompare])){chessLink=chessLink+1;if(chessLink==5){return(true);}}}if(chessLink==(chessLinkTest+1))chessLinkTest++;elsebreak;}for(step=1;step<=4;step++){for(chessCompare=0;chessCompare<=chessBlackCount;chessCompare++) {if(((a-step)*20==chessBlack_x[chessCompare]) &&(b*20==chessBlack_y[chessCompare])){chessLink++;if(chessLink==5){return(true);}}}if(chessLink==(chessLinkTest+1))chessLinkTest++;elsebreak;}chessLink=1;chessLinkTest=1;for(step=1;step<=4;step++){for(chessCompare=0;chessCompare<=chessBlackCount;chessCompare++) {if((a*20==chessBlack_x[chessCompare]) &&((b+step)*20==chessBlack_y[chessCompare])){chessLink++;if(chessLink==5){return(true);}}}if(chessLink==(chessLinkTest+1))chessLinkTest++;elsebreak;for(step=1;step<=4;step++){for(chessCompare=0;chessCompare<=chessBlackCount;chessCompare++) {if((a*20==chessBlack_x[chessCompare]) &&((b-step)*20==chessBlack_y[chessCompare])){chessLink++;if(chessLink==5){return(true);}}}if(chessLink==(chessLinkTest+1))chessLinkTest++;elsebreak;}chessLink=1;chessLinkTest=1;for(step=1;step<=4;step++){for(chessCompare=0;chessCompare<=chessBlackCount;chessCompare++) {if(((a-step)*20==chessBlack_x[chessCompare]) &&((b+step)*20==chessBlack_y[chessCompare])){chessLink++;if(chessLink==5){return(true);}}}if(chessLink==(chessLinkTest+1))chessLinkTest++;elsebreak;}for(step=1;step<=4;step++)for(chessCompare=0;chessCompare<=chessBlackCount;chessCompare++) {if(((a+step)*20==chessBlack_x[chessCompare]) &&((b-step)*20==chessBlack_y[chessCompare])){chessLink++;if(chessLink==5){return(true);}}}if(chessLink==(chessLinkTest+1))chessLinkTest++;elsebreak;}chessLink=1;chessLinkTest=1;for(step=1;step<=4;step++){for(chessCompare=0;chessCompare<=chessBlackCount;chessCompare++) {if(((a+step)*20==chessBlack_x[chessCompare]) &&((b+step)*20==chessBlack_y[chessCompare])){chessLink++;if(chessLink==5){return(true);}}}if(chessLink==(chessLinkTest+1))chessLinkTest++;elsebreak;}for(step=1;step<=4;step++){for(chessCompare=0;chessCompare<=chessBlackCount;chessCompare++)if(((a-step)*20==chessBlack_x[chessCompare]) && ((b-step)*20==chessBlack_y[chessCompare])) {chessLink++;if(chessLink==5){return(true);}}。

相关文档
最新文档