package day04; import java.util.Scanner; /* 1. 初始化棋盘 抽取到一个方法中 打印棋盘 抽取到一个方法中 2. 黑白双方交替下棋 打印棋盘 3. 判断是否越界 判断是否重复 判断输赢 */ public class WZQ { static String white = "☆"; static String black = "★"; static String[][] qp = new String[15][15];//棋盘 static String[] num = {"⒈", "⒉", "⒊", "⒋", "⒌", "⒍", "⒎", "⒏", "⒐", "⒑", "⒒", "⒓", "⒔", "⒕", "⒖"}; static String line = "十"; /** * 打印棋盘 */ public static void init() { //int i = 0, j = 0; for (int i = 0; i < qp.length; i++) { for (int j = 0; j < qp.length; j++) { qp[i][j] = line; if (j == qp.length - 1) qp[i][j] = num[i]; if (i == qp[i].length - 1) qp[i][j] = num[j]; } } } public static void print(){ { for (int i = 0; i < qp.length; i++) { for (int j = 0; j < qp.length; j++) { System.out.print(qp[i][j]); } System.out.println(); } } } public static boolean check(int n,int m){ if(n<0||n>14||m<0||m>14){ return false; } if(!qp[n-1][m-1].equals(line)){ return false; } return true; } public static void startgame(){ boolean f=true; int n,m; Scanner wudi=new Scanner(System.in); while(true){ if(f){ System.out.println(); System.out.println("请黑子下棋"); System.out.println("横坐标为:"); n=wudi.nextInt(); System.out.println("纵坐标为:"); m=wudi.nextInt(); boolean res=check(n,m); if(res){ qp[n-1][m-1]=black; print(); boolean t=iswin(n-1,m-1,black); if(t){ System.out.println("张培轩你的黑子赢了"); break; } f=false; } else{ System.out.println("输入错误请重新输入"); } }else{ System.out.println(); System.out.println("请白子下棋"); System.out.println("横坐标为:"); n=wudi.nextInt(); System.out.println("纵坐标为:"); m=wudi.nextInt(); boolean res=check(n,m); if(res){ qp[n-1][m-1]=white; print(); f=true; boolean t=iswin(n-1,m-1,white); if(t){ System.out.println("你的白子赢咯"); break; } }else{ System.out.println("输入有误请重新输入"); f=false; } } } } public static boolean iswin(int x,int y,String qizi) { //水平查找 int count = 1; for (int lefty = y - 1; lefty >= 0; lefty--) { if (qp[x][lefty].equals(qizi)) { count++; } else { break; } } if (count >= 5) { return true; } for (int righty = y + 1; righty < qp.length; righty++) { if (qp[x][righty].equals(qizi)) { count++; } else { break; } } if (count >= 5) { return true; } count = 1; //竖直查找 for (int lightx = x - 1; lightx >= 0; lightx--) { if (qp[lightx][y].equals(qizi)) { count++; } else { break; } } if (count >= 5) { return true; } for (int downx = x + 1; downx > qp.length; downx++) { if (qp[downx][y].equals(qizi)) { count++; } else { break; } } if (count >= 5) { return true; } count = 1; //左斜方向查找 for (int g = x - 1, h = y - 1; g >= 0 && h >=0; g--, h--) { if (qp[g][h].equals(qizi)) { count++; } else { break; } } if (count >= 5) { return true; } for (int g = x + 1, h = y + 1; g < qp.length && h < qp.length; g++, h++) { if (qp[g][h].equals(qizi)) { count++; } else { break; } } if (count >= 5) { return true; } count = 1; //右斜方向 for (int q = x - 1, e = y + 1; q >= 0 && e < qp.length; q--, e++) { if (qp[q][e].equals(qizi)) { count++; } else { break; } } if (count >= 5) { return true; } for (int q = x + 1, e = y - 1; q < qp.length && e >=0; q++, e--) { if (qp[q][e].equals(qizi)) { count++; } else { break; } } if (count >= 5) { return true; } return false; } }
package day04; public class WZQRun { public static void main(String[] args) { WZQ.init();//棋盘初始化 WZQ.print();//打印棋盘 WZQ.startgame();//下棋 } }