-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathboard-ui.go
163 lines (143 loc) · 3.7 KB
/
board-ui.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
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
package main
import (
"fmt"
"github.com/morras/go-neutrino"
"github.com/nsf/termbox-go"
"strconv"
)
type Board struct {
moveChannel <-chan neutrino.Move
stateChannel <-chan neutrino.State
}
func (self *Board) listenForMoves() {
for move := range self.moveChannel {
self.move(move)
}
}
func (self *Board) listenForStateChanges() {
for state := range self.stateChannel {
switch state {
case neutrino.Player1NeutrinoMove:
writeMessage("Player ones turn to move neutrino")
break
case neutrino.Player1Move:
writeMessage("Player ones turn to move one of their own pieces")
break
case neutrino.Player2NeutrinoMove:
writeMessage("Player twos turn to move neutrino")
break
case neutrino.Player2Move:
writeMessage("Player twos turn to move one of their own pieces")
break
case neutrino.Player1Win:
writeMessage("Player one wins!")
break
case neutrino.Player2Win:
writeMessage("Player two wins!")
break
default:
writeMessage("Invalid state")
}
}
}
var lastMessageLength = 0
func writeMessage(message string) {
for i := 0; i < lastMessageLength; i++ {
termbox.SetCell(1+i, 8, ' ', 0, 0)
}
for i, r := range message {
termbox.SetCell(1+i, 8, r, 0, 0)
}
lastMessageLength = len(message)
termbox.Flush()
}
const (
player1Square = termbox.ColorYellow
player2Square = termbox.ColorGreen
blankSquare = ' '
neutrinoSquare = termbox.ColorWhite
bg = termbox.ColorBlack
fg = termbox.ColorWhite
)
func NewBoard(moveCh <-chan neutrino.Move, stateCh <-chan neutrino.State) *Board {
board := &Board{
moveChannel: moveCh,
stateChannel: stateCh,
}
board.initializeBoard()
board.drawBoard()
go board.listenForMoves()
go board.listenForStateChanges()
return board
}
func (self *Board) move(move neutrino.Move) error {
//the board that is drawn is 1-indexed instead of 0-indexed
move.FromX++
move.ToX++
move.FromY++
move.ToY++
if move.FromX < 1 || move.FromX > 5 || move.FromY < 1 || move.FromY > 5 {
return fmt.Errorf("Old coordinates must be between 1 and 5 inclusive, (%d, %d)", move.FromX, move.FromY)
}
if move.ToX < 1 || move.ToX > 5 || move.ToY < 1 || move.ToY > 5 {
return fmt.Errorf("New coordinates must be between 1 and 5 inclusive, (%d, %d)", move.ToX, move.ToY)
}
buffer := termbox.CellBuffer()
bufferWidth, _ := termbox.Size()
oldCell := buffer[bufferWidth*int(move.FromY)+int(move.FromX)]
oldCh := oldCell.Ch
oldFG := oldCell.Fg
oldBG := oldCell.Bg
termbox.SetCell(int(move.ToX), int(move.ToY), oldCh, oldFG, oldBG)
termbox.SetCell(int(move.FromX), int(move.FromY), ' ', fg, bg)
termbox.Flush()
return nil
}
func (self *Board) initializeBoard() {
err := termbox.Init()
if err != nil {
panic(err)
}
}
func (self *Board) CloseBoard() {
defer termbox.Close()
}
func (self *Board) drawBoard() {
for i := 0; i <= 6; i++ {
for j := 0; j <= 6; j++ {
if (i == 0 || i == 6) && j > 0 && j < 6 {
termbox.SetCell(i, j, rune(strconv.Itoa(j)[0]), fg, bg)
} else if (j == 0 || j == 6) && i > 0 && i < 6 {
r, err := getRuneFromIndex(i)
if err != nil {
panic(err)
}
termbox.SetCell(i, j, r, fg, bg)
} else if j == 1 {
termbox.SetCell(i, j, ' ', termbox.ColorWhite, player1Square)
} else if j == 5 {
termbox.SetCell(i, j, ' ', termbox.ColorWhite, player2Square)
} else if j == 3 && i == 3 {
termbox.SetCell(i, j, ' ', termbox.ColorWhite, neutrinoSquare)
} else {
termbox.SetCell(i, j, ' ', bg, bg)
}
}
}
termbox.Flush()
}
func getRuneFromIndex(j int) (rune, error) {
switch j {
case 1:
return 'A', nil
case 2:
return 'B', nil
case 3:
return 'C', nil
case 4:
return 'D', nil
case 5:
return 'E', nil
}
return 'Q', fmt.Errorf("input is not between 1 and 5,%d", j)
}