HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

2048 Game Algorithm in Java

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
javaalgorithmgame2048

Problem

I created a 2048 game clone in Java. However, I am only writing the fundamental algorithm for the game such as the movement, the losing condition, and the algorithm to generate a new tile. This algorithm should be generic enough to work not only on 4 x 4 grid, but also on any size of square grid.

Any review / feedback would be greatly appreciated!

Tile Class

/**
 * Class of individual tile unit in 2048 game. Value of zero implies an empty tile.
 * 
 * @author dkurniawan
 */
public class Tile {

    private int value;

    /**
     * Instantiate tile with a value of zero (empty). 
     */
    public Tile(){
        this(0);
    }

    /**
     * Instantiate tile with a specific value.
     * 
     * @param value
     */
    public Tile(int value){
        this.value = value;
    }

    public void setValue(int value){    
        this.value = value; 
    }

    public int getValue(){
        return value;
    }

    /**
     * Two tiles are the same if they have the same value. (Useful for merging tile)
     * 
     * @param tile
     * @return true if two tiles are equal, false otherwise
     */
    public boolean equals(Tile tile){
        return tile.getValue() == this.getValue();  
    }

    /**
     * Add the value of this tile by the value of the tile in the parameter.
     * 
     * @param tile
     */
    public void merge(Tile tile){
        this.setValue(value + tile.getValue());
    }

    /**
     * Set the value to zero. In other words, delete / empty the tile.
     * 
     */
    public void clear(){
        this.setValue(0);
    }

    public String toString(){
        return (Integer.toString(this.getValue()));
    }

}


Grid Class (The main algorithm is in this class)

To swipe the tiles in 2048, the game calls the move method (with Direction enum as a parameter)

```
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
/**
* The main game algorithm. The grid contains n x n tiles. Tiles with a value of zero implies

Solution

Did you test this method:

public boolean equals(Tile tile) {
    return tile.getValue() == this.getValue();  
}


Especially, did you test it by passing in the null value? See "Effective Java" by Joshua Bloch, an awesome book. You could find something like: http://www.ideyatech.com/2011/04/effective-java-equals-and-hashcode/

Your equals method should look something more like:

public boolean equals(Object other) {  
    if (this == other) return true;  
    if (!(other instanceof Tile )) return false;  
    final User that = (Tile) other;  
    return this.getUsername().equals(that.getUsername());  
}


Then, it seems like this method will block your app and run forever if all tiles are already filled:

public boolean generateNewTile()


Make sure you have some escape strategy for it.

Code Snippets

public boolean equals(Tile tile) {
    return tile.getValue() == this.getValue();  
}
public boolean equals(Object other) {  
    if (this == other) return true;  
    if (!(other instanceof Tile )) return false;  
    final User that = (Tile) other;  
    return this.getUsername().equals(that.getUsername());  
}
public boolean generateNewTile()

Context

StackExchange Code Review Q#98830, answer score: 4

Revisions (0)

No revisions yet.