Skip to content Search Apps Documentation Source Content Run Reset Eval Playground File Folder Download Copy Actions State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

Community realm. Read the code first. Community realm, deployed by its author. Read the code before you interact or send coins.

types.gno

1.71 Kb · 73 lines
 1package chess
 2
 3// Board represents an 8x8 chess board matrix
 4type Board [8][8]string
 5
 6// CastlingRights tracks whether king or rooks have moved
 7type CastlingRights struct {
 8	WhiteKingMoved      bool
 9	WhiteLeftRookMoved  bool
10	WhiteRightRookMoved bool
11	BlackKingMoved      bool
12	BlackLeftRookMoved  bool
13	BlackRightRookMoved bool
14}
15
16// NewInitialBoard returns the standard starting position for a chess game
17func NewInitialBoard() Board {
18	var b Board
19
20	// Row 0 & 1: Black pieces (lowercase)
21	b[0] = [8]string{"r", "n", "b", "q", "k", "b", "n", "r"}
22	b[1] = [8]string{"p", "p", "p", "p", "p", "p", "p", "p"}
23
24	// Row 2 to 5: Empty squares
25	for i := 2; i < 6; i++ {
26		b[i] = [8]string{".", ".", ".", ".", ".", ".", ".", "."}
27	}
28
29	// Row 6 & 7: White pieces (uppercase)
30	b[6] = [8]string{"P", "P", "P", "P", "P", "P", "P", "P"}
31	b[7] = [8]string{"R", "N", "B", "Q", "K", "B", "N", "R"}
32
33	return b
34}
35
36// GetPieceEmoji converts a piece character string to a unicode chess emoji
37func GetPieceEmoji(p string) string {
38	switch p {
39	// White pieces
40	case "R": return "♖"
41	case "N": return "♘"
42	case "B": return "♗"
43	case "Q": return "♕"
44	case "K": return "♔"
45	case "P": return "♙"
46	// Black pieces
47	case "r": return "♜"
48	case "n": return "♞"
49	case "b": return "♝"
50	case "q": return "♛"
51	case "k": return "♚"
52	case "p": return "♟"
53	default: return " " // Empty square
54	}
55}
56
57// IsWhitePiece returns true if the piece belongs to White
58func IsWhitePiece(p string) bool {
59	return p >= "A" && p <= "Z"
60}
61
62// IsBlackPiece returns true if the piece belongs to Black
63func IsBlackPiece(p string) bool {
64	return p >= "a" && p <= "z"
65}
66
67// Abs returns the absolute value of an integer
68func Abs(x int) int {
69	if x < 0 {
70		return -x
71	}
72	return x
73}