Compare commits
No commits in common. "8347bf0cbc2ce359d1b85150e4447d8fef5448de" and "392cf19ae6a9766ca55484b69036796ce4929c8a" have entirely different histories.
8347bf0cbc
...
392cf19ae6
@ -16,14 +16,9 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type Board struct {
|
type Board struct {
|
||||||
tiles [8][8]*Piece
|
tiles [8][8]*Piece
|
||||||
turn Color
|
turn Color
|
||||||
Moves []string
|
Moves []string
|
||||||
moveIndicators []Tile
|
|
||||||
}
|
|
||||||
|
|
||||||
type Tile struct {
|
|
||||||
X, Y int
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewBoard() *Board {
|
func NewBoard() *Board {
|
||||||
@ -106,7 +101,7 @@ func (b *Board) Image() *image.RGBA {
|
|||||||
x := xi * 128
|
x := xi * 128
|
||||||
y := yi * 128
|
y := yi * 128
|
||||||
rect = image.Rect(x, y, x+128, y+128)
|
rect = image.Rect(x, y, x+128, y+128)
|
||||||
bg = image.NewUniform(getTileColor(b, xi, yi))
|
bg = image.NewUniform(getTileColor(xi, yi))
|
||||||
draw.Draw(img, rect, bg, p, draw.Src)
|
draw.Draw(img, rect, bg, p, draw.Src)
|
||||||
|
|
||||||
piece = b.tiles[xi][yi]
|
piece = b.tiles[xi][yi]
|
||||||
@ -191,7 +186,7 @@ func drawCoordinate(img *image.RGBA, x, y int, flipped bool) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
drawString := func(s string, origin fixed.Point26_6) {
|
drawString := func(s string, origin fixed.Point26_6) {
|
||||||
color := getTileColor(nil, x, y)
|
color := getTileColor(x, y)
|
||||||
if !flipped && color == Light {
|
if !flipped && color == Light {
|
||||||
color = Dark
|
color = Dark
|
||||||
} else if !flipped {
|
} else if !flipped {
|
||||||
@ -807,7 +802,6 @@ func (b *Board) movePawn(position string, fromX int, fromY int, promotion string
|
|||||||
if promotion != "" {
|
if promotion != "" {
|
||||||
return b.promotePawn(toX, toY, promotion)
|
return b.promotePawn(toX, toY, promotion)
|
||||||
}
|
}
|
||||||
b.moveIndicators = []Tile{{fromX, fromY}, {toX, toY}}
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -830,7 +824,6 @@ func (b *Board) movePawn(position string, fromX int, fromY int, promotion string
|
|||||||
if promotion != "" {
|
if promotion != "" {
|
||||||
return b.promotePawn(toX, toY, promotion)
|
return b.promotePawn(toX, toY, promotion)
|
||||||
}
|
}
|
||||||
b.moveIndicators = []Tile{{toX, yPrev}, {toX, toY}}
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -847,7 +840,6 @@ func (b *Board) movePawn(position string, fromX int, fromY int, promotion string
|
|||||||
if promotion != "" {
|
if promotion != "" {
|
||||||
return b.promotePawn(toX, toY, promotion)
|
return b.promotePawn(toX, toY, promotion)
|
||||||
}
|
}
|
||||||
b.moveIndicators = []Tile{{toX, yPrev}, {toX, toY}}
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -957,7 +949,6 @@ func (b *Board) moveRook(position string, queen bool, fromX int, fromY int) erro
|
|||||||
p = b.getPiece(xPrev, yPrev)
|
p = b.getPiece(xPrev, yPrev)
|
||||||
b.tiles[x][y] = p
|
b.tiles[x][y] = p
|
||||||
b.tiles[xPrev][yPrev] = nil
|
b.tiles[xPrev][yPrev] = nil
|
||||||
b.moveIndicators = []Tile{{xPrev, yPrev}, {x, y}}
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -988,7 +979,6 @@ func (b *Board) moveBishop(position string, queen bool) error {
|
|||||||
if ((!queen && piece.Name == Bishop) || (queen && piece.Name == Queen)) && piece.Color == b.turn {
|
if ((!queen && piece.Name == Bishop) || (queen && piece.Name == Queen)) && piece.Color == b.turn {
|
||||||
b.tiles[xPrev][yPrev] = nil
|
b.tiles[xPrev][yPrev] = nil
|
||||||
b.tiles[x][y] = piece
|
b.tiles[x][y] = piece
|
||||||
b.moveIndicators = []Tile{{xPrev, yPrev}, {x, y}}
|
|
||||||
return nil
|
return nil
|
||||||
} else {
|
} else {
|
||||||
// direction blocked by other piece
|
// direction blocked by other piece
|
||||||
@ -1007,7 +997,6 @@ func (b *Board) moveBishop(position string, queen bool) error {
|
|||||||
if ((!queen && piece.Name == Bishop) || (queen && piece.Name == Queen)) && piece.Color == b.turn {
|
if ((!queen && piece.Name == Bishop) || (queen && piece.Name == Queen)) && piece.Color == b.turn {
|
||||||
b.tiles[xPrev][yPrev] = nil
|
b.tiles[xPrev][yPrev] = nil
|
||||||
b.tiles[x][y] = piece
|
b.tiles[x][y] = piece
|
||||||
b.moveIndicators = []Tile{{xPrev, yPrev}, {x, y}}
|
|
||||||
return nil
|
return nil
|
||||||
} else {
|
} else {
|
||||||
break
|
break
|
||||||
@ -1025,7 +1014,6 @@ func (b *Board) moveBishop(position string, queen bool) error {
|
|||||||
if ((!queen && piece.Name == Bishop) || (queen && piece.Name == Queen)) && piece.Color == b.turn {
|
if ((!queen && piece.Name == Bishop) || (queen && piece.Name == Queen)) && piece.Color == b.turn {
|
||||||
b.tiles[xPrev][yPrev] = nil
|
b.tiles[xPrev][yPrev] = nil
|
||||||
b.tiles[x][y] = piece
|
b.tiles[x][y] = piece
|
||||||
b.moveIndicators = []Tile{{xPrev, yPrev}, {x, y}}
|
|
||||||
return nil
|
return nil
|
||||||
} else {
|
} else {
|
||||||
break
|
break
|
||||||
@ -1043,7 +1031,6 @@ func (b *Board) moveBishop(position string, queen bool) error {
|
|||||||
if ((!queen && piece.Name == Bishop) || (queen && piece.Name == Queen)) && piece.Color == b.turn {
|
if ((!queen && piece.Name == Bishop) || (queen && piece.Name == Queen)) && piece.Color == b.turn {
|
||||||
b.tiles[xPrev][yPrev] = nil
|
b.tiles[xPrev][yPrev] = nil
|
||||||
b.tiles[x][y] = piece
|
b.tiles[x][y] = piece
|
||||||
b.moveIndicators = []Tile{{xPrev, yPrev}, {x, y}}
|
|
||||||
return nil
|
return nil
|
||||||
} else {
|
} else {
|
||||||
break
|
break
|
||||||
@ -1137,7 +1124,6 @@ func (b *Board) moveKnight(position string, fromX int, fromY int) error {
|
|||||||
p = b.getPiece(xPrev, yPrev)
|
p = b.getPiece(xPrev, yPrev)
|
||||||
b.tiles[x][y] = p
|
b.tiles[x][y] = p
|
||||||
b.tiles[xPrev][yPrev] = nil
|
b.tiles[xPrev][yPrev] = nil
|
||||||
b.moveIndicators = []Tile{{xPrev, yPrev}, {x, y}}
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1209,8 +1195,6 @@ func (b *Board) moveKing(position string, castle bool) error {
|
|||||||
b.tiles[5][y] = rook
|
b.tiles[5][y] = rook
|
||||||
b.tiles[7][y] = nil
|
b.tiles[7][y] = nil
|
||||||
|
|
||||||
b.moveIndicators = []Tile{{4, y}, {x, y}, {5, y}, {7, y}}
|
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1244,8 +1228,6 @@ func (b *Board) moveKing(position string, castle bool) error {
|
|||||||
b.tiles[3][y] = rook
|
b.tiles[3][y] = rook
|
||||||
b.tiles[0][y] = nil
|
b.tiles[0][y] = nil
|
||||||
|
|
||||||
b.moveIndicators = []Tile{{2, y}, {4, y}, {3, y}, {0, y}}
|
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -1257,7 +1239,6 @@ func (b *Board) moveKing(position string, castle bool) error {
|
|||||||
if piece != nil && piece.Name == King && piece.Color == b.turn {
|
if piece != nil && piece.Name == King && piece.Color == b.turn {
|
||||||
b.tiles[xPrev][yPrev] = nil
|
b.tiles[xPrev][yPrev] = nil
|
||||||
b.tiles[x][y] = piece
|
b.tiles[x][y] = piece
|
||||||
b.moveIndicators = []Tile{{xPrev, yPrev}, {x, y}}
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1268,7 +1249,6 @@ func (b *Board) moveKing(position string, castle bool) error {
|
|||||||
if piece != nil && piece.Name == King && piece.Color == b.turn {
|
if piece != nil && piece.Name == King && piece.Color == b.turn {
|
||||||
b.tiles[xPrev][yPrev] = nil
|
b.tiles[xPrev][yPrev] = nil
|
||||||
b.tiles[x][y] = piece
|
b.tiles[x][y] = piece
|
||||||
b.moveIndicators = []Tile{{xPrev, yPrev}, {x, y}}
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1279,7 +1259,6 @@ func (b *Board) moveKing(position string, castle bool) error {
|
|||||||
if piece != nil && piece.Name == King && piece.Color == b.turn {
|
if piece != nil && piece.Name == King && piece.Color == b.turn {
|
||||||
b.tiles[xPrev][yPrev] = nil
|
b.tiles[xPrev][yPrev] = nil
|
||||||
b.tiles[x][y] = piece
|
b.tiles[x][y] = piece
|
||||||
b.moveIndicators = []Tile{{xPrev, yPrev}, {x, y}}
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1290,7 +1269,6 @@ func (b *Board) moveKing(position string, castle bool) error {
|
|||||||
if piece != nil && piece.Name == King && piece.Color == b.turn {
|
if piece != nil && piece.Name == King && piece.Color == b.turn {
|
||||||
b.tiles[xPrev][yPrev] = nil
|
b.tiles[xPrev][yPrev] = nil
|
||||||
b.tiles[x][y] = piece
|
b.tiles[x][y] = piece
|
||||||
b.moveIndicators = []Tile{{xPrev, yPrev}, {x, y}}
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1301,7 +1279,6 @@ func (b *Board) moveKing(position string, castle bool) error {
|
|||||||
if piece != nil && piece.Name == King && piece.Color == b.turn {
|
if piece != nil && piece.Name == King && piece.Color == b.turn {
|
||||||
b.tiles[xPrev][yPrev] = nil
|
b.tiles[xPrev][yPrev] = nil
|
||||||
b.tiles[x][y] = piece
|
b.tiles[x][y] = piece
|
||||||
b.moveIndicators = []Tile{{xPrev, yPrev}, {x, y}}
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1312,7 +1289,6 @@ func (b *Board) moveKing(position string, castle bool) error {
|
|||||||
if piece != nil && piece.Name == King && piece.Color == b.turn {
|
if piece != nil && piece.Name == King && piece.Color == b.turn {
|
||||||
b.tiles[xPrev][yPrev] = nil
|
b.tiles[xPrev][yPrev] = nil
|
||||||
b.tiles[x][y] = piece
|
b.tiles[x][y] = piece
|
||||||
b.moveIndicators = []Tile{{xPrev, yPrev}, {x, y}}
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1323,7 +1299,6 @@ func (b *Board) moveKing(position string, castle bool) error {
|
|||||||
if piece != nil && piece.Name == King && piece.Color == b.turn {
|
if piece != nil && piece.Name == King && piece.Color == b.turn {
|
||||||
b.tiles[xPrev][yPrev] = nil
|
b.tiles[xPrev][yPrev] = nil
|
||||||
b.tiles[x][y] = piece
|
b.tiles[x][y] = piece
|
||||||
b.moveIndicators = []Tile{{xPrev, yPrev}, {x, y}}
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1334,7 +1309,6 @@ func (b *Board) moveKing(position string, castle bool) error {
|
|||||||
if piece != nil && piece.Name == King && piece.Color == b.turn {
|
if piece != nil && piece.Name == King && piece.Color == b.turn {
|
||||||
b.tiles[xPrev][yPrev] = nil
|
b.tiles[xPrev][yPrev] = nil
|
||||||
b.tiles[x][y] = piece
|
b.tiles[x][y] = piece
|
||||||
b.moveIndicators = []Tile{{xPrev, yPrev}, {x, y}}
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1419,25 +1393,8 @@ func (b *Board) getCollision(position string) (*Piece, error) {
|
|||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func getTileColor(b *Board, x, y int) Color {
|
func getTileColor(x, y int) Color {
|
||||||
|
if x%2 == y%2 {
|
||||||
lightTile := x%2 == y%2
|
|
||||||
|
|
||||||
if b != nil {
|
|
||||||
// highlight move
|
|
||||||
// TODO: refactor using alpha channels
|
|
||||||
for _, t := range b.moveIndicators {
|
|
||||||
if t.X == x && t.Y == y {
|
|
||||||
if lightTile {
|
|
||||||
return LightGreen
|
|
||||||
} else {
|
|
||||||
return DarkGreen
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if lightTile {
|
|
||||||
return Light
|
return Light
|
||||||
} else {
|
} else {
|
||||||
return Dark
|
return Dark
|
||||||
|
@ -58,10 +58,8 @@ const (
|
|||||||
type Color color.Color
|
type Color color.Color
|
||||||
|
|
||||||
var (
|
var (
|
||||||
Light Color = color.RGBA{240, 217, 181, 255}
|
Light Color = color.RGBA{240, 217, 181, 255}
|
||||||
Dark Color = color.RGBA{181, 136, 99, 255}
|
Dark Color = color.RGBA{181, 136, 99, 255}
|
||||||
LightGreen Color = color.RGBA{205, 210, 106, 255}
|
|
||||||
DarkGreen Color = color.RGBA{170, 162, 58, 255}
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func NewPiece(name PieceName, color Color) (*Piece, error) {
|
func NewPiece(name PieceName, color Color) (*Piece, error) {
|
||||||
|
7
main.go
7
main.go
@ -146,11 +146,8 @@ func handleGameStart(req *sn.Item) error {
|
|||||||
return fmt.Errorf("failed to upload image for item %d: %v\n", req.Id, err)
|
return fmt.Errorf("failed to upload image for item %d: %v\n", req.Id, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// reply with algebraic notation, image and info
|
// reply with algebraic notation and image
|
||||||
info := "_A new chess game has been started!_\n\n" +
|
res = strings.Trim(fmt.Sprintf("%s\n\n%s", b.AlgebraicNotation(), imgUrl), " ")
|
||||||
"_Reply with a move like `d5` to continue the game. " +
|
|
||||||
"See [here](https://stacker.news/chess#how-to-continue) for details._"
|
|
||||||
res = strings.Trim(fmt.Sprintf("%s\n\n%s\n\n%s", b.AlgebraicNotation(), imgUrl, info), " ")
|
|
||||||
if _, err = createComment(req.Id, res); err != nil {
|
if _, err = createComment(req.Id, res); err != nil {
|
||||||
return fmt.Errorf("failed to reply to item %d: %v\n", req.Id, err)
|
return fmt.Errorf("failed to reply to item %d: %v\n", req.Id, err)
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user