-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMap.java
162 lines (139 loc) · 4.21 KB
/
Map.java
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/*
*
* This class will holde all the information of the map and will have approperiate
* methods to perform diffrent tasks on map.
*/
package bomborman;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.net.URI;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.util.Random;
import javax.imageio.ImageIO;
import bomborman.Types.BlockType;
public class Map {
static Map mapinst;
public int rows, cols;
public MapBasicBlock[][] map;
public Player player;
public Player player2;
MoveEvaluator move;
MoveExecutor exec;
public int antime;
public String p1;
public String p2;
public String[][] im;
public int[][] c;
public int[][] r;
public Scrub scr;
private Map(int _rows, int _cols){
rows = _rows;
cols = _cols;
map = new MapBasicBlock[rows][cols];
antime = 12;
im = new String[rows][cols];
c = new int[rows][cols];
r = new int[rows][cols];
try{
Registry register = LocateRegistry.getRegistry();
scr = (Scrub)register.lookup("scrub");
}catch(Exception e){
e.printStackTrace();
}
}
boolean isFree(Position pos){
if(map[pos.getRow()][pos.getColumn()] == null){
return true;
}else{
return false;
}
}
public static Map getInstance(int i, int j){
if (mapinst == null){
mapinst = new Map(i,j);
}
return mapinst;
}
public void generateMap(){
move = new MoveEvaluator();
exec = new MoveExecutor();
Random rand = new Random();
BufferedImage wlimage = null;
player = new Player(BlockType.PLAYER,new Position(1,1),1);
player2 = new Player(BlockType.PLAYER,new Position(13,13),2);
// try {
// URI wlurl = getClass().getResource("ublock.png").toURI();
// wlimage = ImageIO.read(new File(wlurl));
// } catch (Exception e) {
// System.out.println("Could not load file");
// }
for(int i=0;i<rows;i++){
for(int j=0;j<cols;j++){
// System.out.println("rows " + i + " cols " + j);
if(i==0 || j==0 || (i%2 == 0 && j%2 == 0) || (j==cols-1)||(i==rows-1)){
map[i][j] = new UnbreakableBlock(BlockType.UNBREKABLE,new Position(i,j));
}else if(i == 1 && j == 1){
}else if(i == 1 && j == 1){
}else if((i==2&&j==1)||(i==1&&j==2)){
continue;
}else if((i==14&&j==13)||(i==13&&j==14)||(i==12&&j==13)||(i==13&&j==12)||(i==13&&j==13)){
continue;
}else if(rand.nextInt(15)<7){
map[i][j] = new BreakableBlock(BlockType.BREAKABLE,new Position(i,j));
}else if((rand.nextInt(25)<2)&& (i>2) &&(j>2)){
map[i][j] = new Enemy(BlockType.ENEMY,new Position(i,j));
map[i-1][j] = null;
map[i][j-1] = null;
}
}
}
}
public void move(KeyEvent e,int i){
if(e==null){
return;
}
if(e.getKeyCode()==KeyEvent.VK_LEFT){
if(move.isValidMove(player,Types.Move.LEFT) && i==1){
exec.executeMove(player,Types.Move.LEFT);
}
if(move.isValidMove(player2,Types.Move.LEFT) && i==2){
exec.executeMove(player2,Types.Move.LEFT);
}
}else if(e.getKeyCode()==KeyEvent.VK_RIGHT){
if(move.isValidMove(player,Types.Move.RIGHT) && i==1){
exec.executeMove(player,Types.Move.RIGHT);
}
if(move.isValidMove(player2,Types.Move.RIGHT) && i==2){
exec.executeMove(player2,Types.Move.RIGHT);
}
}else if(e.getKeyCode()==KeyEvent.VK_UP){
if(move.isValidMove(player,Types.Move.UP) && i==1){
exec.executeMove(player,Types.Move.UP);
}
if(move.isValidMove(player2,Types.Move.UP) && i==2){
exec.executeMove(player2,Types.Move.UP);
}
}else if(e.getKeyCode()==KeyEvent.VK_DOWN){
if(move.isValidMove(player,Types.Move.DOWN) && i==1){
exec.executeMove(player,Types.Move.DOWN);
}
if(move.isValidMove(player2,Types.Move.DOWN) && i==2){
exec.executeMove(player2,Types.Move.DOWN);
}
}else if(e.getKeyCode()==KeyEvent.VK_SPACE){
if(move.isValidMove(player,Types.Move.PLACE_BOMB) && i==1){
exec.executeMove(player,Types.Move.PLACE_BOMB);
}
if(move.isValidMove(player2,Types.Move.PLACE_BOMB) && i==2){
exec.executeMove(player2,Types.Move.PLACE_BOMB);
}
}else{
return;
}
}
public int getTime(){
return 10;
}
}