Compare commits

...

2 Commits

Author SHA1 Message Date
ekzyis 8347bf0cbc Highlight last move 2024-10-18 22:24:41 +02:00
ekzyis 7a5b044b9d Add info to game starts 2024-10-18 21:38:36 +02:00
3 changed files with 59 additions and 11 deletions

View File

@ -16,9 +16,14 @@ import (
)
type Board struct {
tiles [8][8]*Piece
turn Color
Moves []string
tiles [8][8]*Piece
turn Color
Moves []string
moveIndicators []Tile
}
type Tile struct {
X, Y int
}
func NewBoard() *Board {
@ -101,7 +106,7 @@ func (b *Board) Image() *image.RGBA {
x := xi * 128
y := yi * 128
rect = image.Rect(x, y, x+128, y+128)
bg = image.NewUniform(getTileColor(xi, yi))
bg = image.NewUniform(getTileColor(b, xi, yi))
draw.Draw(img, rect, bg, p, draw.Src)
piece = b.tiles[xi][yi]
@ -186,7 +191,7 @@ func drawCoordinate(img *image.RGBA, x, y int, flipped bool) {
}
drawString := func(s string, origin fixed.Point26_6) {
color := getTileColor(x, y)
color := getTileColor(nil, x, y)
if !flipped && color == Light {
color = Dark
} else if !flipped {
@ -802,6 +807,7 @@ func (b *Board) movePawn(position string, fromX int, fromY int, promotion string
if promotion != "" {
return b.promotePawn(toX, toY, promotion)
}
b.moveIndicators = []Tile{{fromX, fromY}, {toX, toY}}
return nil
}
@ -824,6 +830,7 @@ func (b *Board) movePawn(position string, fromX int, fromY int, promotion string
if promotion != "" {
return b.promotePawn(toX, toY, promotion)
}
b.moveIndicators = []Tile{{toX, yPrev}, {toX, toY}}
return nil
}
@ -840,6 +847,7 @@ func (b *Board) movePawn(position string, fromX int, fromY int, promotion string
if promotion != "" {
return b.promotePawn(toX, toY, promotion)
}
b.moveIndicators = []Tile{{toX, yPrev}, {toX, toY}}
return nil
}
@ -949,6 +957,7 @@ func (b *Board) moveRook(position string, queen bool, fromX int, fromY int) erro
p = b.getPiece(xPrev, yPrev)
b.tiles[x][y] = p
b.tiles[xPrev][yPrev] = nil
b.moveIndicators = []Tile{{xPrev, yPrev}, {x, y}}
return nil
}
@ -979,6 +988,7 @@ func (b *Board) moveBishop(position string, queen bool) error {
if ((!queen && piece.Name == Bishop) || (queen && piece.Name == Queen)) && piece.Color == b.turn {
b.tiles[xPrev][yPrev] = nil
b.tiles[x][y] = piece
b.moveIndicators = []Tile{{xPrev, yPrev}, {x, y}}
return nil
} else {
// direction blocked by other piece
@ -997,6 +1007,7 @@ func (b *Board) moveBishop(position string, queen bool) error {
if ((!queen && piece.Name == Bishop) || (queen && piece.Name == Queen)) && piece.Color == b.turn {
b.tiles[xPrev][yPrev] = nil
b.tiles[x][y] = piece
b.moveIndicators = []Tile{{xPrev, yPrev}, {x, y}}
return nil
} else {
break
@ -1014,6 +1025,7 @@ func (b *Board) moveBishop(position string, queen bool) error {
if ((!queen && piece.Name == Bishop) || (queen && piece.Name == Queen)) && piece.Color == b.turn {
b.tiles[xPrev][yPrev] = nil
b.tiles[x][y] = piece
b.moveIndicators = []Tile{{xPrev, yPrev}, {x, y}}
return nil
} else {
break
@ -1031,6 +1043,7 @@ func (b *Board) moveBishop(position string, queen bool) error {
if ((!queen && piece.Name == Bishop) || (queen && piece.Name == Queen)) && piece.Color == b.turn {
b.tiles[xPrev][yPrev] = nil
b.tiles[x][y] = piece
b.moveIndicators = []Tile{{xPrev, yPrev}, {x, y}}
return nil
} else {
break
@ -1124,6 +1137,7 @@ func (b *Board) moveKnight(position string, fromX int, fromY int) error {
p = b.getPiece(xPrev, yPrev)
b.tiles[x][y] = p
b.tiles[xPrev][yPrev] = nil
b.moveIndicators = []Tile{{xPrev, yPrev}, {x, y}}
return nil
}
@ -1195,6 +1209,8 @@ func (b *Board) moveKing(position string, castle bool) error {
b.tiles[5][y] = rook
b.tiles[7][y] = nil
b.moveIndicators = []Tile{{4, y}, {x, y}, {5, y}, {7, y}}
return nil
}
@ -1228,6 +1244,8 @@ func (b *Board) moveKing(position string, castle bool) error {
b.tiles[3][y] = rook
b.tiles[0][y] = nil
b.moveIndicators = []Tile{{2, y}, {4, y}, {3, y}, {0, y}}
return nil
}
}
@ -1239,6 +1257,7 @@ func (b *Board) moveKing(position string, castle bool) error {
if piece != nil && piece.Name == King && piece.Color == b.turn {
b.tiles[xPrev][yPrev] = nil
b.tiles[x][y] = piece
b.moveIndicators = []Tile{{xPrev, yPrev}, {x, y}}
return nil
}
@ -1249,6 +1268,7 @@ func (b *Board) moveKing(position string, castle bool) error {
if piece != nil && piece.Name == King && piece.Color == b.turn {
b.tiles[xPrev][yPrev] = nil
b.tiles[x][y] = piece
b.moveIndicators = []Tile{{xPrev, yPrev}, {x, y}}
return nil
}
@ -1259,6 +1279,7 @@ func (b *Board) moveKing(position string, castle bool) error {
if piece != nil && piece.Name == King && piece.Color == b.turn {
b.tiles[xPrev][yPrev] = nil
b.tiles[x][y] = piece
b.moveIndicators = []Tile{{xPrev, yPrev}, {x, y}}
return nil
}
@ -1269,6 +1290,7 @@ func (b *Board) moveKing(position string, castle bool) error {
if piece != nil && piece.Name == King && piece.Color == b.turn {
b.tiles[xPrev][yPrev] = nil
b.tiles[x][y] = piece
b.moveIndicators = []Tile{{xPrev, yPrev}, {x, y}}
return nil
}
@ -1279,6 +1301,7 @@ func (b *Board) moveKing(position string, castle bool) error {
if piece != nil && piece.Name == King && piece.Color == b.turn {
b.tiles[xPrev][yPrev] = nil
b.tiles[x][y] = piece
b.moveIndicators = []Tile{{xPrev, yPrev}, {x, y}}
return nil
}
@ -1289,6 +1312,7 @@ func (b *Board) moveKing(position string, castle bool) error {
if piece != nil && piece.Name == King && piece.Color == b.turn {
b.tiles[xPrev][yPrev] = nil
b.tiles[x][y] = piece
b.moveIndicators = []Tile{{xPrev, yPrev}, {x, y}}
return nil
}
@ -1299,6 +1323,7 @@ func (b *Board) moveKing(position string, castle bool) error {
if piece != nil && piece.Name == King && piece.Color == b.turn {
b.tiles[xPrev][yPrev] = nil
b.tiles[x][y] = piece
b.moveIndicators = []Tile{{xPrev, yPrev}, {x, y}}
return nil
}
@ -1309,6 +1334,7 @@ func (b *Board) moveKing(position string, castle bool) error {
if piece != nil && piece.Name == King && piece.Color == b.turn {
b.tiles[xPrev][yPrev] = nil
b.tiles[x][y] = piece
b.moveIndicators = []Tile{{xPrev, yPrev}, {x, y}}
return nil
}
@ -1393,8 +1419,25 @@ func (b *Board) getCollision(position string) (*Piece, error) {
return nil, nil
}
func getTileColor(x, y int) Color {
if x%2 == y%2 {
func getTileColor(b *Board, x, y int) Color {
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
} else {
return Dark

View File

@ -58,8 +58,10 @@ const (
type Color color.Color
var (
Light Color = color.RGBA{240, 217, 181, 255}
Dark Color = color.RGBA{181, 136, 99, 255}
Light Color = color.RGBA{240, 217, 181, 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) {

View File

@ -146,8 +146,11 @@ func handleGameStart(req *sn.Item) error {
return fmt.Errorf("failed to upload image for item %d: %v\n", req.Id, err)
}
// reply with algebraic notation and image
res = strings.Trim(fmt.Sprintf("%s\n\n%s", b.AlgebraicNotation(), imgUrl), " ")
// reply with algebraic notation, image and info
info := "_A new chess game has been started!_\n\n" +
"_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 {
return fmt.Errorf("failed to reply to item %d: %v\n", req.Id, err)
}