Java语言+二维数组+非递归实现五子棋游戏

2023-12-17 20:37:04

以前做过一个C语言版五子棋:C语言+二维数组+非递归实现五子棋游戏

现在做一个Java语言版五子棋,规则如下:

1、白子为O;

2、黑子为@;

3、白子先手;

4、格子占满时为平局;

5、四个方向上有连续五个子为胜利;

6、如果选择0 3表示选的第1行与第4列;

废话不多说,直接上代码:

import java.util.Scanner;

/**
 * 命令行版五子棋
 * 规则:
 * 1、白子为O;
 * 2、黑子为@;
 * 3、白子先手;
 * 4、格子占满时为平局;
 * 5、四个方向上有连续五个子为胜利;
 * 6、如果选择0 3表示选的第1行与第4列;
 */
public class WuZiQi {
    public static final int N = 16;
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        char[][] GAME = new char[N][N];
        int count = 0;    //轮换落子计算
        int m, n; //白家
        int x, y; //黑家
        int res = 0;
        wzq_init(GAME);
        wzq_show(GAME);
        while (true) {
            //当棋盘格子被下满了时
            if (N * N == count) {
                System.out.println("棋盘已经下满了,平局!");
                sc.close();
                return;
            }
            if (0 == count % 2) {
                System.out.print("请白家落子(例如0 3或1 3):");
                m = sc.nextInt();
                n = sc.nextInt();
                while (m < 0 || m > N - 1 || n < 0 || n > N - 1) {
                    System.out.print("输入的坐标超出范围,请重新输入:");
                    m = sc.nextInt();
                    n = sc.nextInt();
                }
                while ('+' != GAME[m][n]) {
                    System.out.print("此处已经落过子,请重新落子:");
                    m = sc.nextInt();
                    n = sc.nextInt();
                }
                GAME[m][n] = 'O';
                res = wzq_play(m, n, GAME);
                if (1 == res) {
                    wzq_show(GAME);
                    System.out.println("程序结束");
                    sc.close();
                    return;
                }
            } else {
                System.out.print("请黑家落子(例如0 3或1 3):");
                x = sc.nextInt();
                y = sc.nextInt();
                while (x < 0 || x > N - 1 || y < 0 || y > N - 1) {
                    System.out.print("输入的坐标超出范围,请重新输入:");
                    x = sc.nextInt();
                    y = sc.nextInt();
                }
                while ('+' != GAME[x][y]) {
                    System.out.print("此处已经落过子,请重新落子:");
                    x = sc.nextInt();
                    y = sc.nextInt();
                }
                GAME[x][y] = '@';
                res = wzq_play(x, y, GAME);
                if (1 == res) {
                    wzq_show(GAME);
                    System.out.println("程序结束");
                    sc.close();
                    return;
                }
            }
            count++;
            wzq_show(GAME);
        }
    }

    //显示五子棋棋盘
    public static void wzq_show(char[][] GAME) {
        System.out.println("五子棋棋盘如下:");
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < N; j++) {
                System.out.print(GAME[i][j]);
            }
            System.out.println();
        }
    }

    //初始化五子棋棋盘
    public static void wzq_init(char[][] GAME) {
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < N; j++) {
                GAME[i][j] = '+';
            }
        }
    }

    //游戏是否胜利的运算,一旦有人胜利则返回1
    public static int wzq_play(int a, int b, char[][] GAME) {
        int i = 0, j = 0, k = 0;
        //方向-上的
        for (j = b - 4; j < b + 4; j++) {
            if (j < 0 || j > N - 1) {
                continue;
            }
            for (i = 0; i < 5; i++) {
                if (j + i < 0 || j + i > N - 1) {
                    break;
                }
                if ('@' == GAME[a][j + i] || '+' == GAME[a][j + i]) {
                    break;
                }
                if ('O' == GAME[a][j + i] && 4 == i) {
                    System.out.println("\n恭喜你,白家-方向上获胜!");
                    return 1;
                }
            }
            for (i = 0; i < 5; i++) {
                if (j + i < 0 || j + i > N - 1) {
                    break;
                }
                if ('O' == GAME[a][j + i] || '+' == GAME[a][j + i]) {
                    break;
                }
                if ('@' == GAME[a][j + i] && 4 == i) {
                    System.out.println("\n恭喜你,黑家-方向上获胜!");
                    return 1;
                }
            }
        }

        //方向|上的
        for (i = a - 4; i < a + 4; i++) {
            if (i < 0 || i > N - 1) {
                continue;
            }
            for (j = 0; j < 5; j++) {
                if (i + j < 0 || i + j > N - 1) {
                    break;
                }
                if ('@' == GAME[i + j][b] || '+' == GAME[i + j][b]) {
                    break;
                }
                if ('O' == GAME[i + j][b] && 4 == j) {
                    System.out.println("\n恭喜你,白家|方向上获胜!");
                    return 1;
                }
            }
            for (j = 0; j < 5; j++) {
                if (i + j < 0 || i + j > N - 1) {
                    break;
                }
                if ('O' == GAME[i + j][b] || '+' == GAME[i + j][b]) {
                    break;
                }
                if ('@' == GAME[i + j][b] && 4 == j) {
                    System.out.println("\n恭喜你,黑家|方向上获胜!");
                    return 1;
                }
            }
        }

        //方向\上的
        for (i = a - 4, j = b - 4; i < a + 4 && j < b + 4; i++, j++) {
            if (i < 0 || i > N - 1 || j < 0 || j > N - 1) {
                continue;
            }
            for (k = 0; k < 5; k++) {
                if (i + k < 0 || i + k > N - 1 || j + k < 0 || j + k > N - 1) {
                    break;
                }
                if ('@' == GAME[i + k][j + k] || '+' == GAME[i + k][j + k]) {
                    break;
                }
                if ('O' == GAME[i + k][j + k] && 4 == k) {
                    System.out.println("\n恭喜你,白家\方向上获胜!");
                    return 1;
                }
            }
            for (k = 0; k < 5; k++) {
                if (i + k < 0 || i + k > N - 1 || j + k < 0 || j + k > N - 1) {
                    break;
                }
                if ('O' == GAME[i + k][j + k] || '+' == GAME[i + k][j + k]) {
                    break;
                }
                if ('@' == GAME[i + k][j + k] && 4 == k) {
                    System.out.println("\n恭喜你,黑家\方向上获胜!");
                    return 1;
                }
            }
        }

        //方向/上的
        for (i = a + 4, j = b - 4; i > a - 4 && j < b + 4; i--, j++) {
            if (i < 0 || i > N - 1 || j < 0 || j > N - 1) {
                continue;
            }
            for (k = 0; k < 5; k++) {
                if (i - k < 0 || i - k > N - 1 || j + k < 0 || j + k > N - 1) {
                    break;
                }
                if ('@' == GAME[i - k][j + k] || '+' == GAME[i - k][j + k]) {
                    break;
                }
                if ('O' == GAME[i - k][j + k] && 4 == k) {
                    System.out.println("\n恭喜你,白家/方向上获胜!");
                    return 1;
                }
            }
            for (k = 0; k < 5; k++) {
                if (i - k < 0 || i - k > N - 1 || j + k < 0 || j + k > N - 1) {
                    break;
                }
                if ('O' == GAME[i - k][j + k] || '+' == GAME[i - k][j + k]) {
                    break;
                }
                if ('@' == GAME[i - k][j + k] && 4 == k) {
                    System.out.println("\n恭喜你,黑家/方向上获胜!");
                    return 1;
                }
            }
        }
        return 0;
    }
}

idea里面测试可以正常运行,游戏结果正常,运行环境:jdk1.8+win11。

文章来源:https://blog.csdn.net/jnbbwyth/article/details/135049377
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。