package com.qq.gb; import java.util.Scanner; public class GoBang { Scanner sc = new Scanner(System.in); String [][] num= new String[15][15];//创建二维数组 int userId = 0;//判断当前用户 int x = 0;//横坐标 int y = 0;//纵坐标 static boolean win = false;//用于判断输赢的变量 //初始化二维数组 public void board() { for (x = 0; x < num.length; x++) { for (y = 0; y < num.length; y++) { num[x][y]="+"; } } } //打印棋盘方法 public void printBoard() { for (int x = 0; x < num.length; x++) { for (int y = 0; y < num.length; y++) { System.out.print(num[x][y]+" "); } System.out.println(); } } //用户下棋的方法 public void userXY() { System.out.println("请输入所放棋子的横坐标:"); x = sc.nextInt(); System.out.println("请输入所放棋子的纵坐标:"); y = sc.nextInt(); //判断当前用户如果userId是0则是第一个用户 if (userId==0) { if (!num[x-1][y-1].equals("+")) { System.out.println("该棋子已经存在,请重新输入!"); userXY(); }else { num[x-1][y-1] = "X"; userId = 1; printBoard(); } } else { if (!num[x-1][y-1].equals("+")) { System.out.println("该棋子已经存在,请重新输入!"); userXY(); }else { num[x-1][y-1] = "O"; userId = 0; printBoard(); } } } //判断输赢的方法 public void winOrLose() { //创建Boolean类型的变量判断结果 boolean a1,a2,a3,a4,b1,b2,b3,b4;//a和b分别代表一个玩家,1,2,3,4分别代表能赢的4种情况 for (int x = 0; x < num.length-4; x++) { for (int y = 4; y < num.length; y++) { //当五颗棋子呈右上左下连着的时候游戏结束 a4 = num[x][y].equals("X")&&num[x+1][y-1].equals("X")&&num[x+2][y-2].equals("X")&&num[x+3][y-3].equals("X")&&num[x+4][y-4].equals("X"); b4 = num[x][y].equals("X")&&num[x+1][y-1].equals("X")&&num[x+2][y-2].equals("X")&&num[x+3][y-3].equals("X")&&num[x+4][y-4].equals("X"); if (a4==true){ win = true; System.out.println("游戏结束!"); System.out.printf("玩家%d赢了",userId); break; }else if (b4==true){ win = true; System.out.println("游戏结束!"); System.out.printf("玩家%d赢了",userId); break; } } } for (int x = 0; x < num.length-4; x++) { for (int y = 0; y < num.length-4; y++) { //当五个棋子横排或者竖排连着的时候游戏结束 a1 = num[x][y].equals("X")&&num[x][y+1].equals("X")&&num[x][y+2].equals("X")&&num[x][y+3].equals("X")&&num[x][y+4].equals("X"); b1 = num[x][y].equals("O")&&num[x][y+1].equals("O")&&num[x][y+2].equals("O")&&num[x][y+3].equals("O")&&num[x][y+4].equals("O"); a2 = num[x][y].equals("X")&&num[x+1][y].equals("X")&&num[x+2][y].equals("X")&&num[x+3][y].equals("X")&&num[x+4][y].equals("X"); b2 = num[x][y].equals("O")&&num[x+1][y].equals("O")&&num[x+2][y].equals("O")&&num[x+3][y].equals("O")&&num[x+4][y].equals("O"); //当五颗棋子呈左上右下连着的时候游戏结束 a3 = num[x][y].equals("X")&&num[x+1][y+1].equals("X")&&num[x+2][y+2].equals("X")&&num[x+3][y+3].equals("X")&&num[x+4][y+4].equals("X"); b3 = num[x][y].equals("O")&&num[x+1][y+1].equals("O")&&num[x+2][y+2].equals("O")&&num[x+3][y+3].equals("O")&&num[x+4][y+4].equals("O"); if ((a1==true)||(a2==true)||(a3==true)){ win = true; System.out.println("游戏结束!"); System.out.printf("玩家%d赢了",userId); break; }else if ((b1==true)||(b2==true)||(b3==true)){ win = true; System.out.println("游戏结束!"); System.out.printf("玩家%d赢了",userId); break; } } } } public static void main(String[] args) { GoBang gb = new GoBang(); gb.board();//初始化二维数组 gb.printBoard();//打印棋盘 do { gb.userXY();//循环下棋 gb.winOrLose();//判断输赢 } while (win==false);//当win变量变为ture时停止循环 } }