Created
September 12, 2018 04:01
-
-
Save dulimarta/6ce938e6d6ce4c4038e6cbe146e0bccc to your computer and use it in GitHub Desktop.
Text User Interface for 1024 game
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package game1024.ui; | |
import game1024.*; | |
import java.util.ArrayList; | |
import java.util.Scanner; | |
/** | |
* Created by Hans Dulimarta | |
*/ | |
public class TextUI { | |
private NumberSlider game; | |
private int[][] grid; | |
private static int CELL_WIDTH; | |
private static String NUM_FORMAT, BLANK_FORMAT, RULER; | |
private Scanner inp; | |
static { | |
CELL_WIDTH = 4; | |
RULER = ""; | |
for (int k = 0; k < CELL_WIDTH; k++) | |
RULER += "-"; | |
} | |
public TextUI() { | |
// TODO: Uncomment the following line and replace XYZ with the name | |
// of your class that implements the NumberSlider interface | |
//game = new XYZ(); | |
if (game == null) { | |
System.err.println ("*---------------------------------------------*"); | |
System.err.println ("| You must first modify the UI program. |"); | |
System.err.println ("| Look for the first TODO item in TextUI.java |"); | |
System.err.println ("*---------------------------------------------*"); | |
System.exit(0xE0); | |
} | |
game.resizeBoard(4, 6, 64); | |
grid = new int[4][6]; | |
/* Set the string to %4d */ | |
NUM_FORMAT = String.format("%%%dd|", CELL_WIDTH); | |
/* Set the string to %4s, but without using String.format() */ | |
BLANK_FORMAT = "%" + (CELL_WIDTH) + "s|"; | |
inp = new Scanner(System.in); | |
} | |
private void printSeparatorLine(int cols) { | |
System.out.printf("+"); | |
for (int k = 0; k < cols; k++) { | |
System.out.print(RULER + "+"); | |
} | |
System.out.println(); | |
} | |
private void renderBoard() { | |
/* reset all the 2D array elements to ZERO */ | |
for (int k = 0; k < grid.length; k++) | |
for (int m = 0; m < grid[k].length; m++) | |
grid[k][m] = 0; | |
/* fill in the 2D array using information for non-empty tiles */ | |
for (Cell c : game.getNonEmptyTiles()) | |
grid[c.row][c.column] = c.value; | |
/* Print the 2D array using dots and numbers */ | |
printSeparatorLine(grid[0].length); | |
for (int k = 0; k < grid.length; k++) { | |
System.out.print("|"); | |
for (int m = 0; m < grid[k].length; m++) | |
if (grid[k][m] == 0) | |
System.out.printf (BLANK_FORMAT, "."); | |
else | |
System.out.printf (NUM_FORMAT, grid[k][m]); | |
System.out.println(); | |
printSeparatorLine(grid[k].length); | |
} | |
} | |
/** | |
* The main loop for playing a SINGLE game session. Notice that | |
* the following method contains NO GAME LOGIC! Its main task is | |
* to accept user input and invoke the appropriate methods in the | |
* game engine. | |
*/ | |
public void playLoop() { | |
/* Place the first two random tiles */ | |
game.placeRandomValue(); | |
game.placeRandomValue(); | |
renderBoard(); | |
/* To keep the right margin within 75 columns, we split the | |
following long string literal into two lines | |
*/ | |
System.out.print ("Slide direction (W, S, Z, A), " + | |
"[U]ndo or [Q]uit? "); | |
String resp = inp.next().trim().toUpperCase(); | |
while (!resp.equals("Q")) { | |
switch (resp) { | |
case "W": /* Up */ | |
game.slide(SlideDirection.UP); | |
renderBoard(); | |
break; | |
case "S": | |
game.slide(SlideDirection.RIGHT); | |
renderBoard(); | |
break; | |
case "Z": | |
game.slide(SlideDirection.DOWN); | |
renderBoard(); | |
break; | |
case "A": | |
game.slide(SlideDirection.LEFT); | |
renderBoard(); | |
break; | |
case "U": | |
try { | |
game.undo(); | |
renderBoard(); | |
} catch (IllegalStateException exp) { | |
System.err.println ("Can't undo that far"); | |
} | |
} | |
if (game.getStatus() != GameStatus.IN_PROGRESS) | |
break; | |
/* To keep the right margin within 75 columns, we split the | |
following long string literal into two lines | |
*/ | |
System.out.print ("Slide direction (W, S, Z, A), " + | |
"[U]ndo or [Q]uit? "); | |
resp = inp.next().trim().toUpperCase(); | |
} | |
/* Almost done.... */ | |
switch (game.getStatus()) { | |
case IN_PROGRESS: | |
System.out.println ("Thanks for playing!"); | |
break; | |
case USER_WON: | |
System.out.println ("Congratz"); | |
break; | |
case USER_LOST: | |
System.out.println ("Sorry....!"); | |
break; | |
} | |
} | |
public static void main(String[] arg) { | |
TextUI t = new TextUI(); | |
t.playLoop(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment