This repository has been archived by the owner on Dec 12, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
major refactoring to game engine and worker
- Loading branch information
Showing
12 changed files
with
214 additions
and
251 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
<% if flash[:alert] %> | ||
alert("<%= escape_javascript(flash.delete(:alert)) %>"); | ||
<% else %> | ||
addMoves("<%= @game.moves_after(params[:after].to_i) %>", "<%= @game.current_player_id.to_i %>"); | ||
$('.profile .details #score_<%= @game.black_player_id.to_i %>').html(<%= raw game_score(@game.black_score, @game.finished?).to_s.inspect %>); | ||
$('.profile .details #score_<%= @game.white_player_id.to_i %>').html(<%= raw game_score(@game.white_score, @game.finished?).to_s.inspect %>); | ||
<% end %> | ||
addMoves("<%= escape_javascript(@game.moves_after(params[:after].to_i)) %>", "<%= @game.current_player_id.to_i %>"); | ||
$('.profile .details #score_<%= @game.black_player_id.to_i %>').html("<%= escape_javascript(game_score(@game.black_score, @game.finished?).to_s) %>"); | ||
$('.profile .details #score_<%= @game.white_player_id.to_i %>').html("<%= escape_javascript(game_score(@game.white_score, @game.finished?).to_s) %>"); | ||
<% end %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,7 @@ | ||
<% if @moves.blank? %> | ||
setTimeout(pollMoves, pollTimer); | ||
<% elsif @game.resigned? %> | ||
alert("<%= @game.black_score.zero? ? "Black" : "White" %> resigned."); | ||
<% else %> | ||
addMoves("<%= @moves %>", "<%= @game.current_player_id.to_i %>"); | ||
$('.profile .details #score_<%= @game.black_player_id.to_i %>').html(<%= raw game_score(@game.black_score, @game.finished?).to_s.inspect %>); | ||
$('.profile .details #score_<%= @game.white_player_id.to_i %>').html(<%= raw game_score(@game.white_score, @game.finished?).to_s.inspect %>); | ||
addMoves("<%= escape_javascript(@moves) %>", "<%= @game.current_player_id.to_i %>"); | ||
$('.profile .details #score_<%= @game.black_player_id.to_i %>').html("<%= escape_javascript(game_score(@game.black_score, @game.finished?).to_s) %>"); | ||
$('.profile .details #score_<%= @game.white_player_id.to_i %>').html("<%= escape_javascript(game_score(@game.white_score, @game.finished?).to_s) %>"); | ||
<% end %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
class GameEngine | ||
class Error < StandardError; end | ||
class IllegalMove < Error; end | ||
class OutOfTurn < Error; end | ||
|
||
def self.run(options = {}, &block) | ||
Go::GTP.run_gnugo do |gtp| | ||
gtp.boardsize(options[:board_size]) unless options[:board_size].to_s.empty? | ||
gtp.fixed_handicap(options[:handicap]) if options[:handicap].to_i.nonzero? | ||
gtp.komi(options[:komi]) unless options[:komi].to_s.empty? | ||
yield GameEngine.new(gtp, options) | ||
end | ||
end | ||
|
||
def initialize(gtp, options = {}) | ||
@gtp = gtp | ||
@board_size = options[:board_size] || 19 | ||
@handicap = options[:handicap] || 0 | ||
end | ||
|
||
def replay(moves) | ||
colors = [:black, :white].cycle | ||
colors.next if first_color == :white | ||
moves.to_s.split("-").each do |move| | ||
play(colors.next, (move =~ /[A-Z]/ ? move : move[0..1])) | ||
end | ||
end | ||
|
||
def play(color, vertex) | ||
@gtp.play(color, gnugo_point(vertex)) | ||
raise IllegalMove unless @gtp.success? | ||
end | ||
|
||
def move(color, vertex = nil) | ||
if %w[PASS RESIGN].include? vertex | ||
play(color, vertex) | ||
vertex | ||
else | ||
other_stones = @gtp.list_stones(opposite(color)) | ||
if vertex | ||
play(color, vertex) | ||
else | ||
vertex = sgf_point(@gtp.genmove(color)) | ||
end | ||
captured = other_stones - @gtp.list_stones(opposite(color)) | ||
sgf_point(vertex) + captured.map { |v| sgf_point(v) }.join | ||
end | ||
end | ||
|
||
def positions(color) | ||
@gtp.list_stones(color).map { |v| sgf_point(v) }.join | ||
end | ||
|
||
def captures(color) | ||
@gtp.captures(color) | ||
end | ||
|
||
def black_score | ||
score_for("B") | ||
end | ||
|
||
def white_score | ||
score_for("W") | ||
end | ||
|
||
def game_finished? | ||
@gtp.over? | ||
end | ||
|
||
def first_color | ||
@handicap > 0 ? :white : :black | ||
end | ||
|
||
private | ||
|
||
def score_for(color) | ||
@gtp.final_score[/^#{color}\+([\d\.]+)$/, 1].to_f | ||
end | ||
|
||
def opposite(color) | ||
color == :black ? :white : :black | ||
end | ||
|
||
def point(vertex) | ||
args = [vertex] | ||
if vertex =~ /\A[A-HJ-T](?:1\d|[1-9])\z/ | ||
args << {:board_size => @board_size} | ||
end | ||
Go::GTP::Point.new(*args) | ||
end | ||
|
||
def gnugo_point(vertex) | ||
if %w[PASS RESIGN].include? vertex | ||
vertex | ||
else | ||
point(vertex).to_gnugo(@board_size) | ||
end | ||
end | ||
|
||
def sgf_point(vertex) | ||
if %w[PASS RESIGN].include? vertex | ||
vertex | ||
else | ||
point(vertex).to_sgf | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.