用java语言下五子棋

一、构思实现方法:

1、初始化棋盘(抽取到一个方法中)

2、打印棋盘 (抽取到一个方法中)

3、实现黑白双方交替下棋,每次下完后打印棋盘

4、判断棋子是否正确落下(是否越界,是否重复)

5、判断输赢

二、分部实现:

1、设置初始变量

public class WZQ {

    static String white = "☆";
    static String black = "★";
    static String[][] qp = new String[15][15];//棋盘
    static String[] num = {"⒈", "⒉", "⒊", "⒋", "⒌", "⒍", "⒎", "⒏", "⒐", "⒑", "⒒", "⒓", "⒔", "⒕", "⒖"};
    static String line = "十";
}

2、初始化棋盘:

public static void init() {
        for (int i = 0; i < qp.length; i++)
            for (int j = 0; j < qp[i].length; j++) {
                if (j == qp[i].length - 1) {
                    qp[i][j] = num[i];
                    continue;
                }
                if (i == qp.length - 1) {
                    qp[i][j] = num[j];
                    continue;
                }
                qp[i][j] = line;
            }
    }

3、编写输出程序:

public static void print() {
        for (int i = 0; i < qp.length; i++) {
            for (int j = 0; j < qp[i].length; j++) {
                System.out.print(qp[i][j]);
            }
            System.out.println();
        }
    }

4、开始下棋:

        import java.util.Scanner;
        public static void startGame() {
        Scanner scanner = new Scanner(System.in);
        boolean flage = true;//true==黑棋   false==白棋
        int flage1 = 1;//判断是否越界  0越界  1不越界
        do {
            if (flage) {
                //下黑棋
                System.out.print("请输入黑棋横坐标:");
                int x = scanner.nextInt() - 1;
                System.out.print("请输入黑棋纵坐标:");
                int y = scanner.nextInt() - 1;
                qp[x][y] = black;
                print();
                flage = false;
            }
            if (flage == false) {
                //下白棋
                System.out.print("请输入白棋横坐标:");
                int x = scanner.nextInt() - 1;
                System.out.print("请输入白棋纵坐标:");
                int y = scanner.nextInt() - 1;
                qp[x][y] = white;
                print();
                flage = true;
            }
        } while (true);
      }

5、判断棋子是否正确落下(是否越界,是否重复)

startGame内部:

                flage1 = which(x, y);
                if (flage1 == 0) {
                    System.out.println("黑棋越界或重复,请重新输入");
                    continue;
                }

 

public static int which(int a, int b) {
        //是否越界
        if ((a < 0 || a > 13) || (b < 0 || b > 13)) {
            return 0;
        }
        //是否重复
        if (qp[a][b] == white || qp[a][b] == black) {
            return 0;
        }
        return 1;
    }

6、判断输赢

startGame内部:

                boolean winer = win(x, y, black);
                if (winer) {
                    break;
                }
                flage = false;
    public static boolean win(int a, int b, String what) {
        int n = 1;
        //判断行
        for (int y = b + 1; y < qp[a].length; y++) {
            if (qp[a][y] == what) {
                n += 1;
            } else {
                break;
            }
        }
        for (int y = b - 1; y >= 0; y--) {
            if (qp[a][y] == what) {
                n += 1;
            } else {
                break;
            }
        }
        //判断列
        n = 1;
        for (int x = a + 1; x < qp.length; x++) {
            if (qp[x][b] == what) {
                n += 1;
            } else {
                break;
            }
        }
        for (int x = a - 1; x >= 0; x--) {
            if (qp[x][b] == what) {
                n += 1;
            } else {
                break;
            }
        }
        //判断右下到左上
        n = 1;
        for (int x = a + 1, y = b + 1; x < qp.length && y < qp[x].length; x++, y++) {
            if (qp[x][y] == what) {
                n += 1;
            } else {
                break;
            }
        }
        for (int x = a - 1, y = b - 1; x >= 0 && y >= 0; x--, y--) {
            if (qp[x][y] == what) {
                n += 1;
            } else {
                break;
            }
        }
        //判断右上到左下
        for (int x = a - 1, y = b + 1; x >= 0 && y < qp[x].length; x--, y++) {
            if (qp[x][y] == what) {
                n += 1;
            } else {
                break;
            }
        }
        for (int x = a + 1, y = b - 1; x < qp.length && y >= 0; x++, y--) {
            if (qp[x][y] == what) {
                n += 1;
            } else {
                break;
            }
        }
        if (n >= 5) {
            return true;
        }
        return false;
    }

三、总代码:

import java.util.Scanner;

public class WZQ {

    static String white = "☆";
    static String black = "★";
    static String[][] qp = new String[15][15];//棋盘
    static String[] num = {"⒈", "⒉", "⒊", "⒋", "⒌", "⒍", "⒎", "⒏", "⒐", "⒑", "⒒", "⒓", "⒔", "⒕", "⒖"};
    static String line = "十";

    public static void main(String[] args) {
        init();
        print();
        startGame();
    }

    /*public static void start() {
        init();
        print();
        startGame();

    }*/

    /**
     * 开始下棋
     */
    public static void startGame() {
        Scanner scanner = new Scanner(System.in);
        boolean flage = true;//true==黑棋   false==白棋
        int flage1 = 1;//判断是否越界  0越界  1不越界
        do {
            if (flage) {
                //下黑棋
                System.out.print("请输入黑棋横坐标:");
                int x = scanner.nextInt() - 1;
                System.out.print("请输入黑棋纵坐标:");
                int y = scanner.nextInt() - 1;
                //判断下棋是否规范
                flage1 = which(x, y);
                if (flage1 == 0) {
                    System.out.println("黑棋越界或重复,请重新输入");
                    continue;
                }
                qp[x][y] = black;
                print();
                //判断输赢
                boolean winer = win(x, y, black);
                if (winer) {
                    break;
                }
                flage = false;
            }
            if (flage == false) {
                //下白棋
                System.out.print("请输入白棋横坐标:");
                int x = scanner.nextInt() - 1;
                System.out.print("请输入白棋纵坐标:");
                int y = scanner.nextInt() - 1;
                //判断输入是否正确
                flage1 = which(x, y);
                if (flage1 == 0) {
                    System.out.println("白棋越界或重复,请重新输入");
                    continue;
                }
                qp[x][y] = white;
                print();
                //判断输赢
                boolean winer = win(x, y, white);
                if (winer) {
                    break;
                }
                flage = true;
            }
        } while (true);
        if (flage) {
            System.out.println("黑棋胜");
        } else {
            System.out.println("白棋胜");
        }

    }

    /**
     * 判断越界重复
     */
    public static int which(int a, int b) {
        //是否越界
        if ((a < 0 || a > 13) || (b < 0 || b > 13)) {
            return 0;
        }
        //是否重复
        if (qp[a][b] == white || qp[a][b] == black) {
            return 0;
        }
        return 1;
    }

    /**
     * 判断输赢
     */
    public static boolean win(int a, int b, String what) {
        int n = 1;
        //判断行
        for (int y = b + 1; y < qp[a].length; y++) {
            if (qp[a][y] == what) {
                n += 1;
            } else {
                break;
            }
        }
        for (int y = b - 1; y >= 0; y--) {
            if (qp[a][y] == what) {
                n += 1;
            } else {
                break;
            }
        }
        //判断列
        n = 1;
        for (int x = a + 1; x < qp.length; x++) {
            if (qp[x][b] == what) {
                n += 1;
            } else {
                break;
            }
        }
        for (int x = a - 1; x >= 0; x--) {
            if (qp[x][b] == what) {
                n += 1;
            } else {
                break;
            }
        }
        //判断右下到左上
        n = 1;
        for (int x = a + 1, y = b + 1; x < qp.length && y < qp[x].length; x++, y++) {
            if (qp[x][y] == what) {
                n += 1;
            } else {
                break;
            }
        }
        for (int x = a - 1, y = b - 1; x >= 0 && y >= 0; x--, y--) {
            if (qp[x][y] == what) {
                n += 1;
            } else {
                break;
            }
        }
        //判断右上到左下
        for (int x = a - 1, y = b + 1; x >= 0 && y < qp[x].length; x--, y++) {
            if (qp[x][y] == what) {
                n += 1;
            } else {
                break;
            }
        }
        for (int x = a + 1, y = b - 1; x < qp.length && y >= 0; x++, y--) {
            if (qp[x][y] == what) {
                n += 1;
            } else {
                break;
            }
        }
        if (n >= 5) {
            return true;
        }
        return false;
    }


    /**
     * 初始化棋盘
     */
    public static void init() {
        for (int i = 0; i < qp.length; i++)
            for (int j = 0; j < qp[i].length; j++) {
                if (j == qp[i].length - 1) {
                    qp[i][j] = num[i];
                    continue;
                }
                if (i == qp.length - 1) {
                    qp[i][j] = num[j];
                    continue;
                }
                qp[i][j] = line;
            }
    }

    /**
     * 打印棋盘
     */
    public static void print() {
        for (int i = 0; i < qp.length; i++) {
            for (int j = 0; j < qp[i].length; j++) {
                System.out.print(qp[i][j]);
            }
            System.out.println();
        }
    }
}

  • 10
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
以下是一个简单的 Java 五子棋游戏的实现,希望能够帮助到你: ```java import java.util.Scanner; public class GobangGame { private static final int BOARD_SIZE = 15; private String[][] board = new String[BOARD_SIZE][BOARD_SIZE]; private String currentPlayer; private Scanner scanner; public GobangGame() { scanner = new Scanner(System.in); currentPlayer = "X"; initializeBoard(); } public void start() { printBoard(); while (true) { System.out.println("It's " + currentPlayer + "'s turn."); int row = getRow(); int col = getCol(); if (isValidMove(row, col)) { board[row][col] = currentPlayer; printBoard(); if (isWinningMove(row, col)) { System.out.println("Congratulations, " + currentPlayer + " wins!"); break; } if (isBoardFull()) { System.out.println("The game is a tie!"); break; } currentPlayer = currentPlayer.equals("X") ? "O" : "X"; } else { System.out.println("Invalid move, please try again."); } } scanner.close(); } private int getRow() { System.out.print("Enter row number (1-" + BOARD_SIZE + "): "); return scanner.nextInt() - 1; } private int getCol() { System.out.print("Enter column number (1-" + BOARD_SIZE + "): "); return scanner.nextInt() - 1; } private boolean isValidMove(int row, int col) { if (row < 0 || row >= BOARD_SIZE || col < 0 || col >= BOARD_SIZE) { return false; } return board[row][col].equals("-"); } private boolean isWinningMove(int row, int col) { String target = board[row][col]; // Check horizontal for (int i = Math.max(col - 4, 0); i <= Math.min(col, BOARD_SIZE - 5); i++) { if (board[row][i].equals(target) && board[row][i + 1].equals(target) && board[row][i + 2].equals(target) && board[row][i + 3].equals(target) && board[row][i + 4].equals(target)) { return true; } } // Check vertical for (int i = Math.max(row - 4, 0); i <= Math.min(row, BOARD_SIZE - 5); i++) { if (board[i][col].equals(target) && board[i + 1][col].equals(target) && board[i + 2][col].equals(target) && board[i + 3][col].equals(target) && board[i + 4][col].equals(target)) { return true; } } // Check diagonal (top-left to bottom-right) for (int i = Math.max(row - 4, 0), j = Math.max(col - 4, 0); i <= Math.min(row, BOARD_SIZE - 5) && j <= Math.min(col, BOARD_SIZE - 5); i++, j++) { if (board[i][j].equals(target) && board[i + 1][j + 1].equals(target) && board[i + 2][j + 2].equals(target) && board[i + 3][j + 3].equals(target) && board[i + 4][j + 4].equals(target)) { return true; } } // Check diagonal (bottom-left to top-right) for (int i = Math.min(row + 4, BOARD_SIZE - 1), j = Math.max(col - 4, 0); i >= Math.max(row, 4) && j <= Math.min(col, BOARD_SIZE - 5); i--, j++) { if (board[i][j].equals(target) && board[i - 1][j + 1].equals(target) && board[i - 2][j + 2].equals(target) && board[i - 3][j + 3].equals(target) && board[i - 4][j + 4].equals(target)) { return true; } } return false; } private boolean isBoardFull() { for (String[] row : board) { for (String cell : row) { if (cell.equals("-")) { return false; } } } return true; } private void initializeBoard() { for (int i = 0; i < BOARD_SIZE; i++) { for (int j = 0; j < BOARD_SIZE; j++) { board[i][j] = "-"; } } } private void printBoard() { System.out.print(" "); for (int i = 0; i < BOARD_SIZE; i++) { System.out.print(" " + (i + 1)); } System.out.println(); for (int i = 0; i < BOARD_SIZE; i++) { System.out.print(i + 1); for (int j = 0; j < BOARD_SIZE; j++) { System.out.print(" " + board[i][j]); } System.out.println(); } } public static void main(String[] args) { GobangGame game = new GobangGame(); game.start(); } } ``` 实现的五子棋游戏比较简单,玩家可以通过控制台输入行列坐标来下棋,程序会检查输入是否合法,并判断胜负和平局。你可以根据需要进行修改和优化。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值