Skip to content

Commit 6c3c21b

Browse files
author
Bastian Isensee
committed
Add deadly random spawning enemies
1 parent 0a6b675 commit 6c3c21b

File tree

1 file changed

+44
-14
lines changed

1 file changed

+44
-14
lines changed

Diff for: main.go

+44-14
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@ const (
1919
lineVertical = '|'
2020
lineHorizontal = '-'
2121
empty = ' '
22-
player = 'o'
22+
player = '0'
2323
item = '$'
24+
enemy = 'X'
2425
)
2526

2627
// model encapsulates our data for displaying and updating.
@@ -30,49 +31,68 @@ type model struct {
3031
playerRow int
3132
playerCol int
3233

33-
itemRow int
34-
itemCol int
34+
gameOver bool
3535
}
3636

3737
func (m *model) spawnItem() {
38+
row, col := m.randomFreeCoordinates()
39+
m.table[row][col] = item
40+
}
41+
42+
func (m *model) spawnEnemy() {
43+
row, col := m.randomFreeCoordinates()
44+
m.table[row][col] = enemy
45+
}
46+
47+
func (m *model) randomFreeCoordinates() (row, col int) {
3848
// Generate some random coordinates.
39-
row, col := randomCoordinates()
49+
row, col = randomCoordinates()
4050

4151
// Check that the random cell is empty.
4252
// If not repeat randomizing until we find an empty cell.
4353
for m.table[row][col] != 0 {
4454
row, col = randomCoordinates()
4555
}
4656

47-
m.table[row][col] = item
48-
m.itemRow = row
49-
m.itemCol = col
57+
return
5058
}
5159

5260
// randomCoordinates returns a new set of random coordinates within the
5361
// playing field excluding borders. However, it is not guaranteed that the
5462
// cell under the returned coordinates is actually empty.
55-
func randomCoordinates() (row int, col int) {
63+
func randomCoordinates() (row, col int) {
5664
row = rand.Intn(tableHeight-2) + 1
5765
col = rand.Intn(tableWidth-2) + 1
5866

5967
return
6068
}
6169

6270
func (m *model) movePlayer(row, col int) {
71+
if m.gameOver {
72+
return
73+
}
74+
6375
// Clear old player location.
6476
m.table[m.playerRow][m.playerCol] = 0
6577

78+
if m.table[row][col] == enemy {
79+
// We ran into an enemy. Signal game over and skip further
80+
// processing.
81+
m.gameOver = true
82+
return
83+
}
84+
85+
if m.table[row][col] == item {
86+
// We collected an item. A new item and enemy needs to be
87+
// spawned.
88+
m.spawnItem()
89+
m.spawnEnemy()
90+
}
91+
6692
// Set new player location.
6793
m.table[row][col] = player
6894
m.playerRow = row
6995
m.playerCol = col
70-
71-
// Check if we are at the position of an item. Then we need to
72-
// spawn a new item.
73-
if m.itemRow == row && m.itemCol == col {
74-
m.spawnItem()
75-
}
7696
}
7797

7898
func (m *model) playerUp() {
@@ -160,6 +180,11 @@ func (m *model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
160180

161181
case tea.KeyMsg:
162182

183+
// If our game is lost, any key shall terminate the program.
184+
if m.gameOver {
185+
return m, tea.Quit
186+
}
187+
163188
switch msg.String() {
164189

165190
// Exit program on ctrl+c or q typing.
@@ -182,6 +207,11 @@ func (m *model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
182207
// View is required for building what we want to show on the screen.
183208
// That means we need to translate our model data into a string for displaying.
184209
func (m *model) View() string {
210+
if m.gameOver {
211+
// Just inform about game over and don't continue.
212+
return "Player died, Game Over!"
213+
}
214+
185215
builder := strings.Builder{}
186216

187217
// Iterate our table (2d array) and print non-empty fields as set in

0 commit comments

Comments
 (0)