package demo;
import java.util.Scanner;
public class GoBang {
//声明一个二维数组存储棋盘的行和列。
private static char[][] ChessBoard;
//声明棋盘行和列的序号(即定义坐标)
private static char[]number= {
'1','2','3','4','5','6','7','8','9',
'A','B','C','D','E','F','G','H','I','J'
};
private static Scanner sc=new Scanner(System.in);
//初始化棋盘
private static void InitChessBoard() {
ChessBoard=new char[19][19];
for(int i=0;i<ChessBoard.length;i++) {
for(int j=0;j<ChessBoard.length;j++) {
if(i==0) {
if(j==0) {
ChessBoard[i][j]='┏';
}else if(j==ChessBoard.length-1) {
ChessBoard[i][j]='┓';
}else {
ChessBoard[i][j]='┳';
}
}else if(i==ChessBoard.length-1) {
if(j==0) {
ChessBoard[i][j]='┗';
}else if(j==ChessBoard.length-1) {
ChessBoard[i][j]='┛';
}else {
ChessBoard[i][j]='┻';
}
}else {
if(j==0) {
ChessBoard[i][j]='┣';
}else if(j==ChessBoard.length-1) {
ChessBoard[i][j]='┫';
}else {
ChessBoard[i][j]='╋';
}
}
}
}
}
//打印棋盘
private static void PrintChessBoard() {
// TODO Auto-generated method stub
//打印列序号
System.out.println("五子棋");
System.out.print(" ");
for (int i = 0; i < number.length; i++) {
System.out.print(" "+number[i]);
}
System.out.println();
//打印棋盘和行序号
for (int i = 0; i < ChessBoard.length; i++) {
System.out.print(number[i]+" ");
for (int j = 0; j < ChessBoard.length; j++) {
if(j==ChessBoard.length-1) {
System.out.print(ChessBoard[i][j]);
}else{
System.out.print(ChessBoard[i][j]+"━");
}
}
System.out.println();
}
}
// 判断字符是不是 1~9 A~J
public static boolean isCharOk(char c) {
if ((c >= '1' && c <= '9') || (c >= 'A' && c <= 'J')) {
return true;
} else {
return false;
}
}
//获取所下棋子坐标
public static char[] getLocation() {
String input = sc.nextLine();
char c1 = 0;
char c2 = 0;
char c3 = 0;
while (true) {
if (input.length() == 3) {
c1 = input.charAt(0);
c2 = input.charAt(1);
c3 = input.charAt(2);
if (isCharOk(c1) && isCharOk(c3) && c2 == ',') {
break;
}
}
System.out.print("输入有误,请重新输入:");
input = sc.nextLine();
}
char[] location = { c1, c3 };
return location;
}
// 把字符转成 数组下标
public static int charToInt(char c) {
int num = 0;
if (c >= '1' && c <= '9') {
num = c - '1';
} else if (c >= 'A' && c <= 'J') {
num = c - 'A' + 9;
}
return num;
}
// 是否可以落子
public static boolean canPutDown(char[] location) {
char c1 = location[0];// 拿到行号 可能1~9 A~J
char c2 = location[1];// 拿到列号 可能1~9 A~J
int rowIndex = charToInt(c1);
int columnIndex = charToInt(c2);
if (ChessBoard[rowIndex][columnIndex] == '●' || ChessBoard[rowIndex][columnIndex] == '○') {
return false;
} else {
return true;
}
}
// 落子
public static void putDown(char[] location, char c) {
char c1 = location[0];// 拿到行号 可能1~9 A~J
char c2 = location[1];// 拿到列号 可能1~9 A~J
int rowIndex = charToInt(c1);
int columnIndex = charToInt(c2);
ChessBoard[rowIndex][columnIndex] = c;
}
// 看当前坐标左边有几个一样的棋子
public static int leftSameCount(char[] location) {
char c1 = location[0];// 拿到行号 可能1~9 A~J
char c2 = location[1];// 拿到列号 可能1~9 A~J
int rowIndex = charToInt(c1);
int columnIndex = charToInt(c2);
char c = ChessBoard[rowIndex][columnIndex];
int count = 0;
int j = columnIndex - 1;
while (j >= 0) {
if (ChessBoard[rowIndex][j] == c) {
count++;
j--;
} else {
break;
}
}
return count;
}
// 看当前坐标右边有几个一样的棋子
public static int rightSameCount(char[] location) {
char c1 = location[0];// 拿到行号 可能1~9 A~J
char c2 = location[1];// 拿到列号 可能1~9 A~J
int rowIndex = charToInt(c1);
int columnIndex = charToInt(c2);
char c = ChessBoard[rowIndex][columnIndex];
int count = 0;
int j = columnIndex + 1;
while (j <= 19) {
if (ChessBoard[rowIndex][j] == c) {
count++;
j++;
} else {
break;
}
}
return count;
}
// 看当前坐标上边有几个一样的棋子
public static int upSameCount(char[] location) {
char c1 = location[0];// 拿到行号 可能1~9 A~J
char c2 = location[1];// 拿到列号 可能1~9 A~J
int rowIndex = charToInt(c1);
int columnIndex = charToInt(c2);
char c = ChessBoard[rowIndex][columnIndex];
int count = 0;
int i = rowIndex - 1;
while (i >= 0) {
if (ChessBoard[i][columnIndex] == c) {
count++;
i--;
} else {
break;
}
}
return count;
}
// 看当前坐标下边有几个一样的棋子
public static int downSameCount(char[] location) {
char c1 = location[0];// 拿到行号 可能1~9 A~J
char c2 = location[1];// 拿到列号 可能1~9 A~J
int rowIndex = charToInt(c1);
int columnIndex = charToInt(c2);
char c = ChessBoard[rowIndex][columnIndex];
int count = 0;
int i = rowIndex + 1;
while (i <= 19) {
if (ChessBoard[i][columnIndex] == c) {
count++;
i++;
} else {
break;
}
}
return count;
}
// 看当前坐标左下边有几个一样的棋子
public static int leftDownSameCount(char[] location) {
char c1 = location[0];// 拿到行号 可能1~9 A~J
char c2 = location[1];// 拿到列号 可能1~9 A~J
int rowIndex = charToInt(c1);
int columnIndex = charToInt(c2);
char c = ChessBoard[rowIndex][columnIndex];
int count = 0;
int i = rowIndex + 1;
int j = columnIndex - 1;
while (i <= 19 && j >= 0) {
if (ChessBoard[i][j] == c) {
count++;
i++;
j--;
} else {
break;
}
}
return count;
}
// 看当前坐标右上边有几个一样的棋子
public static int rightUpSameCount(char[] location) {
char c1 = location[0];// 拿到行号 可能1~9 A~J
char c2 = location[1];// 拿到列号 可能1~9 A~J
int rowIndex = charToInt(c1);
int columnIndex = charToInt(c2);
char c = ChessBoard[rowIndex][columnIndex];
int count = 0;
int i = rowIndex - 1;
int j = columnIndex + 1;
while (i >= 0 && j <= 19) {
if (ChessBoard[i][j] == c) {
count++;
i--;
j++;
} else {
break;
}
}
return count;
}
// 看当前坐标左上边有几个一样的棋子
public static int leftUpSameCount(char[] location) {
char c1 = location[0];// 拿到行号 可能1~9 A~J
char c2 = location[1];// 拿到列号 可能1~9 A~J
int rowIndex = charToInt(c1);
int columnIndex = charToInt(c2);
char c = ChessBoard[rowIndex][columnIndex];
int count = 0;
int i = rowIndex - 1;
int j = columnIndex - 1;
while (i >= 0 && j >= 0) {
if (ChessBoard[i][j] == c) {
count++;
i--;
j--;
} else {
break;
}
}
return count;
}
// 看当前坐标右下边有几个一样的棋子
public static int rightDownSameCount(char[] location) {
char c1 = location[0];// 拿到行号 可能1~9 A~J
char c2 = location[1];// 拿到列号 可能1~9 A~J
int rowIndex = charToInt(c1);
int columnIndex = charToInt(c2);
char c = ChessBoard[rowIndex][columnIndex];
int count = 0;
int i = rowIndex + 1;
int j = columnIndex + 1;
while (i <= 19 && j <= 19) {
if (ChessBoard[i][j] == c) {
count++;
i++;
j++;
} else {
break;
}
}
return count;
}
// 判断输赢的方法.参数是你下的子的坐标,和子的种类
// 返回值是赢(true)还是没赢
public static boolean isWin(char[] location) {
// 当前下的子算1个子
int count = 1;
// 横向向左查看子的个数.
count += leftSameCount(location);
// 横向向右查看子的个数
count += rightSameCount(location);
// 如果横向大于等于5,说明赢了
if (count >= 5) {
return true;
}
// 纵向
count = 1;
// 纵向向上找有几个一样的子
count += upSameCount(location);
// 纵向向下找有几个一样的子
count += downSameCount(location);
if (count >= 5) {
return true;
}
// 斜着(左下-右上)
count = 1;
count += leftDownSameCount(location);
count += rightUpSameCount(location);
if (count >= 5) {
return true;
}
// 斜着(左上-右下)
count = 1;
count += leftUpSameCount(location);
count += rightDownSameCount(location);
if (count >= 5) {
return true;
}
return false;
}
public static void main(String[] args) {
// 初始化棋盘.把棋盘上的所有点置空.
InitChessBoard();
// 打印棋盘
PrintChessBoard();
while (true) {
// 黑子下棋
System.out.print("请黑方下棋(输入棋子坐标,例如:7,A):");
char[] loc = getLocation();
boolean canPutDown = canPutDown(loc);
while (canPutDown == false) {
System.out.print("此处已经有棋子,请重新输入棋子坐标:");
loc = getLocation();
canPutDown = canPutDown(loc);
}
putDown(loc, '●');
// 打印棋盘
PrintChessBoard();
// 判断输赢
if (isWin(loc)) {
System.out.println("黑方获胜!");
break;
}
System.out.print("请白方下棋(输入棋子坐标,例如:7,A):");
char[] loc2 = getLocation();
boolean canPutDown2 = canPutDown(loc2);
while (canPutDown2 == false) {
System.out.print("此处已经有棋子,请重新输入棋子坐标:");
loc2 = getLocation();
canPutDown2 = canPutDown(loc2);
}
putDown(loc2, '○');
// 打印棋盘
PrintChessBoard();
// 判断输赢
if (isWin(loc2)) {
System.out.println("白方获胜!");
break;
}
}
}
}