Java编程:基于socket实现局域网双人联机对战五子棋 您所在的位置:网站首页 五子棋网页版双人联机怎么玩 Java编程:基于socket实现局域网双人联机对战五子棋

Java编程:基于socket实现局域网双人联机对战五子棋

2024-07-13 02:12| 来源: 网络整理| 查看: 265

客户端: 

package fivechess; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.Socket; import java.util.Scanner; public class MyClient { public static final String mip="10.25.50.131"; public static boolean gameover=true; public static void main(String[] args) throws IOException { ChessBroad chessBroad=new ChessBroad(); Socket socket=new Socket(mip,MyServer.mdk); OutputStream ou=socket.getOutputStream(); InputStream in =socket.getInputStream(); Scanner scanner=new Scanner(System.in); while(gameover){ while(true){ if(ChessBroad.broad[0][0]==null){ ChessBroad.init(); ChessBroad.draw(); } System.out.println("等待对方落子"); byte[] buf=new byte[1024]; int len=in.read(buf); String creceive=new String(buf,0,len); System.out.println("获得黑棋坐标:"+creceive); ChessBroad.iswhite=false; MyGame.list.add(creceive); String []ss=creceive.split(","); int x=new Integer(ss[0]); int y=new Integer(ss[1]); ChessBroad.broad[x][y]=ChessBroad.iswhite ? "○":"●"; ChessBroad.draw(); if(!MyGame.isover(x, y)){ break; } System.out.println("请输入坐标:x,y"); String line= scanner.next(); while(true){ if(!MyGame.list.contains(line)){ MyGame.list.add(line); break; }else{ System.out.println("已存在旗子"); line=scanner.next(); } } ou.write(line.getBytes()); MyGame.list.add(line); ChessBroad.iswhite=true; ss=line.split(","); x=new Integer(ss[0]); y=new Integer(ss[1]); ChessBroad.broad[x][y]=ChessBroad.iswhite ? "○":"●"; ChessBroad.draw(); gameover=MyGame.isover(x, y); } System.out.println("请选择:1:重新开始,2:退出"); int op=scanner.nextInt(); if (op==1){ gameover=true; }else if(op==2){ System.exit(0); } } } }

服务器:

package fivechess; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.InetAddress; import java.net.ServerSocket; import java.net.Socket; import java.util.ArrayList; import java.util.Scanner; public class MyServer { public ArrayList list=new ArrayList(); public static final int mdk=7790; public static boolean gameover=true; public static void main(String[] args) throws IOException { ChessBroad chessBroad=new ChessBroad(); ServerSocket server=new ServerSocket(mdk); Socket client= server.accept(); System.out.println("连接成功"); InetAddress address=client.getInetAddress(); System.out.println(address.getHostAddress()); InputStream in= client.getInputStream(); OutputStream ou=client.getOutputStream(); Scanner sc=new Scanner(System.in); while(gameover){ while(true){ if(ChessBroad.broad[0][0]==null){ ChessBroad.init(); ChessBroad.draw(); } System.out.println("请输入坐标:x,y"); String send=sc.next(); while(true){ if(!MyGame.list.contains(send)){ MyGame.list.add(send); break; }else{ System.out.println("已存在旗子"); send=sc.next(); } } ou.write(send.getBytes()); MyGame.list.add(send); ChessBroad.iswhite=false; String []ss=send.split(","); int x=new Integer(ss[0]); int y=new Integer(ss[1]); ChessBroad.broad[x][y]=ChessBroad.iswhite ? "○":"●"; System.out.println(chessBroad.iswhite); ChessBroad.draw(); if(!MyGame.isover(x, y)){ break; } System.out.println("等待对方落子"); byte []buf=new byte[1024]; int len =in.read(buf); String receive=new String(buf,0,len); System.out.println("白棋坐标:"+receive); ChessBroad.iswhite=true; MyGame.list.add(receive); ss=receive.split(","); x=new Integer(ss[0]); y=new Integer(ss[1]); ChessBroad.broad[x][y]=ChessBroad.iswhite ? "○":"●"; ChessBroad.draw(); gameover=MyGame.isover(x, y); } System.out.println("请选择:1:重新开始,2:退出"); int op=scanner.nextInt(); if (op==1){ gameover=true; }else if(op==2){ System.exit(0); } } } }

棋盘:

 

 

package fivechess; public class ChessBroad { public static boolean iswhite = true; public static String[][] broad = new String[15][15]; private static final int mlineNum = 15; public static void init() { for (int i = 0; i < mlineNum; i++) { for (int j = 0; j < mlineNum; j++) { if (i == 0 && j == 0) { broad[i][j] = "┏"; } else if (i == 0 && j > 0 && j < 14) { broad[i][j] = "┳"; } else if (j < 14 && j > 0 && i == 14) { broad[i][j] = "┻"; } else if (i == 0 && j == 14) { broad[i][j] = "┓"; } else if (i > 0 && i < 14 && j == 0) { broad[i][j] = "┣"; } else if (i > 0 && i < 14 && j == 14) { broad[i][j] = "┫"; } else if (j == 0 && i == 14) { broad[i][j] = "┗"; } else if (j == 14 && i == 14) { broad[i][j] = "┛"; } else { broad[i][j] = "╋"; } } } } public static void draw () { for (int i = 0; i < broad.length; i++) { for (int j = 0; j < broad.length; j++) { System.out.print(broad[i][j]); } System.out.println(); } } }

附:特殊符号打法:

1:软键盘:特殊符号:制表符

2.如图

判断输赢:

package fivechess; import java.util.ArrayList; public class MyGame { public static ArrayList list=new ArrayList(); public static boolean isover(int x,int y){ int i = x; int j = y; int count = 0; //判断横向向左是否五连 for (j = y; j >= 0 ; j--) { if (list.contains(i+","+j)) { count++; }else { break; } } //判断横向向右是否五连 for (j = y; j = 5) { System.out.println( ChessBroad.iswhite?"白":"黑"+ "棋胜!游戏结束!!!"); return false; } //判断纵向向上是否五连 i = x; j = y; count = 0; for (i = x; i >= 0 ; i--) { if (list.contains(i+","+j)) { count++; }else { break; } } //判断纵向向下是否五连 for (i = x; i = 5) { System.out.println( ChessBroad.iswhite?"白":"黑"+ "棋胜!游戏结束!!!"); return false; } //判断左上是否五连 count = 0; for (i = x,j = y; i >= 0 && j >=0 ; i--,j--) { if (list.contains(i+","+j)) { count++; }else { break; } } //判断右下是否五连 for (i = x,j = y; i < 15 && j < 15 ; i++,j++) { if (list.contains(i+","+j)) { count++; }else { break; } } count--; if (count >= 5) { System.out.println( ChessBroad.iswhite?"白":"黑"+ "棋胜!游戏结束!!!"); return false; } //判断左下是否五连 count = 0; for (i = x,j = y; i < 15 && j >=0 ; i++,j--) { if (list.contains(i+","+j)) { count++; }else { break; } } //判断右上是否五连 for (i = x,j = y; i >= 0 && j < 15 ; i--,j++) { if (list.contains(i+","+j)) { count++; }else { break; } } count--; if (count >= 5) { System.out.println(ChessBroad.iswhite?"白":"黑"+ "棋胜!游戏结束!!!"); return false; } return true; } }

成果展示:

githab源码地址:

https://github.com/cainiaolibiao/inspiration01/tree/master/FiveChess 



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有