import java.util.Scanner;
public class Test {
public static void main(String[] args) {
//棋盘长度
int length = 20;
//棋盘容器
String[][] goBang = new String[length][length];
//棋盘符号
String add = "┼";
String white = "○";
String black = "●";
String[] indexs = {"⒈","⒉","⒊","⒋","⒌","⒍","⒎","⒏","⒐","⒑","⒒","⒓","⒔","⒕","⒖","⒗","⒘","⒙","⒚","⒛"};
//初始化棋盘
for (int i = 0; i < goBang.length; i++) {
for (int j = 0; j < goBang[i].length; j++) {
if(i == length-1){//最后一行
goBang[i][j] = indexs[j];
}else if(j == length-1){//每行的最后一列
goBang[i][j] =indexs[i];
}else{
goBang[i][j] = add;
}
}
}
//打印棋盘
for (String[] ss : goBang) {
for (String element : ss) {
System.out.print(element);
}
System.out.println();
}
Scanner scan = new Scanner(System.in);
//棋子
boolean bool = true;//true-黑子 false-白子
boolean flag = true;//赢后可以设置为false
while(flag){
//输入坐标
System.out.println("请" + ((bool)?"黑":"白") + "子输入坐标:");
int x = scan.nextInt()-1;
int y = scan.nextInt()-1;
//判断下标是否越界
if(x<0 || x>length-2 || y<0 || y>length-2){
System.out.println("输入错误,坐标已经超出棋盘范围,请重新输入...");
continue;
}
//判断下标上是否有棋子
if(!goBang[x][y].equals(add)){
System.out.println("输入错误,坐标上已有棋子,请重新输入...");
continue;
}
//落子
String piece = (bool)?black:white;
goBang[x][y] = piece;
//打印棋盘
for (String[] ss : goBang) {
for (String element : ss) {
System.out.print(element);
}
System.out.println();
}
//判断输赢
if(method01(x, y, piece, length, goBang) + method02(x, y,piece, length, goBang) + 1 >= 5){
System.out.println(((bool)?"黑":"白") + "赢得比赛");
flag = false;
}
if(method03(x, y, piece, length, goBang) + method04(x, y,piece, length, goBang) + 1 >= 5){
System.out.println(((bool)?"黑":"白") + "赢得比赛");
flag = false;
}
//判断输赢
if(method05(x, y, piece, length, goBang) + method06(x, y,piece, length, goBang) + 1 >= 5){
System.out.println(((bool)?"黑":"白") + "赢得比赛");
flag = false;
}
if(method07(x, y, piece, length, goBang) + method08(x, y,piece, length, goBang) + 1 >= 5){
System.out.println(((bool)?"黑":"白") + "赢得比赛");
flag = false;
}
//置反
bool = !bool;
}
scan.close();
}
//→ (12,13)
public static int method01(int x,int y,String piece,int length,String[][] goBang){
int count = 0;
while(y < length-1){
y++;
if(goBang[x][y].equals(piece)){
count++;
}else{
return count;
}
}
return count;
}
//← (12,13) (12,12)
public static int method02(int x,int y,String piece,int length,String[][] goBang){
int count = 0;
while(y > 0){
y--;
if(goBang[x][y].equals(piece)){
count++;
}else{
return count;
}
}
return count;
}
//↓
public static int method03(int x,int y,String piece,int length,String[][] goBang){
int count = 0;
while(x < length-1){
x++;
if(goBang[x][y].equals(piece)){
count++;
}else{
return count;
}
}
return count;
}
//↑
public static int method04(int x,int y,String piece,int length,String[][] goBang){
int count = 0;
while(x > 0){
x--;
if(goBang[x][y].equals(piece)){
count++;
}else{
return count;
}
}
return count;
}
//↖
public static int method05(int x,int y,String piece,int length,String[][] goBang){
int count = 0;
while(x > 0&&y > 0){
x--;
y--;
if(goBang[x][y].equals(piece)){
count++;
}else{
return count;
}
}
return count;
}
//↘
public static int method06(int x,int y,String piece,int length,String[][] goBang){
int count = 0;
while(x < length-1 && y < length-1){
x++;
y++;
if(goBang[x][y].equals(piece)){
count++;
}else{
return count;
}
}
return count;
}
//↙
public static int method07(int x,int y,String piece,int length,String[][] goBang){
int count = 0;
while(x > 0 && y < length-1){
x--;
y++;
if(goBang[x][y].equals(piece)){
count++;
}else{
return count;
}
}
return count;
}
//↗
public static int method08(int x,int y,String piece,int length,String[][] goBang){
int count = 0;
while(x < length-1 && y >0 ){
x++;
y--;
if(goBang[x][y].equals(piece)){
count++;
}else{
return count;
}
}
return count;
}
}
07-12
403
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
06-15
529
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交