Java实现五子棋

代码实现 

public class Demo4 {
    public static class WZQ {

        static Scanner K=new Scanner(System.in);
        static String white = "☆";
        static String black = "★";
        static String[][] qp = new String[15][15];
        static String[] num = new String[]{"⒈", "⒉", "⒊", "⒋", "⒌", "⒍", "⒎", "⒏", "⒐", "⒑", "⒒", "⒓", "⒔", "⒕", "⒖"};
        static String line = "十";
        static int z = 5;//可以改变五子棋获胜的连珠数
//        static int sum=0;//判断是否为5子连珠的依据,judge方法里会用到


        public static void main(String[] args) {
            System.out.println("欢迎来到五子棋");
            //打印棋盘
            System.out.println("请选择:>");
            System.out.println("********************");
            System.out.println("******1.开始游戏******");
            System.out.println("******2.结束游戏******");
            System.out.println("********************");
            int s=K.nextInt();
            //检测是否进入游戏
            while(true){
                if(s==1){
                    init();
                    start();
                }
                else if(s==2){
                    break;
                }
                else{
                    System.out.println("请重新输入:>");
                }
            }
        }

        public static void check() {//打印棋盘
            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();//换行
            }
        }//打印棋盘

        public static void init() {//初始化棋盘
            for (int i = 0; i < qp.length; i++) {
                for (int j = 0; j < qp.length; j++) {
                    qp[i][j] = line;//初始化棋盘主题
                    if (j == qp.length - 1) {
                        qp[i][j] = num[i];//初始化棋盘内的行列数
                    }
                }
                if (i == qp.length - 1) {
                    qp[i] = num;
                }
            }
            check();//打印棋盘
        }//初始化棋盘

        public static void start() {
            boolean flag = true;
            while (true) {
                //黑子下棋阶段
                Scanner s = new Scanner(System.in);
                System.out.println("请输入位置\n[a,b](1~15)");
                int m = s.nextInt() - 1;
                int n = s.nextInt() - 1;
                //判断是否越界
                boolean rule=qp[m][n] != black && qp[m][n] != white&&m>=0&&m< qp.length&&n>=0&&n< qp.length;
                if (rule) {//判断该点是否已有落子
                    if (flag) {
                        qp[m][n] = black;
                        flag = !flag;//黑子落子,flag改变,下一次循环进入白子下棋阶段
                        check();
                        if(win(n,m,black)){
                            System.out.println("游戏结束,黑子胜利");
                            break;
                        }
                    } else {
                        qp[m][n] = white;
                        flag = !flag;//白子落子,flag改变,下一次循环进入黑子下棋阶段
                        check();
                        if(win(n,m,white)){
                            System.out.println("游戏结束,白子胜利");
                            break;
                        }
                    }
                }
                else {//若该点已有落子,则重新输入(flag仍为true,while循环依旧进入黑子下棋阶段)
                    System.out.println("非法坐标,请重新输入:");
                }
            }
        }//游戏主体

        public static boolean win(int x,int y,String chess_pieces){
            int spsum=1;//水平
            int czsum=1;//垂直
            int zxsum=1;//左下
            int yxsum=1;//右下
            //判断横向
            for(int xx=x-1;xx>=0;xx--) {//判断横左
                if(qp[y][xx].equals(chess_pieces)) {
                    spsum++;
                }
                else{
                    break;
                }
            }//判断横左
            if(spsum>=z){//判断是否已经胜利
                return true;
            }
            else{//判断横右
                for(int xx=x+1;xx< qp.length-1;xx++){
                    if(qp[y][xx].equals(chess_pieces)) {
                        spsum++;
                    }
                    else{
                        break;
                    }
                    if(spsum>=z){
                        return true;
                    }
                }
            }//判断横右
            //判断纵向
            for(int yy=y-1;yy>=0;yy--) {//判断纵上
                if(qp[yy][x].equals(chess_pieces)) {
                    czsum++;
                }
                else{
                    break;
                }
            }//判断纵上
            if(czsum>=z){
                return true;
            }
            else{//判断纵下
                for(int yy=y+1;yy< qp.length-1;yy++){
                    if(qp[yy][x].equals(chess_pieces)) {
                        czsum++;
                    }
                    else{
                        break;
                    }
                    if(czsum>=z){
                        return true;
                    }
                }
            }//判断纵下
            //判断左下
            for(int xy1=x-1,xy2=y+1;xy1>=0&&xy2<qp.length-1;xy1--,xy2++) {//判断左下
                if(qp[xy2][xy1].equals(chess_pieces)) {
                    zxsum++;
                }
                else{
                    break;
                }
            }//判断左下
            if(zxsum>=z){
                return true;
            }
            else{//判断右上
                for(int xy1=x+1,xy2=y-1;xy1< qp.length-1&&xy2>=0;xy1++,xy2--){
                    if(qp[xy2][xy1].equals(chess_pieces)) {
                        zxsum++;
                    }
                    else{
                        break;
                    }
                    if(zxsum>=z){
                        return true;
                    }
                }
            }//判断右上
            //判断右下
            for(int yx1=x+1,yx2=y+1;yx1< qp.length-1&&yx2<qp.length-1;yx1++,yx2++) {//判断右下
                if(qp[yx2][yx1].equals(chess_pieces)) {
                    yxsum++;
                }
                else{
                    break;
                }
            }//判断右下
            if(yxsum>=z){
                return true;
            }
            else{//判断左上
                for(int yx1=x-1,yx2=y-1;yx1>=0&&yx2>=0;yx1--,yx2--){
                    if(qp[yx2][yx1].equals(chess_pieces)) {
                        yxsum++;
                    }
                    else{
                        break;
                    }
                    if(yxsum>=z){
                        return true;
                    }
                }
            }//判断左上
            return false;
        }//判断胜利
    }
}

效果展示

### Java 实现五子棋游戏示例代码 #### 控制台版本的五子棋游戏 为了创建一个简单的五子棋游戏,可以先从控制台应用开始。这个游戏会允许两名玩家轮流输入坐标来放置自己的棋子。 ```java public class Gomoku { private static final int SIZE = 15; private char[][] board = new char[SIZE][SIZE]; private boolean playerOneTurn; public Gomoku() { initializeBoard(); playerOneTurn = true; // Player one starts the game. } private void initializeBoard() { for (int i = 0; i < SIZE; ++i) { for (int j = 0; j < SIZE; ++j) { board[i][j] = '-'; } } } public void playGame() { while (!isGameOver()) { displayBoard(); makeMove(); } System.out.println("Player " + (playerOneTurn ? "Two wins!" : "One wins!")); } private void makeMove() { java.util.Scanner scanner = new java.util.Scanner(System.in); int row, col; do { System.out.print("Enter move for player " + (playerOneTurn ? "one" : "two") + "(row and column): "); row = scanner.nextInt(); col = scanner.nextInt(); } while (!isValidMove(row, col)); placePiece(row, col); if (checkWinCondition(row, col)) { return; } playerOneTurn = !playerOneTurn; } private boolean isValidMove(int row, int col) { return row >= 0 && row < SIZE && col >= 0 && col < SIZE && board[row][col] == '-'; } private void placePiece(int row, int col) { board[row][col] = playerOneTurn ? 'X' : 'O'; } private boolean checkWinCondition(int lastRow, int lastCol) { // Check all directions around the latest piece placed to see if there are five consecutive pieces of same type. // This is a simplified version that only checks horizontal line here due to space limitation. char currentSymbol = board[lastRow][lastCol]; // Horizontal win condition checking... int count = 0; for (int c = Math.max(0, lastCol - 4); c <= Math.min(SIZE - 1, lastCol + 4); ++c) { if (board[lastRow][c] == currentSymbol) { count++; if (count == 5) break; } else { count = 0; } } return count == 5; } private void displayBoard() { for (char[] chars : board) { for (char ch : chars) { System.out.print(ch + " "); } System.out.println(); } } private boolean isGameOver() { // A very simple way to determine whether the game should end, // either because someone has won or no more moves can be made. return false; // Placeholder logic needs implementation. } public static void main(String[] args) { Gomoku gomoku = new Gomoku(); gomoku.playGame(); } } ``` 这段代码提供了一个基本框架用于构建更复杂的功能[^3]。对于希望进一步扩展此项目的开发者来说,可能想要添加更多特性如AI对手、图形用户界面等。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值