import java.util.Scanner;
public class Gobang {
public static int length = 15;
public static String[][] chessBoard = new String[length][length];
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String black = "★";
String white = "☆";
String grid = "╋";
String[] grade = { "⒈", "⒉", "⒊", "⒋", "⒌", "⒍", "⒎", "⒏", "⒐", "⒑", "⒒", "⒓", "⒔", "⒕", "⒖" };
// 1.创建棋盘
for (int i = 0; i < chessBoard.length; i++) {
for (int j = 0; j < chessBoard.length; j++) {
chessBoard[i][j] = grid;
if (j == (chessBoard.length - 1)) {
chessBoard[i][j] = grade[i];
}
if (i == (chessBoard.length - 1)) {
chessBoard[i][j] = grade[j];
}
System.out.print(chessBoard[i][j]);
}
System.out.println();
}
// 下子
boolean flage = true;
boolean bool = true;
while (flage) {
System.out.println("请" + (bool ? "黑" : "白") + "子下子");
String seed = scanner.next();
String[] split = seed.split(",");
int x = Integer.parseInt(split[0]) - 1;
int y = Integer.parseInt(split[1]) - 1;
// 判断棋子的有效性
if (x < 0 || y < 0 || x > 13 || y > 13) {
System.out.println("超出棋盘区域,请重新下子");
} else {
if (!chessBoard[x][y].equals(grid)) {
System.out.println("此处有子,请重新下子:");
} else {
if (bool) {
chessBoard[x][y] = black;
if (isWin(x, y, black)) {
System.out.println("黑棋获胜");
flage = false;
}
} else {
chessBoard[x][y] = white;
if (isWin(x, y, white)) {
System.out.println("白棋获胜");
flage = false;
}
}
bool = !bool;
}
}
for (String[] strings : chessBoard) {
for (String string : strings) {
System.out.print(string);
}
System.out.println();
}
}
}
// 判断输赢的方法
public static boolean isWin(int x, int y, String color) {
// 横向
for (int i = 0; i < chessBoard.length - 5; i++) {
if (chessBoard[x][i].equals(color) && chessBoard[x][i + 1].equals(color)
&& chessBoard[x][i + 2].equals(color) && chessBoard[x][i + 3].equals(color)
&& chessBoard[x][i + 4].equals(color)) {
return true;
}
}
// 竖向
for (int i = 0; i < chessBoard.length - 5; i++) {
if (chessBoard[i][y].equals(color) && chessBoard[i + 1][y].equals(color)
&& chessBoard[i + 2][y].equals(color) && chessBoard[i + 3][y].equals(color)
&& chessBoard[i + 4][y].equals(color)) {
return true;
}
}
// 斜向下
if (x >= y) {
int j = 0;
for (int i = x - y; i < chessBoard.length - 5; i++) {
if (chessBoard[i][j].equals(color) && chessBoard[i + 1][j + 1].equals(color)
&& chessBoard[i + 2][j + 2].equals(color) && chessBoard[i + 3][j + 3].equals(color)
&& chessBoard[i + 4][j + 4].equals(color)) {
return true;
}
j++;
}
} else {
int i = 0;
for (int j = y - x; j < chessBoard.length - 5; j++) {
if (chessBoard[i][j].equals(color) && chessBoard[i + 1][j + 1].equals(color)
&& chessBoard[i + 2][j + 2].equals(color) && chessBoard[i + 3][j + 3].equals(color)
&& chessBoard[i + 4][j + 4].equals(color)) {
return true;
}
i++;
}
}
// 斜向上
int i = 0;
for (int k = x + y - 2; k >= 0; k--) {
if (chessBoard[i][k].equals(color) && chessBoard[i + 1][k - 1].equals(color)
&& chessBoard[i + 2][k - 2].equals(color) && chessBoard[i + 3][k - 3].equals(color)
&& chessBoard[i + 4][k - 4].equals(color)) {
return true;
}
i++;
}
return false;
}
}
Java版-五子棋(二维数组)
最新推荐文章于 2024-05-12 22:08:15 发布