2048game demo

286 阅读2分钟


import java.io.IOException;
import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;
import java.util.Set;


public class Game2048 {
    private int[][] chessboard;
    private final int[] baseNumber;
    private Random random;
    private int row,col;
    private int count;
    private int max;

    public Game2048(int row, int col) {
        chessboard = new int[row][col];
        baseNumber = new int[]{2,4,8};
        random = new Random();
        this.row = row;
        this.col = col;
        count= 0;
        max = row * col;
        creatItem();
        output();

    }

    public static void main(String[] args) throws InterruptedException, IOException {
        Game2048 game2048 = new Game2048(6,6);
        // 创建一个Scanner对象,用于读取控制台输入
        try(Scanner scanner = new Scanner(System.in)){
            while (true){
                System.out.println("请进行下一步操作:");
                String operation = scanner.nextLine();
                game2048.operation(operation);
            }
            // 关闭Scanner对象
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    private void operation(String direction){
        if (direction.isBlank()|| !Set.of("a","w","s","d").contains(direction)){
            System.out.println("请输入方向:");
            return;
        }
        count = max;
        switch (direction) {
            case "w":
                up();
                break;
            case "s":
                down();
                break;
            case "a":
                left();
                break;
            case "d":
                right();
                break;
        }
        creatItem();
        output();
    }

    private void creatItem(){
        int i = random.nextInt(3);
        checkGameOver();
        while (true){
            int x = random.nextInt(row);
            int y = random.nextInt(col);
            if (chessboard[x][y] == 0){
                chessboard[x][y] = baseNumber[i];
                count--;
                return;
            }
        }
    }

    private void checkGameOver() {
        if (count == max){
            System.out.println("游戏结束");
            System.exit(0);
        }
    }

    private void down(){
        for (int y = 0; y < row; y++) {
            int xhave = col - 1;
            for (int x = col - 1; x >= 0; x--) {
                if (chessboard[x][y] == 0){
                    continue;
                }
                int x1 = x - 1;
                while (x1 >= 0 && chessboard[x1][y] == 0){
                    x1--;
                }
                if (x1 >= 0 && chessboard[x1][y] == chessboard[x][y]){
                    chessboard[xhave--][y] = chessboard[x][y] * 2;
                    x = x1;
                }else {
                    chessboard[xhave--][y] = chessboard[x][y];
                    x = x1 + 1;
                }
            }
            while (xhave >= 0){
                count--;
                chessboard[xhave--][y] = 0;
            }
        }
    }

    private void up(){
        for (int y = 0; y < row; y++) {
            int xhave = 0;
            for (int x = 0; x <col; x++) {
                if (chessboard[x][y] == 0){
                    continue;
                }
                int x1 = x + 1;
                while (x1 < col && chessboard[x1][y] == 0){
                    x1++;
                }
                if (x1 < col && chessboard[x1][y] == chessboard[x][y]){
                    chessboard[xhave++][y] = chessboard[x][y] * 2;
                    x = x1;
                }else {
                    chessboard[xhave++][y] = chessboard[x][y];
                    x = x1 - 1;
                }
            }
            while (xhave < col){
                count--;
                chessboard[xhave++][y] = 0;
            }

        }
    }

    private void right(){
        for (int[] ints : this.chessboard) {
            int have = row - 1;
            for (int i = row - 1; i >= 0; i--) {
                if (ints[i] == 0){
                    continue;
                }
                int j = i - 1;
                while (j >= 0 && ints[j] == 0){
                    j--;
                }
                if (j>=0 && ints[i] == ints[j]){
                    ints[have--] = ints[i] * 2;
                    i = j;
                }else {
                    ints[have--] = ints[i];
                    i = j+1;
                }
            }
            while (have >= 0){
                count--;
                ints[have--] = 0;
            }
        }
    }

    private void left(){
        for (int[] ints : this.chessboard) {
            int have = 0;
            for (int i = 0; i < row; i++) {
                if (ints[i] == 0){
                    continue;
                }
                int j = i + 1;
                while (j < row && ints[j] == 0){
                    j++;
                }
                if (j<row &&ints[i] == ints[j]){
                    ints[have++] = ints[i] * 2;
                    i = j;
                }else {
                    ints[have++] = ints[i];
                    i = j-1;
                }
            }
            while (have < row){
                count--;
                ints[have++] = 0;
            }
        }
    }


    public  void output() {
        System.out.println();
        int[] maxWidths = new int[row];
        Arrays.fill(maxWidths, 5);


        // 打印表格的上边框
        printHorizontalBorder(maxWidths);

        // 打印表格内容
        for (int[] row : chessboard) {
            System.out.print("|");
            for (int i = 0; i < row.length; i++) {
                if (row[i] == 0) {
                    System.out.printf("%" + maxWidths[i] + "s|", " ");
                } else {
                    System.out.printf("%" + maxWidths[i] + "d|", row[i]);
                }
            }
            System.out.println();
            printHorizontalBorder(maxWidths);
        }
    }

    private static void printHorizontalBorder(int[] maxWidths) {
        System.out.print("+");
        for (int width : maxWidths) {
            for (int i = 0; i < width; i++) {
                System.out.print("-");
            }
            System.out.print("+");
        }
        System.out.println();
    }
}