Home  >  Article  >  Java  >  How to implement simple interfaceless backgammon in Java

How to implement simple interfaceless backgammon in Java

WBOY
WBOYforward
2023-04-27 18:40:071110browse

Project introduction:

This design is a small backgammon game program developed based on knowledge points Java classes and objects and arrays. At the beginning of the game, choose black or white chess to start, place a chess piece on the first coordinate of the chessboard, and then take turns to place the chess pieces, and so on, until one side first connects five pieces in the vertical, horizontal, or diagonal directions of the chessboard. line, that side wins the round.

Project implementation ideas:

1. The chessboard is designed to be 10*10 grids. The chessboard type is Chess[][] two-dimensional array, which contains the attribute String chessType; the first chessType value of the chessboard is "➕ ".
2. Initialize the two-dimensional array
3. After the player selects the black and white circle, he starts playing chess. Enter the row and column coordinates of the chess pieces to be played, and the black and white chess pieces take turns to be placed. When one side has five pieces in a row or the board is full, the game ends (the side with five pieces in a row wins, and the full board is a draw).
4. After each successful placement, immediately determine whether there are chess pieces of the same color in the eight directions centered on the position: up, down, left, right, upper left, lower left, upper right, and lower right. If If five pieces are connected, the game ends and the corresponding information is output.
5. When one side of the game wins, the victory information is displayed. From the surface of the program, this is a two-dimensional plane map, so the data is represented by a two-dimensional array. The two subscripts of the array can represent the position on the chessboard. The value of the array element represents the state of the chessboard. There are three situations, respectively. Yes, ⭕ represents white chess, ● represents black chess, and ➕ represents grid.

Source code

1. Chess piece

/**
 * @author hudongsheng
 * @date 2020/10/29 - 9:28
 */
public class ChessType {
    private String chessType;
    private int x;
    private int y;

    public int getX() {
        return x;
    }

    public void setX(int x) {
        this.x = x;
    }

    public int getY() {
        return y;
    }

    public void setY(int y) {
        this.y = y;
    }


    public ChessType() {

    }

    public String getChessType() {
        return chessType;
    }

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

2. Playing chess

/**
 * @author hudongsheng
 * @date 2020/10/29 - 9:27
 */
public class Gobang {
    private int size = 1;
    private ChessType[][] chessTypes;
    private int row;
    private int colum;
    private int x;
    private int y;


    //创建一个棋盘
    public Gobang(int row,int colum){
        this.row = row;
        this.colum = colum;
        chessTypes = new ChessType[row][colum];
    }

    //初始化棋盘
    public void initChessType(){

        for(int i = 0; i< chessTypes.length; i++){
            for (int j = 0; j< chessTypes[i].length; j++){
                chessTypes[i][j] = new ChessType();
                chessTypes[i][j].setChessType("➕");
            }
        }
    }

    //下白棋
    public void setWhiteChess(int x,int y){
        chessTypes[x][y].setChessType("⭕");
    }

    //下黑棋
    public void setBlackChess(int x,int y){
        chessTypes[x][y].setChessType("●");
    }

   //判断是否胜利
        public boolean checkWin(int i,int j) {
            // TODO Auto-generated method stub
            boolean flag = false;
            //判断纵向是否有五个棋子是相同的颜色
            int count1 = 1;//相同颜色棋子的个数
            String color = chessTypes[i][j].getChessType(); //刚下的棋子的颜色
            int a = 1;  //棋子索引的增量
            while((i+a)<row && color == chessTypes[i+a][j].getChessType()){
                count1++;
                a++;
            }
            a = 1;
            while((i-a)>=0 && color == chessTypes[i-a][j].getChessType()){
                count1++;
                a++;
            }
            if(count1 >= 5){
                flag = true;
            }

            //判断纵向是否有五个棋子是相同的颜色
            int count2 = 1;
            a = 1;
            while((j+a)<colum && color == chessTypes[i][j+a].getChessType()){
                count2++;
                a++;
            }
            a = 1;
            while((j-a)>=0 && color == chessTypes[i][j-a].getChessType()){
                count2++;
                a++;
            }
            if(count2 >= 5){
                flag = true;
            }

            //右上    左下 是否有五个棋子是相同的颜色
            int count3 = 1;
            a = 1;
            while((i+a)<row && (j-a)>=0 && color == chessTypes[i+a][j-a].getChessType()){
                count3++;
                a++;
            }
            a = 1;
            while((i-a)>=0 && (j+a)<colum && color == chessTypes[i-a][j+a].getChessType()){
                count3++;
                a++;
            }
            if(count3 >= 5){
                flag = true;
            }

            //左上  右下  是否有五个棋子是相同的颜色
            int count4 = 1;
            a = 1;
            while((i-a)>0 && (j-a)>=0 && color == chessTypes[i-a][j-a].getChessType()){
                count4++;
                a++;
            }
            a = 1;
            while((i+a)<row && (j+a)<colum && color == chessTypes[i+a][j+a].getChessType()){
                count4++;
                a++;
            }
            if(count4 >= 5){
                flag = true;
            }
            return flag;
        }

        //落子后打印棋盘
    public void print(){
        for(int i = 0; i< chessTypes.length; i++){
            for (int j = 0; j< chessTypes[i].length; j++){
                System.out.print(chessTypes[i][j].getChessType());
            }
            System.out.println();
        }
    }

}

3.Test

**
 * @author hudongsheng
 * @date 2020/10/29 - 9:27
 */
public class Test {
    public static void main(String[] args) {
        boolean flag = true;
        int x;
        int y;

        Gobang gobang = new Gobang(10,10);
        Scanner scanner = new Scanner(System.in);

        gobang.initChessType();
        //下棋

        System.out.println("黑棋执先");
        while (true){
            gobang.print();
            System.out.println("请输入下黑棋的坐标:");
            x = scanner.nextInt();
            y = scanner.nextInt();
            gobang.setBlackChess(x,y);
            if(gobang.checkWin(x,y)){
                gobang.print();
                System.out.println("黑棋胜!");
                break;
            }

            gobang.print();
            System.out.println("请输入下白棋的坐标:");
            x = scanner.nextInt();
            y = scanner.nextInt();
            gobang.setWhiteChess(x,y);

            if(gobang.checkWin(x,y)){
                gobang.print();
                System.out.println("白棋胜!");
                break;
            }

        }
    }
}

The above is the detailed content of How to implement simple interfaceless backgammon in Java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete