Skip to content

Instantly share code, notes, and snippets.



Last active Jun 13, 2017
What would you like to do?
Encode white pawns and black pawns into bitmasks
# Program to encode pawns as bitmasks from a FEN
# Usage: ruby pawn_encoder.rb "rn3rk1/4bpp1/pq2bn1p/2pp4/B6B/2N1PN2/PP3PPP/2RQ1RK1"
def encode(fen)
board = fen.gsub("8"," 7").gsub("7"," 6").gsub("6"," 5").gsub("5"," 4").gsub("4"," 3").gsub("3"," 2").gsub("2"," 1").gsub("1"," ")
wpawnsA = board.gsub(/[^P\/]/,"0").gsub(/P/,"1").split("/")
bpawnsA = board.gsub(/[^p\/]/,"0").gsub(/p/,"1").split("/")
wpawnsB = wpawnsA.rotate
bpawnsB = bpawnsA.rotate(-1)
wpawns1 = wpawnsA.join.split(//).map {|c| c.ord - '0'.ord}
bpawns1 = bpawnsA.join.split(//).map {|c| c.ord - '0'.ord}
wpawns2 = wpawnsB.join.split(//).map {|c| c.ord - '0'.ord}
bpawns2 = bpawnsB.join.split(//).map {|c| c.ord - '0'.ord}
wpawns = {|i, j| (i+j+1)/2}
bpawns = {|i, j| (i+j+1)/2}
[ {|i| i.to_s}.join.slice(24, 32), {|i| i.to_s}.join.slice(8, 32)].join
puts encode(ARGV[0])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.