Class: BareGato::Game

Inherits:
Object
  • Object
show all
Defined in:
lib/bare_gato/game.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ Game

Returns a new instance of Game.



5
6
7
8
9
10
11
12
# File 'lib/bare_gato/game.rb', line 5

def initialize args
  @grid = [
    [nil, nil, nil],
    [nil, nil, nil],
    [nil, nil, nil],
  ]
  @players = args[:players]
end

Instance Attribute Details

#strategiesObject

Returns the value of attribute strategies.



3
4
5
# File 'lib/bare_gato/game.rb', line 3

def strategies
  @strategies
end

Instance Method Details

#got_a_winner?Boolean

Returns:

  • (Boolean)


18
19
20
21
22
23
# File 'lib/bare_gato/game.rb', line 18

def got_a_winner?
  !!strategies.find do |strategy_class|
    strategy = strategy_class.new @grid
    strategy.winner?
  end
end

#includes_player?(player) ⇒ Boolean

Returns:

  • (Boolean)


14
15
16
# File 'lib/bare_gato/game.rb', line 14

def includes_player? player
  @players.include? player
end

#nextObject



25
26
27
# File 'lib/bare_gato/game.rb', line 25

def next
  @players.last
end

#next?(player) ⇒ Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/bare_gato/game.rb', line 29

def next? player
  @players.last == player
end

#play(move) ⇒ Object



33
34
35
36
37
# File 'lib/bare_gato/game.rb', line 33

def play move
  raise "Unexpected player moving" unless next? move.player
  @grid[move.y][move.x] = move.player
  @players.reverse!
end

#statusObject



39
40
41
42
43
44
# File 'lib/bare_gato/game.rb', line 39

def status
  {
    game: @grid,
    next: self.next
  }
end