Skip to content

Instantly share code, notes, and snippets.

@mortdeus
Created February 7, 2020 09:36
Show Gist options
  • Save mortdeus/3a55f160f08110db422284242cbf4fd8 to your computer and use it in GitHub Desktop.
Save mortdeus/3a55f160f08110db422284242cbf4fd8 to your computer and use it in GitHub Desktop.
emoji chess
package main
import (
"fmt"
)
const printHistory = false // change this to true if you want to see every move printed out
// ♙ ♔ ♕ ♗ ♘ ♖ ♟ ♚ ♛ ♝ ♞ ♜
var moves = [...]struct{ piece, from, to rune }{
// add your move to this list here and then click "Run" above to see the board printed out
// move format and code syntax is laid out like the following i.e '{piece, from, to},'
// and the supported terms are defined at the bottom.
{wp, g3, f3},
{bp, b3, c3},
{wh, h6, f5},
{bp, b4, d4},
{wh, f5, d4},
// also you can also declare your moves using the chess piece emojis as well.
{'♞', a1, c0},
//go ahead and delete lines 16 - 30 once your up to speed and make the first move.
// then click share and send me the generated hyperlink on messenger.
}
var whosTurn = "white"
func main() {
for i, n := range moves {
move(n.piece, n.from, n.to)
switch n.piece {
case wp, wk, wq, wb, wh, wc:
whosTurn = "black"
case bp, bk, bq, bb, bh, bc:
whosTurn = "white"
}
if printHistory {
fmt.Println("**********************************************\nMove #", i)
printboard()
}
}
fmt.Println("**********************************************\n It's currently", whosTurn, "'s turn to move")
fmt.Println()
printboard()
}
// you can either copy and paste these symbols into the move function,
//(note you have to put single quotes around the emoji [e.g. move('♙', b0, c0) ])
//
// ♙ ♔ ♕ ♗ ♘ ♖ ♟ ♚ ♛ ♝ ♞ ♜
//
// or you can just pass in bp, wq, wk, bb, etc...
// (i defined them to semantically mean the same thing. [ e.g. move('♙', b0, c0) == move(wp, b0, c0) ])
func move(piece, from, to rune) {
if gameboard[from] != piece {
panic("Wrong piece selected!")
}
if to > h7 || to < a0 {
panic("Incorrect board index! (call format is `move(bp, b0, c0)` -> moves black pawn forward one square)")
}
switch piece {
case bp, bk, bq, bb, bh, bc:
switch gameboard[to] {
case bp, bk, bq, bb, bh, bc:
panic("Cant move there dummy!")
}
case wp, wk, wq, wb, wh, wc:
switch gameboard[to] {
case wp, wk, wq, wb, wh, wc:
panic("Cant move there dummy!")
}
}
gameboard[from] = emptyboard[from]
gameboard[to] = piece
}
func printboard() {
fmt.Println("\t[0]\t[1]\t[2]\t[3]\t[4]\t[5]\t[6]\t[7]")
for i := a0; i <= h7; i++ {
c := string(gameboard[i])
if c != "⬛" && c != "⬜" {
c = fmt.Sprintf("\t%s", c)
} else {
if c == "⬛" {
c = "\t⬛"
}
if c == "⬜" {
c = "\t⬜"
}
}
switch i {
case a0, b0, c0, d0, e0, f0, g0, h0:
fmt.Print("[" + dict[i] + "] " + " " + c)
case a7, b7, c7, d7, e7, f7, g7, h7:
fmt.Print(c + "\n\n")
default:
fmt.Print(c)
}
}
}
var dict map[rune]string = map[rune]string{
a0: "a", b0: "b", c0: "c", d0: "d", e0: "e", f0: "f", g0: "g", h0: "h",
'♟': "bp", '♚': "bk", '♔': "wk", '♕': "wq", '♘': "wh", '♙': "wp", '♗': "wb", '♛': "bq", '♝': "bb", '♞': "bh", '♖': "wc", '♜': "bc",
}
var emptyboard map[rune]rune = map[rune]rune{
a0: '⬜', a1: '⬛', a2: '⬜', a3: '⬛', a4: '⬜', a5: '⬛', a6: '⬜', a7: '⬛',
b0: '⬛', b1: '⬜', b2: '⬛', b3: '⬜', b4: '⬛', b5: '⬜', b6: '⬛', b7: '⬜',
c0: '⬜', c1: '⬛', c2: '⬜', c3: '⬛', c4: '⬜', c5: '⬛', c6: '⬜', c7: '⬛',
d0: '⬛', d1: '⬜', d2: '⬛', d3: '⬜', d4: '⬛', d5: '⬜', d6: '⬛', d7: '⬜',
e0: '⬜', e1: '⬛', e2: '⬜', e3: '⬛', e4: '⬜', e5: '⬛', e6: '⬜', e7: '⬛',
f0: '⬛', f1: '⬜', f2: '⬛', f3: '⬜', f4: '⬛', f5: '⬜', f6: '⬛', f7: '⬜',
g0: '⬜', g1: '⬛', g2: '⬜', g3: '⬛', g4: '⬜', g5: '⬛', g6: '⬜', g7: '⬛',
h0: '⬛', h1: '⬜', h2: '⬛', h3: '⬜', h4: '⬛', h5: '⬜', h6: '⬛', h7: '⬜',
}
var gameboard map[rune]rune = map[rune]rune{
a0: '♜', a1: '♞', a2: '♝', a3: '♚', a4: '♛', a5: '♝', a6: '♞', a7: '♜',
b0: '♟', b1: '♟', b2: '♟', b3: '♟', b4: '♟', b5: '♟', b6: '♟', b7: '♟',
c0: '⬜', c1: '⬛', c2: '⬜', c3: '⬛', c4: '⬜', c5: '⬛', c6: '⬜', c7: '⬛',
d0: '⬛', d1: '⬜', d2: '⬛', d3: '⬜', d4: '⬛', d5: '⬜', d6: '⬛', d7: '⬜',
e0: '⬜', e1: '⬛', e2: '⬜', e3: '⬛', e4: '⬜', e5: '⬛', e6: '⬜', e7: '⬛',
f0: '⬛', f1: '⬜', f2: '⬛', f3: '⬜', f4: '⬛', f5: '⬜', f6: '⬛', f7: '⬜',
g0: '♙', g1: '♙', g2: '♙', g3: '♙', g4: '♙', g5: '♙', g6: '♙', g7: '♙',
h0: '♖', h1: '♘', h2: '♗', h3: '♕', h4: '♔', h5: '♗', h6: '♘', h7: '♖',
}
const (
a0 rune = iota
a1
a2
a3
a4
a5
a6
a7
b0
b1
b2
b3
b4
b5
b6
b7
c0
c1
c2
c3
c4
c5
c6
c7
d0
d1
d2
d3
d4
d5
d6
d7
e0
e1
e2
e3
e4
e5
e6
e7
f0
f1
f2
f3
f4
f5
f6
f7
g0
g1
g2
g3
g4
g5
g6
g7
h0
h1
h2
h3
h4
h5
h6
h7
)
const (
wp = '♙'
wk = '♔'
wq = '♕'
wh = '♘'
wb = '♗'
wc = '♖'
bp = '♟'
bk = '♚'
bq = '♛'
bb = '♝'
bh = '♞'
bc = '♜'
)
const (
a = iota
b
c
d
e
f
g
h
)
/*
[0] [1] [2] [3] [4] [5] [6] [7]
[a] ♜ ♞ ♝ ♚ ♛ ♝ ♞ ♜
[b] ♟ ♟ ♟ ♟ ♟ ♟ ♟ ♟
[c] ⬜ ⬛ ⬜ ⬛ ⬜ ⬛ ⬜ ⬛
[d] ⬛ ⬜ ⬛ ⬜ ⬛ ⬜ ⬛ ⬜
[e] ⬜ ⬛ ⬜ ⬛ ⬜ ⬛ ⬜ ⬛
[f] ⬛ ⬜ ⬛ ⬜ ⬛ ⬜ ⬛ ⬜
[g] ♙ ♙ ♙ ♙ ♙ ♙ ♙ ♙
[h] ♖ ♘ ♗ ♕ ♔ ♗ ♘ ♖
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment