Compare commits
No commits in common. "8347bf0cbc2ce359d1b85150e4447d8fef5448de" and "392cf19ae6a9766ca55484b69036796ce4929c8a" have entirely different histories.
8347bf0cbc
...
392cf19ae6
@ -19,11 +19,6 @@ type Board struct {
|
||||
tiles [8][8]*Piece
|
||||
turn Color
|
||||
Moves []string
|
||||
moveIndicators []Tile
|
||||
}
|
||||
|
||||
type Tile struct {
|
||||
X, Y int
|
||||
}
|
||||
|
||||
func NewBoard() *Board {
|
||||
@ -106,7 +101,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(b, xi, yi))
|
||||
bg = image.NewUniform(getTileColor(xi, yi))
|
||||
draw.Draw(img, rect, bg, p, draw.Src)
|
||||
|
||||
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) {
|
||||
color := getTileColor(nil, x, y)
|
||||
color := getTileColor(x, y)
|
||||
if !flipped && color == Light {
|
||||
color = Dark
|
||||
} else if !flipped {
|
||||
@ -807,7 +802,6 @@ 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
|
||||
}
|
||||
|
||||
@ -830,7 +824,6 @@ 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
|
||||
}
|
||||
|
||||
@ -847,7 +840,6 @@ 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
|
||||
}
|
||||
|
||||
@ -957,7 +949,6 @@ 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
|
||||
}
|
||||
|
||||
@ -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 {
|
||||
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
|
||||
@ -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 {
|
||||
b.tiles[xPrev][yPrev] = nil
|
||||
b.tiles[x][y] = piece
|
||||
b.moveIndicators = []Tile{{xPrev, yPrev}, {x, y}}
|
||||
return nil
|
||||
} else {
|
||||
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 {
|
||||
b.tiles[xPrev][yPrev] = nil
|
||||
b.tiles[x][y] = piece
|
||||
b.moveIndicators = []Tile{{xPrev, yPrev}, {x, y}}
|
||||
return nil
|
||||
} else {
|
||||
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 {
|
||||
b.tiles[xPrev][yPrev] = nil
|
||||
b.tiles[x][y] = piece
|
||||
b.moveIndicators = []Tile{{xPrev, yPrev}, {x, y}}
|
||||
return nil
|
||||
} else {
|
||||
break
|
||||
@ -1137,7 +1124,6 @@ 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
|
||||
}
|
||||
|
||||
@ -1209,8 +1195,6 @@ 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
|
||||
}
|
||||
|
||||
@ -1244,8 +1228,6 @@ 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
|
||||
}
|
||||
}
|
||||
@ -1257,7 +1239,6 @@ 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
|
||||
}
|
||||
|
||||
@ -1268,7 +1249,6 @@ 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,7 +1259,6 @@ 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
|
||||
}
|
||||
|
||||
@ -1290,7 +1269,6 @@ 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
|
||||
}
|
||||
|
||||
@ -1301,7 +1279,6 @@ 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
|
||||
}
|
||||
|
||||
@ -1312,7 +1289,6 @@ 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
|
||||
}
|
||||
|
||||
@ -1323,7 +1299,6 @@ 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
|
||||
}
|
||||
|
||||
@ -1334,7 +1309,6 @@ 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
|
||||
}
|
||||
|
||||
@ -1419,25 +1393,8 @@ func (b *Board) getCollision(position string) (*Piece, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
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 {
|
||||
func getTileColor(x, y int) Color {
|
||||
if x%2 == y%2 {
|
||||
return Light
|
||||
} else {
|
||||
return Dark
|
||||
|
@ -60,8 +60,6 @@ type Color color.Color
|
||||
var (
|
||||
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) {
|
||||
|
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)
|
||||
}
|
||||
|
||||
// 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), " ")
|
||||
// reply with algebraic notation and image
|
||||
res = strings.Trim(fmt.Sprintf("%s\n\n%s", b.AlgebraicNotation(), imgUrl), " ")
|
||||
if _, err = createComment(req.Id, res); err != nil {
|
||||
return fmt.Errorf("failed to reply to item %d: %v\n", req.Id, err)
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user