-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrendering.go
122 lines (102 loc) · 2.91 KB
/
rendering.go
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
/*
* Minesweeper API
* Copyright (C) 2017 Ritchie Borja
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
package minesweeper
import (
"fmt"
"strings"
"github.com/rrborja/minesweeper/rendering"
"github.com/rrborja/minesweeper/visited"
)
type recordedActions struct {
*visited.History
}
func (game *game) BombLocations() []rendering.Position {
bombPlacements := make([]rendering.Position, int(float32(game.Height*game.Width)*game.difficultyMultiplier))
var counter int
game.iterateBlocks(func(block *Block) bool {
if block.Node == Bomb {
bombPlacements[counter] = *block
counter++
}
return true
})
return bombPlacements
}
// Not recommended to call this function until a new update to improve the performance of this method
func (game *game) HintLocations() []rendering.Position {
hintPlacements := make([]rendering.Position, 0) // TODO: Improve this performance
game.iterateBlocks(func(block *Block) bool {
if block.Node == Number {
hintPlacements = append(hintPlacements, *block)
}
return true
})
return hintPlacements
}
func (game *game) History() *visited.History {
return game.recordedActions.History
}
func (game *game) LastAction() visited.Record {
return game.recordedActions.History.Record
}
func (game *game) Print() {
bombs := game.BombLocations()
hints := game.HintLocations()
star := '*'
var board = make([][]*rune, game.Width)
for i := range board {
board[i] = make([]*rune, game.Height)
}
for _, bomb := range bombs {
x := bomb.X()
y := bomb.Y()
board[x][y] = &star
}
for _, hint := range hints {
x := hint.X()
y := hint.Y()
value := rune(hint.(Block).Value + 48)
board[x][y] = &value
}
var boardLayout = make([]string, game.Width)
for i, row := range board {
cellLayout := make([]rune, (game.Height * 2))
for j, cell := range row {
switch cell {
case nil:
cellLayout[j*2] = '.'
default:
cellLayout[j*2] = *cell
}
cellLayout[j*2+1] = ' '
}
boardLayout[i] = string(cellLayout)
}
fmt.Println(strings.Join(boardLayout, "\n"))
}
func (game *recordedActions) add(record visited.Record) {
if game.History == nil {
game.History = new(visited.History)
} else {
temp := game.History
game.History = new(visited.History)
game.History.History = temp
}
game.Record = record
}