Created
February 5, 2015 00:21
-
-
Save suicide/9daf873ff5b511e63c13 to your computer and use it in GitHub Desktop.
Another game of life from an interview
This file contains 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
import java.util.LinkedList; | |
import java.util.List; | |
import java.util.Scanner; | |
/** | |
* @author psy | |
* | |
*/ | |
public class Solution { | |
/** | |
* @param args | |
*/ | |
public static void main(String[] args) { | |
Scanner in = new Scanner(System.in); | |
List<String> output = new LinkedList<String>(); | |
String numberOfTestsString = in.nextLine(); | |
int numberOfTests = Integer.parseInt(numberOfTestsString); | |
for (int testNumber = 1; testNumber <= numberOfTests; testNumber++) { | |
String infoLine = in.nextLine(); | |
String[] info = infoLine.split("\\s+"); | |
int size = Integer.parseInt(info[0]); | |
int iterations = Integer.parseInt(info[1]); | |
Solution solution = new Solution(size); | |
Tile[][] grid = new Tile[size][size]; | |
for (int row = 0; row < size; row++) { | |
String rowLine = in.nextLine(); | |
// first entry in tileStrings is empty | |
String[] tileStrings = rowLine.split(""); | |
for (int col = 0; col < size; col++) { | |
grid[row][col] = Tile.getBySymbol(tileStrings[col + 1]); | |
} | |
} | |
Tile[][] result = solution.iterateGrid(grid, iterations); | |
output.add("Board " + testNumber); | |
for (Tile[] tiles : result) { | |
StringBuilder sb = new StringBuilder(); | |
for (Tile tile : tiles) { | |
sb.append(tile.symbol); | |
} | |
output.add(sb.toString()); | |
} | |
} | |
// output | |
for (int i = 0; i < output.size() - 1; i++) { | |
System.out.println(output.get(i)); | |
} | |
// last output without new line | |
System.out.print((output.get(output.size() - 1))); | |
} | |
public Solution(int maxSize) { | |
this.maxSize = maxSize; | |
} | |
/** | |
* max size of each side of the grid | |
*/ | |
private int maxSize; | |
/** | |
* do iterations on grid | |
* | |
* @param grid | |
* @param iterations | |
* @return grid | |
*/ | |
public Tile[][] iterateGrid(Tile[][] grid, int iterations) { | |
Tile[][] newGrid = grid; | |
for (int i = 0; i < iterations; i++) { | |
newGrid = updateGrid(newGrid); | |
} | |
return newGrid; | |
} | |
/** | |
* | |
* Updates the grid by the rules of the game by 1 iteration | |
* | |
* @param oldGrid | |
* @return updated grid status | |
*/ | |
private Tile[][] updateGrid(Tile[][] oldGrid) { | |
Tile[][] newGrid = copyGrid(oldGrid); | |
// iterate through grid and update each tile status to the new grid | |
for (int row = 1; row < maxSize - 1; row++) { | |
for (int col = 1; col < maxSize - 1; col++) { | |
newGrid[row][col] = updateTile(row, col, oldGrid); | |
} | |
} | |
return newGrid; | |
} | |
/** | |
* copy old grid as the outter tiles are not touched | |
* | |
* @param grid | |
* @return | |
*/ | |
private Tile[][] copyGrid(Tile[][] grid) { | |
Tile[][] copy = new Tile[maxSize][maxSize]; | |
for (int i = 0; i < maxSize; i++) { | |
for (int j = 0; j < maxSize; j++) { | |
copy[i][j] = grid[i][j]; | |
} | |
} | |
return copy; | |
} | |
/** | |
* updates a Tile by looking at its neighbors and applying the game rules | |
* | |
* @param currentRow | |
* @param currentCol | |
* @param grid | |
* @return a tile | |
*/ | |
private Tile updateTile(int currentRow, int currentCol, Tile[][] grid) { | |
int whiteNeighbors = 0; | |
// iterate over neighbors | |
// +2 because maxSize is from ".length" | |
for (int row = Math.max(0, currentRow - 1); row < Math.min(currentRow + 2, maxSize); row++) { | |
for (int col = Math.max(0, currentCol - 1); col < Math.min(currentCol + 2, maxSize); col++) { | |
// just count the white tiles | |
if (grid[row][col].isWhite) { | |
whiteNeighbors++; | |
} | |
} | |
} | |
return getColor(whiteNeighbors); | |
} | |
/** | |
* checks the rules and decides whether the tile becomes black or white | |
* depending on the number of white/black neighbors | |
* | |
* @param whiteNeighbors | |
* @return a Tile | |
*/ | |
private Tile getColor(int whiteNeighbors) { | |
// there are 9 tiles in 3x3 grid | |
// if there are more than 5 white tiles, there cannot be more black ones | |
if (whiteNeighbors >= 5) { | |
return Tile.WHITE; | |
} else { | |
return Tile.BLACK; | |
} | |
} | |
/** | |
* a Tile | |
* | |
* @author psy | |
* | |
*/ | |
private static enum Tile { | |
WHITE("w", true), BLACK("b", false); | |
/** | |
* string representation | |
*/ | |
private final String symbol; | |
/** | |
* status of liveliness | |
*/ | |
private final boolean isWhite; | |
/** | |
* constructor | |
* | |
* @param symbol | |
* @param isWhite | |
*/ | |
private Tile(String symbol, boolean isWhite) { | |
this.symbol = symbol; | |
this.isWhite = isWhite; | |
} | |
@Override | |
public String toString() { | |
return symbol; | |
} | |
/** | |
* returns a Tile instance by its symbol | |
* | |
* @param symbol | |
* @return a Tile or null of the symbol was not found | |
*/ | |
public static Tile getBySymbol(String symbol) { | |
for (Tile tile : Tile.values()) { | |
if (tile.symbol.equals(symbol)) { | |
return tile; | |
} | |
} | |
return null; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment