import java.util.*;
public class Bejeweled {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("How to play: type a row and column index, followed by the direction to move it: u (up), r (right), d (down), l (left)");
char[][] gameChars = new char[8][8];
char[] options = new char[] {'*', '$', '@', '+', '!', '&'};
populateRandom(gameChars, options);
drawGrid(gameChars);
System.out.println("Enter <row> <column> <direction to move>, or q to quit");
if (scan.hasNextInt()) {
int row = scan.nextInt();
int col = scan.nextInt();
String direction = scan.next();
while (isWithinGrid(row,col,direction,gameChars) == false) {
System.out.println("Enter <row> <column> <direction to move>, or q to quit");
row = scan.nextInt();
col = scan.nextInt();
direction = scan.next();
}
swap((row-1), (col-1), direction, gameChars);
drawGrid(gameChars);
}
else {System.out.print("Adios!");
}
}
public static void drawGrid(char[][] grid) {
for (int i = 0; i < grid.length; i++) {
for (int j = 0; j < grid[i].length; j++) {
System.out.print(grid[i][j] + " ");
}
System.out.println();
}
}
public static void populateRandom(char[][] grid, char[] options) {
Random randomNum = new Random();
for (int i = 0; i < grid.length; i++) {
for (int j = 0; j < grid[i].length; j++) {
int randomChar = randomNum.nextInt(5);
grid[i][j] = options[randomChar];
}
}
}
public static boolean isWithinGrid(int row, int col, String cmd, char[][] grid) {
if (row > 1 && row < 8) {
if (col > 1 && col < 8) {
if (cmd.equals("u") | cmd.equals("l") | cmd.equals("r") | cmd.equals("d")) {
return true;
}
else {return false;}
}
else if (col == 8) {
if (cmd.equals("u") | cmd.equals("l") | cmd.equals("r")) {
return true;
}
else {return false;}
}
else if (col == 1) {
if (cmd.equals("d") | cmd.equals("l") | cmd.equals("r")) {
return true;
}
else {return false;}
}
else {return false;}
}
else if (row == 8) {
if (col > 1 && col > 8) {
if (cmd.equals("u") | cmd.equals("l") | cmd.equals("d")) {
return true;
}
else {return false;}
}
else if (col == 8) {
if (cmd.equals("u") | cmd.equals("l")) {
return true;
}
else {return false;}
}
else if (col == 1) {
if (cmd.equals("d") | cmd.equals("r") | cmd.equals("l")) {
return true;
}
else {return false;}
}
}
else if (row == 1) {
if (col > 1 && col < 8) {
if (cmd.equals("u") | cmd.equals("r") | cmd.equals("d")) {
return true;
}
else {return false;}
}
else if (col == 1) {
if (cmd.equals("r") | cmd.equals("d")) {
return true;
}
else if (col == 8) {
if (cmd.equals("r") | cmd.equals("u")) {
return true;
}
}
else {return false;}
}
} else {
return false;
}
return false;
}
public static void swap(int row, int col, String cmd, char[][] grid) {
if (cmd.equals("u")) {
char temp = grid[row][col];
grid[row][col] = grid[row-1][col];
grid[row-1][col] = temp;
}
if (cmd.equals("d")) {
char temp = grid[row][col];
grid[row][col] = grid[row+1][col];
grid[row+1][col] = temp;
}
if (cmd.equals("l")) {
char temp = grid[row][col];
grid[row][col] = grid[row][col-1];
grid[row][col-1] = temp;
}
if (cmd.equals("r")) {
char temp = grid[row][col];
grid[row][col] = grid[row][col+1];
grid[row][col+1] = temp;
}
}
}\