Table of Contents
Problem Statement
For problem statement, refer Here.
Class Diagram
Will be creating 4 Classes for representing 3 important aspects of the game.
-
GameConsole: This will we resposible for conducting the whole game. can be called as the brain of the game.
will take care of input, initilising objects, output, etc.
-
GameRules: This will store all the rules of the game by which the game will perform checks for validity.
-
GameState: This will be responsible for storing and conducting the whole game. Every change in the state of game
will be detected by this. all the operations are being executed and maintained by this.
-
Constants: As the name suggests this is just a contants class which used to declare all the constants.
Class diagram below explains this:
classDiagram
GameConsole *-- GameRules : Composition
GameConsole *-- GameState : Composition
class GameConsole {
- GameConsole instance
- GameRules gameRules
- GameState gameState
- GameConsole()
+ getInstance()
+ setup()
- start()
}
class GameRules {
- GameRules instance
- long basePower
- long randomNumberPower
- long gameWinningNumberPower
- long rowNumber
- long columnNumber
+ GameRules()
- getInstance()
+ setRandomNumberPower()
+ setGameWinningNumberPower()
+ getGameWinningNumberPower()
+ getRowNumber()
+ getColumnNumber()
+ getRandomNumberPower()
+ getBasePower()
}
class GameState {
- GameState instance
- ArrayList~ArrayList~Long~~ grid
- boolean hasWon
- GameState()
+ getInstance()
+ doOperation()
- addRandom()
- printGrid()
- doOperationAndReturn()
- doLeft()
- doRight()
- doUp()
- doDown()
+ getHasWon()
}
class Constants {
+ int LEFT
+ int RIGHT
+ int UP
+ int DOWN
+ long EMPTY_CELL
}
Relationships
- GameConsole has a has a, one-to-one relationship with gameRules, as there can be only one set of rules in a game.
- GameConsole has a has a, one-to-one relationship with GameState, as there can be only one state in a game.
Design Patterns
- In GameConsole, I have used Singleton Pattern as there is no need to have more than one console interactor.
- In GameRules, I have used Singleton Pattern as there can only be one set of rules per game.
- In GameState, I have used Singleton Pattern as there is only one game with one set of rules hence, only one state per game is enough.
Code Files
Main.java
import puzzle.GameConsole;
public class Main {
public static void main(String[] args) {
GameConsole gameConsole = GameConsole.getInstance();
gameConsole.setup();
}
}
puzzle/GameConsole.java
package puzzle;
import java.util.Scanner;
public class GameConsole {
private static GameConsole instance;
private GameRules gameRules;
private GameState gameState;
private Scanner scanner = new Scanner(System.in);
private GameConsole() {
}
public static GameConsole getInstance() {
if(instance == null) {
instance = new GameConsole();
}
return instance;
}
public void setup() {
long basePower,rowNumber,columnNumber;
long randomNumberPower,gameWinningNumberPower;
System.out.println("Please provide base power,
row number, column number : ");
basePower = scanner.nextLong();
rowNumber = scanner.nextLong();
columnNumber = scanner.nextLong();
gameRules = GameRules.getInstance(basePower,rowNumber,columnNumber);
boolean correct = false;
while(!correct) {
System.out.println("Please provide random Number Power : ");
randomNumberPower = scanner.nextLong();
correct = gameRules.setRandomNumberPower(randomNumberPower);
}
correct = false;
while(!correct) {
System.out.println("Please provide game winning Number Power : ");
gameWinningNumberPower = scanner.nextLong();
correct = gameRules.setGameWinningNumberPower(gameWinningNumberPower);
}
System.out.println("let's play 3....2....1");
gameState = GameState.getInstance(rowNumber,columnNumber);
start();
}
private void start() {
int nextMove;
while (true) {
System.out.println("Please provide your next move");
System.out.println(Constants.LEFT+"->left");
System.out.println(Constants.RIGHT+"->right");
System.out.println(Constants.UP+"->up");
System.out.println(Constants.DOWN+"->down");
nextMove = scanner.nextInt();
if(!gameState.doOperation(nextMove)) {
if(gameState.getHasWon()) {
System.out.println("Congratulations YOU WON");
return;
}
else {
System.out.println("Uh-Oh YOU LOST");
return;
}
}
}
}
}
puzzle/GameRules.java
package puzzle;
public class GameRules {
private static GameRules instance;
private static long basePower;
private static long randomNumberPower;
private static long gameWinningNumberPower;
private static long rowNumber;
private static long columnNumber;
private GameRules(long basePow,long rowNum, long columnNum) {
basePower = basePow;
rowNumber = rowNum;
columnNumber = columnNum;
}
public static GameRules getInstance(long basePower,
long rowNumber, long columnNumber) {
if(instance == null) {
instance = new GameRules(basePower,rowNumber,columnNumber);
}
return instance;
}
public boolean setRandomNumberPower(long randomNumber) {
if(randomNumberPower<0)
{
return false;
}
randomNumberPower = randomNumber;
return true;
}
public boolean setGameWinningNumberPower(long gameWinningNumber) {
if (gameWinningNumberPower<0){
return false;
}
gameWinningNumberPower = gameWinningNumber;
return true;
}
public static long getGameWinningNumberPower() {
return gameWinningNumberPower;
}
public static long getRowNumber() {
return rowNumber;
}
public static long getColumnNumber() {
return columnNumber;
}
public static long getRandomNumberPower() {
return randomNumberPower;
}
public static long getBasePower() {
return basePower;
}
}
puzzle/GameState.java
package puzzle;
import java.util.*;
public class GameState {
private static GameState instance;
private ArrayList<ArrayList<Long>> grid;
private boolean hasWon;
private GameState(long row, long col) {
this.hasWon = false;
grid = new ArrayList<>();
for (long i = 0; i < row; i++) {
ArrayList<Long> rowList = new ArrayList<>();
for (long j = 0; j < col; j++) {
rowList.add(Constants.EMPTY_CELL);
}
grid.add(rowList);
}
addRandom();
printGrid();
}
public static GameState getInstance(long row,long col) {
if(instance==null)
instance = new GameState(row,col);
return instance;
}
public boolean doOperation(int move) {
switch (move) {
case Constants.LEFT:
doLeft();
break;
case Constants.DOWN:
doDown();
break;
case Constants.UP:
doUp();
break;
case Constants.RIGHT:
doRight();
break;
default:
System.out.println("Please give correct input");
return true;
}
if(hasWon) {
return false;
}
if(!addRandom())
return false;
printGrid();
return true;
}
private boolean addRandom() {
Random random = new Random();
Long num = random.nextLong(GameRules.getRandomNumberPower()+1);
List<int[]> emptyPositions = new ArrayList<>();
for (int i = 0; i < grid.size(); i++) {
for (int j = 0; j < grid.get(i).size(); j++) {
if (grid.get(i).get(j).equals(-1L)) {
emptyPositions.add(new int[]{i, j});
}
}
}
if (emptyPositions.isEmpty()) {
return false;
}
int[] pos = emptyPositions.get(random.nextInt(emptyPositions.size()));
grid.get(pos[0]).set(pos[1], num);
return true;
}
private void printGrid() {
for (ArrayList<Long> i : grid) {
for (Long j : i) {
if (j.equals(Constants.EMPTY_CELL)) {
System.out.print("_ ");
} else {
System.out.print((long) Math.pow(
GameRules.getBasePower(), j) + " ");
}
}
System.out.println();
}
}
private ArrayList<Long> doOperationAndReturn(ArrayList<Long> array) {
ArrayList<Long> newArray = new ArrayList<>();
boolean doMerge = true;
for(Long i:array)
{
if(i!=Constants.EMPTY_CELL &&
doMerge && !newArray.isEmpty() &&
newArray.get(newArray.size()-1).equals(i)) {
newArray.set(newArray.size()-1, newArray.get(newArray.size()-1)+1);
if(newArray.get(newArray.size()-1).
equals(GameRules.getGameWinningNumberPower())){
hasWon = true;
}
doMerge=false;
}
else if (i.equals(Constants.EMPTY_CELL)) {
continue;
}
else {
newArray.add(i);
doMerge=true;
}
}
int length = array.size();
while(newArray.size()<length)
{
newArray.add(Constants.EMPTY_CELL);
}
return newArray;
}
private void doLeft() {
for (int i = 0; i < GameRules.getRowNumber(); i++) {
grid.set(i, doOperationAndReturn(grid.get(i)));
}
}
private void doRight() {
for (int i = 0; i < GameRules.getRowNumber(); i++) {
ArrayList<Long> newArray = grid.get(i);
Collections.reverse(newArray);
newArray = doOperationAndReturn(newArray);
Collections.reverse(newArray);
grid.set(i, newArray);
}
}
private void doUp() {
for (int j = 0; j < GameRules.getColumnNumber(); j++) {
ArrayList<Long> column = new ArrayList<>();
for (int i = 0; i < GameRules.getRowNumber(); i++) {
column.add(grid.get(i).get(j));
}
column = doOperationAndReturn(column);
for (int i = 0; i < GameRules.getRowNumber(); i++) {
grid.get(i).set(j, column.get(i));
}
}
}
private void doDown() {
for (int j = 0; j < GameRules.getColumnNumber(); j++) {
ArrayList<Long> column = new ArrayList<>();
for (int i = 0; i < GameRules.getRowNumber(); i++) {
column.add(grid.get(i).get(j));
}
Collections.reverse(column);
column = doOperationAndReturn(column);
Collections.reverse(column);
for (int i = 0; i < GameRules.getRowNumber(); i++) {
grid.get(i).set(j, column.get(i));
}
}
}
public boolean getHasWon() {
return hasWon;
}
}
puzzle/Constants.java
package puzzle;
public class Constants {
public static final int LEFT = 4;
public static final int RIGHT = 6;
public static final int UP = 8;
public static final int DOWN = 2;
public static final long EMPTY_CELL = -1L;
}
Summary
While there are many approaches to this problem, the one presented here is the simplest to devise and easiest to apply.
One thing to note is that while this code fulfills the basic requirements provided in here, it
is also extensible enough to let the user choose base powers, winning powers, random value max powers, grid size.
|