The world’s Largest Sharp Brain Virtual Experts Marketplace Just a click Away
Levels Tought:
Elementary,Middle School,High School,College,University,PHD
| Teaching Since: | Apr 2017 |
| Last Sign in: | 103 Weeks Ago, 3 Days Ago |
| Questions Answered: | 4870 |
| Tutorials Posted: | 4863 |
MBA IT, Mater in Science and Technology
Devry
Jul-1996 - Jul-2000
Professor
Devry University
Mar-2010 - Oct-2016
Purpose
The purpose of this project is to help you become better at using arrays, 2D arrays and bounds checking. One of the most dangerous bugs that most recently affected Internet security was heartbleed, which was caused by not checking the bounds of an array!
Introduction
2048 is a game that became popular overnight a few years ago. You are going to implement a version of it in this lab.
The rules of the game you will implement are as follows:
To get a true feel for the game, you can play it using the link above.
When you boot up our version of the game, you will see a board that looks like this:
Current score: 0 | Best score: 0 ------------------------- | | | | | ------------------------- | | | | | ------------------------- | | | 2| | ------------------------- | | | | | ------------------------- Enter up, down, left or right to move in that direction. Enter reset to reset the game or quit to exit.
You can grab the skeleton code here
Part 1 - ConstructorProblem
The constructor will set up the game so the user can start playing. Our version of 2048 will not be limited to a 4×4 grid. Instead, the constructor will receive a parameter that specifies the width and height of the board. In our version, the board is still square.
Preparation
Procedure
Notes
Part 2 - Resetting the gameProblem
When the game is over (or when a user feels like they could do better), they may want to reset the game. This is what the reset() method is for.
Procedure
Hints
Part 3 - Placing New PiecesProblem
The board needs a new 2 added after each turn. To place new pieces you'll need to create two new methods.
Preparation
Procedure
Part 4 - Moving PiecesProblem
Shifting pieces around and combining them is what makes the game so fun! The moveTo method receives four parameters, which are two sets of coordinates. The first pair are coordinates of the cell from which we are trying to move, while the second pair are the coordinates of the cell to which we are trying to move. Since moveTo is a helper function for later overall board functions, moveTo will only move or merge a piece at most one space in any direction (i.e. the from piece and the to piece should be adjacent).
2048 has the following rules when it comes to shifting adjacent pieces:
Preparation
Procedure
Implement the moveTo method. This method returns true if a piece was moved, otherwise it returns false.
Part 5 - Move (up|right|down|left)Problem
If you play 2048, you can see that single pieces do not just move to the right when you send the “move right” command, but rather the entire board. Now that we can move pieces, the next step is to write functions that move an entire board in one direction (merging when necessary). The moveUp, moveDown, moveLeft, and moveRight functions will do this operation on the entire board, using the moveTo helper function to make moving cells more manageable.
Preparation
Procedure
Implement moveUp, moveDown, moveLeft, and moveRight. The procedure to move an entire board in direction X is as follows:
Example
Note: moveX() only moves all pieces on the board at most 1 space in that direction. A single call to moveRight(), for example, will cause the following change to occur in the below example (note the order in which the merge happens, starting with the cell furthest to the right in the row, since this is a moveRight() call):
------------------------- | 4| 4| 2| 2| ------------------------- | | | | 4| ------------------------- | | | | | ------------------------- | | | | | ------------------------- moveRight() -> ------------------------- | | 4| 4| 4| ------------------------- | | | | 4| ------------------------- | | | | | ------------------------- | | | | | -------------------------
If you look at the main(), main calls moveX() in a loop until it returns false to achieve the desired behavior of the game. main() is also responsible for calling placeRandom(); you should not do this in the moveX() functions. Below is an example of a complete move to the right, with a board that performs several merges while moving, calling moveRight() many times:
Current score: 4 | Best score: 4 ------------------------- | 4| 4| 2| 2| ------------------------- | | | | 4| ------------------------- | | | | | ------------------------- | | | | | ------------------------- Enter up, down, left or right to move in that direction. Enter reset to reset the game or quit to exit. right Current score: 8 | Best score: 8 ------------------------- | | | 4| 8| ------------------------- | | | | 4| ------------------------- | | 2| | | ------------------------- | | | | | ------------------------- Enter up, down, left or right to move in that direction. Enter reset to reset the game or quit to exit.
Hints
Remember, you can use your function moveTo and its return value to determine how an attempted move went.
import java.util.Random;
import java.util.Scanner;
public class TwentyFortyEight {
//This instance variable represents the board. Use this to make changes.
private int board;
//This variable keeps track of the current score.
private int score;
//Constructor
public TwentyFortyEight(int boardWidth){
// TODO
}
//This function resets the board to its initial state
public void reset() {
// TODO
}
//This function returns the total number of blank spaces on the board
public int numBlanks() {
// TODO
}
//This function places a 2 at a random blank space on the board.
//It does nothing if there are no blank spaces.
public void placeRandom(){
// TODO
}
//This function attempts to move a cell at coordinates fromRow,fromCol to
//the cell toRow,toCol. Refer to the handout for movement rules.
public boolean moveTo(int fromRow, int fromCol, int toRow, int toCol) {
// TODO
}
//The following four functions move the board in a single direction.
public boolean moveUp(){
// TODO
}
public boolean moveDown() {
// TODO
}
public boolean moveRight() {
// TODO
}
public boolean moveLeft() {
// TODO
}
////////////////////////////////////////////////////////////////////////
// Do not edit the methods below, they are for testing and running the
// program.
////////////////////////////////////////////////////////////////////////
public int getBoard() {
return board;
}
public void setBoard(int newBoard) {
board = newBoard;
} /**
* The main will allow you to play the game.
*
* @param args
*/
public static void main(String args) {
TwentyFortyEight tfe = new TwentyFortyEight(4);
Scanner s = new Scanner(System.in);
int bestScore = 0;
boolean resetFlag = false;
while(true) {
System.out.println("Current score: " + tfe.getScore() + " | Best
score: " + bestScore);
tfe.showBoard();
System.out.println("Enter up, down, left or right to move in that
direction.");
System.out.println("Enter reset to reset the game or quit to
exit.");
String line = s.nextLine();
switch(line){
case "up":
while(tfe.moveUp()){
// do nothing
}
break;
case "down":
while(tfe.moveDown()){
// do nothing
}
break;
case "left":
while(tfe.moveLeft()){
// do nothing
}
break;
case "right":
while(tfe.moveRight()){
// do nothing
}
break;
case "reset":
tfe.reset();
resetFlag = true;
break;
case "quit":
return;
default:
System.out.println("Invalid move, Please try again!!!!\n");
continue;
}
bestScore = Math.max(bestScore, tfe.getScore());
if(!resetFlag) {
tfe.placeRandom();
resetFlag = false;
} } } public void showBoard() {
for(int x = 0; x < boardWidth * 6 + 1; x++){
System.out.print("-");
}
System.out.println();
for(int x = 0; x < boardWidth; x++) {
for(int y = 0; y < boardWidth; y++) {
String square = (board[x][y] == 0)? "":board[x][y] + "";
System.out.printf("|%5s", square);
}
System.out.println("|");
for(int y = 0; y < boardWidth * 6 + 1; y++){
System.out.print("-");
}
System.out.println();
} } public int getScore() {
return score;
}
}