-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMole.scala
204 lines (189 loc) · 5.81 KB
/
Mole.scala
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
package moleTerritory
import java.awt.{Color as JColor}
class Mole(
val name: String,
var dir: (Int, Int),
val color: java.awt.Color,
val areaColor: java.awt.Color,
val keyControl: KeyControl,
val game: Game
) {
var area = Array.empty[Pos]
var kills = 0
var suicide = 0
var deaths = 0
var lastDir = (0,0)
var pos = (-5, -5)
var currentPath = Array.empty[Pos]
var currentPathColor = Array.empty[JColor]
var prevColor = java.awt.Color.white
var eliminated = false
override def toString: String = {
s"Mole[name=$name, pos=$pos, dir=$dir, points=$area]"
}
def addArea(poses: Array[Pos]): Unit = {
area ++= poses;
game.moleWillClaimPos(poses, this)
}
def removeArea(pos: Pos): Unit = {
area = area.filter(_!=pos)
}
def killed(mole: Mole): Unit = {
if (mole != this) {
kills += 1
} else {
suicide += 1
}
mole.deaths += 1
if (GameProperties.lives-mole.deaths <= 0) {
mole.eliminated = true
}
}
def die(window: BlockWindow, otherMoles: Array[Mole]): Unit = {
for (pos <- area) {
window.setBlock(pos)(JColor.white)
}
for (otherMole <- otherMoles) {
for (otherPathPos <- otherMole.currentPath) {
if (area.contains(otherPathPos)) {
val index = otherMole.currentPath.indexOf(otherPathPos)
otherMole.currentPathColor(index) = JColor.white
window.setBlock(otherPathPos)(combineColors(otherMole.areaColor, JColor.white))
}
}
}
area = Array.empty[Pos]
for ((pos, color) <- currentPath.zip(currentPathColor)) {
//val colorResult = window.getBlock(pos)
//window.setBlock(pos)(getcolorFactor(areaColor, colorResult))
window.setBlock(pos)(color)
}
currentPath = Array.empty[Pos]
currentPathColor = Array.empty[JColor]
//CODE123window.setBlock(pos)(prevColor)
lastDir = (0, 0)
dir = (0, 0)
spawn(window)
}
/** Om keyControl.has(key) så uppdateras riktningen dir enligt keyControl */
def setDir(key: String): Unit = {
if (keyControl.has(key) || key == "FORCE") {
val tempDir = keyControl.direction(key);
if (tempDir != getReverseDir() && tempDir != makeReverseDir(lastDir)) {
dir = tempDir
if (tempDir != (0, 0)) {
lastDir = tempDir
}
}
}
}
/** Uppdaterar dir till motsatta riktningen. */
def reverseDir(): Unit = {
dir = getReverseDir()
}
def getReverseDir(): (Int, Int) = {
(dir._1 * -1, dir._2 * -1)
}
def makeReverseDir(dirToReverse: (Int, Int)): (Int, Int) = {
(dirToReverse._1 * -1, dirToReverse._2 * -1)
}
/** Uppdaterar pos så att den blir nextPos */
def move(): Unit = {
pos = nextPos;
if (prevColor != areaColor) {
if (!currentPath.contains(pos) && !area.contains(pos)) {
currentPath :+= pos;
currentPathColor :+= prevColor;
//println("added: "+pos)
}
}
}
/** Ger nästa position enligt riktningen dir utan att uppdatera pos */
def nextPos: Pos = {
(pos._1 + dir._1, pos._2 + dir._2);
}
// checks if mole is out of bounds
def isMoleOutOfBounds(): Unit = {
import GameProperties.*
if (isPosOutOfPlayingField(this.nextPos)) {
setDir("FORCE");
//reverseDir();
}
}
def getNewRandomPos(): Pos = {
import scala.util.Random.nextInt
import GameProperties.windowSize.*
val xPos = nextInt(width-2)+1
val yPos = nextInt(height-2)+1;
(xPos, yPos)
}
def spawn(window: BlockWindow): Unit = {
if (!eliminated) {
area = Array.empty[Pos]
/*while
pos = getNewRandomPos()
arePosCloseToTerritory(window, pos, 3)
do()*/
// finds all the possible poses
val notPossiblePoses = game.moles
.filter(_ != this)
.foldLeft(Set.empty[Pos]) { (accumulatedPoses, otherMole) =>
accumulatedPoses ++ otherMole.area
}
import GameProperties.windowSize.*
val possiblePoses = (padLef + 1 to padLef + width - 2).flatMap { xPos =>
(padTop + 1 to padTop + height - 2).filter { yPos =>
!notPossiblePoses.contains(xPos, yPos) && !arePosCloseToAnyTerritory((xPos, yPos))(notPossiblePoses, 3)
}.map { yPos =>
(xPos, yPos)
}
}.toList
if (possiblePoses.length == 0) { // no spots left to be spawned
// cannot respawn or perma dead
eliminated = true
} else {
import scala.util.Random.nextInt
pos = possiblePoses(nextInt(possiblePoses.length))
for (xDiff <- -1 to 1) {
for (yDiff <- -1 to 1) {
window.setBlock(pos._1 + xDiff, pos._2 + yDiff)(areaColor)
area :+= (pos._1 + xDiff, pos._2 + yDiff)
}
}
}
}
}
}
def arePosClose(pos1: Pos, pos2: Pos, distance: Int): Boolean = {
val xDiff = (pos2._1 - pos1._1).abs
val yDiff = (pos2._2 - pos1._2).abs
(distance >= xDiff && distance >= yDiff)
}
def arePosCloseToAnyTerritory(pos: Pos)(area: Set[Pos], distance: Int): Boolean = {
val (x, y) = pos
val windowSize = GameProperties.windowSize
var loopQuit = false
for (xDiff <- -distance to distance) if (!loopQuit) {
for (yDiff <- -distance to distance) if (!loopQuit) {
val newX = x + xDiff
val newY = y + yDiff
if (!windowSize.isPosOutOfBounds(newX, newY) && area.contains((newX, newY))) {
loopQuit = true
}
}
}
loopQuit
}
def arePosCloseToTerritory(window: BlockWindow, pos: Pos, distance: Int): Boolean = {
var suroundingColors = Array.empty[JColor]
for (xDiff <- -distance to distance) {
for (yDiff <- -distance to distance) {
if (!window.windowSize.isPosOutOfBounds(pos._1 + xDiff, pos._2 + yDiff)) {
suroundingColors :+= window.getBlock(pos._1 + xDiff, pos._2 + yDiff)
} else {
suroundingColors :+= JColor.black
}
}
}
suroundingColors.map(_ == JColor.white).contains(false)
}