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

199 阅读4分钟

项目介绍:

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

项目功能:

(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)