From da0b5a84453ef03dd8eba5ec41ac26aa1f2083b9 Mon Sep 17 00:00:00 2001
From: Paolo Capriotti
Date: Sun, 28 Jun 2009 22:56:17 +0200
Subject: [PATCH] Add shogi PSN load/save support.
---
lib/games/chess/pgn.rb | 26 +++++++++++++++++---------
lib/games/shogi/piece.rb | 3 ++-
lib/games/shogi/psn.rb | 19 +++++++++++++++++++
lib/plugins/games/shogi.rb | 10 +++++++++-
4 files changed, 47 insertions(+), 11 deletions(-)
create mode 100644 lib/games/shogi/psn.rb
diff --git a/lib/games/chess/pgn.rb b/lib/games/chess/pgn.rb
index ee8ebeb..fb13ecb 100644
--- a/lib/games/chess/pgn.rb
+++ b/lib/games/chess/pgn.rb
@@ -22,8 +22,7 @@ class PGN
tag(:site, info[:site]) +
tag(:date, date) +
tag(:round, info[:round]) +
- tag(:white, info.fetch(:players, {})[:white]) +
- tag(:black, info.fetch(:players, {})[:black]) +
+ player_tags(info[:players] || { }) +
tag(:result, result(info[:result])) +
"\n" +
game(history) + " " +
@@ -39,11 +38,7 @@ class PGN
scanner.scan(/\n*/)
# insert players into info[:players]
- info[:players] = {
- :white => info[:white],
- :black => info[:black] }
- info.delete(:white)
- info.delete(:black)
+ info[:players] = read_players(info)
state = @state_factory.new
state.setup
@@ -74,8 +69,7 @@ class PGN
end
result = scanner.scan(/1-0|0-1|1\/2-1\/2|\*/)
- raise ParseError.new("Expected result") unless result
- info[:result] = result
+ info[:result] = result if result
history
end
@@ -87,6 +81,20 @@ class PGN
end
end
+ def read_players(info)
+ result = {
+ :white => info[:white],
+ :black => info[:black] }
+ info.delete(:white)
+ info.delete(:black)
+ result
+ end
+
+ def player_tags(players)
+ tag(:white, players[:white]) +
+ tag(:black, players[:black])
+ end
+
def result(value)
case value
when String
diff --git a/lib/games/shogi/piece.rb b/lib/games/shogi/piece.rb
index 389f6f9..d8ce690 100644
--- a/lib/games/shogi/piece.rb
+++ b/lib/games/shogi/piece.rb
@@ -9,7 +9,8 @@ class Piece < Chess::Piece
'N' => :horse,
'G' => :gold,
'S' => :silver,
- 'L' => :lance }
+ 'L' => :lance,
+ 'K' => :king }
SYMBOLS = TYPES.invert
def self.type_from_symbol(sym)
diff --git a/lib/games/shogi/psn.rb b/lib/games/shogi/psn.rb
new file mode 100644
index 0000000..a237660
--- /dev/null
+++ b/lib/games/shogi/psn.rb
@@ -0,0 +1,19 @@
+module Shogi
+
+class PSN < Chess::PGN
+ def read_players(info)
+ result = {
+ :black => info[:sente],
+ :white => info[:gote] }
+ info.delete(:sente)
+ info.delete(:gote)
+ result
+ end
+
+ def player_tags(players)
+ tag(:sente, players[:black]) +
+ tag(:gote, players[:white])
+ end
+end
+
+end
diff --git a/lib/plugins/games/shogi.rb b/lib/plugins/games/shogi.rb
index de2c6c3..ae3dcbe 100644
--- a/lib/plugins/games/shogi.rb
+++ b/lib/plugins/games/shogi.rb
@@ -6,6 +6,7 @@ require 'games/shogi/policy'
require 'games/shogi/serializer'
require 'games/shogi/notation'
require 'games/shogi/piece'
+require 'games/shogi/psn'
require 'plugins/plugin'
require 'games/game_actions'
@@ -24,7 +25,7 @@ class Game
attr_reader :size, :state, :board, :pool,
:policy, :move, :animator, :validator,
:piece, :players, :types, :serializer,
- :notation
+ :notation, :game_writer, :game_extensions
def initialize
@size = Point.new(9, 9)
@@ -44,6 +45,9 @@ class Game
@serializer = Factory.new(Serializer) {|rep|
Serializer.new(rep, validator, move, piece, notation) }
@notation = Notation.new(piece, size)
+
+ @game_writer = PSN.new(serializer.new(:compact), state)
+ @game_extensions = %w(psn)
action :autopromote,
:checked => true,
@@ -51,6 +55,10 @@ class Game
policy.autopromote = value
end
end
+
+ def game_reader
+ @game_writer
+ end
end
end
\ No newline at end of file
--
2.11.4.GIT