Java Edition 2048

Source: Internet
Author: User

A Application Scenarios and Cases

Some time ago 2048 compared to fire, so this case is the application of observer mode to complete the Java version of the 2048 game.

Game preview: 2048 The game uses the arrow keys to move the block up or down. If two squares with the same number collide in the move, they are merged into a single block, and the number is changed to the sum of the two. Each time you move, a new block with a value of 2 or 4 appears. When a block with a value of 2048 appears, the game wins.

Two Case studies and issues

2048 the game needs to give users a clear understanding of the number of changes in each square, so we need to have a view to achieve the purpose of data display.

Since the game requires the use of directional keys to allow the block to control the data, so we need the controller to play a different level of organizational role, to control the application process. It handles the event and responds. An "event" includes the user's behavior and changes on the data model.

If two squares with the same number collide in the move, they are merged into a single block, and the number is changed to the sum of the two. Each time you move, a new block with a value of 2 or 4 appears. When a block with a value of 2048 appears, the game wins. So we need to have a model that encapsulates the data related to the business logic of the application and how the data is processed. "Model" has the right to direct data access, such as access to the database. "Model" does not depend on "view" and "Controller", that is, the model does not care how it will be displayed or how it is manipulated. However, changes in the data in the model are generally advertised through a refresh mechanism. To implement this mechanism, the views that are used to monitor this model must be registered in advance on this model, so that the view can understand the changes that have occurred on the data model.

The game using observer mode can be very good to achieve a lot of features, such as multi-player at the same time competitive games, the competition between each other can observe the situation of the game, and to achieve a multi-person at the same time to watch a game, users can choose different difficulties according to their own needs, increase the game fun and the sense of urgency.

Three Role descriptions and UML diagrams

Subject:

The topic interface subject provides a way for you to add, remove observers, and notify the observer to update the data in a specific topic.

Observer:

The Observer interface specifies the method by which a specific observer can update the data.

Frame:

The specific theme frame notifies the specific observer by means of the Updata () method, by traversing the collection where the subject is used to hold the observer's reference, and by having each of the specific observations in the collection perform the Observer Interface (OBSERVER) to specify how the data should be updated.

Calculatoreasy:

Calculatordifficult:

The specific observer (Calculator) observes the change of the subject class data, whenever the subject class uses the customer to request the data change by the button, the specific observer (Calculator) invokes the corresponding method to complete the request, and sends the changed data to the specific topic class.

Five Program Run Results

Java Edition 2048

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.