Skip to content

Instantly share code, notes, and snippets.

@chrislukkk
Created August 17, 2014 03:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chrislukkk/cc3970a0f22c06443b08 to your computer and use it in GitHub Desktop.
Save chrislukkk/cc3970a0f22c06443b08 to your computer and use it in GitHub Desktop.
Career cup 9.7
package Chapter9;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Random;
public class FillScreen {
private enum Direction {
None, Left, Right, Top, Bottom
}
public static void fill(char[][] screen, int x, int y, char oldColor,
char newColor, Direction from) {
if (x < 0 || x > screen.length - 1 || y < 0 || y > screen[0].length - 1
|| screen[x][y] != oldColor)
return;
// set current point color
screen[x][y] = newColor;
// left
if (y > 0 && from != Direction.Left)
fill(screen, x, y - 1, oldColor, newColor, Direction.Right);
// right
if (y < screen[0].length - 1 && from != Direction.Right)
fill(screen, x, y + 1, oldColor, newColor, Direction.Left);
// top
if (x > 0 && from != Direction.Top)
fill(screen, x - 1, y, oldColor, newColor, Direction.Bottom);
// bottom
if (x < screen.length - 1 && from != Direction.Bottom)
fill(screen, x + 1, y, oldColor, newColor, Direction.Top);
}
private static void showScreen(char[][] screen) {
System.out.println("Current screen:");
for (char[] row : screen) {
for (char c : row) {
System.out.print("[" + c + "] ");
}
System.out.println();
}
System.out.println();
}
public static void main(String[] args) {
char[][] screen = new char[6][8];
char[] ColorSet = new char[] { 'R', 'G', 'B' };
Random r = new Random();
for (int i = 0; i < screen.length; i++)
for (int j = 0; j < screen[0].length; j++)
screen[i][j] = ColorSet[Math.abs(r.nextInt()) % ColorSet.length];
showScreen(screen);
try {
BufferedReader br = new BufferedReader(new InputStreamReader(
System.in));
while (true) {
System.out.print("Enter the x axis, y axis and new color: ");
String[] data = br.readLine().split(" ");
int x = Integer.parseInt(data[0]);
int y = Integer.parseInt(data[1]);
char c = data[2].charAt(0);
fill(screen, x, y, screen[x][y], c, Direction.None);
showScreen(screen);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment