-
Notifications
You must be signed in to change notification settings - Fork 0
/
Grid.java
163 lines (146 loc) · 5.6 KB
/
Grid.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
163
package com.company;
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;
public class Grid extends ArrayList<ArrayList<Cell>> {
static Integer linii, coloane;
static ArrayList<ArrayList<Cell>> map = new ArrayList<>();
static Character curent; //Referinta la caracterul curent
static Cell curent_cell; //Celula curenta
private Grid() {
}
public static void setMap(ArrayList<ArrayList<Cell>> newMap) {
map = newMap;
curent_cell = map.get(0).get(0);
curent_cell.vizitat = true;
}
public static void setCurent(Character newCurent) {
curent = newCurent;
}
public static void setColoane(Integer newColoane) {
coloane = newColoane;
}
public static void setLinii(Integer newLinii) {
linii = newLinii;
}
public static Integer getLinii() {
return linii;
}
public static Integer getColoane() {
return coloane;
}
public static ArrayList<ArrayList<Cell>> generare(Integer lungime, Integer coloane) {
int i, j;
for (i = 0; i < lungime; i++) {
ArrayList<Cell> rand = new ArrayList<>();
for (j = 0; j < coloane; j++) {
Cell nou = new Cell(i, j, CellEnum.EMPTY, false);
rand.add(nou);
}
map.add(rand);
}
System.out.println("Introduceti indicele corespunzator modului generare a hartii.");
System.out.println("1 - Harta de testare");
System.out.println("2 - Harta generata random");
Scanner input = new Scanner(System.in);
int rasp;
rasp = input.nextInt();
if (rasp == 1) {
// Harta va fi de testare
map.get(0).get(3).setTip_element(CellEnum.SHOP);
map.get(1).get(3).setTip_element(CellEnum.SHOP);
map.get(2).get(0).setTip_element(CellEnum.SHOP);
map.get(3).get(4).setTip_element(CellEnum.ENEMY);
map.get(4).get(4).setTip_element(CellEnum.FINISH);
} else {
// Harta va fi generata random
Random rn = new Random();
int contor = 0, lin, col;
Cell curent;
// Plasam in 4 pozitii random inamici
while (contor < 4) {
lin = rn.nextInt(Grid.getLinii());
col = rn.nextInt(Grid.getColoane());
curent = map.get(lin).get(col);
if (curent.tip_element.equals(CellEnum.EMPTY) && !(lin == 0 && col == 0)) {
map.get(lin).get(col).setTip_element(CellEnum.ENEMY);
contor++;
}
}
contor = 0;
// Plasam in 2 pozitii random shopurile
while (contor < 2) {
lin = rn.nextInt(Grid.getLinii());
col = rn.nextInt(Grid.getColoane());
curent = map.get(lin).get(col);
if (curent.tip_element.equals(CellEnum.EMPTY) && !(lin == 0 && col == 0)) {
map.get(lin).get(col).setTip_element(CellEnum.SHOP);
contor++;
}
}
// Punem pozitia de final random
map.get(Grid.getLinii() - 1).get(Grid.getColoane() - 1).setTip_element(CellEnum.FINISH);
}
return map;
}
static void giveCoins() {
if (!curent_cell.vizitat && curent_cell.tip_element == CellEnum.EMPTY) {
// Alegem un nr random intre 1 si 5
// Sansa de 20% == sansa de a fi nr. 3
Random rn = new Random();
int nr = rn.nextInt(5) + 1;
if (nr == 3) {
// Se pot gasi intre 30 si 60 de monede
int c = rn.nextInt(31) + 30;
curent.inventar.addCoins(c);
System.out.println("Ai primit " + c + ". Noua balanta: " + curent.inventar.coins);
}
}
}
static void goNorth() {
// curent_cell.Ox - deplasare sus jos
if (curent_cell.Ox == 0) {
System.out.println("Nu se poate merge mai sus.");
} else {
curent_cell = map.get(curent_cell.Ox - 1).get(curent_cell.Oy);
Game.getStory(curent_cell);
Game.getOptions(curent_cell);
giveCoins();
curent_cell.vizitat = true;
}
}
static void goSouth() {
if (curent_cell.Ox == Grid.getLinii() - 1) {
System.out.println("Nu se poate merge mai jos.");
} else {
curent_cell = map.get(curent_cell.Ox + 1).get(curent_cell.Oy);
Game.getStory(curent_cell);
Game.getOptions(curent_cell);
giveCoins();
curent_cell.vizitat = true;
}
}
static void goWest() {
// Deplasare stanga - dreapta
if (curent_cell.Oy == 0) {
System.out.println("Nu se poate merge la stanga");
} else {
curent_cell = map.get(curent_cell.Ox).get(curent_cell.Oy - 1);
Game.getStory(curent_cell);
Game.getOptions(curent_cell);
giveCoins();
curent_cell.vizitat = true;
}
}
static void goEast() {
if (curent_cell.Oy == Grid.getColoane() - 1) {
System.out.println("Nu se poate merge la dreapta");
} else {
curent_cell = map.get(curent_cell.Ox).get(curent_cell.Oy + 1);
Game.getStory(curent_cell);
Game.getOptions(curent_cell);
giveCoins();
curent_cell.vizitat = true;
}
}
}