import java.util.Scanner;

class Lab7c {
	public static void main(String[] args) {

		Scanner kb = new Scanner(System.in);

		char[][] field = new char[10][10];

		initializePlayingField(field);			

		int playerRow = 9;
		int playerCol = 0;
		char choice = '?';;
		boolean isValid = false;
	

		do {
			printField(field);

			System.out.print("Enter a direction: w (up), a (left), s (down), d (right), x (to exit): ");
			String input = kb.nextLine();

			if (input.length() == 0) {
				isValid = false;
			}
			else {	
				choice = input.charAt(0);
				isValid = isValidDirection(playerRow, playerCol, choice);
			}

			if (isValid == true) {
				updateField(field, playerRow, playerCol, choice);

				if(choice == 'w') {
					playerRow = playerRow - 1;
				}
				if (choice == 'd') {
					playerCol = playerCol + 1;	
				}
				if (choice == 's') {
					playerRow = playerRow + 1;
				}
				if (choice == 'a') {
					playerCol = playerCol - 1;
				}
			}		
			
			System.out.print("\033[H\033[2J"); 
			System.out.flush();

		} while(choice != 'x'); 


	}
	
	static void initializePlayingField(char[][] arr) {

		for(int i = 0; i < arr.length; i++) {
			char[] row = arr[i];

			for(int j = 0; j < row.length; j++) {
				row[j] = '=';
			}
		}

		int len = arr.length;

		arr[len - 1][0] = 'X';
	}

	static void printField(char[][] arr) {
		for(char[] row : arr) {
			for(char elm : row) {
				System.out.print(elm + " ");
			}
			System.out.println();
		}
	}	

	static boolean isValidDirection(int row, int col, char direction) {
	
		if (direction != 'w' && direction != 'a' && direction != 's' && direction != 'd') {
			return false;
		}

		if(row == 0 && direction == 'w') {
			return false;
		}
		if (col == 9 && direction == 'd') {
			return false;
		}
		if (row == 9 && direction == 's') {
			return false;
		}
		if (col == 0 && direction == 'a') {
			return false;
		}

		return true;
	}

	static void updateField(char[][] matrix, int row, int col, char direction) {

		matrix[row][col] = '=';

		switch (direction) {
			case 'w':
				matrix[row-1][col] = 'X';
				break;
			case 'a':
				matrix[row][col-1] = 'X';
				break;
			case 's':
				matrix[row+1][col] = 'X';
				break;
			case 'd':
				matrix[row][col+1] = 'X';
				break;
		}
	} 
}







