Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- public class MoveInArray
- {
- private static int[][] grid = new int[5][5];
- public static void main(String[] args){
- int playerRow = 0;
- int playerCol = 0;
- for(int i = 0; i < grid.length; i++){
- for(int j = 0; j < grid[i].length; j++){
- grid[i][j] = 0;
- }
- }
- grid[playerRow][playerCol] = 1;
- printArray(grid);
- Scanner scan = new Scanner(System.in);
- while(true){
- try{
- System.out.println("Direction to move?");
- String input = scan.nextLine();
- switch(input){
- case "w":
- grid[playerRow-1][playerCol] = 1;
- grid[playerRow][playerCol] = 0;
- playerRow--;
- break;
- case "a":
- grid[playerRow][playerCol-1] = 1;
- grid[playerRow][playerCol] = 0;
- playerCol--;
- break;
- case "s":
- grid[playerRow+1][playerCol] = 1;
- grid[playerRow][playerCol] = 0;
- playerRow++;
- break;
- case"d":
- grid[playerRow][playerCol+1] = 1;
- grid[playerRow][playerCol] = 0;
- playerCol++;
- break;
- }
- }catch(Exception e){
- System.out.println("invalid input");
- }
- printArray(grid);
- }
- }
- public static void printArray(int[][] arr){
- for(int i = 0; i < arr.length; i++){
- for(int j = 0; j < arr[i].length; j++){
- System.out.print(arr[i][j] + " ");
- }
- System.out.println("");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement