-
Notifications
You must be signed in to change notification settings - Fork 0
/
Model.java
102 lines (95 loc) · 2.76 KB
/
Model.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
import java.util.Random;
import java.util.LinkedList;
//0 = Tom rute || 1 = Skatt || 2 = Slange kropp || 3 = Slange hode
public class Model {
Random rand;
View view;
Rute[][] ruter;
Rute head;
int boardHeight;
int boardLength;
LinkedList<Rute> slange;
int score;
Model(int x, int y) {
boardHeight = x;
boardLength = y;
score = 0;
rand = new Random();
view = new View();
ruter = view.ruter;
head = ruter[boardHeight / 2][boardLength / 2];
head.bliHode();
for(int i = 0; i < amountOfTreasures(boardHeight, boardLength);i++){
placeTreasure(10, y);
}
slange = new LinkedList<Rute>();
slange.add(head);
Thread teller = new Thread(new TelleTrad(this));
teller.start();
}
void placeTreasure(int x, int y) {
int randx = rand.nextInt(x);
int randy = rand.nextInt(y);
// Skatt kan bare plasseres på tom plass.
if(ruter[randx][randy].isFree){
ruter[randx][randy].bliScore();
}
else{
placeTreasure(x, y);
}
}
int amountOfTreasures(int x, int y) {
// Generer hvor mange skatter skal være på skjermen
return Math.round((x / 4) + (y / 4));
}
//Movement of the snake
void moveSnake(){
//determine direction of movement
String direction = view.direction;
int loc_x = head.x;
int loc_y = head.y;
if(direction == "W"){
loc_y --;
}
else if(direction == "E"){
loc_y ++;
}
else if(direction == "N"){
loc_x --;
}
else{
loc_x++;
}
//Must check if the route it is moving into will end the game
if(!legalMove(loc_x, loc_y)){
System.exit(0);
}
//If game does not end, two possible operations:
//Snake moves, last element back to empty, head to body, empty to head
//Snake gets treasure, Head to body, empty to head
if(ruter[loc_x][loc_y].isScore == true){
score++;
head.bliSlange();
ruter[loc_x][loc_y].bliHode();
slange.add(ruter[loc_x][loc_y]);
head = ruter[loc_x][loc_y];
}
else{
head.bliSlange();
ruter[loc_x][loc_y].bliHode();
slange.add(ruter[loc_x][loc_y]);
head = ruter[loc_x][loc_y];
Rute gone = slange.remove();
gone.bliTom();
}
}
boolean legalMove(int x, int y){
if(x >= ruter.length || y >= ruter[0].length || x < 0 || y < 0){
return false;
}
if(ruter[x][y].isSnake){
return false;
}
return true;
}
}