Class catan.logic.HexProduction
A data structure for resource production of hexes.
Methods
catan.logic.hexproduction:new () | Create an empty hex production. |
catan.logic.hexproduction:get (player, res) | Get the number of resources produced by player. |
catan.logic.hexproduction:set (player, res, n) | Set the number of resources produced by player. |
catan.logic.hexproduction:add (player, res, n) | Add n to the number of resources produced by player. |
catan.logic.hexproduction:iter (f) | Iterate through all the associations. |
Methods
- catan.logic.hexproduction:new ()
-
Create an empty hex production.
Returns:
- catan.logic.hexproduction:get (player, res)
-
Get the number of resources produced by player.
Parameters:
Returns:
-
number
the number of resources
Usage:
local hexprod = HexProduction:new() local player = "red" local res = "grain" print(hexprod:get(player, res)) --> 0 hexprod:add(player, res, 3) print(hexprod:get(player, res)) --> 3
- catan.logic.hexproduction:set (player, res, n)
-
Set the number of resources produced by player.
Parameters:
Usage:
local hexprod = HexProduction:new() local player = "red" local res = "grain" print(hexprod:get(player, res)) --> 0 hexprod:add(player, res, 3) print(hexprod:get(player, res)) --> 3 hexprod:set(player, res, 1) print(hexprod:get(player, res)) --> 1 hexprod:set(player, res, nil) print(hexprod:get(player, res)) --> 0
- catan.logic.hexproduction:add (player, res, n)
-
Add
n
to the number of resources produced by player. You can also decrease such number by passing a negative value forn
.Parameters:
Usage:
local hexprod = HexProduction:new() local player = "red" local res = "grain" print(hexprod:get(player, res)) --> 0 hexprod:add(player, res, 3) print(hexprod:get(player, res)) --> 3
- catan.logic.hexproduction:iter (f)
-
Iterate through all the associations.
Parameters:
- f
function
the iterator that will be called
for each association, receiving as parameter the
values
player
,res
andn
. Be mindful thatn
can have the value zero, and that the iteration is not exaustive for all player-resource combinations. If this function returns a value different fromnil
orfalse
, iteration is interrupted and this value is returned immediately.
Usage:
local haystack = HexProduction:new() -- ... local key = haystack:iter(function (player, res, n) if n == 5 then return {player = player, res = res} end end) if key ~= nil then print(haystack:get(key.player, key.res)) --> 5 end
- f
function
the iterator that will be called
for each association, receiving as parameter the
values