-
Notifications
You must be signed in to change notification settings - Fork 0
/
board.go
83 lines (70 loc) · 2.23 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
package main
import (
"fmt"
"github.com/gdamore/tcell/v2"
"github.com/mattn/go-runewidth"
)
type Board struct {
h, w int
offsetx, offsety int
}
func NewBoard(h, w, offsety, offsetx int) *Board {
return &Board{
h,
w,
offsetx,
offsety,
}
}
func emitStr(s tcell.Screen, x, y int, style tcell.Style, str string) {
for _, c := range str {
var comb []rune
w := runewidth.RuneWidth(c)
if w == 0 {
comb = []rune{c}
c = ' '
w = 1
}
s.SetContent(x, y, c, comb, style)
x += w
}
}
func (b *Board) displayScreenToSmall(s tcell.Screen, currentW, currentH int) {
style := tcell.StyleDefault.Foreground(tcell.ColorIndianRed.TrueColor()).Background(tcell.Color(tcell.ColorBlack.TrueColor()))
displayText := fmt.Sprintf("Current Screen size too small, requird size: %dw x %dh", b.w, b.h)
currentSize := fmt.Sprintf("current Size: %dw x %dh", currentW, currentH)
emitStr(s, (currentW-len(displayText))/2, currentH/2, style, displayText)
emitStr(s, (currentW-len(currentSize))/2, currentH/2+1, tcell.StyleDefault, currentSize)
}
func (b *Board) displayScore(s tcell.Screen, score int) {
scoreText := fmt.Sprintf("Score: %d", score)
emitStr(s, (b.w - len(scoreText) - 2), 0, tcell.StyleDefault, scoreText)
}
func (b *Board) displayBoard(s tcell.Screen) {
style := tcell.StyleDefault.Foreground(tcell.ColorWhite.TrueColor()).Background(tcell.ColorReset)
// x == w, y == h
s.SetContent(0+b.offsetx, 0+b.offsety, tcell.RuneULCorner, nil, style)
s.SetContent(0+b.offsetx, b.h+b.offsety, tcell.RuneLLCorner, nil, style)
s.SetContent(b.w+b.offsetx, 0+b.offsety, tcell.RuneURCorner, nil, style)
s.SetContent(b.w+b.offsetx, b.h+b.offsety, tcell.RuneLRCorner, nil, style)
for x := 0; x < b.w; x++ {
for y := 0; y < b.h; y++ {
if y == 0 && x != 0 {
s.SetContent(x+b.offsetx, y+b.offsety, tcell.RuneHLine, nil, style)
}
if x == 0 && y != 0 {
s.SetContent(x+b.offsetx, y+b.offsety, tcell.RuneVLine, nil, style)
}
}
}
for x := 0; x <= b.w; x++ {
for y := 0; y <= b.h; y++ {
if x != b.w && x != 0 && y == b.h {
s.SetContent(x+b.offsetx, y+b.offsety, tcell.RuneHLine, nil, style)
}
if y != b.h && y != 0 && x == b.w {
s.SetContent(x+b.offsetx, y+b.offsety, tcell.RuneVLine, nil, style)
}
}
}
}