一定义常量类:
public class Constant {
public static String OCCUPY = "这个位置已经被占用过了";
public static String ERROR_1 = "输入位置错误";
public static String ERROR_2 = "请正确的输入坐标(坐标越界)";
public static String GAMEOVER = "方胜!游戏结束!!!";
}
二定义我们的棋盘类:
public class Chess {
private String[][] chess;
public Chess() {
super();
chess = new String[15][15];
initView();// 初始化
showChess();// 打印棋盘
playChess();// 下棋
}
private void playChess() {
Scanner scanner = new Scanner(System.in);
int i = 0;// 棋盘的横坐标
int j = 0;// 棋盘的纵坐标
String str = "";
boolean flag = true;// 用于轮流输入棋子
while (true) {
/**
* 黑棋为1 白棋为0
*/
String chesser = flag ? "1" : "0";
System.out.println("棋盘坐标为A-O");
System.out.println("请 " + chesser + "输入棋盘坐标");
str = scanner.nextLine();// 接收输入的行列坐标
str.toUpperCase();// 防止小写输入
if (str.equals("0")) {
System.exit(0);
}
// 错误输入
if (str.length() != 2) {
System.out.println(Constant.ERROR_1);
continue;
}
/**
* 正常输入的情况
*/
// 判断是否越界
if (!isOverStep(str)) {
// 计算棋盘所在位置的实际坐标
i = str.charAt(0) - (int) 'A';
j = str.charAt(1) - (int) 'A';
} else {
System.out.println(Constant.ERROR_2);
continue;
} // end if
// 判断是否有人已经下过这个地方
if (!isOccupied(i, j)) {
chess[i][j] = chesser;
} else {
System.out.println(Constant.OCCUPY);
continue;
}
showChess();
if (!isGameOver(i, j, chesser)) {
break;// 游戏结束
}
flag = !flag;// 转化游戏对象
} // end while
}
private void showChess() {
// 打印棋盘
for (int i = 0; i < 15; i++) {
System.out.print(" " + (char) ('A' + i) + " ");
}
System.out.println();
for (int i = 0; i < chess.length; i++) {
System.out.print((char) ('A' + i));
for (int j = 0; j < chess[i].length; j++) {
if (j < 14) {
System.out.print(chess[i][j] + "-━");// 加长棋盘
} else {
System.out.print(chess[i][j]);
}
}
System.out.println();
if (i < 14) {// 加高度
System.out.println(" ┃ ┃ ┃ ┃ ┃ ┃ ┃ ┃ ┃ ┃ ┃ ┃ ┃ ┃ ┃");
}
}
}
private void initView() {
for (int i = 0; i < chess.length; i++) {
for (int j = 0; j < chess[i].length; j++) {
if (i == 0 && j == 0) {
chess[i][j] = "┏-";
} else if (i == 0 && j > 0 && j < 14) {
chess[i][j] = "-┳-";
} else if (j < 14 && j > 0 && i == 14) {
chess[i][j] = "-┻-";
} else if (i == 0 && j == 14) {
chess[i][j] = "-┓";
} else if (i > 0 && i < 14 && j == 0) {
chess[i][j] = "┣-";
} else if (i > 0 && i < 14 && j == 14) {
chess[i][j] = "-┫";
} else if (j == 0 && i == 14) {
chess[i][j] = "┗-";
} else if (j == 14 && i == 14) {
chess[i][j] = "-┛";
} else {
chess[i][j] = "-╋-";
}
}
}
}
/**
* 判断游戏是否结束
* @param x 横坐标
* @param y 纵坐标
* @param chesser 游戏方
* @return 没结束则返回ture
* 否则返回false
*/
private boolean isGameOver(int x, int y, String chesser) {
int i = x;
int j = y;
int count = 0;
/**
* 第一种匹配模式左右匹配
*/
// 判断横向向左是否五连
for (j = y; j >= 0; j--) {
if (chess[i][j].equals(chesser)) {
count++;
} else {
break;
}
}
// 判断横向向右是否五连
for (j = y; j <= 14; j++) {
if (chess[i][j].equals(chesser)) {
count++;
} else {
break;
}
}
count--;//多加了一次要减去
if (count >= 5) {
System.out.println(chesser + Constant.GAMEOVER);
return false;
}
/**
* 第二种匹配模式上下匹配
*/
// 判断纵向向上是否五连
i = x;
j = y;
count = 0;
for (i = x; i >= 0; i--) {
if (chess[i][j].equals(chesser)) {
count++;
} else {
break;
}
}
// 判断纵向向下是否五连
for (i = x; i <= 14; i++) {
if (chess[i][j].equals(chesser)) {
count++;
} else {
break;
}
}
count--;//多加了一次要减去
if (count >= 5) {
System.out.println(chesser + Constant.GAMEOVER);
return false;
}
/**
* 第三种匹配模式主对角线匹配
*/
// 判断左上是否五连
count = 0;
for (i = x, j = y; i >= 0 && j >= 0; i--, j--) {
if (chess[i][j].equals(chesser)) {
count++;
} else {
break;
}
}
// 判断右下是否五连
for (i = x, j = y; i < chess.length && j < chess.length; i++, j++) {
if (chess[i][j].equals(chesser)) {
count++;
} else {
break;
}
}
count--;
if (count >= 5) {
System.out.println(chesser + Constant.GAMEOVER);
return false;
}
/**
* 第四种匹配模式副对角线
*/
// 判断左下是否五连
count = 0;
for (i = x, j = y; i < chess.length && j >= 0; i++, j--) {
if (chess[i][j].equals(chesser)) {
count++;
} else {
break;
}
}
// 判断右上是否五连
for (i = x, j = y; i >= 0 && j < chess.length; i--, j++) {
if (chess[i][j].equals(chesser)) {
count++;
} else {
break;
}
}
count--;
if (count >= 5) {
System.out.println(chesser + Constant.GAMEOVER);
return false;
}
return true;
}
/**
* 判断是否被占有
*
* @param i
* 横坐标
* @param j
* 纵坐标
* @return
*/
private boolean isOccupied(int i, int j) {
if (!chess[i][j].equals("0") && !chess[i][j].equals("1")) {
return false;// 没有被占用过
}
return true;// 被占用了
}
/**
*
* @param str
* 输入参数
* @return
*/
private boolean isOverStep(String str) {
if (str.charAt(0) <= 'O' && str.charAt(0) >= 'A' && str.charAt(1) <= 'O' && str.charAt(1) >= 'A'
&& str.length() == 2) {
// 计算棋盘所在位置的实际坐标
return false;// 没有越界
} else {
return true;// 越界
} // end if
}
}
三定义我们的测试类:
public class ChessTest {
public static void main(String[] args) {
while(true) {
Scanner scanner = new Scanner(System.in);
String str = scanner.next();
str.toUpperCase();//转化为大写;
if(str.equals("y")) {
Chess chess = new Chess();
}else {
break;
}
}
}
}