java CMD 实现2048 小游戏

/* Represents each Block in the grid */
public class Block {
   private int val;        // The value of the Block
   private boolean isNew;  // If the Block is newly genereated or old
   
   /* Constructing a Block object with a specified
    * value.
    * @param val - The value of the block */
   public Block(int val) {
       this.val = val;
   }
   
   /* Get the value of the Block
    * @return - The value of the Block it contains */
   public int GetVal() {
     return val; // Dummy return value. Needs to be changed.
   }
   
   /* Set the value of the Block
    * @param val - The new value that the Block will
    *              use. */
   public void SetVal(int val) {
        this.val = val;
   }
   
   /* Get if the Block is newly created or not
    * @return - True if the Block is newly generated
    *           False otherwise. */   
   public boolean GetIsNew() {
      return isNew; // Dummy return value. Needs to be changed.
   }
   
   /* Set if the Block is newly created or not
    * @param val - The value you want the Block to be 
    *              Set to true to indicate that the Block is new
    *              Set to false to indicate that the Block is old */   
   public void SetIsNew(boolean val){
       this.isNew = val;
   }
}
/* A class that handles all the printing of
 * the game. */
public class Displayer {


   public Displayer() {
   }

   /* Prints the grid and its content on the screen
    * @param grid - The Block object 2D array. */
   public void PrintGrid( Block[][] grid ) {
       String line = "-";
       for (int k = 0; k < grid[0].length; k++) {
           line += "-------";
       }
       System.out.println(line);
       for (int i = 0; i < grid.length; i++) {
           for (int j = 0; j < grid[0].length; j++) {
               Block block = grid[i][j];
               String str = "";
               if (block != null && block.GetVal() != 0){
                   str = String.valueOf(block.GetVal());
               }
               if (block != null && block.GetIsNew()){
                    str = "{"+block.GetVal()+"}";
               }
               System.out.printf("|%4s%s  ", str,"");
           }
           System.out.println("|");
           // System.out.println("-----------------------------");
           System.out.println(line);
       }
   }

   /* Prints the scores of the two players
    * @param p1 - First player's Player object
    * @param p2 - Second player's Player object */
   public void PrintScores(Player p1, Player p2 ) {
       System.out.println("=============== SCORES =============== ");
       System.out.println("| PLAYER 0:      "+p1.GetScore() + " | " + "PLAYER 1:      " + p2.GetScore() + "|");
       System.out.println("======================================");
   }
}
import java.util.Random;

/* Manages the game and all of its attributes */
public class GameSystem {
    public final static int DEFAULT_GRID_HEIGHT = 4;
    public final static int DEFAULT_GRID_WIDTH = 4;
    public final static int DEFAULT_WINNING_VAL = 2048;
    public final static int DEFAULT_WINNING_BKL = 2048;
    public final static int LEFT = 0;
    public final static int UP = 1;
    public final static int RIGHT = 2;
    public final static int DOWN = 3;
    private Block[][] grid;                              // The grid of the game
    private Player[] allPlayer;                          // To keep track of the two players

    private int winningVal;                              // Value that must be reached to win the game.
    private int winningBlk;
    private Player currPlayer;                           // Keeps track of the current player

    //=================== CONSTRUCTOR =====================//
    /* The constructor to initialize the grid. */
    public GameSystem() {

    }

    /* The constructor to customize the winning value.
     * @param winningBlk - The block that the player must reach
     *                     to win the game.
     * @param winningVal - The value that the player must reach
     *                     to win the game.
     * @param height     - Height of the grid
     * @param width      - Width of the grid */
    public GameSystem(int winningBlk, int winningVal, int height, int width) {
        this.winningBlk = winningBlk;
        this.winningVal = winningVal;
        this.grid = new Block[height][width];

        Player player = new Player(0);
        Player player1 = new Player(1);
        allPlayer = new Player[2];
        allPlayer[0] = player;
        allPlayer[1] = player1;
        SetCurrPlayer(0);
    }

    //================== PRIVATE METHODS ====================//
    /* (Write your private methods here) */
    private int moveLeft() {
        boolean moved = false;
        int score = 0;
        for (int i = 0; i < grid.length; i++) {
            for (int j = 1; j < grid[0].length; j++) {
                if (grid[i][j] != null && grid[i][j].GetVal() != 0) {
                    for (int k = j - 1; k >= 0; k--) {
                        if (grid[i][k] == null || grid[i][k].GetVal() == 0) {
                            grid[i][k] = grid[i][j];
                            grid[i][j] = null;
                            j = k;
                            moved = true;
                        } else if (grid[i][k].GetVal() == grid[i][j].GetVal()) {
                            // grid[i][k] *= 2;
                            grid[i][k] = new Block(grid[i][k].GetVal() * 2);
                            grid[i][j] = new Block(0);
                            Player player = GetCurrPlayer();
                            int i1 = player.GetScore();
                            player.SetScore(i1 + grid[i][k].GetVal());
                            score =  grid[i][k].GetVal();
                            moved = true;
                            break;
                        } else {
                            break;
                        }
                    }
                }
            }
        }
        int check = check(moved);
        if (check == -10){
            return -10;
        }
        return score;
    }

    private int moveUp() {
        int score = 0;
        boolean moved = false;
        for (int j = 0; j < grid[0].length; j++) {
            for (int i = 1; i < grid.length; i++) {
                if (grid[i][j] != null && grid[i][j].GetVal() != 0) {
                    for (int k = i - 1; k >= 0; k--) {
                        if (grid[k][j] == null || grid[k][j].GetVal() == 0) {
                            grid[k][j] = grid[i][j];
                            grid[i][j] = null;
                            i = k;
                            moved = true;
                        } else if ((grid[k][j] != null && null != grid[i][j]) && (grid[k][j].GetVal() == grid[i][j].GetVal())) {
                            // grid[k][j] *= 2;
                            grid[k][j] = new Block(grid[k][j].GetVal() * 2);
                            grid[i][j] = new Block(0);

                            Player player = GetCurrPlayer();
                            int i1 = player.GetScore();
                            player.SetScore(i1 + grid[k][j].GetVal());
                            score = grid[k][j].GetVal();
                            moved = true;
                            break;
                        } else {
                            break;
                        }
                    }
                }
            }
        }
        int check = check(moved);
        if (check == -10){
            return -10;
        }
        return score;
    }

    private int moveDown() {
        int score = 0;
        boolean moved = false;
        for (int j = 0; j < grid[0].length; j++) {
            for (int i = grid.length/2; i >= 0; i--) {
                if (grid[i][j] != null && grid[i][j].GetVal() != 0) {
                    for (int k = i + 1; k < grid.length; k++) {
                        if (grid[k][j] == null || grid[k][j].GetVal() == 0) {
                            grid[k][j] = grid[i][j];
                            grid[i][j] = null;
                            i = k;
                            moved = true;
                        } else if ((grid[k][j]  != null && null != grid[i][j]) && (grid[k][j].GetVal() == grid[i][j].GetVal())) {
                            // grid[k][j] *= 2;
                            // grid[i][j] = 0;
                            grid[k][j] = new Block(grid[k][j].GetVal() * 2);
                            grid[i][j] = new Block(0);
                            Player player = GetCurrPlayer();
                            int i1 = player.GetScore();
                            player.SetScore(i1 + grid[k][j].GetVal());
                            moved = true;
                            score = grid[k][j].GetVal();
                            break;
                        } else {
                            break;
                        }
                    }
                }
            }
        }
        int check = check(moved);
        if (check == -10){
            return -10;
        }
        return score;
    }

    private int moveRight() {
        int score = 0;
        boolean moved = false;
        for (int i = 0; i < grid.length; i++) {
            for (int j = grid.length / 2; j >= 0; j--) {
                if (grid[i][j] != null && grid[i][j].GetVal() != 0) {
                    for (int k = j + 1; k < grid[0].length; k++) {
                        if (grid[i][k] == null || grid[i][k].GetVal() == 0) {
                            grid[i][k] = grid[i][j];
                            grid[i][j] = null;
                            j = k;
                            moved = true;
                        } else if (grid[i][k].GetVal() == grid[i][j].GetVal()) {
                            grid[i][k] = new Block(grid[i][k].GetVal() * 2);
                            grid[i][j] = new Block(0);
                            Player player = GetCurrPlayer();
                            int i1 = player.GetScore();
                            player.SetScore(i1 + grid[i][k].GetVal());
                            score = grid[i][k].GetVal();
                            moved = true;
                            break;
                        } else {
                            break;
                        }
                    }
                }
            }
        }
        int check = check(moved);
        if (check == -10){
            return -10;
        }
        return score;
    }

    private int check(boolean moved) {
        if (IsGridFull()) {
            return -10;
        }
        if (moved) {
            RandBlock();
            if (IsGridFull()) {
                return -10;
            }
        }
        return 0;
    }

    //================== PUBLIC METHODS ====================//
    /* Get the whole grid of the game
     * @return - The Block object 2D array */
    public Block[][] GetGrid() {
        if (this.grid != null){
            return this.grid;  // Dummy return value. Needs to be changed.
        }
        System.out.println("返回了 4");
        return new Block[DEFAULT_GRID_WIDTH][DEFAULT_GRID_HEIGHT];
    }

    /* Randomize a block and its value (between 2 or 4) and place it
     * in the grid. Note: Must not randomize a position that already
     * has a block. */
    public void RandBlock() {
        Random random = new Random();
        int row, col;
        do {
            row = random.nextInt(grid.length);
            col = random.nextInt(grid[0].length);
        } while (grid[row][col] != null && grid[row][col].GetVal() != 0);
        Block block = new Block(random.nextInt(10) == 0 ? 4 : 2);
        for (Block[] blocks : grid) {
            if (blocks != null){
                for (Block block1 : blocks) {
                    if (block1 != null){
                        block1.SetIsNew(false);
                    }
                }
            }
        }
        block.SetIsNew(true);
        grid[row][col] = block; // 10% chance of 4, 90% chance of 2
    }

    /* Move all the blocks to the specified direction. If the direction is
     * invalid, it will not move any blocks.
     * @param dir - The direction that the user wants to move to.
     *              Refer to GameSystem global variables for direction
     *              values.
     * @return    - Returns points earned from this round of move.
     *              Returns -1 if the direction is invalid, and blocks
     *              will not be moved. */
    public int Move(int dir) {
        switch (dir){
            case 97:
                return moveLeft();
            case 119:
                return moveUp();
            case 115:
                return moveDown();
            case 100:
                return moveRight();
        }
        return -1;

    }

    /* Set who will be the player turn
     * @param playerId - The ID of the player */
    public void SetCurrPlayer(int playerId) {
        currPlayer = GetPlayer(playerId);
    }

    /* Get the Player who is currently his/her turn
     * @return - The Player object of the player who is currently
     *           his/her turn */
    public Player GetCurrPlayer() {
        return currPlayer;  // Dummy return value. Needs to be changed.
    }

    /* Get the Player with the indicated player ID
     * @param playerId - The ID of the player
     * @return         - The Player object of the player */
    public Player GetPlayer(int playerId) {
        for (Player player : allPlayer) {
            if (player.GetId() == playerId){
                return player;
            }
        }
        return null;  // Dummy return value. Needs to be changed.
    }

    /* Switch player's turn. If it is player 0 turn, then it will
     * switch to player 1, and vice versa */
    public void SwitchPlayer() {
        if (currPlayer.GetId() == 0){
            currPlayer = GetPlayer(1);
        }else {
            currPlayer = GetPlayer(0);
        }
    }

    /* Check if the player wins or not by reaching the
     * specified winning value.
     * @return - True if at least one of the blocks is equals to the wining value.
     *           False otherwise. */
    public boolean CheckWinner() {
        Player player = GetCurrPlayer();
        if (grid.length == 4 && grid[0].length == 4){
            if (player.GetScore() >= winningVal){
                System.out.println("Player " + player.GetId() + " winning");
                return true;
            }
        }else {
            if (player.GetScore() >= winningVal){
                System.out.println("Player " + player.GetId() + " winning");
                return true;
            }
        }
        return false;  // Dummy return value. Needs to be changed.
    }

    /* Check if the grid is full or not
     * @return - True if the grid has no more empty blocks
     *           False otherwise. */
    public boolean IsGridFull() {
        // Check if there are any empty tiles
        for (int i = 0; i < grid.length; i++) {
            for (int j = 0; j < grid[0].length; j++) {
                if (grid[i][j] == null || grid[i][j].GetVal() == 0) {
                    return false;
                }
            }
        }
        // Check if there are any adjacent tiles with the same value
        for (int i = 0; i < grid.length; i++) {
            for (int j = 0; j < grid[0].length-1; j++) {
                if (grid[i][j].GetVal() == grid[i][j + 1].GetVal() || grid[j][i].GetVal() == grid[j + 1][i].GetVal()) {
                    return false;
                }
            }
        }
        return true;
    }

}
import java.io.IOException;
import java.util.Scanner;


public class Main2048 {
    public static void main(String[] args) throws IOException {
        GameSystem gs = null;
        Displayer disp = new Displayer();
        int X = 0;
        int Y = 0;
        Block model[][] = null;
        Scanner scanner = new Scanner(System.in);


        /* Write your code here */
        System.out.println("which game to play?");
        System.out.println("1. Default Game (2048 winning value and 4 x 4 grid)");
        System.out.println("2. Custom Game");
        System.out.print("Your Option:");
        int opt = scanner.nextInt();
        if (opt == 1) {
            gs = new GameSystem(GameSystem.DEFAULT_WINNING_BKL,GameSystem.DEFAULT_WINNING_VAL,GameSystem.DEFAULT_GRID_HEIGHT,GameSystem.DEFAULT_GRID_WIDTH);
            System.out.println("DIRECTION GUIDE: ");
            System.out.println("a - left");
            System.out.println("w - up");
            System.out.println("d - right");
            System.out.println("s - down");
        }else if (opt == 2){
            System.out.print("Winning Block: ");
            int winningBlock = scanner.nextInt();
            System.out.print("Winning Val: ");
            int WinningVal = scanner.nextInt();
            System.out.print("Grid Height: ");
            X = scanner.nextInt();
            System.out.print("Grid Width: ");
            Y = scanner.nextInt();
            System.out.println("DIRECTION GUIDE: ");
            System.out.println("a - left");
            System.out.println("w - up");
            System.out.println("d - right");
            System.out.println("s - down");
            gs = new GameSystem(winningBlock,WinningVal,X,Y);

        }else {
            System.out.println("请输入正确的操作");
        }

        model = gs.GetGrid();

        int randomX, randomY;
        randomX = (int) (Math.random()*X);
        randomY = (int) (Math.random()*model[randomX].length);
        Block block = new Block(2);
        block.SetIsNew(true);
        model[randomX][randomY] = block;

        disp.PrintGrid(model);

        while(!false){
            String read = scanner.next();
            int move = gs.Move(read.charAt(0));
            disp.PrintGrid(gs.GetGrid());
            disp.PrintScores(gs.GetPlayer(0),gs.GetPlayer(1));
            System.out.println("Player " + gs.GetCurrPlayer().GetId() +" turn:"+read);
            if (move == -10){
                System.out.println("The grid is full ! Player " + gs.GetCurrPlayer().GetId() + " is lose");
                return;
            }
            if (move == -1){
                System.out.println("WARNING "+gs.GetCurrPlayer().GetId()+" Invalid direction");
            }
            System.out.println("PLAYER " + gs.GetCurrPlayer().GetId() +" got " + move + " in this round");
            if (gs.CheckWinner()){
                return;
            }
            gs.SwitchPlayer();
        }
    }
}
/* To keep track of a player's information */
public class Player {
  private int playerId;      // The player's ID
  private int score;         // The player's score
  
  /* Constructing a Player object with an assigned 
   * player ID
   * @param id - The ID given to the player */
  public Player( int id ) {
    this.playerId = id;
    this.score = 0;
  }
  
  /* Get the player's value 
   * @return - The ID that belongs to this 
   *           Player object */
  public int GetId() {
    return playerId;  // Dummy return false. Needs to be changed.
  }
  
  /* Set the player's score 
   * @param score - The new score that the player get */
  public void SetScore(int score) {
    this.score = score;
  }
  
  /* Get the player's score
   * @return - Get the player's current score */
  public int GetScore() {
    return score;  // Dummy return false. Needs to be changed.
  }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值