Class: Dat::Games

Inherits:
Object
  • Object
show all
Defined in:
lib/dat/games.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(logger) ⇒ Games

Returns a new instance of Games.



13
14
15
16
17
# File 'lib/dat/games.rb', line 13

def initialize(logger)
  @games = {}
  @dict = Dict.new
  @logger = logger
end

Instance Attribute Details

#dictObject (readonly)

Returns the value of attribute dict.



11
12
13
# File 'lib/dat/games.rb', line 11

def dict
  @dict
end

Instance Method Details

#[](gid) ⇒ Object



25
26
27
28
29
# File 'lib/dat/games.rb', line 25

def [](gid)
  @games.fetch(gid)
rescue KeyError
  raise NoGameError, "Game does not exist"
end

#add(gid, opt = {}) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/dat/games.rb', line 31

def add(gid, opt={})
  opt.merge(:dict => @dict)

  game = Game.new(@logger.create(gid),  opt)

  opt[:players].each do |p|
    if p.respond_to?(:bot?) && p.bot?
      p.init(game)
    end
  end

  @games[gid] = game
  @games[gid].next_move!
end

#check!(gid) ⇒ Object



19
20
21
22
23
# File 'lib/dat/games.rb', line 19

def check!(gid)
  if @games[gid] && @games[gid].won
    raise NoGameError, "Game has been won."
  end
end