Skip to content

code updated, optimized, game.to_h added #18

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .rspec
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
--color
--format progress
--profile 2
17 changes: 8 additions & 9 deletions lib/pgn.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
require "pgn/board"
require "pgn/fen"
require "pgn/game"
require "pgn/move"
require "pgn/move_calculator"
require "pgn/parser"
require "pgn/position"
require "pgn/version"
require 'pgn/board'
require 'pgn/fen'
require 'pgn/game'
require 'pgn/move'
require 'pgn/move_calculator'
require 'pgn/parser'
require 'pgn/position'
require 'pgn/version'

module PGN

# @param pgn [String] a pgn representation of one or more chess games
# @return [Array<PGN::Game>] a list of games
#
Expand Down
80 changes: 80 additions & 0 deletions lib/pgn/base.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
# frozen_string_literal: true

#
# Base with common attributes
#
module PGN
CODE = {
rulers: {
black: %w[k q],
white: %w[K Q]
},
castling: {
kingside: 'O-O',
queenside: 'O-O-O'
},
check: '+',
checkmate: '#',
color: {
black: 'b',
white: 'w'
},
pawns: %w[P p],
piece: {
b: 'b', k: 'k', n: 'n', p: 'p', q: 'q', r: 'r',
B: 'B', K: 'K', N: 'N', P: 'P', Q: 'Q', R: 'R',
},
players: %i[white black],
}

TAGS = {
#
# Seven Tag Roster
#
# Name of the tournament or match event.
event: 'Event',
# Location of the event. This is in City, Region COUNTRY format, where
# COUNTRY is the three-letter International Olympic Committee code for the
# country. An example is New York City, NY USA.
#
# Although not part of the specification, some online chess platforms will
# include a URL or website as the site value.
site: 'Site',
# Starting date of the game, in YYYY.MM.DD form. ?? is used for unknown values.
date: 'Date',
# Playing round ordinal of the game within the event.
round: 'Round',
# Player of the white pieces, in Lastname, Firstname format.
white: 'White',
# Player of the black pieces, same format as White.
black: 'Black',
# Result of the game. It is recorded as White score, dash, then Black score, or * (other, e.g., the game is ongoing).
result: 'Result',
#
# Optional tag pairs
#
# The person providing notes to the game.
annotator: 'Annotator',
# String value denoting the total number of half-moves played.
ply_count: 'PlyCount',
# e.g. 40/7200:3600 (moves per seconds: sudden death seconds)
time_control: 'TimeControl',
# Time the game started, in HH:MM:SS format, in local clock time.
time: 'Time',
# Gives more details about the termination of the game. It may be
# abandoned, adjudication (result determined by third-party adjudication),
# death, emergency, normal, rules infraction, time forfeit, or
# unterminated.
termination: 'Termination',
# OTB (over-the-board) ICS (Internet Chess Server)
mode: 'Mode',
# The initial position of the chessboard, in Forsyth–Edwards Notation.
# This is used to record partial games (starting at some initial
# position). It is also necessary for chess variants such as Chess960,
# where the initial position is not always the same as traditional chess.
fen: 'FEN',
# If a FEN tag is used, a separate tag pair SetUp must also appear and
# have its value set to 1.
setup: 'SetUp'
}
end
70 changes: 37 additions & 33 deletions lib/pgn/board.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# frozen_string_literal: true

require_relative './base'

module PGN
# {PGN::Board} represents the squares of a chess board and the pieces on
# each square. It is responsible for translating between a human readable
Expand All @@ -13,14 +17,14 @@ class Board
# The starting, internal representation of a chess board
#
START = [
["R", "P", nil, nil, nil, nil, "p", "r"],
["N", "P", nil, nil, nil, nil, "p", "n"],
["B", "P", nil, nil, nil, nil, "p", "b"],
["Q", "P", nil, nil, nil, nil, "p", "q"],
["K", "P", nil, nil, nil, nil, "p", "k"],
["B", "P", nil, nil, nil, nil, "p", "b"],
["N", "P", nil, nil, nil, nil, "p", "n"],
["R", "P", nil, nil, nil, nil, "p", "r"],
[PGN::CODE[:piece][:R], PGN::CODE[:piece][:P], nil, nil, nil, nil, PGN::CODE[:piece][:p], PGN::CODE[:piece][:r]],
[PGN::CODE[:piece][:N], PGN::CODE[:piece][:P], nil, nil, nil, nil, PGN::CODE[:piece][:p], PGN::CODE[:piece][:n]],
[PGN::CODE[:piece][:B], PGN::CODE[:piece][:P], nil, nil, nil, nil, PGN::CODE[:piece][:p], PGN::CODE[:piece][:b]],
[PGN::CODE[:piece][:Q], PGN::CODE[:piece][:P], nil, nil, nil, nil, PGN::CODE[:piece][:p], PGN::CODE[:piece][:q]],
[PGN::CODE[:piece][:K], PGN::CODE[:piece][:P], nil, nil, nil, nil, PGN::CODE[:piece][:p], PGN::CODE[:piece][:k]],
[PGN::CODE[:piece][:B], PGN::CODE[:piece][:P], nil, nil, nil, nil, PGN::CODE[:piece][:p], PGN::CODE[:piece][:b]],
[PGN::CODE[:piece][:N], PGN::CODE[:piece][:P], nil, nil, nil, nil, PGN::CODE[:piece][:p], PGN::CODE[:piece][:n]],
[PGN::CODE[:piece][:R], PGN::CODE[:piece][:P], nil, nil, nil, nil, PGN::CODE[:piece][:p], PGN::CODE[:piece][:r]]
]

FILE_TO_INDEX = {
Expand All @@ -31,7 +35,7 @@ class Board
'e' => 4,
'f' => 5,
'g' => 6,
'h' => 7,
'h' => 7
}
INDEX_TO_FILE = Hash[FILE_TO_INDEX.map(&:reverse)]

Expand All @@ -43,28 +47,29 @@ class Board
'5' => 4,
'6' => 5,
'7' => 6,
'8' => 7,
'8' => 7
}
INDEX_TO_RANK = Hash[RANK_TO_INDEX.map(&:reverse)]

# algebraic to unicode piece lookup
#
UNICODE_PIECES = {
'k' => "\u{265A}",
'q' => "\u{265B}",
'r' => "\u{265C}",
'b' => "\u{265D}",
'n' => "\u{265E}",
'p' => "\u{265F}",
'K' => "\u{2654}",
'Q' => "\u{2655}",
'R' => "\u{2656}",
'B' => "\u{2657}",
'N' => "\u{2658}",
'P' => "\u{2659}",
nil => '_',
# black
PGN::CODE[:piece][:k] => "\u{265A}",
PGN::CODE[:piece][:q] => "\u{265B}",
PGN::CODE[:piece][:r] => "\u{265C}",
PGN::CODE[:piece][:b] => "\u{265D}",
PGN::CODE[:piece][:n] => "\u{265E}",
PGN::CODE[:piece][:p] => "\u{265F}",
# white
PGN::CODE[:piece][:K] => "\u{2654}",
PGN::CODE[:piece][:Q] => "\u{2655}",
PGN::CODE[:piece][:R] => "\u{2656}",
PGN::CODE[:piece][:B] => "\u{2657}",
PGN::CODE[:piece][:N] => "\u{2658}",
PGN::CODE[:piece][:P] => "\u{2659}",
nil => '_'
}

attr_accessor :squares

# @return [PGN::Board] a board in the starting position
Expand Down Expand Up @@ -108,9 +113,9 @@ def initialize(squares)
def at(*args)
case args.length
when 1
self.at(*coordinates_for(args.first))
at(*coordinates_for(args.first))
when 2
self.squares[args[0]][args[1]]
squares[args[0]][args[1]]
end
end

Expand All @@ -121,7 +126,7 @@ def at(*args)
#
def change!(changes)
changes.each do |square, piece|
self.update(square, piece)
update(square, piece)
end
self
end
Expand All @@ -134,7 +139,7 @@ def change!(changes)
#
def update(square, piece)
coords = coordinates_for(square)
self.squares[coords[0]][coords[1]] = piece
squares[coords[0]][coords[1]] = piece
self
end

Expand All @@ -159,23 +164,22 @@ def position_for(coordinates)
file, rank = coordinates
file_chr = INDEX_TO_FILE[file]
rank_chr = INDEX_TO_RANK[rank]
[file_chr, rank_chr].join('')
[file_chr, rank_chr].join
end

# @return [String] the board in human readable format with unicode
# pieces
#
def inspect
self.squares.transpose.reverse.map do |row|
row.map{|chr| UNICODE_PIECES[chr] }.join(' ')
squares.transpose.reverse.map do |row|
row.map { |chr| UNICODE_PIECES[chr] }.join(' ')
end.join("\n")
end

# @return [PGN::Board] a copy of self with duplicated squares
#
def dup
PGN::Board.new(self.squares.map(&:dup))
PGN::Board.new(squares.map(&:dup))
end

end
end
Loading