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
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:

  • player string the player
  • res string the kind of resource
  • n optional 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
    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 for n.

Parameters:

  • player string the player
  • res string the kind of resource
  • n number the number of resources to add

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 and n. Be mindful that n can have the value zero, and that the iteration is not exaustive for all player-resource combinations. If this function returns a value different from nil or false, 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
generated by LDoc 1.5.0 Last updated 2023-07-02 05:10:58