In https://github.com/Koistinen/chess/blob/main/chess.nim I am using the unicode flexibility Nim allows:
type Piece = enum
♚ = -6, ♛, ♜, ♝, ♞, ♟, □, ♙, ♘, ♗, ♖, ♕, ♔
type Pos* = object # 64 bytes
bd: array[0..63, Piece]
g50: int
side: int
ep: int
castling: int
proc StartingPos: Pos =
result.bd = [
♖,♘,♗,♕,♔,♗,♘,♖,
♙,♙,♙,♙,♙,♙,♙,♙,
□,□,□,□,□,□,□,□,
□,□,□,□,□,□,□,□,
□,□,□,□,□,□,□,□,
□,□,□,□,□,□,□,□,
♟,♟,♟,♟,♟,♟,♟,♟,
♜,♞,♝,♛,♚,♝,♞,♜]
result.castling = 0xf
In the future I am planning to use variable names like ♔sq for the square of the white king. My understanding is that this is not guaranteed for the future but it is likely that only unicode characters that are likely to be used as operators and similar will likely cause the code to break if used in this way.
Am I right in my understanding?
I have not seen others use Nim this way, what examples are there?
Any comments on my code? (It is supposed to prioritize clarity and not care about speed.)
:abbreviate king ♔
Then, when you type 'king' and an non-alphabet character, it automatically replaced with '♔'.
Examples:
abbreviate noteq ≠
abbreviate cong ≡
abbreviate notcong ≢
abbreviate sigma Σ
abbreviate sqroot √
abbreviate curoot ∛
abbreviate blspade ♠
abbreviate whspade ♤
I have not seen others use Nim this way, what examples are there?
from std/random import randomize, sample
randomize()
type
✊🤚✌️ = enum
✊, 🤚, ✌️
🏆 = enum
👈, 🤷, 👉
proc `>`(👤, 👥: ✊🤚✌️): bool =
👤 == 🤚 and 👥 == ✊ or
👤 == ✌️ and 👥 == 🤚 or
👤 == ✊ and 👥 == ✌️
proc 👋(👤, 👥: ✊🤚✌️): 🏆 =
if 👤 == 👥: 🤷
elif 👤 > 👥: 👈
else: 👉
proc 🆚(👤, 👥: ✊🤚✌️): void =
echo(👤, " ", 👤.👋 👥 , " ", 👥)
template ❓: ✊🤚✌️ =
sample([✊, 🤚, ✌️])
✊.🆚 ✊
🤚.🆚 ✊
✌️.🆚 ✊
✊.🆚 🤚
🤚.🆚 🤚
✌️.🆚 🤚
✊.🆚 ✌️
🤚.🆚 ✌️
✌️.🆚 ✌️
❓.🆚 ✊
✊.🆚 ❓
❓.🆚 ❓
What if they later want to allow chessboards o| different sizes? seq[seq[T]] has overhead over a flat seq.
Good one. Chessboards are 8x8 and an array is not a seq.
I have not seen others use Nim this way, what examples are there?
I use Nim's unicode support rather extensively.
just to give some examples.
: The unicode is fine but array[0..63, Piece] should be array[8, array[8, Piece]]. Don't worry about the nesting, it has no overhead.
For a normal chess engine, this would be fine. I am going for a tablebase generator that might be able to generate 11-man endgames and to be able to do that I need to use whole squares indices and an indexing scheme that separates out 3 bits which stay invariant under all three rotations. That way required ram and communication might be kept down, allowing for a distributed compute solution.
I have not seen others use Nim this way, what examples are there?
type Zodiak* = enum
♈︎ = (1, "♈︎") ## | Beran | Κριός/Kriós | Aries | Meṣa
♉︎ = "♉︎" ## | Býk | Ταῦρος/Tauros | Taurus | Vṛṣabha
♊︎ = "♊︎" ## | Blíženci | Δίδυμοι/Didymoi | Gemini | Mithuna
♋︎ = "♋︎" ## | Rak | Καρκῖνος/Karkīnos | Cancer | Karkaṭa
♌︎ = "♌︎" ## | Lev | Λέων/Léōn | Leo | Siṃha
♍︎ = "♍︎" ## | Panna | Παρθένος/Parthénos | Virgo | Kanyā
♎︎ = "♎︎" ## | Váhy | Ζυγός/Zygós | Libra | Tulā
♏︎ = "♏︎" ## | Štír | Σκоρπιός/Skorpiós | Scorpius | Vṛścika
♐︎ = "♐︎" ## | Střelec | Τοξότης/Toxótēs | Sagittarius | Dhanuṣa
♑︎ = "♑︎" ## | Kozoroh | Αἰγόκερως/Aigókerōs| Capricornus | Makara
♒︎ = "♒︎" ## | Vodnář | Ὑδροχόος/Hydrochóos| Aquarius | Kumbha
♓︎ = "♓︎" ## | Ryby | Ἰχθύες/Ichthýes | Pisces | Mīna