Class: World

Inherits:
Object
  • Object
show all
Defined in:
lib/demigodGame/World.rb

Constant Summary collapse

BUILDABLE =
{:wood_cutter => 'w', :sheep => 's', :mines => 'm', :chapel => 'c', :house => 'h', :dragon => 'd'}
DECISIONS =
{:build => {:w => 'wood_cutter', :s => 'sheep', :m => 'mines', :c => 'chapel', :h => 'house', :d => 'dragon'},
:raise => {:r => 'ridge', :f => 'forest', :p => 'plains'}}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(size = 5) ⇒ World



15
16
17
18
19
20
# File 'lib/demigodGame/World.rb', line 15

def initialize(size = 5)
  @buildings = {:wood_cutter => 0, :sheep => 0, :mines => 0, :chapel => 0, :house => 0, :dragon => 0}
  @size = size

  generate_tiles(size)
end

Instance Attribute Details

#buildingsObject (readonly)

Returns the value of attribute buildings.



13
14
15
# File 'lib/demigodGame/World.rb', line 13

def buildings
  @buildings
end

#sizeObject (readonly)

Returns the value of attribute size.



13
14
15
# File 'lib/demigodGame/World.rb', line 13

def size
  @size
end

#tilesObject (readonly)

Returns the value of attribute tiles.



13
14
15
# File 'lib/demigodGame/World.rb', line 13

def tiles
  @tiles
end

Instance Method Details

#advance(tile, order) ⇒ Object

Advances the world map based on given 1-letter instruction Assumes all given decisions were checked by tile.accept



33
34
35
36
37
38
39
40
41
42
# File 'lib/demigodGame/World.rb', line 33

def advance(tile, order)
  if order.match(/\A[rfp]\Z/) # for raise of Ridge, Forest or Plain

    raise_tile(tile.x, tile.y, DECISIONS[:raise][order.to_sym])

  elsif order.match(/\A[wsmchd]\Z/) # for build of any building

    build_on_tile(tile, DECISIONS[:build][order.to_sym])
  else
    UiHandler.print_error()
  end
end

#get_tile(str) ⇒ Object

returns a specific tile from an str with a space



23
24
25
26
27
28
29
# File 'lib/demigodGame/World.rb', line 23

def get_tile(str)
  str.strip!
  str = str.split(" ")
  x = str[0].to_i - 1
  y = str[1].to_i - 1
  @tiles[x][y]
end

#production(resources) ⇒ Object

Adds to resources by number of buildings. Also checks the only lose condition in the game



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/demigodGame/World.rb', line 45

def production(resources)
  resources[:wood] += @buildings[:wood_cutter]
  resources[:food] += @buildings[:sheep] -resources[:population]
  resources[:iron] += @buildings[:mines]
  resources[:favor] += @buildings[:chapel] * (resources[:population] / 10 + 1)
  resources[:favor] = (resources[:favor] * 0.9).to_i

  if resources[:population] < resources[:food]
    resources[:population] += @buildings[:house]
  else
    puts UiHandler::NOT_ENOUGH_FOOD
    resources[:population] -= resources[:population] - resources[:food]
  end
  UiHandler.print_status(resources, @buildings)

  resources.each do |name, value| # lose condition

    if value < 0
      system "clear" or system "cls"
      UiHandler.print_lost_message
      exit
    end
  end
  resources
end

#valid?(str) ⇒ Boolean



70
71
72
73
74
# File 'lib/demigodGame/World.rb', line 70

def valid? (str)
  str.strip!
  return false if !str.match(/\A\d+\s\d+\Z/)
  true
end