中国象棋对弈 程序 代码大全(如何编写一款让人惊艳的中国象棋对弈程序)


游戏规则:

  • 1.将/帅:不能出田字格,不能走斜线,只能前进后退向左向右,每次只走一格;
  • 2.士/仕:不能出田字格,只能走斜线,每次只走一格;
  • 3.象/相:只能走田字格,中间防止蹩脚,不能有棋;
中国象棋对弈 程序 代码大全
  • 4.马:只能走日,(这个比较麻烦,且看下图标识)
中国象棋对弈 程序 代码大全
  • 5.车:车只能走直线,这个很好理解,不细说了;
  • 6.炮:
    情况一:纯走路—-中间和目的地都不能有棋
    情况二:吃棋—–中间要有一颗棋,目标也有棋,且是敌方的棋,毕竟不能自己吃自己哈
  • 7.卒/兵:
    河这边:只能前进不能后退,不能横着走!
    河对面:可以前进,可以横着走,不能后退!

面板设计

中国象棋对弈 程序 代码大全

看上去一定很挫!哈哈,别急,听我细细道来!
一般的界面设计我们都知道横纵坐标是这样的:

中国象棋对弈 程序 代码大全

但是我选择背道而行,不是因为别的,是为了更好的去让初学者理解,我们把原本的x坐标看成数组的列(col),把y坐标看成我们数组的行(row),这样是不是更好理解一点呢,当然了我就是这么理解的,哈哈,接下来的游戏代码编程我们会把横坐标用y*width,纵坐标用x*height你们应该就能理解为什么了,因为x是数组的行,也就是坐标纵坐标(y)的体现,同理数组中的y也是如此。

数据传输:这里我们采用udp协议来进行通讯,所以你们也要先去了解一下udp协议的一些基本方法,这里就不细讲了。

通讯协议:这里我们自定义通讯协议啊:

“play|”——–以此开头代表一端发出了游戏邀请,等待另一端的回应;

“connect|”——-以此开头代表另一端收到邀请并且同意建立连接通讯!如果邀请者收到这条消息就代表通讯建立成功,可以开始游戏了;

“move|”——以此开头代表移动数据传输,如果一端移动了棋子,那么另一端也要收到信息,重新绘制界面;

“lose|”——–以此开头代表一方认输,如果有一方认输就会向另一方发送该信息;

“quit|”——-以此开头代表一方退出游戏,任意一方离开都会向对方发送该信息,以提示对方;

“success|”—–以此开头代表胜利,某一方胜利就向对方发出信息,通知对方;

“regret|”——以此开头表示悔棋,这个不用讲了吧,大家都明白,但是本次编程中我没有把这个加进去,这个你们可以自己根据自己需要去添加。

(肯定有人问我这个协议是谁定义的,啊哈哈,让你们失望了,这是我自己定义的,这个通讯协议只适用于你们写的这个代码,和使用这个代码的两个人通讯使用,所以协议如何自己可以定义的哦)

代码实现

1.chess类:package internet_chess;import java.awt.graphics;import java.awt.image;import java.awt.point;import java.awt.toolkit;import java.awt.image.imageobserver;import javax.swing.jpanel;public class chess { public int row = 12; public int col = 11; public string chessname;//当前棋子对象的名字 public int owner;//当前棋子对象的所有者--黑方还是红方 public point point;//当前棋子对象的位置 public image chessimage;//当前棋子对象的图像 private int blackchess = 1; private int redchess = 0;//红方0,黑方1 private int width = 40; private int height = 40; public chess(string name, int own,point point)//获取每一个棋子对象名字,所有者,位置,和图片信息 { this.chessname = name; this.owner = own; this.point = point; if(owner == blackchess)//如果所有者是黑方 { if(chessname.equals("将")) { chessimage = toolkit.getdefaulttoolkit().getimage("f:/image/chess/0.png"); } else if(chessname.equals("士")) { chessimage = toolkit.getdefaulttoolkit().getimage("f:/image/chess/1.png"); } else if(chessname.equals("象")) { chessimage = toolkit.getdefaulttoolkit().getimage("f:/image/chess/3.png"); } else if(chessname.equals("马")) { chessimage = toolkit.getdefaulttoolkit().getimage("f:/image/chess/5.png"); } else if(chessname.equals("车")) { chessimage = toolkit.getdefaulttoolkit().getimage("f:/image/chess/7.png"); } else if(chessname.equals("炮")) { chessimage = toolkit.getdefaulttoolkit().getimage("f:/image/chess/9.png"); } else if(chessname.equals("卒")) { chessimage = toolkit.getdefaulttoolkit().getimage("f:/image/chess/11.png"); } } else//如果所有者是红方 { if(chessname.equals("帅")) { chessimage = toolkit.getdefaulttoolkit().getimage("f:/image/chess/16.png"); } else if(chessname.equals("仕")) { chessimage = toolkit.getdefaulttoolkit().getimage("f:/image/chess/17.png"); } else if(chessname.equals("相")) { chessimage = toolkit.getdefaulttoolkit().getimage("f:/image/chess/19.png"); } else if(chessname.equals("马")) { chessimage = toolkit.getdefaulttoolkit().getimage("f:/image/chess/21.png"); } else if(chessname.equals("车")) { chessimage = toolkit.getdefaulttoolkit().getimage("f:/image/chess/23.png"); } else if(chessname.equals("炮")) { chessimage = toolkit.getdefaulttoolkit().getimage("f:/image/chess/25.png"); } else if(chessname.equals("兵")) { chessimage = toolkit.getdefaulttoolkit().getimage("f:/image/chess/27.png"); } } } protected void paint(graphics g,jpanel i)//画棋子 { g.drawimage(chessimage, point.y*width-width/2, point.x*height-height/2, width, height, (imageobserver)i); } protected void paintseclected(graphics g)//画鼠标选择了以后的棋子对象的边框 { g.drawrect(point.y*width-width/2, point.x*height-height/2, width, height); } public void setpos(int x, int y)//重新设置移动以后的棋子对象的位置坐标 { point.x = x; point.y = y; } public void reversepos()//将该对象的位置坐标逆置输出,用于方便显示信息情况 { point.x = row-1 - point.x; point.y = col-1 - point.y; }}2.chesspanel类:package internet_chess;import java.awt.color;import java.awt.font;import java.awt.graphics;import java.awt.point;import java.awt.event.mouseadapter;import java.awt.event.mouseevent;import java.io.ioexception;import java.net.datagrampacket;import java.net.datagramsocket;import java.net.inetaddress;import java.net.socketexception;import java.net.unknownhostexception;import javax.swing.joptionpane;import javax.swing.jpanel;public class chesspanel extends jpanel implements runnable{ private int blackchess = 1; private int redchess = 0;//黑棋是1,红旗是0 public chess chess[] = new chess[32];//创建了32个棋子对象 private int width = 40; private int height = 40; public int row = 12; public int col = 11;//11行10列 public int map[][] = new int [row][col]; public int player;//设置当前玩家对象 private boolean isfirst = false;//判断是否是第一次点击的棋子,以此分开两次点击棋子的碰撞矛盾 private int x1,y1,x2,y2;//用来保存第一次第二次选中的坐标的 private boolean flag = true;//用来控制线程的运行 private boolean isplay = false; private chess firstchess = null; private chess secondchess = null;//设置第一次点击的棋子和第二次选中的棋子对象 private inetaddress myid;//自己id地址 private inetaddress youid;//目标id地址 private int sendport;//发送端口 private int receiveport = 8888;//接收端口 public chesspanel()//构造函数 { init_map();//初始化棋盘 //给这个面板添加鼠标监听机制 this.addmouselistener(new mouseadapter() { public void mouseclicked(mouseevent e) { if(isplay == true)//判断是否该本方下棋 { selectchess(e.getpoint());//选择要移动的棋子 repaint(); } } public void selectchess(point pos) { int x = pos.x; int y = pos.y;//获取此时此刻鼠标点击的位置坐标 system.out.println("选择要移动的棋子坐标:x-"+x+" y-"+y); if(x 0 x (col-1)*width y 0 y (row-1)*height)//判断鼠标是否在合理的范围,不在就直接退出 { point point = resetid(x,y); if(isfirst)//判断是否是第一次选中的棋子 { x1 = point.x; y1 = point.y; //判断第一次选中的棋子是不是自己的棋子或者是不是无效棋子,不是就失败 int id = map[x1][y1]; if(id != -1 chess[id].owner == player) { isfirst = false; firstchess = chess[id]; system.out.println("id-"+id); } else//第一次选择的棋子无效 { joptionpane.showconfirmdialog(null, "提示", "第一次选棋无效!请重新选择!", joptionpane.ok_option); isfirst = true; } } else//第二次选中的棋子 { x2 = point.x; y2 = point.y; int id = map[x2][y2]; if(id != -1 chess[id].owner != player)//第二次选择了敌方棋子,将敌方棋子保存起来 { isfirst = true; secondchess = chess[id]; //开始判断是否可以移动棋子,如果可以就将棋子移动,并发信息给对方我们已经移动的棋子信息 //判断是否可以移动棋子 if(ismovechess(firstchess,x2,y2))//可以移动-吃棋 { int idx1 = map[x1][y1]; map[x1][y1] = -1; map[x2][y2] = idx1; chess[id] = null; firstchess.setpos(x2, y2); repaint(); send("move|"+string.valueof(idx1)+"|"+string.valueof(row-1-x2)+"|"+string.valueof(col-1-y2)+"|",youid,sendport); if(id == 0)//吃掉了黑棋将军 { send("success|红棋赢",youid,sendport); isplay = false; joptionpane.showconfirmdialog(null, "红棋胜利", "恭喜您,您赢了!", joptionpane.ok_option); return; } else if(id == 16)//吃掉了红棋大帅 { send("success|黑棋赢",youid,sendport); isplay = false; joptionpane.showconfirmdialog(null, "黑棋胜利", "恭喜您,您赢了!", joptionpane.ok_option); return; } isfirst = true; isplay = false; } else//表示不能吃棋,重新下棋 { joptionpane.showconfirmdialog(null, "提示", "对不起,移动棋子失败,请重新选择目标!", joptionpane.error_message); isfirst = false; } } else if(id != -1 chess[id].owner == player)//第二次又选择了自己的棋子,那么就将第二次选择的棋子当做第一次选择的棋子 { firstchess = chess[id]; x1 = x2; y1 = y2; isfirst = false; } else//第二次选择的棋子是空,那么就是单纯的移动棋子 { secondchess = null; if(ismovechess(firstchess,x2,y2))//可以移动-吃棋 { int idx1 = map[x1][y1]; map[x1][y1] = -1; map[x2][y2] = idx1;// chess[id] = null; firstchess.setpos(x2, y2); send("move|"+string.valueof(idx1)+"|"+string.valueof(row-1-x2)+"|"+string.valueof(col-1-y2)+"|",youid,sendport); system.out.println("第二次选中棋子为空:目标-》"+(row-1-x2)+" "+(col-1-y2)); repaint(); isfirst = true; isplay = false; } } } } } }); } public boolean ismovechess(chess chess,int x,int y)//判断是否可以移动棋子----移动棋子的规则 { if(chess.chessname.equals("将") || chess.chessname.equals("帅"))//只能走一步 { int x0 = chess.point.x; int y0 = chess.point.y; if(x = 8 x = 10 y = 4 y = 6) {// int x0 = chess.point.x;// int y0 = chess.point.y; if(math.abs(x - x0) 1 || math.abs(y - y0) 1) return false; else if(math.abs(x - x0)*math.abs(y - y0) != 0)//不能走斜线 return false; else return true; } if(this.chess[map[x][y]].chessname.equals("将") || this.chess[map[x][y]].chessname.equals("帅") (y0 == y))//判断两个将领之间吃棋 { int min = x x0? x : x0; int max = x x0? x : x0; for(min = min+1; min max; min++) { if(map[min][y] != -1) return false; } return true; } else return false; } else if(chess.chessname.equals("士") || chess.chessname.equals("仕"))//士也不能出田字格,且,士走斜线,每次只走一格 { if(x = 8 x = 10 y = 4 y = 6) { int x0 = chess.point.x; int y0 = chess.point.y; if(math.abs(x - x0) * math.abs(y - y0) != 1) { return false; } else return true; } else//越界 return false; } else if(chess.chessname.equals("象") || chess.chessname.equals("相"))//相走田字,且不能过河 { if(x = 6 x != 11 y = 1 y = 9) { int x0 = chess.point.x; int y0 = chess.point.y; if(math.abs(x - x0) * math.abs(y - y0) != 4) { return false; } else if(math.abs(x - x0) == 2 math.abs(y - y0) == 2) { int xm,ym;//求取中间值,防止中间有值不能走棋 xm = x x0? x-1:x0-1; ym = y y0? y-1:y0-1; if(map[xm][ym] != -1)//表示中间有棋 return false; return true; } else//防止1*4 == 4的情况出现 return false; } else return false; } else if(chess.chessname.equals("马"))//马走日,防止蹩脚的情况出现 { if(x = 1 x =10 y = 1 y = 9) { int x0 = chess.point.x; int y0 = chess.point.y; if(math.abs(x - x0) * math.abs(y - y0) == 2)//判断是否走日 { //判断是否蹩脚 if(math.abs(y - y0) == 2) { if(map[x0][y+1] != -1 y y0)//左边 { return false; } if(map[x0][y-1] != -1 y0 y)//右边 { return false; } return true; } else { if(map[x+1][y0] != -1 x x0)//上边 { return false; } if(map[x-1][y0] != -1 x x0)//下边 { return false; } return true; } } else return false; } else return false; } else if(chess.chessname.equals("车"))//车走直线 { if(x = 1 x =10 y = 1 y = 9) { int x0 = chess.point.x; int y0 = chess.point.y; if(x == x0)//水平移动 { int i = y y0 ? y : y0; int max = y y0 ? y : y0; for(i = i+1; i max; i++)//不判断目标状态,目标状态能否走在外面判断 { if(map[x][i] != -1) {// if(i == max this.chess[map[x][i]].owner != chess.owner)// {// return true;// } return false; } } return true; } else if(y == y0)//垂直移动 { int i = x x0 ? x : x0; int max = x x0 ? x : x0; for(i = i+1; i max; i++) { if(map[i][y] != -1) {// if(i == max this.chess[map[i][y]].owner != chess.owner)// {// return true;// } return false; } } return true; } return false; } else return false;//越界 } else if(chess.chessname.equals("炮"))//跑隔山打牛,不隔山就走路,水平或者垂直移动 { if(x = 1 x =10 y = 1 y = 9) { int x0 = chess.point.x; int y0 = chess.point.y; int countx = 0; int county = 0; if(x == x0)//水平移动 { int i = y y0 ? y : y0; int max = y y0 ? y : y0; for(i = i+1; i max; i++) { if(map[x][i] != -1) { countx++; } } } else if(y == y0)//垂直移动 { int i = x x0 ? x : x0; int max = x x0 ? x : x0; for(i = i+1; i max; i++) { if(map[i][y] != -1) { county++; } } } if(countx == 1 || county == 1)//说明中间有一个棋 {// if(this.chess[map[x][y]].owner != chess.owner)// {// return true;// }// else// return false; system.out.println("countx:"+countx); system.out.println("county:"+county); return true; } else if(countx == 0 county == 0)//说明中间没有棋 { if(map[x][y] == -1)//目标没有棋 { return true; } else return false;// return true; } else return false; } else return false;//越界 } else if(chess.chessname.equals("兵") || chess.chessname.equals("卒"))//卒子在自己区域不能退,只能前进,每次只走一步 { if(x 7 x = 1 y = 1 y = 9) { int x0 = chess.point.x; int y0 = chess.point.y; if(x == x0 math.abs(y-y0) == 1)//横向只走一步,判断是否在河的这边还是那边 { //如果是河的这边就不能走横向 if(x == 6) return false; else return true; } if(y == y0 x - x0 == -1)//纵向只走一步,且必须向前走 { return true; } return false; } else return false; } return false; } public point resetid(int x, int y)//重置id,将id转化成可辨识的坐标信息 { int posx = (y+height/2)/height; int posy = (x+width/2)/width; return new point(posx,posy); } public void init_map()//初始化棋盘 { for(int i = 0; i row; i++) { for(int j = 0; j col; j++) { map[i][j] = -1;//将棋盘初始化为-1,表示没有棋子id } } } public void paint(graphics g)//自己画棋盘 { super.paint(g); g.clearrect(0,0,this.getwidth(),this.getheight()); //画棋盘 int a = 1; int b = 8; int c = 5;//两军中间的分界线 for(int j = 1; j row-1; j++)//画横线 { g.drawline(a*width, j*height, (col-2)*width, j*height); } for(int i = 1; i col-1; i++)//画竖线 { g.drawline(i*width, a*height, i*width, (row-2)*height); if(i == 4) { g.drawline(i*width, a*height, (i+2)*width, (a+2)*height); g.drawline(i*width, b*height, (i+2)*width, (b+2)*height); } if(i == 6) { g.drawline(i*width, a*height, (i-2)*width, (a+2)*height); g.drawline(i*width, b*height, (i-2)*width, (b+2)*height); } } g.drawrect(0, 0, (col-1)*width, (row-1)*height); g.setcolor(color.gray); g.fillrect(a*width, c*height,(col-2-a)*width, height); g.setfont(new font("黑体",font.bold,20)); g.setcolor(color.white); g.drawstring("楚 河 汉 界", 3*width, (c+1)*height-10); g.setcolor(color.black); //画棋子 for(int i = 0; i chess.length; i++) { if(chess[i] != null) { chess[i].paint(g, this); } } if(firstchess != null) { firstchess.paintseclected(g); } if(secondchess != null) { secondchess.paintseclected(g); } } public void send(string str,inetaddress ip,int port) //发送数据报 { datagramsocket s = null; try{ s = new datagramsocket();//创建一个数据报套接字 byte data[] = new byte[100]; data = str.getbytes(); datagrampacket pocket = new datagrampacket(data,data.length,ip,port);//将数据报的信息放入自寻址包中,自寻址信息包括数据,数据长度,目标ip地址,目标端口号 s.send(pocket);//发送自寻址包 system.out.println("发送信息:"+str); }catch(ioexception ex) { ex.printstacktrace(); }finally { if(s != null) s.close(); } } public void startgame(inetaddress ip, int otherport, int myport)//游戏正式开始的起点入口 { youid = ip; this.sendport = otherport; this.receiveport = myport; try{ myid = inetaddress.getlocalhost(); }catch(unknownhostexception ex) { ex.printstacktrace(); } send("play|",youid,sendport);//发送邀请,等待目标ip的回应----开启一个线程,不断监听端口,检查是否有消息,是否建立连接成功 thread t = new thread(this); t.start(); } public void firstpaintchess()//第一次画棋盘---将每个棋盘上的棋子对象摆放完好,设置各自的初始属性 { //原本把黑棋放上面,红棋放下面,但是为了显示效果,凡是玩家玩,都把玩家的花色放下面 init_map();//如果再玩一局就要先清空棋盘,然后再重置棋子,否则上一局某些位置上面的id会遗留下来 paintchess(); if(player == blackchess) { reversechess(); } repaint(); } public void reversechess()//转置,将坐标改变,以便于下棋者下棋 { //先改变坐标// for(int i = 0; i 32; i++)// {// if(chess[i] != null)// chess[i].reversepos();// } //改变map地图id for(int i = 0; i 32; i++) { if(chess[i] != null) { chess[i].reversepos(); int xx = chess[i].point.x; int yy = chess[i].point.y; map[xx][yy] = i; } } } public void paintchess()//画棋盘显示,上面黑棋,下面红棋 { //黑方 chess[0] = new chess("将",blackchess,new point(1,5)); map[1][5] = 0; chess[1] = new chess("士",blackchess,new point(1,4)); map[1][4] = 1; chess[2] = new chess("士",blackchess,new point(1,6)); map[1][6] = 2; chess[3] = new chess("象",blackchess,new point(1,3)); map[1][3] = 3; chess[4] = new chess("象",blackchess,new point(1,7)); map[1][7] = 4; chess[5] = new chess("马",blackchess,new point(1,2)); map[1][2] = 5; chess[6] = new chess("马",blackchess,new point(1,8)); map[1][8] = 6; chess[7] = new chess("车",blackchess,new point(1,1)); map[1][1] = 7; chess[8] = new chess("车",blackchess,new point(1,9)); map[1][9] = 8; chess[9] = new chess("炮",blackchess,new point(3,2)); map[3][2] = 9; chess[10] = new chess("炮",blackchess,new point(3,8)); map[3][8] = 10; for(int i = 11,j = 1; i 16; i++,j+=2) { chess[i] = new chess("卒",blackchess,new point(4,j)); map[4][j] = i; } //画红棋 chess[16] = new chess("帅",redchess,new point(10,5)); map[10][5] = 16; chess[17] = new chess("仕",redchess,new point(10,4)); map[10][4] = 17; chess[18] = new chess("仕",redchess,new point(10,6)); map[10][6] = 18; chess[19] = new chess("相",redchess,new point(10,3)); map[10][3] = 19; chess[20] = new chess("相",redchess,new point(10,7)); map[10][7] = 20; chess[21] = new chess("马",redchess,new point(10,2)); map[10][2] = 21; chess[22] = new chess("马",redchess,new point(10,8)); map[10][8] = 22; chess[23] = new chess("车",redchess,new point(10,1)); map[10][1] = 23; chess[24] = new chess("车",redchess,new point(10,9)); map[10][9] = 24; chess[25] = new chess("炮",redchess,new point(8,2)); map[8][2] = 25; chess[26] = new chess("炮",redchess,new point(8,8)); map[8][8] = 26; for(int i = 27, j = 1; i 32; i++, j+=2) { chess[i] = new chess("兵",redchess,new point(7,j)); map[7][j] = i; } } @override public void run() { datagramsocket sock = null; try { sock = new datagramsocket(receiveport);//打开监听窗口 byte data[] = new byte[100]; datagrampacket pocket = new datagrampacket(data,data.length); while(flag) { sock.receive(pocket);//接收数据 //读取接收信息 string str = new string(data); string s[] = new string[6]; s = str.split("\|");//将数据信息按照|进行分割 if(s[0].equals("play"))//表示此时这个对象是一个被邀请的对象,将被邀请的对象设置为黑棋 { player = blackchess;//被邀请者设为黑棋 send("connect|",youid,sendport); //开始画棋盘 firstpaintchess(); isplay = false;//因为是红棋先走,所以黑棋此时不能下棋 } else if(s[0].equals("connect"))//表示此时的对象是游戏发出者对象,并且已经和被邀请对象建立连接 { player = redchess;//游戏发出者设为红棋对象 firstpaintchess(); isplay = true;//因为此时是红棋,而红旗先走,所以红棋此时可以下棋 } else if(s[0].equals("lose"))//对方认输 { joptionpane.showconfirmdialog(null, "认输", "对方棋手认输!", joptionpane.ok_option); isplay = false; } else if(s[0].equals("success"))//对方赢了 { if(s[1].equals("黑棋赢")) { joptionpane.showconfirmdialog(null, "输了", "黑棋赢了!您输了!", joptionpane.ok_option); } else if(s[1].equals("红棋赢")) { joptionpane.showconfirmdialog(null, "输了", "红棋赢了!您输了!", joptionpane.ok_option); } isplay = false; } else if(s[0].equals("move"))//对方走棋 { int indx = integer.parseint(s[1]); system.out.println("indx-"+indx); int posx = integer.parseint(s[2]); system.out.println("posx-"+posx); int posy = integer.parseint(s[3]); system.out.println("posy-"+posy); int x = chess[indx].point.x; int y = chess[indx].point.y; map[x][y] = -1; chess[indx].point.x = posx; chess[indx].point.y = posy; if(map[posx][posy] != -1) { chess[map[posx][posy]] = null; } map[posx][posy] = indx; repaint(); isplay = true; } else if(s[0].equals("quit"))//对方退出 { joptionpane.showconfirmdialog(null, "提示", "对方离开,游戏结束!", joptionpane.ok_option); isplay = false; flag = false;//退出线程 } } } catch (socketexception e) { e.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); }finally { if(sock != null) { sock.close(); } } }}3.chessframe类:package internet_chess;import java.awt.borderlayout;import java.awt.container;import java.awt.dimension;import java.awt.flowlayout;import java.awt.rectangle;import java.awt.event.mouseadapter;import java.awt.event.mouseevent;import java.net.inetaddress;import java.net.unknownhostexception;import javax.swing.imageicon;import javax.swing.jbutton;import javax.swing.jframe;import javax.swing.jlabel;import javax.swing.jpanel;import javax.swing.jtextfield;public class chessframe extends jframe{ private jbutton start = new jbutton("开始"); private jbutton end = new jbutton("结束"); private jbutton lose = new jbutton("认输"); private jpanel paneup = new jpanel(); private chesspanel chesspanel = new chesspanel(); private jpanel panedown = new jpanel(); private jlabel iplabel = new jlabel("ip:"); private jlabel otherportlabel = new jlabel("目标端口"); private jlabel imageicon = new jlabel(); private jtextfield ip_address = new jtextfield("127.0.0.1"); private jtextfield otherport = new jtextfield("9999"); private inetaddress myid;//自己id地址 private inetaddress youid;//目标id地址 private int sendport;//发送端口 private int receiveport = 8888;//接收端口 public chessframe()//构造函数 { panedown.setlayout(new flowlayout()); iplabel.setbounds(10, 10, 40, 20); ip_address.setbounds(new rectangle(60,10,50,20)); panedown.add(iplabel); panedown.add(ip_address); panedown.add(otherportlabel); panedown.add(otherport); panedown.add(start); panedown.add(lose); panedown.add(end); lose.setenabled(false); imageicon.setbounds(new rectangle(300,0,100,100)); imageicon.seticon(new imageicon("f:/image/chess/0.png"));//标签加载图片 paneup.add(imageicon,borderlayout.center); container con = this.getcontentpane(); con.add(paneup,borderlayout.north); con.add(chesspanel,borderlayout.center); con.add(panedown,borderlayout.south); this.settitle("8888网络象棋"); this.setsize(new dimension(600,700)); this.setvisible(true); this.setdefaultcloseoperation(jframe.exit_on_close); start.addmouselistener(new mouseadapter() { public void mouseclicked(mouseevent e) { try { string ip = ip_address.gettext();//获取当前目标ip地址 sendport = integer.parseint(otherport.gettext());//获取目标连接端口 myid = inetaddress.getlocalhost();//获取本地ip地址 youid = inetaddress.getbyname(ip);//获取目标ip地址 } catch (unknownhostexception e1) { e1.printstacktrace(); } chesspanel.startgame(youid,sendport,receiveport); lose.setenabled(true); } }); end.addmouselistener(new mouseadapter() { public void mouseclicked(mouseevent e) { try{ chesspanel.send("quit|",youid,sendport);//向对方发送离开信息,同时断开连接 system.exit(0); }catch(exception ex) { ex.printstacktrace(); } } }); lose.addmouselistener(new mouseadapter() { public void mouseclicked(mouseevent e) { try{ chesspanel.send("lose|",youid,sendport);//向对方发送认输信息 }catch(exception ex) { ex.printstacktrace(); } } }); //加一个对方求和的按钮 } public static void main(string[] args) { // todo auto-generated method stub new chessframe(); }}

代码结果


中国象棋对弈 程序 代码大全


中国象棋对弈 程序 代码大全

【温馨提示】如果文章内容有帮助到您,别忘动动小手指分享给好友哦!

相关文章

  • 象棋美女唐丹和谁结婚了(唐丹这位象棋美女与谁共筑棋盘人生)

    象棋美女唐丹和谁结婚了(唐丹这位象棋美女与谁共筑棋盘人生)

    大家知道象棋界唯一的特级大师夫妻是谁吗?很多人可能第一想到的是吕钦、许银川等大佬。实际是另一对来自广东的情侣,他们就是许国义、陈丽淳。本篇的主人公女子棋后陈丽淳,一直有唐丹克星之称。在陈丽淳还没有夺得全国冠军之前,对上唐丹就特别来劲,86年出生的她,多次在唐丹的手下,拿下各种胜利。印象最深的有一次,陈丽淳马炮卒竟然击败了唐丹的马炮双兵,也是厉害了。

    销魂飞刀 2023-11-17 阅读 6646
  • 老梁说象棋名家各自特点(老梁揭秘,象棋名家们独特的棋艺风格,你了解多少)

    老梁说象棋名家各自特点(老梁揭秘,象棋名家们独特的棋艺风格,你了解多少)

    洪智胡荣华蒋川刘锦祺(左)霍文会(右)这位就是柳大华 当年的象棋五冠军广东名将吕钦孙勇证陶汉明等级分第一的王天一谢靖徐天红许银川特大张强特大张强许银川赵鑫鑫特大郑惟桐庄玉庭也许大家没见过这位,下面简单介绍一下这位就是《棋艺》杂志编辑部主编张志强老师。《棋艺杂志》是梁宏达老梁创办的。经过几经发展,现在已经风风火火。

    销魂飞刀 2023-11-04 阅读 3505
  • 中国象棋棋力最强的软件是什么(探寻中国象棋软件之巅——谁才是棋力最强的霸主)

    中国象棋棋力最强的软件是什么(探寻中国象棋软件之巅——谁才是棋力最强的霸主)

    本文从以下6个部分来全方位介绍中国象棋软件的来龙去脉:第一,中国象棋软件的发展简史第二,中国象棋软件的地球排名第三,人类与象棋软件实力差距第四,中国象棋软件展望和弱点第五,理性看待纯人和棋软下棋第六,如何分辨象棋软件主播第一部分:中国象棋软件的发展简史90年代中期,已经有中国象棋软件,此时的象棋软件非常粗糙,比如电脑的大局观差、开局不行、喜欢吃子、弃子取势分不清等等,县市级的棋手都可以轻松胜之。

    销魂飞刀 2023-11-16 阅读 1868
  • 中国象棋2017打不开(无法打开2017年的中国象棋)

    中国象棋2017打不开(无法打开2017年的中国象棋)

    天天象棋是一款多人在线的手机象棋游戏。那么电脑如何安装天天象棋呢?有两种方法可以让你在电脑上玩天天象棋。天天象棋方法一:如果电脑上有安装电脑管家1、打开电脑管家电脑管家2、打开软件管理,搜索:天天象棋搜索天天象棋3,点击安装。因为天天象棋是手机软件游戏,所以自动安装时会安装腾讯手游助手,所有功能与手机版天天象棋无二,但对于对电脑不太熟悉的朋友,可能有些麻烦。

    销魂飞刀 2023-11-13 阅读 1785
  • 山东象棋大师名单(山东象棋大师名单,谁是全国最强的山东象棋大师)

    山东象棋大师名单(山东象棋大师名单,谁是全国最强的山东象棋大师)

    今年,象棋的大战举办的较少,受到多重因素的影响,鲜有大的赛会。这不,在山东棋界,举行一项红强杯的大战,不限制参赛的棋手属地和人数,报名参战的棋手众多,多位大师、名手都齐聚于此。比赛奖金数万元,众多的棋坛特级大师、国家大师,以及业余杀手,省市冠军,全力拼杀,采取七轮积分制,单日内赛罢,闪电对决。结果经过激烈的比拼,来自山东的象棋省冠级好手赵勇霖扮演黑马角色,他以七战4胜3平积13分的战绩勇夺桂冠,称霸群雄。

    销魂飞刀 2023-11-13 阅读 1420