forked from codelympics/go_game_jam
-
Notifications
You must be signed in to change notification settings - Fork 1
/
board.go
94 lines (83 loc) · 1.88 KB
/
board.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
package main
import (
tl "github.com/JoelOtter/termloop"
)
type Square struct {
boardX int
boardY int
problem *Problem
visible bool
canvas []tl.Cell
entity *tl.Entity
}
// Board contains all number pairs for the level
type Board [][]*Square
func NewSquare(boardX, boardY int, pr *Problem) *Square {
str := []rune(pr.String())
c := make([]tl.Cell, len(str))
for i := range c {
c[i] = tl.Cell{Ch: str[i]}
}
sq := Square{
boardX: boardX,
boardY: boardY,
problem: pr,
visible: true,
canvas: c,
entity: tl.NewEntity(1, 1, squareWidth, squareHeight),
}
sq.entity.SetPosition(sq.getPosition())
return &sq
}
func (sq *Square) getPosition() (int, int) {
x := offsetX + borderWidth + (sq.boardX * squareWidth) + sq.boardX*squareOffsetX + 1
y := offsetY + borderHeight + (sq.boardY * squareHeight) + sq.boardY*squareOffsetY + 1
return x, y
}
func (sq *Square) Tick(event tl.Event) {}
func (sq *Square) Draw(screen *tl.Screen) {
if sq.visible {
x, y := sq.getPosition()
for i := 0; i < 5; i++ {
screen.RenderCell(x+i, y, &sq.canvas[i])
}
}
}
func (sq *Square) Hit() bool {
if sq.problem.valid && sq.visible {
sq.visible = false
return true
}
return false
}
// NewBoard generates new level board
func NewBoard() *Board {
board := make(Board, boardHeight)
for i := range board {
board[i] = make([]*Square, boardWidth)
}
return &board
}
func (b *Board) isLevelComplete() bool {
board := *b
for y := range board {
for x := range board {
if board[x][y].problem.valid && board[x][y].visible {
return false
}
}
}
return true
}
func (b *Board) populateBoard(gameLevel, answersPerLevel int, level tl.Level) {
pl := NewProblemList(gameLevel, answersPerLevel, boardWidth*boardHeight)
i := 0
for y := 0; y < boardHeight; y++ {
for x := 0; x < boardWidth; x++ {
rc := NewSquare(x, y, pl[i])
(*b)[y][x] = rc
level.AddEntity(rc)
i++
}
}
}