Java 基础俩人对下五子棋

Java五子棋,一看就懂

``
import java.io.IOException;
import java.util.Scanner;

public class Chess {

/*棋盘大小
 */
final private int SIZE = 10;

/**表示棋盘的二维数组
 */
private String[][] chessBoard;

/**表示当前是黑棋还是白棋
 */
private boolean flag = true;

/**多少个棋子连线就胜利
 */
private int WIN_COUNT = 5;

/** 最后一次下棋的位置
 */
private int x = 0;
private int y = 0;

public Chess() {
    this.init();
    this.render();
}

public static void main(String[] args) throws IOException, InterruptedException {
    Chess chess = new Chess();
    while (true) {
        Scanner scanner = new Scanner(System.in);
        // 下棋
        chess.player(scanner);
        // 打印棋盘
        chess.render();
        // 判断是否已经游戏结束
        if (chess.checkIsWin(chess.x, chess.y)) {
            if (chess.flag) {
                System.out.println("黑方胜出");
            } else {
                System.out.println("白方胜出");
            }
            break;
        }
    }
}

/** 初始化棋盘
 */
private void init() {
    int size = this.SIZE;
    chessBoard = new String[size][];
    for (int i = 0; i < size; i++) {
        String[] row = new String[size];
        for (int j = 0; j < size; j++) {
            row[j] = "_";
        }
        chessBoard[i] = row;
    }
}

private void player(Scanner scanner) {
    System.out.println("请下棋,示例:2 3");
    String orgin = scanner.nextLine();
    String[] orginArr = orgin.split(" ");
    if (orginArr.length != 2) {
        System.out.println("请按照格式输入坐标!");
        player(scanner);
        return;
    }
    int x = 0;
    int y = 0;
    try {
        x = Integer.parseInt(orginArr[0]);
        y = Integer.parseInt(orginArr[1]);
    } catch (NumberFormatException e) {
        System.out.println("请输入正确的数字!");
        player(scanner);
        return;
    }
    if (x > this.SIZE || y > this.SIZE) {
        System.out.println("超出棋盘,请重新输入!");
        player(scanner);
        return;
    }
    if (!this.chessBoard[x][y].equals("_")) {
        System.out.println("当前位置已被占!");
        player(scanner);
        return;
    }
    // 下棋
    if (flag) {
        chessBoard[x][y] = "●";
        flag = !flag;
    } else {
        chessBoard[x][y] = "○";
        flag = !flag;
    }
    this.x = x;
    this.y = y;
}

/** 设计棋盘
 */

private void render() {
    int size = this.SIZE;
    System.out.print("wzq");
    for (int i = 0; i < size; i++) {
        System.out.print(i + " ");
    }
    System.out.println();
    for (int i = 0; i < size; i++) {
        System.out.print(i + "  ");
        for (int j = 0; j < size; j++) {
            String value = chessBoard[i][j];
            if (value.equals("●")) {
                System.out.print(value + " ");
            } else if (value.equals("○")) {
                System.out.print(value + " ");
            } else {
                System.out.print(value + " ");
            }
        }
        System.out.println();
    }
}

/** 遍历是否胜利
 */
private boolean checkIsWin(final int x, final int y) {
    // 棋盘大小
    int size = SIZE;
    // 记录有多少个棋子连线
    int count = 1;

    // 当前坐标棋子
    String currentChess = chessBoard[x][y];

    // 遍历竖直方向
    // 遍历下方
    for (int i = x + 1; i < size; i++) {
        if (chessBoard[i][y].equals(currentChess)) {
            count++;
        } else {
            break;
        }
    }
    // 遍历上方
    for (int i = x - 1; i >= 0; i--) {
        if (chessBoard[i][y].equals(currentChess)) {
            count++;
        } else {
            break;
        }
    }
    // System.out.println("当前竖直连珠: " + count);
    if (count >= WIN_COUNT) {
        return true;
    } else {
        count = 1;
    }

    // 遍历左右方向
    // 遍历左方
    for (int i = y - 1; i >= 0; i--) {
        if (chessBoard[x][i].equals(currentChess)) {
            count++;
        } else {
            break;
        }
    }
    // 遍历右方
    for (int i = y + 1; i < size; i++) {
        if (chessBoard[x][i].equals(currentChess)) {
            count++;
        } else {
            break;
        }
    }
    // System.out.println("当前横向连珠: " + count);
    if (count >= WIN_COUNT) {
        return true;
    } else {
        count = 1;
    }

    // 遍历 / 方向
    // 遍历右上
    for (int i = x - 1, j = y + 1; i >= 0 && j < size; i--, j++) {
        if (chessBoard[i][j].equals(currentChess)) {
            count++;
        } else {
            break;
        }
    }
    // 遍历左下
    for (int i = x + 1, j = y - 1; i < size && j >= 0; i++, j--) {
        if (chessBoard[i][j].equals(currentChess)) {
            count++;
        } else {
            break;
        }
    }
    // System.out.println("当前 / 连珠: " + count);
    if (count >= WIN_COUNT) {
        return true;
    } else {
        count = 1;
    }

    // 遍历 \ 方向
    // 遍历左上
    for (int i = x - 1, j = y - 1; i >= 0 && j >= 0; i--, j--) {
        if (chessBoard[i][j].equals(currentChess)) {
            count++;
        } else {
            break;
        }
    }
    // 遍历右下
    for (int i = x + 1, j = y + 1; i < size && j < size; i++, j++) {
        if (chessBoard[i][j].equals(currentChess)) {
            count++;
        } else {
            break;
        }
    }
    // System.out.println("当前 \\ 连珠: " + count);
    if (count >= this.WIN_COUNT) {
        return true;
    } else {
        count = 1;
    }
    return false;
}

对于输出达到交互效果,用while(true)
再if(退出条件){break;}就能简单实现

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值