Java代写:CITS1001-2048

Introduction

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.

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…