代写C C++ Java Python 视频讲解客服在线客服在线

代做国外C C++ Java程序 Python QQ 2365427650 Email 2365427650@qq.com 免费咨询编程问题

« Simple Weather Data using JavaFX

JavaFx 2048 Game

 

The game is played on a grid of tiles, typically 4 tiles by 4 tiles. Each tile may be blank or contain an integer. The game starts with all tiless blank except one tile that contains the integer 1, chosen randomly.

A player makes a "move" by choosing a direction and all the non-black tiles slide in that direction. In the normal game, the possible directions are "up", "down", "right", and "left", but our game will also add the diagonal directions "up-left", "up-right", "down-left", and "down-right".

Here is how a slide "left" works. The rest act similarly. In each row, each non-blank tile, starting at the left most non-blank tile, is slid as far as it can go to the left. It stops when it either hits the edge of the board or another non-blank tile. However, if the tile "hits" another tile with the same value, the two tiles are "merged" by adding the sliding tile's value to the tile that it "hits".

For example, if the tiles in one row are {0, 1, 0, 2} (with 0 representing a blank), the result of sliding left should be {1, 2, 0, 0} as first the 1 slides left until it gets to the left most edge, and the 2 is then slid left until it hits the 1. The remaining two tiles in that row are now blank. If the tiles in one row are {0, 1, 0, 1}, the result of sliding left should be {2, 0, 0, 0} as the left 1 slides left until it gets to the edge of the board, and the second 1 slides left until it hits the 1 causing it to merge with the 1. Now the leftmost tile stores the 2 (the result of the merge) and the rest of the tiles in that row are blank. Since you slide the leftmost non-zero tile first, if a row has {0, 1, 1, 1, 1}, the result of sliding left should be {2, 2, 0, 0, 0} and if the row has {0, 1, 1, 2, 0}, the result of sliding left should be {4, 0, 0, 0, 0}.

 

After the tiles are slid and merged in the desired direction, then one of the blank tiles on the board is chosen at random and a 1 placed on that tile.

What you must do:

Important: Read the instructions for the testing part of this project and especially the hints for testing a JavaFX application. You need to design your code so that it is easy to test. If you put off writing the testing code to the end of the project, you may need to rewrite your code to get testing to work.

Designing your program

You are to create a class called SlideGame. The SlideGame class extends the JavaFX Appliction class.

Create the board

You are to create a board by making a two-dimensional grid of Buttons. This simplest way to to that is to create a GridPane instance that will hold the buttons, and add the GridPane to the JavaFX Scene.

You can then create a 2-dimensional array of Buttons and add them to the GridPane using the add method of GridPane with the appropriate values so that the location of a button in the array corresponds to its location in the display grid. (You are welcome to change this, but you should have a two-dimensional array in your program somewhere that stores the current board situation.)

Create the initial board

All of the buttons should be blank, but choose one button at random and set its text to display the number 1.

Respond to button clicks

You should create an EventHandler for (some of) the buttons. Recall that an EventHandler has a method handle that is called every time the button is pressed. The handle method has a single parameter, ActionEvent e.

If a button on the left column is clicked, but not one of the corner buttons, every "tile" should slide horizontally to the left (see the slide rule explanation above).

If a button on the right column is clicked, but not one of the corner buttons, every "tile" should slide horizontally to the right.

If a button on the top row is clicked, but not one of the corner buttons, every "tile" should slide vertically to the top.

If a button on the bottom row is clicked, but not one of the corner buttons, every "tile" should slide vertically to the bottom.

If the button in the top right corner is clicked, every "tile" should slide diagonally up and to the right.

If the button in the top left corner is clicked, every "tile" should slide diagonally up and to the left.

If the button in the bottom right corner is clicked, every "tile" should slide diagonally down and to the right.

If the button in the bottom left corner is clicked, every "tile" should slide diagonally down and to the left.

Clicking any other button of the grid should have no affect.

If the click resulting in at least one game tile sliding, after the sliding is done, choose a blank "tile" at random and change that tile to now have a 1. If no tile moved as a result of the click, then do not add a 1 to the board.

Step 5: Add a main method

The SlideGame class should have a main method that launches the game. With a main method, you should be able to play your game by typing:

java SlideGame

in the Interactions pane (or outside of DrJava). You should also allow the user to enter two arguments for the board size. For example: 
java SlideGame should start a game with a 4x4 grid, and
java SlideGame 4 6 should start a game with 4 rows and 6 columns. If the user enters something other than realistic numbers, your code should do something appropriate, but not crash.

 

Do Not adjust the game board size in the main method directly. Instead, pass the command line arguments to the launch method. Then in the start method, you can access them using the getParameters().getRaw() method of Application. Once the startmethod has the parameters, use them to set the game board dimensions.

Hint: Do not try to code everything at once! Code things one piece at atime and then test that method. For example, code just the slide left method on arrays of int, or code just the ability to display a grid of buttons. If you try to code multiple things at once, small errors could cascade into big errors.

 

 

  • 相关文章:

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

最新评论及回复

最近发表

Powered By Z-Blog 1.8 Walle Build 100427

Copyright 代写C.