Minor refactor

This commit is contained in:
Trevor Slocum 2021-09-07 19:53:07 -07:00
parent ee35198ae2
commit c6d589455c
1 changed files with 32 additions and 23 deletions

View File

@ -93,6 +93,8 @@ type Board struct {
func NewBoard(client *Client) *Board {
b := &Board{
client: client,
s: make([]string, 52),
v: make([]int, 50),
}
b.ResetMoves()
@ -116,10 +118,14 @@ func NewBoard(client *Client) *Board {
// TODO refactor
func (b *Board) GetStringState() []string {
b.Lock()
defer b.Unlock()
return b.s
}
func (b *Board) GetIntState() []int {
b.Lock()
defer b.Unlock()
return b.v
}
@ -178,7 +184,7 @@ func (b *Board) autoSendMoves() {
}
var to string
if b.premove[i][1] == b.playerHomeSpace() {
if b.premove[i][1] == b.PlayerHomeSpace() {
to = "off"
} else {
to = strconv.Itoa(b.premove[i][1])
@ -210,16 +216,19 @@ func (b *Board) SetState(state string) {
lastTurn = b.v[StateTurn]
}*/
b.s = strings.Split(state, ":")
s := strings.Split(state, ":")
copy(b.s, s)
b.v = make([]int, len(b.s)-2)
var err error
for i := 0; i < 46; i++ {
b.v[i], err = strconv.Atoi(b.s[i+2])
v := make([]int, 50)
for i := 0; i < 50; i++ {
v[i], err = strconv.Atoi(b.s[i+2])
if err != nil {
log.Fatal(err)
}
}
copy(b.v, v)
/*if b.v[StateTurn] != lastTurn {
if lastTurn == b.v[StatePlayerColor] {
@ -242,7 +251,7 @@ func (b *Board) SetState(state string) {
}
func (b *Board) Draw() {
// TODO
b.client.Event <- &EventDraw{}
}
func (b *Board) renderSpace(index int, spaceValue int) []byte {
@ -255,10 +264,10 @@ func (b *Board) renderSpace(index int, spaceValue int) []byte {
var pieceColor string
value := b.v[StateBoardSpace0+index]
if index == b.playerBarSpace() {
if index == b.PlayerBarSpace() {
value = b.v[StatePlayerBar]
pieceColor = playerColor
} else if index == 25-b.playerBarSpace() {
} else if index == 25-b.PlayerBarSpace() {
value = b.v[StateOpponentBar]
pieceColor = opponentColor
} else {
@ -343,7 +352,7 @@ func (b *Board) renderSpace(index int, spaceValue int) []byte {
}
}
// Highlight legal moves
highlightSpace := b.validMove(b.selected[0], index)
highlightSpace := b.ValidMove(b.selected[0], index)
highlightSpace = false // TODO Make configurable, disable by default
//+(b.v[StatePlayerDice1]*b.v[StatePlayerColor]) ||b.selected[0] == index+(b.v[StatePlayerDice2]*b.v[StatePlayerColor])) && b.selected[1] > 0
if b.selected[1] > 0 && highlightSpace && index != 25 && index != 0 {
@ -360,9 +369,9 @@ func (b *Board) renderSpace(index int, spaceValue int) []byte {
if b.selected[0] == index && b.selected[1] > 0 && spaceValue <= abs && spaceValue > abs-b.selected[1] {
r = []byte("*")
} else if b.Premovefrom[index] > 0 && spaceValue > (abs+b.Premoveto[index])-b.Premovefrom[index] && spaceValue <= abs+b.Premoveto[index] {
if index == 25-b.playerBarSpace() {
if index == 25-b.PlayerBarSpace() {
r = []byte("▾")
} else if index == b.playerBarSpace() {
} else if index == b.PlayerBarSpace() {
r = []byte("▴")
} else if rightArrowFrom {
r = []byte("▸")
@ -478,7 +487,7 @@ func (b *Board) GetValidMoves(from int) []int {
}
if b.allPlayerPiecesInHomeBoard() {
homeSpace := b.playerHomeSpace()
homeSpace := b.PlayerHomeSpace()
spacesHome := from - homeSpace
if spacesHome < 0 {
spacesHome *= -1
@ -509,23 +518,23 @@ CHECKSPACES:
return validMoves
}
func (b *Board) playerBarSpace() int {
return 25 - b.playerHomeSpace()
func (b *Board) PlayerBarSpace() int {
return 25 - b.PlayerHomeSpace()
}
func (b *Board) playerHomeSpace() int {
func (b *Board) PlayerHomeSpace() int {
if b.v[StateDirection] == -1 {
return 0
}
return 25
}
func (b *Board) validMove(f int, t int) bool {
func (b *Board) ValidMove(f int, t int) bool {
if b.v[StateTurn] != b.v[StatePlayerColor] || b.v[StatePlayerDice1] == 0 || b.v[StatePlayerDice2] == 0 {
return false
}
if t == b.playerHomeSpace() {
if t == b.PlayerHomeSpace() {
// TODO bear off logic, only allow high roll
return b.allPlayerPiecesInHomeBoard()
}
@ -544,14 +553,14 @@ func (b *Board) parseMoveString(player int, s string) int {
if err != nil {
space = SpaceUnknown
if s == "bar" {
barSpace := b.playerBarSpace()
barSpace := b.PlayerBarSpace()
if b.v[StatePlayerColor] == player {
space = barSpace
} else {
space = 25 - barSpace
}
} else if s == "off" {
space = b.playerHomeSpace()
space = b.PlayerHomeSpace()
}
}
return space
@ -601,9 +610,9 @@ func (b *Board) SimplifyMoves() {
func (b *Board) AddPreMove(from int, to int) bool {
// Allow bearing off when the player moves their own pieces on to the bar
if to == 0 || to == 25 {
to = b.playerHomeSpace()
to = b.PlayerHomeSpace()
}
if !b.validMove(from, to) {
if !b.ValidMove(from, to) {
return false
}
b.premove = append(b.premove, [2]int{from, to})
@ -652,9 +661,9 @@ func (b *Board) Render() []byte {
if j == -1 {
if i <= 4 {
return b.renderSpace(25-b.playerBarSpace(), spaceValue)
return b.renderSpace(25-b.PlayerBarSpace(), spaceValue)
}
return b.renderSpace(b.playerBarSpace(), spaceValue)
return b.renderSpace(b.PlayerBarSpace(), spaceValue)
}
var index int