@ 一个简单的五子棋盘(二维数组)
棋盘的大小定义
首先得定义一个二维的数组int[][] chessboard = new int [i][j]
其中 i的值代表一个棋盘的纵数, y代表横数,
五子棋盘应该是19*19没记错的话,所以可以将括号内的i,j改成19,19
先给棋盘的每一行每一列都加上序号,方便查阅,
比如 1 2 3 4 5 6
1 + + + + + +
2 + + + + + +
3 + + + + + +
绘制棋盘代码
但是其中个位数跟十位数显示的长度不一样,所以我们需要跟他们补上一些空格,显得美观一些。
即区分10以下的部分跟十以上的部分,
这个属于绘制棋盘,玩家下一次子我们就得绘制一次棋盘,所以我们需要把这个做成一个方法。
public static void printChessboard(int[][] chessboard) {
for (int i = 0; i < chessboard.length; i++) {
if (i == 0) {
for (int j1 = 0; j1 < chessboard.length; j1++) {
if (j1 < 9) {
if(j1==0) {
System.out.print(" "+1);
} else {
System.out.print(" " + (j1+1)+"");
}
}
if(j1>=9) {
System.out.print(" "+(j1+1));
}
}System.out.println();
}
for (int j = 0; j < chessboard[i].length; j++) {
if (j == 0) {
if (i < 9) {
if (i == 0) {
System.out.print(" "+1+" ");
} else {
System.out.print(" " +(i+1) + " ");
}
} else {
System.out.print(" " + (i+1) + " ");
}
}
下棋的三个状态
+表示空的棋盘,另外两种就是黑白两个棋手下的棋
switch (chess) {
case 0:
System.out.print(" + ");
break;
case 1:
System.out.print(" ○ ");
break;
case 2:
System.out.print(" ● ");
}
然后打印出来就行了
# 效果图
效果如下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
1 + + + + + + + +</