-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMapUpdater.java
More file actions
50 lines (38 loc) · 1.02 KB
/
MapUpdater.java
File metadata and controls
50 lines (38 loc) · 1.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import java.util.*;
public class MapUpdater{
private static final int MAP_WIDTH = 10;
private static final int MAP_HEIGHT = 10;
private static char[][]map = new char[MAP_WIDTH][MAP_HEIGHT];
public static void main(String[]args) throws InterruptedException{
initializeMap();
while(true){
printMap();
updateMap();
Thread.sleep(4000);
}
}
public static void initializeMap(){
for(int i = 0; i < MAP_WIDTH; i++){
for(int j = 0; j < MAP_HEIGHT; j++){
map[i][j] = '·';
}
}
map[5][5] = 'X'; //Initial position
}
public static void updateMap(){
int x = (int)(Math.random() * MAP_WIDTH);
int y = (int)(Math.random() * MAP_HEIGHT);
map[x][y] = 'X';
}
public static void printMap(){
//Clear the screen
System.out.printf("\033[H \033[2J"); // Use of ANSI code. Works correctly in Linux, not in Windows
System.out.flush();
for(int i = 0; i < MAP_WIDTH; i++){
for(int j = 0; j < MAP_HEIGHT; j++){
System.out.print(map[i][j] + " ");
}
System.out.println();
}
}
}