import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Arrays;
public class FiveChess {
private static final int CELL = 16;
private static final char CELL_CHAR = '┼';
private char[][] cells;
private char a = 'a';
private boolean flag = false;//true:黑;false:白
/*
* 初始化棋盘,二维数组cells =new char[16][16]填充'┼'
*/
public FiveChess() {
cells = new char[CELL][CELL];
for (int i = 0; i < cells.length; i++)
Arrays.fill(cells[i], CELL_CHAR);
}
/*
* 打印棋盘
*/
public void printChess() {
System.out.print("");
//打印第一行为 " abcdefghijklmnop"(第一列、第一行打印空格)
for (int i = 0; i <= cells[0].length; i++){//注意"<="
if(i==0){//第一列、第一行打印为空格
System.out.print(" ");
}else{
System.out.print((char) (a + (i-1)) + "");
}
}
//打印完第一行之后,换行
System.out.println();
///打印第一列为 " abcdefghijklmnop"(第一列、第一行不打印)
for (int i = 0; i < cells.length; i++) {//注意"<"
for (int j = 0; j < cells[i].length; j++) {
if (j == 0) {
System.out.print((char) (a + i) + "");
}
System.out.print(cells[i][j] + "");
}
System.out.println();
}
}
/*
* 放棋子并判断输赢
*/
public void putChess(int row, int column) {
if (row > 15 || row < 0 || column > 15 || column < 0) {
System.out.println("棋子位置错误,请重新输入!!");
} else {
if (cells[row][column] == '┼') {
if (flag == false) {
cells[row][column] = 'O';
flag = true;
printChess();
boolean winFlag = isWin(row, column, 'O');
if (winFlag == true) {
System.out.println("白方获胜!本局游戏结束!");
System.exit(1);
}
} else {
cells[row][column] = '●';
flag = false;
printChess();
boolean winFlag = isWin(row, column, '●');
if (winFlag == true) {
System.out.println("黑方获胜!本局游戏结束!");
System.exit(1);
}
}
} else {
System.out.println("此位置棋子已经存在,请重新输入!!");
}
}
}
/*
* 判断输赢的5个方法
*/
public boolean isWin(int row, int column, char c) {
boolean win = false;
if (rowSearch(row, column, c) >= 6 // 当前棋子重复计算 所以要>=6
|| columnSearch(row, column, c) >= 6
|| leftSearch(row, column, c) >= 6
|| rightSearch(row, column, c) >= 6)
win = true;
return win;
}
/*
* 判断行
*/
public int rowSearch(int row, int column, char c) {
int tp1 = 0;
int tp2 = 0;
while (cells[row][column - tp1] == c) {
tp1++;
if ((column - tp1) < 0)
break;
}
while (cells[row][column + tp2] == c) {
tp2++;
if ((column + tp2) > 15)
break;
}
return tp1 + tp2;
}
/*
* 判断列
*/
public int columnSearch(int row, int column, char c) {
int tp1 = 0;
int tp2 = 0;
while (cells[row - tp1][column] == c) {
tp1++;
if ((row - tp1) < 0)
break;
}
while (cells[row + tp2][column] == c) {
tp2++;
if ((row + tp2) > 15)
break;
}
return tp1 + tp2;
}
/*
* 判断对角线(左高右低)
*/
public int leftSearch(int row, int column, char c) {
int tp1 = 0;
int tp2 = 0;
while (cells[row - tp1][column - tp1] == c) {
tp1++;
if ((row - tp1) < 0 || (column - tp1) < 0)
break;
}
while (cells[row + tp2][column + tp2] == c) {
tp2++;
if ((row + tp2) > 15 || (column + tp2) > 15)
break;
}
return tp1 + tp2;
}
/*
* 判断对角线(左低右高)
*/
public int rightSearch(int row, int column, char c) {
int tp1 = 0;
int tp2 = 0;
while (cells[row - tp1][column + tp1] == c) {
tp1++;
if ((row - tp1) < 0 || (column + tp1) > 15)
break;
}
while (cells[row + tp2][column - tp2] == c) {
tp2++;
if ((row + tp2) > 15 || (column - tp2) < 0)
break;
}
return tp1 + tp2;
}
/*
* 开始下棋
*/
public void Chess() {
System.out.println("欢迎使用可乐淘五子棋系统!");
//打印棋盘
printChess();
while (true) {
System.out.println("现在请[" + (flag ? "黑" : "白") + "]方放子");
System.out.println("请输入您下棋的座标,应以x,y的格式(如 b,d ):");
String str = null;
try {
BufferedReader buf;
buf = new BufferedReader(new InputStreamReader(System.in));
str = buf.readLine();
} catch (Exception e) {
System.out.println("error");
}
//将用户输入的字符串以逗号(,)作为分隔符,分隔成2个字符串
String[] posStrArr = str.split(",");
//将2个字符串转换成用户下棋的座标
int row = posStrArr[0].charAt(0)-'a';
int column = posStrArr[1].charAt(0)-'a';
putChess(row, column);
}
}
public static void main(String[] args) {
FiveChess keletao = new FiveChess();//初始化棋盘
keletao.Chess();
}
}
结果输出为:
欢迎使用可乐淘五子棋系统!
abcdefghijklmnop
a ┼┼┼┼┼┼┼┼┼┼┼┼┼┼┼┼
b ┼┼┼┼┼┼┼┼┼┼┼┼┼┼┼┼
c ┼┼┼┼┼┼┼┼┼┼┼┼┼┼┼┼
d ┼┼┼┼┼┼┼┼┼┼┼┼┼┼┼┼
e ┼┼┼┼┼┼┼┼┼┼┼┼┼┼┼┼
f ┼┼┼┼┼┼┼┼┼┼┼┼┼┼┼┼
g ┼┼┼┼┼┼┼┼┼┼┼┼┼┼┼┼
h ┼┼┼┼┼┼┼┼┼┼┼┼┼┼┼┼
i ┼┼┼┼┼┼┼┼┼┼┼┼┼┼┼┼
j ┼┼┼┼┼┼┼┼┼┼┼┼┼┼┼┼
k ┼┼┼┼┼┼┼┼┼┼┼┼┼┼┼┼
l ┼┼┼┼┼┼┼┼┼┼┼┼┼┼┼┼
m ┼┼┼┼┼┼┼┼┼┼┼┼┼┼┼┼
n ┼┼┼┼┼┼┼┼┼┼┼┼┼┼┼┼
o ┼┼┼┼┼┼┼┼┼┼┼┼┼┼┼┼
p ┼┼┼┼┼┼┼┼┼┼┼┼┼┼┼┼
现在请[白]方放子
请输入您下棋的座标,应以x,y的格式(如 b,d ):