Java代写:CITS1001 2048

Introduction

做一个2048的游戏,和手机上的类似。

Overview

2048 is a recent online computer game. Play takes place on a 4x4 board of squares on which a sequence of tiles appears, each holding a number. Players move tiles around by sliding rows and columns, with the aim of combining identical tiles to make higher numbers. The player’s score is determined largely by the tiles on the final board.
You can play 2048 online to get familiar with the game. In this project you will construct a computer player for a simplified version of 2048, and also an interface for a human player.

The rules of 2048

Tiles

2048 is played on a 4x4 grid of squares. Each square is either empty, or it holds a tile holding a number in {2, 4, 8, 16, 32, 64, …}, i.e. a power of two. At the start of the game the board holds between one and
sixteen 2tiles.

Moves

The player makes a sequence of moves, each of which is Left, Right, Up, or Down. The description below is for Up moves probably you can translate for the other three possibilities, otherwise they are described here.
An Up move causes all columns on the board to attempt to slide upwards.

  • If no column can legally slide, nothing happens.
  • Otherwise all legal slides are implemented, and then a new 2-tile appears in the leftmost empty square of the bottom row.

Testing slides

A column’s attempt to slide upwards is deemed legal if either or both of the following apply.

  • A nonempty square in that column has an empty square above it.
  • Two adjacent squares in that column hold identical tiles.
    A column cannot slide upwards if it has X tiles at the top with no adjacent identical tiles, and 4X empty squares at the bottom.

Implementing slides

When a slide is implemented, the following occurs.

  • All squares slide to the top of the column, then
  • where there are two adjacent squares holding identical tiles K, those tiles combine into a single tile 2K on the higher square, and all lower tiles slide up again. A tile can participate in only one combination in a given move.

Game over

The game is over when no more successful (i.e. legal) moves are possible.

Scoring

Each successful move gets one point, and each unsuccessful move loses one point so the score can go up or down as the game proceeds.

Representations

Squares

Each square on the board is represented by an object belonging to the class Square. The class records what value the square currently holds, with 0 representing an empty square; it also provides methods to interrogate this value, and to set or modify it safely. You are required to write the constructor for Square, and one public method.

  • public void setSquare(int x) throws Exception
    sets the square to the value x if it is 0 or a legal tile value, otherwise it throws a checked exception with a suitable error message.
  • public Square(int x)
    creates the square with the value x if it is 0 or a legal tile value, otherwise it creates an empty square.

The game state

The state of the game as it progresses is represented by an object belonging to the class GameState.
This records the current state of the board, and the current score. You are required to write two constructors for GameState, and the following public methods.

  • public GameState(String file) throws Exception
    sets up the board according to the contents of the file file, e.g. B1.txt. You may assume that the file format is exactly the same as in the example. If all squares are set to empty, the constructor throws a checked exception with a suitable error message.
  • public GameState(double p) throws Exception
    sets up the board with each square independently set to either 2 (with probability p) or empty. If all squares are set to empty, the constructor throws a checked exception with a suitable error message.
    public boolean forward(Square row)
    attempts to slide row towards the front of the array, in the manner described above. Based on the nine pictured examples, it would do the following.
    {0,0,0,0} is unchanged
    {2,8,4,0} is unchanged
    {0,2,0,4} turns into {2,4,0,0}
    {4,2,0,4} turns into {4,2,4,0}
    {2,2,0,0} turns into {4,0,0,0}
    {0,2,0,2} turns into {4,0,0,0}
    {2,2,4,4} turns into {4,8,0,0}
    {2,0,2,4} turns into {4,4,0,0}
    {2,2,2,2} turns into {4,4,0,0}
    If the method makes any changes to row, it returns true; otherwise it returns false.
  • public void left()
    attempts a Left move. Remember to include the placement of the new tile, if appropriate, and remember to update the score.
  • public void clockwise()
    rotates the board 90 degrees clockwise
  • public void anticlockwise()
    rotates the board 90 degrees anticlockwise,
  • public void right()
    attempts a Right move.
  • public void up()
    attempts an Up move.
  • public void down()
    attempts a Down move.
  • public boolean gameOver()
    returns true iff there are no legal moves available on the current board.

The game

The class Play2048 defines the code for actually playing 2048. You are required to write three constructors for 2048, and one public method.

  • public Play2048(String file)
    sets up a game using the file file, and displays the initial board.
  • public Play2048(double p)
    sets up a game using the probability p, and displays the initial board.
  • public Play2048()
    sets up a game using the probability 0.3, and displays the initial board.
  • public void play2048()
    makes the computer play a game of 2048, with each (attempted) move chosen uniformly randomly.
    Display the score as you go along, and display a “Game Over” message when no legal moves are available.

It will almost certainly help you to split up some of these methods, i.e. to write private helper methods.

Tasks

Each link above gives you a skeleton of the required class, including instance variables, constructors, and methods. You are required to complete the methods and constructors whose bodies are marked with the comment TODO, in Play2048.java, GameState.java, and Square.java. Make sure you understand the connections between the classes, and where each method fits into the scheme described above.
I suggest you tackle these tasks in roughly the order above, although it may be helpful to write a constructor for Play2048 and a board display method early, to make testing easier. Here are some tips. If
I add to this list, I will update the document version number.

  • The move description on the main page is for an Up move; whereas the first move you are likely to implement is a Left move. Make sure that this discrepancy doesn’t cause you any problems!
  • When performing moves, you need to update the existing board. Creating new array(s) will not make the tester happy.
  • You may add instance variables to Play2048, although make sure that the information being stored really needs an instance variable. Do not add instance variables to the other classes.
  • Avoid repetitious code! There is much less work here than it may appear…