Class: JustGo::GameState
- Inherits:
-
Object
- Object
- JustGo::GameState
- Defined in:
- lib/just_go/game_state.rb
Overview
Game State
Represents a game of Go in progress.
Constant Summary collapse
- BOARD_SIZE =
19
Instance Attribute Summary collapse
-
#current_player_number ⇒ Object
readonly
Returns the value of attribute current_player_number.
-
#errors ⇒ Object
readonly
Returns the value of attribute errors.
-
#last_change ⇒ Object
readonly
Returns the value of attribute last_change.
-
#player_stats ⇒ Object
readonly
Returns the value of attribute player_stats.
-
#points ⇒ Object
readonly
Returns the value of attribute points.
-
#previous_state ⇒ Object
readonly
Returns the value of attribute previous_state.
Class Method Summary collapse
Instance Method Summary collapse
- #as_json ⇒ Object
-
#initialize(current_player_number:, points:, previous_state: nil, player_stats: []) ⇒ GameState
constructor
A new instance of GameState.
- #move(player_number, point_id) ⇒ Object
- #pass(player_number) ⇒ Object
- #score ⇒ Object
- #winner ⇒ Object
Constructor Details
#initialize(current_player_number:, points:, previous_state: nil, player_stats: []) ⇒ GameState
Returns a new instance of GameState.
17 18 19 20 21 22 23 24 |
# File 'lib/just_go/game_state.rb', line 17 def initialize(current_player_number: , points: , previous_state: nil, player_stats: []) @current_player_number = current_player_number @points = JustGo::PointSet.new(points: points) @previous_state = previous_state @player_stats = player_stats.map { |ps| JustGo::PlayerStat.new(**ps) } @errors = [] @last_change = {} end |
Instance Attribute Details
#current_player_number ⇒ Object (readonly)
Returns the value of attribute current_player_number.
26 27 28 |
# File 'lib/just_go/game_state.rb', line 26 def current_player_number @current_player_number end |
#errors ⇒ Object (readonly)
Returns the value of attribute errors.
30 31 32 |
# File 'lib/just_go/game_state.rb', line 30 def errors @errors end |
#last_change ⇒ Object (readonly)
Returns the value of attribute last_change.
31 32 33 |
# File 'lib/just_go/game_state.rb', line 31 def last_change @last_change end |
#player_stats ⇒ Object (readonly)
Returns the value of attribute player_stats.
29 30 31 |
# File 'lib/just_go/game_state.rb', line 29 def player_stats @player_stats end |
#points ⇒ Object (readonly)
Returns the value of attribute points.
27 28 29 |
# File 'lib/just_go/game_state.rb', line 27 def points @points end |
#previous_state ⇒ Object (readonly)
Returns the value of attribute previous_state.
28 29 30 |
# File 'lib/just_go/game_state.rb', line 28 def previous_state @previous_state end |
Class Method Details
.default ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/just_go/game_state.rb', line 33 def self.default self.new( current_player_number: 1, points: BOARD_SIZE.times.map do |row| BOARD_SIZE.times.map do |col| { id: row*BOARD_SIZE + col, x: col, y: row, stone: nil } end end.flatten, previous_state: nil, player_stats: [ { player_number: 1, prisoner_count: 0, passed: false }, { player_number: 2, prisoner_count: 0, passed: false } ] ) end |
Instance Method Details
#as_json ⇒ Object
54 55 56 57 58 59 60 61 |
# File 'lib/just_go/game_state.rb', line 54 def as_json { current_player_number: current_player_number, points: points.as_json, previous_state: previous_state, player_stats: player_stats.map(&:as_json) } end |
#move(player_number, point_id) ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/just_go/game_state.rb', line 63 def move(player_number, point_id) @errors = [] point = points.find_by_id(point_id.to_i) if current_player_number != player_number @errors.push JustGo::NotPlayersTurnError.new elsif point.nil? @errors.push JustGo::PointNotFoundError.new elsif point.occupied? @errors.push JustGo::PointNotEmptyError.new elsif points.liberties_for(point).zero? && points.deprives_liberties?(point, player_number) && !points.deprives_opponents_liberties?(point, player_number) @errors.push JustGo::NoLibertiesError.new else dupped = points.dup dupped.perform_move(point, player_number) if dupped.minify == @previous_state @errors.push JustGo::KoRuleViolationError.new else @player_stats.detect { |p| p.player_number == next_player_number }.mark_as_continuing @previous_state = points.minify stone_count = points.perform_move(point, player_number) @player_stats.detect { |pc| pc.player_number == player_number }.add_to_prisoner_count(stone_count) pass_turn end end errors.empty? end |
#pass(player_number) ⇒ Object
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/just_go/game_state.rb', line 98 def pass(player_number) if current_player_number != player_number @errors.push JustGo::NotPlayersTurnError.new else @player_stats.detect { |ps| ps.player_number == player_number }.mark_as_passed next_player_passed = @player_stats.detect { |ps| ps.player_number == next_player_number }.passed if next_player_passed points.mark_territories else pass_turn end end errors.empty? end |
#score ⇒ Object
114 115 116 117 118 119 |
# File 'lib/just_go/game_state.rb', line 114 def score [ { player_number: 1, score: player_score(1) }, { player_number: 2, score: player_score(2) } ] end |
#winner ⇒ Object
121 122 123 124 125 126 127 |
# File 'lib/just_go/game_state.rb', line 121 def winner if @player_stats.map { |ps| ps.passed }.all? score.max_by { |line| line[:score] }[:player_number] else nil end end |