关于java:利用Java类和对象以及数组开发一个小型五子棋游戏

3次阅读

共计 4815 个字符,预计需要花费 13 分钟才能阅读完成。

我的项目介绍:
开发一个小型五子棋手游游戏程序。游戏开始时,抉择黑棋、白棋开局,将一枚棋子落在棋盘一坐标上,而后轮流落子,如此轮流下子,直到某一方首先在棋盘的竖、横或两斜四方向上的五子连成线,则该方该局获胜。

我的项目性能:
(1)输入棋盘;
(2)提醒用户下子;
(3)查看用户是否出界或者落子地位是否被占用;
(4)轮流黑棋白棋抉择坐标地位进行下子;
(5)判断游戏是否输赢(平棋???悔棋????认输???人机模式???);
(6)判断是否进入下一局;
(7)退出游戏。

我的项目知识点:
类和对象、二维数组

我的项目实现思路:
1)棋盘设计为 10*10 格, 棋盘类型 Chess[][] 二维数组,所含属性 String chessType; 棋盘首先 chessType 值是”➕”。
2)初始化二维数组
3) 玩家抉择黑白圈后,开始下棋。输出要下棋子的行列坐标,黑白棋子轮流落子,当一方连成五子或下满棋盘时,游戏完结(连成五子的一方获胜,下满棋盘为和棋)。
4)每一次落子胜利后,马上判断以该地位为核心的八个方向:上、下、左、右、左上、左下、右上、右下是否有雷同色彩的棋子连成五子,如果连成五子,则游戏完结,输入相应的信息。
5)当游戏一方胜利后显示胜利信息。

剖析:
从程序外表看,这是一个二维平面图,所以数据用二维数组来示意,数组两个下标能够示意棋盘上的地位,数组元素的值代表棋格上的状态,共有三种状况,别离是,⭕代表白棋,●代表黑棋,➕代表格子。

代码
Chess

// 棋子
public class Chess {
    private  String chessType;

    public Chess(String chessType) {this.chessType = chessType;}
    public String getChessType() {return chessType;}

    public void setChessType(String chessType) {this.chessType = chessType;}
}

GoBang

import java.util.Scanner;

// 五子棋
public class GoBang {private Chess[][] chess;// 棋盘
    private int row;
    private int column;
    // 常量个数比拟少时,就定义到以后类里
    // final  示意不可批改   static 示意存储在办法区,只有一份,多个对象共享
    private static final int INITSIZE = 10;
    private static final int CONNECTION = 5;// 连贯胜利棋子数
    private static Scanner scanner = new Scanner(System.in);// 因为要多处用到

    // 构造函数
    public GoBang() {
        this.column = INITSIZE;
        this.row = INITSIZE;
        chess = new Chess[row][column];
    }

    // 初始化棋盘(start 后,才初始化)private void initValue() {for (int i = 0; i < row; i++) {for (int j = 0; j < column; j++) {chess[i][j] = new Chess("➕");// 输入法,打 加号  圆圈
            }
        }
    }

    public void print() {for (int i = 0; i < row; i++) {for (int j = 0; j < column; j++) {System.out.print(chess[i][j].getChessType());
            }
            System.out.println();}
    }

    // 获取开始棋子类型
    private String getBeginChess() {System.out.println("游戏开始,请抉择 1. 白棋⭕   2. 黑棋●");
        String beginChess;

        if (scanner.nextInt() == 2) {beginChess = "●";} else {beginChess = "⭕";}
        return beginChess;
    }

    public void start() {initValue();
        String beginChess = getBeginChess();
        while (true) {
            // 怕玩家遗记本人是什么棋子
            System.out.println("请输出" + beginChess + "棋子坐标行列数(如 2 3)");
            int xPos = scanner.nextInt();// 行列数
            int yPos = scanner.nextInt();
            if (xPos < 1 || yPos < 1 || xPos > row || yPos > column || !chess[xPos - 1][yPos - 1].getChessType().equals("➕")) {System.out.println("输出坐标不非法  请从新输出");
            } else {chess[xPos - 1][yPos - 1].setChessType(beginChess);
                print();
                // 若曾经有人胜利,就不必持续下了,新开一盘
                boolean result = isSuccess(beginChess, xPos - 1, yPos - 1);
                if (result  || (isFull()&&!result)) {System.out.println("是否持续游戏 1: 是  2:否");
                    if (scanner.nextInt() == 1) {start();// 从新开始一个棋盘
                    } else {break;}
                } else {// 还没赢,换一方持续下
                    if (beginChess.equals("⭕")) {beginChess = "●";} else {beginChess = "⭕";}

                }
            }
        }
    }

    /*
         另一方是电脑,本人下棋???横向
         纵向
         主对角线
         副对角线

         落子点两侧有没有连着的 5 个
    * */
    private boolean isFull() {for (int i = 0; i < row; i++) {for (int j = 0; j < column; j++) {if(chess[i][j].getChessType().equals("➕")|| chess[i][j].getChessType().equals("●")|| chess[i][j].getChessType().equals("⭕")){return false;// 只有有空,就是不满}
            }
        }
       return true;
    }

    private boolean isSuccess(String chessType, int i, int j) {
        // 平棋???悔棋????//  横  纵   主对角线  副对角线
        //(表达式 1)||(表达式 2)运算后果为 false  <=>  表达式 1,表达式 2 都为假,如果表达式 1 为真,则不计算表达式 2 了
        if (judgeHorizontal(chessType, i, j) || judgeVertical(chessType, i, j) || judgeVertical(chessType, i, j)
                || judgeDiagonal(chessType, i, j) || judgeSubdiagonal(chessType, i, j)) {return true;} else {return false;}
    }
/*
    0  1  2  ...  y 列数
    1
    2
    ..
    x
   行数

* */

    // 留神办法的修饰符
    private boolean judgeHorizontal(String chessType, int i, int j) {
        int x = i,// 由上往下 第几行
                y = j;// 由左往右第几列
        int count = 1;// 自身落的棋子算一个
        // 横向 i,j 右边
        while (y - 1 >= 0 && chess[x][y - 1].getChessType().equals(chessType)) {
            count++;
            y--;
        }
        // 横向 i,j 左边
        x = i;
        y = j;
        while (y + 1 < column && chess[x][y + 1].getChessType().equals(chessType)) {
            count++;
            y++;
        }
        if (count >= CONNECTION) {System.out.println(chessType + "最终获胜");
            return true;
        }
        return false;
    }

    private boolean judgeVertical(String chessType, int i, int j) {
        int x = i, y = j;
        int count = 1;// 自身落的棋子算一个
        // 纵向 i,j 上边
        while (x - 1 >= 0 && chess[x - 1][y].getChessType().equals(chessType)) {
            count++;
            x--;
        }
        // 纵向 i,j 下边
        x = i;
        y = j;
        while (x + 1 < row && chess[x + 1][y].getChessType().equals(chessType)) {
            count++;
            x++;
        }
        if (count >= CONNECTION) {System.out.println(chessType + "最终获胜");
            return true;
        }
        return false;
    }


    private boolean judgeDiagonal(String chessType, int i, int j) {
        int x = i, y = j;
        int count = 1;// 自身落的棋子算一个
        // 右上
        while (x - 1 >= 0 && y + 1 < column && chess[x - 1][y + 1].getChessType().equals(chessType)) {
            count++;
            x--;
            y++;
        }
        // 左下
        x = i;
        y = j;
        while (x + 1 < column && y - 1 > 0 && chess[x + 1][y - 1].getChessType().equals(chessType)) {
            count++;
            x++;
            y--;
        }
        if (count >= CONNECTION) {System.out.println(chessType + "最终获胜");
            return true;
        }
        return false;
    }



    private boolean judgeSubdiagonal(String chessType, int i, int j) {
        int x = i,// 由上往下 第几行
                y = j;// 由左往右第几列
        int count = 1;// 自身落的棋子算一个
        // 左上
        while (x - 1 >= 0 && y - 1 > 0 && chess[x - 1][y - 1].getChessType().equals(chessType)) {
            count++;
            x--;
            y--;
        }
        // 右下
        x = i;
        y = j;
        while (x + 1 < column && y + 1 < column && chess[x + 1][y + 1].getChessType().equals(chessType)) {
            count++;
            x++;
            y++;
        }
        if (count >= CONNECTION) {System.out.println(chessType + "最终获胜");
            return true;
        }
        return false;
    }

}


Main

public class Main {public static void main(String[] args) {GoBang goBang=new GoBang();
        goBang.start();}
}

运行后果:

游戏开始,请抉择 1. 白棋⭕   2. 黑棋●
1
请输出⭕棋子坐标行列数(如 2 3)2 3
➕➕➕➕➕➕➕➕➕➕
➕➕⭕➕➕➕➕➕➕➕
➕➕➕➕➕➕➕➕➕➕
➕➕➕➕➕➕➕➕➕➕
➕➕➕➕➕➕➕➕➕➕
➕➕➕➕➕➕➕➕➕➕
➕➕➕➕➕➕➕➕➕➕
➕➕➕➕➕➕➕➕➕➕
➕➕➕➕➕➕➕➕➕➕
➕➕➕➕➕➕➕➕➕➕
请输出●棋子坐标行列数(如 2 3)2 4
➕➕➕➕➕➕➕➕➕➕
➕➕⭕●➕➕➕➕➕➕
➕➕➕➕➕➕➕➕➕➕
➕➕➕➕➕➕➕➕➕➕
➕➕➕➕➕➕➕➕➕➕
➕➕➕➕➕➕➕➕➕➕
➕➕➕➕➕➕➕➕➕➕
➕➕➕➕➕➕➕➕➕➕
➕➕➕➕➕➕➕➕➕➕
➕➕➕➕➕➕➕➕➕➕
请输出⭕棋子坐标行列数(如 2 3)
正文完
 0