-
Notifications
You must be signed in to change notification settings - Fork 0
/
brainvita_cli.go
251 lines (212 loc) · 6.01 KB
/
brainvita_cli.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
package main
import (
"fmt"
"github.com/gdamore/tcell/v2"
"github.com/rivo/tview"
)
type Player struct {
name string
score int
}
type Cell struct {
row int
col int
}
type Game struct {
player Player
board *tview.Table
isValidMove bool
}
func Abs(x int) int {
if x < 0 {
return -x
}
return x
}
func (source Cell) isValidMove(dest Cell, game Game) bool {
// dest should not be one of the dead cells
if game.board.GetCell(dest.row, dest.col).Text == " " {
return false
}
if game.board.GetCell(dest.row, dest.col).Text == "0" {
// check if 2 cells are vertically aligned
if source.col == dest.col {
if Abs(source.row-dest.row) == 2 {
return true
} else {
return false
}
}
// check if 2 cells are horizontally aligned
if source.row == dest.row {
if Abs(source.col-dest.col) == 2 {
return true
} else {
return false
}
}
// check if 2 cells are diagnol
if source.row != dest.row && source.col != dest.col {
// Corner cases for cell jumping along diagnols containing -1 in b/w
if source.row > dest.row {
if game.board.GetCell(source.row-1, dest.col-1).Text == " " {
return false
}
} else {
if game.board.GetCell(source.row+1, dest.col+1).Text == " " {
return false
}
}
// for diagnol jumping the abs diff should be 2
if Abs(source.row-dest.row) == 2 && Abs(source.col-dest.col) == 2 {
return true
} else {
return false
}
}
} else {
return false
}
return true
}
func (source Cell) move(dest Cell, game *Game) {
if game.isValidMove {
// Peg is removed so mark it as 0
game.board.GetCell(source.row, source.col).Text = "0"
game.board.GetCell(dest.row, dest.col).Text = "1"
// Mark the middle cell as 0
// vertically aligned
if source.col == dest.col {
if source.row > dest.row {
game.board.GetCell(source.row-1, source.col).Text = "0"
}
if source.row < dest.row {
game.board.GetCell(source.row+1, source.col).Text = "0"
}
}
// horizontally aligned
if source.row == dest.row {
if source.col > dest.col {
game.board.GetCell(source.row, source.col-1).Text = "0"
}
if source.col < dest.col {
game.board.GetCell(source.row, source.col+1).Text = "0"
}
}
// check if 2 cells are diagnol
if source.row > dest.row && source.col > dest.col {
game.board.GetCell(source.row-1, source.col-1).Text = "0"
}
if source.row < dest.row && source.col < dest.col {
game.board.GetCell(source.row+1, source.col+1).Text = "0"
}
}
}
func initialize_game(player Player, table *tview.Table) *Game {
cols, rows := 7, 7
for r := 0; r < rows; r++ {
for c := 0; c < cols; c++ {
color := tcell.ColorWhite
if r == 3 && c == 3 {
table.SetCell(r, c,
tview.NewTableCell("0").
SetTextColor(color).
SetAlign(tview.AlignCenter))
} else if (r == 0 || r == 1) && (c == 0 || c == 1) {
table.SetCell(r, c,
tview.NewTableCell(" ").
SetTextColor(color).
SetAlign(tview.AlignCenter))
} else if (r == 5 || r == 6) && (c == 0 || c == 1 || c == 5 || c == 6) {
table.SetCell(r, c,
tview.NewTableCell(" ").
SetTextColor(color).
SetAlign(tview.AlignCenter))
} else if (r == 0 || r == 1) && (c == 5 || c == 6) {
table.SetCell(r, c,
tview.NewTableCell(" ").
SetTextColor(color).
SetAlign(tview.AlignCenter))
} else {
table.SetCell(r, c,
tview.NewTableCell("1").
SetTextColor(color).
SetAlign(tview.AlignCenter))
}
}
}
return &Game{player: player, board: table}
}
func (game *Game) play(app *tview.Application, cells []Cell) {
game.board.Select(0, 0).SetFixed(1, 1).SetDoneFunc(func(key tcell.Key) {
if key == tcell.KeyEscape {
modal := tview.NewModal().
SetText("Do you want to quit the application?").
AddButtons([]string{"Quit", "Cancel"}).
SetDoneFunc(func(buttonIndex int, buttonLabel string) {
if buttonLabel == "Quit" {
app.Stop()
}
})
if err := app.SetRoot(modal, true).EnableMouse(true).Run(); err != nil {
panic(err)
}
app.Stop()
}
if key == tcell.KeyEnter {
game.board.SetSelectable(true, true)
}
}).SetSelectedFunc(func(row int, column int) {
selectedCell := Cell{row: row, col: column}
cells = append(cells, selectedCell)
game.board.GetCell(row, column).SetTextColor(tcell.ColorRed)
game.board.SetSelectable(false, false)
fmt.Println(cells)
if len(cells) == 2 {
source, dest := cells[0], cells[1]
isValid := source.isValidMove(dest, *game)
game.isValidMove = isValid
if isValid {
source.move(dest, game)
game.player.score += 1
} else {
fmt.Println(isValid)
}
cells = nil
game.board.GetCell(source.row, source.col).SetTextColor(tcell.ColorWhite)
game.board.GetCell(dest.row, dest.col).SetTextColor(tcell.ColorWhite)
}
})
}
func main() {
player := Player{name: "Rag"}
app := tview.NewApplication()
table := tview.NewTable().SetBorders(true)
newPrimitive := func(text string) tview.Primitive {
return tview.NewTextView().
SetTextAlign(tview.AlignCenter).
SetText(text)
}
menu := newPrimitive("Game Menu\n\n 1. Press Esc to Quit. \n\n 2. Press Enter to select the source grid, then press enter the dest grid to jump\n\n")
//main := newPrimitive("Main content")
sideBar := newPrimitive("Score")
grid := tview.NewGrid().
SetRows(3, 0, 3).
SetColumns(30, 0, 30).
SetBorders(true).
AddItem(newPrimitive("Brainvita\n\n Can leave with least amount of Pegs in Board????????"), 0, 0, 1, 3, 0, 0, false)
// Layout for screens narrower than 100 cells (menu and side bar are hidden).
grid.AddItem(menu, 0, 0, 0, 0, 0, 0, false).
AddItem(table, 1, 0, 1, 3, 0, 0, true).
AddItem(sideBar, 0, 0, 0, 0, 0, 0, false)
// Layout for screens wider than 100 cells.
grid.AddItem(menu, 1, 0, 1, 1, 0, 100, false).
AddItem(table, 1, 1, 1, 1, 0, 100, true).
AddItem(sideBar, 1, 2, 1, 1, 0, 100, false)
game := initialize_game(player, table)
cells := make([]Cell, 0, 2)
game.play(app, cells)
if err := app.SetRoot(grid, true).EnableMouse(true).Run(); err != nil {
panic(err)
}
}