import java.util.Scanner;
public class ChessBoardPrintTest {
public static String[][] games = new String[16][16]; //棋盘数组
public static Scanner scanner = new Scanner(System.in);
public static int player = 0; //奇数代表白棋 偶数代表黑棋
private static String sit = "●"; //搜狗输入法里面的特殊符号
private static String sig = "○";
public static void main(String[] args) {
//1.初始化棋盘
initgames();
//2.打印棋盘
printgames();
//3.开始游戏
startGame();
}
//1.初始化棋盘
public static void initgames() {
for (int i = 0; i < games.length; i++) {
for (int j = 0; j < games[i].length; j++) {
games[i][j] = "+";
}
}
}
//2.打印棋盘
public static void printgames() {
System.out.print(" ");
//打印列号
for (int i = 0; i < games.length; i++) {
System.out.print(Integer.toHexString(i) + " "); //Integer类的toHexString()方法,输出十六进制数
}
System.out.println();
for (int i = 0; i < games.length; i++) {
//打印行号
System.out.print(Integer.toHexString(i) + " "); //Integer类的toHexString()方法,输出十六进制数
//打印每一行的棋盘
for (int j = 0; j < games[i].length; j++) {
System.out.print(games[i][j] + " ");
}
System.out.println();
}
}
//3.开始游戏
private static void startGame() {
while (!isGameOver(games, sit, sig)) { //当条件为假时跳出循环体,输出结果
if (player % 2 == 0) { // 说明player为偶数 黑方下棋
System.out.println("请黑方下棋:");
playChess("●");
} else { // player为奇数 白方下棋
System.out.println("请白方下棋:");
playChess("○");
}
player++; // 通过player自增实现奇偶数循环输出,达到黑棋和白棋互换
}
System.out.println("游戏结束");
}
//落子方法
private static void playChess(String chess) {
System.out.print("请输入棋子的横坐标:");
int i = scanner.nextInt() - 1;
if (i > 16) { //对输入的横坐标进行判断,如果超出最大横坐标,则调用棋盘方法,重新输入
System.out.println("选择的行数超出棋盘范围,无效");
startGame();
}
System.out.print("请输入棋子的纵坐标:");
int j = scanner.nextInt() - 1;
if (i > 16) { //对输入的纵坐标进行判断,如果超出最大纵坐标,则调用棋盘方法,重新输入
System.out.println("选择的列数超出棋盘范围,无效");
startGame();
}
if (games[i][j].equals("+")) { //如果落子的位置是+号,表示此处没有棋子,可以放子
games[i][j] = chess;
printgames();
} else {
System.out.println("此处已有棋子,请重新输入");
player--;
}
}
//判断结果
private static boolean isGameOver(String[][] games, String sit, String sig) {
for (int i = 0; i < games.length; i++) {
for (int j = 0; j < games[i].length; j++) {
if (!games[i][j].equals("+")) {
//横向
if (j < 11) {
//横向判断黑棋
if (games[i][j].equals(games[i][j + 1])
&& games[i][j].equals(games[i][j + 2])
&& games[i][j].equals(games[i][j + 3])
&& games[i][j].equals(games[i][j + 4])
&& games[i][j].equals(sit)) {
System.out.println("黑棋已经达到5颗,黑棋获胜");
return true;
}
//横向判断白棋
if (games[i][j].equals(games[i][j + 1])
&& games[i][j].equals(games[i][j + 2])
&& games[i][j].equals(games[i][j + 3])
&& games[i][j].equals(games[i][j + 4])
&& games[i][j].equals(sig)) {
System.out.println("白棋已经达到5颗,白棋获胜");
return true;
}
}
//竖向
if (i < 11) {
//竖向判断黑棋
if (games[i][j].equals(games[i + 1][j])
&& games[i][j].equals(games[i + 2][j])
&& games[i][j].equals(games[i + 3][j])
&& games[i][j].equals(games[i + 4][j])
&& games[i][j].equals(sit)) {
System.out.println("黑棋已经达到5颗,黑棋获胜");
return true;
}
//竖向判断白棋
if (games[i][j].equals(games[i + 1][j])
&& games[i][j].equals(games[i + 2][j])
&& games[i][j].equals(games[i + 3][j])
&& games[i][j].equals(games[i + 4][j])
&& games[i][j].equals(sig)) {
System.out.println("白棋已经达到5颗,白棋获胜");
return true;
}
}
//Y = -X
if (i < 11 && j < 11) {
if (games[i][j].equals(games[i + 1][j + 1])
&& games[i][j].equals(games[i + 2][j + 2])
&& games[i][j].equals(games[i + 3][j + 3])
&& games[i][j].equals(games[i + 4][j + 4])
&& games[i][j].equals(sit)) {
System.out.println("黑棋已经达到5颗,黑棋获胜");
return true;
}
if (games[i][j].equals(games[i + 1][j + 1])
&& games[i][j].equals(games[i + 2][j + 2])
&& games[i][j].equals(games[i + 3][j + 3])
&& games[i][j].equals(games[i + 4][j + 4])
&& games[i][j].equals(sig)) {
System.out.println("白棋已经达到5颗,白棋获胜");
return true;
}
}
//Y = X
if (i > 3 && j < 11) {
if (games[i][j].equals(games[i - 1][j + 1])
&& games[i][j].equals(games[i - 2][j + 2])
&& games[i][j].equals(games[i - 3][j + 3])
&& games[i][j].equals(games[i - 4][j + 4])
&& games[i][j].equals(sit)) {
System.out.println("黑棋已经达到5颗,黑棋获胜");
return true;
}
if (games[i][j].equals(games[i - 1][j + 1])
&& games[i][j].equals(games[i - 2][j + 2])
&& games[i][j].equals(games[i - 3][j + 3])
&& games[i][j].equals(games[i - 4][j + 4])
&& games[i][j].equals(sig)) {
System.out.println("白棋已经达到5颗,白棋获胜");
return true;
}
}
}
}
}
return false;
}
}
Java使用二维数组在控制台模拟实现五子棋
最新推荐文章于 2023-02-24 15:36:28 发布