import java.util.Scanner; public class Test01 { public static void main(String[] args) { int qbcd=20;//定义棋盘大小 String qbf="╋";//设置棋盘符号 String[][] qiban=new String[qbcd][qbcd];//二维数组棋盘宽和高 //设置棋盘坐标数 String[] qbs= {"⒈","⒉","⒊","⒋","⒌","⒍","⒎","⒏","⒐","⒑","⒒","⒓","⒔","⒕","⒖","⒗","⒘","⒙","⒚","⒛"}; //便利棋盘到棋盘最后一位坐标符号依次替换为坐标数 for (int i = 0; i < qiban.length; i++) { for (int j = 0; j < qiban[i].length; j++) { if (i==qbcd-1) { qiban[i][j]=qbs[j]; }else if (j==qbcd-1) { qiban[i][j]=qbs[i]; }else { qiban[i][j]=qbf; } } } //打印棋盘 for (String[] strings : qiban) { for (String string : strings) { System.out.print(string); } System.out.println(); } boolean bool=true; String hz="■";//设置棋子符合 String bz="○";//设置棋子符合 Scanner scan = new Scanner(System.in);//定义控制台输入 while(true){ System.out.println("请输入"+((bool)?"黑":"白")+"子,坐标。(x,y)"); String zb=scan.next(); String[] zzb=zb.split(",");//分割为数组 int x=Integer.parseInt(zzb[0])-1;//取出数组0转换为int类型 int y=Integer.parseInt(zzb[1])-1;//取出数组1转换为int类型 if (x>18||x<0||y>18||y<0) {//判断输入合法性 System.out.println("您的输入有误,请重新输入:"); continue;//跳过后面的代码程序回到开头位置 } if (!qiban[x][y].equals(qbf)) {//判断棋子是否重复 System.out.println("您输入的当前位置已经有棋子了,请重新输入:"); continue;//跳过后面的代码程序回到开头位置 } qiban[x][y]=bool?hz:bz;//判断是什么棋子提交到数组指定坐标上 //打印棋盘 for (String[] strings : qiban) { for (String string : strings) { System.out.print(string); } System.out.println(); } bool = !bool;//制反黑白子 } } }
发表评论