服务器:
import java.io.*;
import java.net.*;
import java.util.Scanner;
public class Mserver {
/**
* 棋盘数组
*/
static String[][] strings = new String[17][17];
public static void main(String[] args) throws IOException {
init();
}
/**
* 创建服务器 等待客户端连接
* @throws IOException
*/
static public void init() throws IOException {
//服务器IP:10.25.50.136, 端口号 7878
//1.创建ServerSocket对象 并绑定端口号
ServerSocket server = new ServerSocket(7878);
Socket socket = server.accept();
InetAddress address = socket.getInetAddress();
System.out.println("客户端:" + address.getHostAddress());
new Server(socket).inout();
}
/**
* 判断游戏结束
* @return boolean
*/
public static boolean jugeO() {
for (int i = 5; i < 17; i++) {
for (int j = 5; j < 17; j++) {
if ((strings[i][j] == "O " && strings[i - 1][j - 1] == "O "
&& strings[i - 2][j - 2] == "O " && strings[i - 3][j - 3] == "O "
&& strings[i - 4][j - 4] == "O ")) {
System.out.println("O玩家胜利");
return true;
}
}
}
for (int i = 1; i < 14; i++) {
for (int j = 1; j < 14; j++) {
if ((strings[i][j] == "O " && strings[i + 1][j + 1] == "O "
&& strings[i + 2][j + 2] == "O " && strings[i + 3][j + 3] == "O "
&& strings[i + 4][j + 4] == "O ")) {
System.out.println("O玩家胜利");
return true;
}
}
}
for (int i = 1; i < 17; i++) {
for (int j = 5; j < 17; j++) {
if ((strings[i][j] == "O " && strings[i][j - 1] == "O "
&& strings[i][j - 2] == "O " && strings[i][j - 3] == "O "
&& strings[i][j - 4] == "O ")) {
System.out.println("O玩家胜利");
return true;
}
}
}
for (int i = 5; i < 17; i++) {
for (int j = 1; j < 17; j++) {
if ((strings[i][j] == "O " && strings[i - 1][j] == "O "
&& strings[i - 2][j] == "O " && strings[i - 3][j] == "O "
&& strings[i - 4][j] == "O ")) {
System.out.println("O玩家胜利");
return true;
}
}
}
return false;
}
public static boolean jugeX() {
for (int i = 5; i < 17; i++) {
for (int j = 5; j < 17; j++) {
if ((strings[i][j] == "X " && strings[i - 1][j - 1] == "X "
&& strings[i - 2][j - 2] == "X " && strings[i - 3][j - 3] == "X "
&& strings[i - 4][j - 4] == "X ")) {
System.out.println("X玩家胜利");
return true;
}
}
}
for (int i = 1; i < 14; i++) {
for (int j = 1; j < 14; j++) {
if ((strings[i][j] == "X " && strings[i + 1][j + 1] == "X "
&& strings[i + 2][j + 2] == "X " && strings[i + 3][j + 3] == "X "
&& strings[i + 4][j + 4] == "X ")) {
System.out.println("X玩家胜利");
return true;
}
}
}
for (int i = 1; i < 17; i++) {
for (int j = 5; j < 17; j++) {
if ((strings[i][j] == "X " && strings[i][j - 1] == "X "
&& strings[i][j - 2] == "X " && strings[i][j - 3] == "X "
&& strings[i][j - 4] == "X ")) {
System.out.println("X玩家胜利");
return true;
}
}
}
for (int i = 5; i < 17; i++) {
for (int j = 1; j < 17; j++) {
if ((strings[i][j] == "X " && strings[i - 1][j] == "X "
&& strings[i - 2][j] == "X " && strings[i - 3][j] == "X "
&& strings[i - 4][j] == "X ")) {
System.out.println("X玩家胜利");
return true;
}
}
}
return false;
}
static class Server {
/**
* 构造方法
*/
Socket socket;
public Server(Socket socket) {
this.socket = socket;
}
/**
* 打印棋盘
*/
public void printChessboard() {
for (int i = 0; i < 17; i++) {
for (int j = 0; j < 17; j++) {
if (i == 0) {
System.out.printf("| %02d ", j);
} else if (j == 0) {
System.out.printf("| %02d ", i);
} else {
if (strings[i][j] == null) {
strings[i][j] = "__";
}
System.out.print("| " + strings[i][j] + " ");
}
}
System.out.println("|");
}
}
/**
* 流的建立 输入输出
* @throws IOException
*/
public void inout() throws IOException {
InputStream r = socket.getInputStream();
OutputStream w = socket.getOutputStream();
Scanner sc = new Scanner(System.in);
try {
while (true) {
System.out.println("请等待对方落子");
byte[] bytes=new byte[1024];
r.read(bytes);
String s1=new String(bytes);
String[] split1 = s1.split(" ");
strings[Integer.parseInt(split1[0])][Integer.parseInt(split1[1].trim())] = "O ";
if (jugeO()==true)return;
printChessboard();
while (true){
System.out.println();
System.out.println("**********************************************************************");
System.out.println("请落子 \n输入坐标(行 列 )");
String s2 = sc.nextLine();
String[] split2 = s2.split(" ");
if (Integer.parseInt(split2[0]) < 1 || Integer.parseInt(split2[0].trim()) >17 ||
Integer.parseInt(split2[1]) < 1 || Integer.parseInt(split2[1].trim())>17 ||
strings[Integer.parseInt(split2[0])][Integer.parseInt(split2[1].trim())]!="__"){
System.out.println("输入错误 请重新输入");continue;
}
strings[Integer.parseInt(split2[0])][Integer.parseInt(split2[1].trim())] = "X ";
w.write(s2.getBytes());
System.out.println();
System.out.println("**********************************************************************");
printChessboard();
if (jugeX()==true)return;
break;
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
客户端:
import java.io.*;
import java.net.*;
import java.util.Scanner;
public class MClient {
/**
* 棋盘数组
*/
String[][] strings = new String[17][17];
public static void main(String[] args) throws IOException {
Socket socket = new Socket("10.25.50.137", 7878);
new MClient().inout(socket);
}
/**
* 打印棋盘
*/
public void printChessboard() {//打印棋盘
for (int i = 0; i < 17; i++) {
for (int j = 0; j < 17; j++) {
if (i == 0) {
System.out.printf("| %02d ", j);
} else if (j == 0) {
System.out.printf("| %02d ", i);
} else {
if (strings[i][j] == null) {
strings[i][j] = "__";
}
System.out.print("| " + strings[i][j] + " ");
}
}
System.out.println("|");
}
}
/**
* 判断游戏结束
* @return boolean
*/
public boolean jugeO() {
for (int i = 5; i < 17; i++) {
for (int j = 5; j < 17; j++) {
if ((strings[i][j] == "O " && strings[i - 1][j - 1] == "O "
&& strings[i - 2][j - 2] == "O " && strings[i - 3][j - 3] == "O "
&& strings[i - 4][j - 4] == "O ")) {
System.out.println("O玩家胜利");
return true;
}
}
}
for (int i = 1; i < 14; i++) {
for (int j = 1; j < 14; j++) {
if ((strings[i][j] == "O " && strings[i + 1][j + 1] == "O "
&& strings[i + 2][j + 2] == "O " && strings[i + 3][j + 3] == "O "
&& strings[i + 4][j + 4] == "O ")) {
System.out.println("O玩家胜利");
return true;
}
}
}
for (int i = 1; i < 17; i++) {
for (int j = 5; j < 17; j++) {
if ((strings[i][j] == "O " && strings[i][j - 1] == "O "
&& strings[i][j - 2] == "O " && strings[i][j - 3] == "O "
&& strings[i][j - 4] == "O ")) {
System.out.println("O玩家胜利");
return true;
}
}
}
for (int i = 5; i < 17; i++) {
for (int j = 1; j < 17; j++) {
if ((strings[i][j] == "O " && strings[i - 1][j] == "O "
&& strings[i - 2][j] == "O " && strings[i - 3][j] == "O "
&& strings[i - 4][j] == "O ")) {
System.out.println("O玩家胜利");
return true;
}
}
}
return false;
}
public boolean jugeX() {
for (int i = 5; i < 17; i++) {
for (int j = 5; j < 17; j++) {
if ((strings[i][j] == "X " && strings[i - 1][j - 1] == "X "
&& strings[i - 2][j - 2] == "X " && strings[i - 3][j - 3] == "X "
&& strings[i - 4][j - 4] == "X ")) {
System.out.println("X玩家胜利");
return true;
}
}
}
for (int i = 1; i < 14; i++) {
for (int j = 1; j < 14; j++) {
if ((strings[i][j] == "X " && strings[i + 1][j + 1] == "X "
&& strings[i + 2][j + 2] == "X " && strings[i + 3][j + 3] == "X "
&& strings[i + 4][j + 4] == "X ")) {
System.out.println("X玩家胜利");
return true;
}
}
}
for (int i = 1; i < 17; i++) {
for (int j = 5; j < 17; j++) {
if ((strings[i][j] == "X " && strings[i][j - 1] == "X "
&& strings[i][j - 2] == "X " && strings[i][j - 3] == "X "
&& strings[i][j - 4] == "X ")) {
System.out.println("X玩家胜利");
return true;
}
}
}
for (int i = 5; i < 17; i++) {
for (int j = 1; j < 17; j++) {
if ((strings[i][j] == "X " && strings[i - 1][j] == "X "
&& strings[i - 2][j] == "X " && strings[i - 3][j] == "X "
&& strings[i - 4][j] == "X ")) {
System.out.println("X玩家胜利");
return true;
}
}
}
return false;
}
/**
* 流的建立 输入输出
* @throws IOException
*/
public void inout(Socket socket) throws IOException {
InputStream r = socket.getInputStream();
OutputStream w = socket.getOutputStream();
Scanner sc = new Scanner(System.in);
try {
while (true) {
while (true) {
printChessboard();
System.out.println();
System.out.println("**********************************************************************");
System.out.println("请落子 \n输入坐标(行 列 )");
String s2 = sc.nextLine();
String[] split2 = s2.split(" ");
if (Integer.parseInt(split2[0]) < 1 || Integer.parseInt(split2[0].trim()) > 17 ||
Integer.parseInt(split2[1]) < 1 || Integer.parseInt(split2[1].trim()) > 17 ||
strings[Integer.parseInt(split2[0])][Integer.parseInt(split2[1].trim())] != "__") {
System.out.println("输入错误 请重新输入");
continue;
}
strings[Integer.parseInt(split2[0])][Integer.parseInt(split2[1].trim())] = "O ";
w.write(s2.getBytes());
System.out.println();
System.out.println("**********************************************************************");
printChessboard();
if (jugeO()==true)return;
break;
}
System.out.println("请等待对方落子");
byte[] bytes = new byte[1024];
r.read(bytes);
String s1 = new String(bytes);
String[] split1 = s1.split(" ");
strings[Integer.parseInt(split1[0])][Integer.parseInt(split1[1].trim())] = "X ";
System.out.println();
System.out.println("**********************************************************************");
printChessboard();
if (jugeX()==true)return;
}
} catch (IOException e) {
e.printStackTrace();
}
}
}