0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

農業はReplace()されました 開発記録 -01

0
Posted at

芝/木/にんじん/かぼちゃ/ひまわり の栽培自動化したコード

image.png

[エリア:12 × 12]

設計

エリア分け

  • [0:2] 木材+芝エリア(0~2列目)
  • [3]ひまわりエリア
  • [4:7] にんじんエリア
  • [8:11] かぼちゃエリア

事前準備

  • worldサイズを取得
  • 3列目以降を耕す

木 + 芝

  • 市松模様になるように植林
    • 偶数列は偶数行に、奇数列は奇数行に植林
    • それ以外は芝生

ひまわり

配列を組んでもっと最適化できるはずだが、sort も sorted もなくて諦めた・・・

  • 全マスを確認してすべて成熟しているか確認
  • ひまわりの花びらが多い順に収穫(脳筋プレー💪)
    • 花びらが15枚のものを収穫
    • 花びらが14枚のものを収穫
    • 花びらが13枚のものを収穫
    • 花びらが・・・(以下略:7枚まで繰り返す)
  • ひまわりを植える
  • 水分量が 0.5 を下回っていたら水やり

にんじん

  • 作物が成熟していたら収穫
    • 水分量が 0.5 を下回っていたら水やり
    • にんじんを植える

かぼちゃ

にんじんとほぼ同じ

肥料について・・・

なくても作物が育つので使っていない

コード

w = get_world_size()


# ========================================
# 任意の座標へ移動
# ========================================

def move_to(target_x, target_y):
    while get_pos_x() != target_x:
        if get_pos_x() < target_x:
            move(East)
        else:
            move(West)

    while get_pos_y() != target_y:
        if get_pos_y() < target_y:
            move(North)
        else:
            move(South)


# ========================================
# 初期準備
#
# 0~2列 : 木材・芝
# 3列    : ひまわり
# 4~7列 : にんじん
# 8~11列: カボチャ
# ========================================

def prepare():
    clear()

    # 作物エリアを耕す
    for x in range(3, 12):
        for y in range(w):
            move_to(x, y)

            if get_ground_type() != Grounds.Soil:
                till()


# ========================================
# 0~2列
# 木材 + 芝
# ========================================

def farm_wood():

    for x in range(3):
        for y in range(w):

            move_to(x, y)

            # 市松模様に木を植える
            if (x + y) % 2 == 0:

                # 木がある
                if get_entity_type() == Entities.Tree:

                    if can_harvest():
                        harvest()
                        plant(Entities.Tree)

                # 木がない
                else:

                    if can_harvest():
                        harvest()

                    plant(Entities.Tree)

            # 木を植えない場所は芝
            else:

                if can_harvest():
                    harvest()


# ========================================
# 3列
# ひまわり
# ========================================
def farm_sunflower():

    all_ready = True

    # ========================================
    # まず全マス確認
    # 無ければ植える
    # 全部成熟しているか確認
    # ========================================

    for y in range(w):

        move_to(3, y)

        if get_entity_type() != Entities.Sunflower:

            if can_harvest():
                harvest()

            plant(Entities.Sunflower)
            all_ready = False

        elif not can_harvest():
            all_ready = False

        if get_water() < 0.5:
            use_item(Items.Water)

    # まだ1本でも未成熟なら今回は収穫しない
    if not all_ready:
        return


    # ========================================
    # 花びら15枚 → 7枚の順番で収穫
    # ========================================

    petals = 15

    while petals >= 7:

        for y in range(w):

            move_to(3, y)

            if get_entity_type() == Entities.Sunflower:

                if measure() == petals:

                    if can_harvest():
                        harvest()

        petals = petals - 1


    # ========================================
    # 全部収穫後、新しいひまわりを植える
    # ========================================

    for y in range(w):

        move_to(3, y)

        plant(Entities.Sunflower)

        if get_water() < 0.5:
            use_item(Items.Water)

# ========================================
# 4~7列
# にんじん
# ========================================

def farm_carrots():

    for x in range(4, 8):

        for y in range(w):

            move_to(x, y)

            # にんじんがある
            if get_entity_type() == Entities.Carrot:

                # 育っていたら収穫して植え直す
                if can_harvest():
                    harvest()
                    plant(Entities.Carrot)

            # にんじんがない
            else:

                if can_harvest():
                    harvest()

                plant(Entities.Carrot)

            # 水だけ
            # 肥料は使わない
            if get_water() < 0.5:
                use_item(Items.Water)


# ========================================
# 8~11列
# カボチャ
# ========================================

def farm_pumpkins():

    for x in range(8, 12):

        for y in range(w):

            move_to(x, y)

            # カボチャがある
            if get_entity_type() == Entities.Pumpkin:

                # 成熟していたら収穫して植え直す
                if can_harvest():
                    harvest()
                    plant(Entities.Pumpkin)

            # カボチャがない / 枯れている
            else:

                plant(Entities.Pumpkin)

            # 水分が少なければ水やり
            if get_water() < 0.5:
                use_item(Items.Water)


# ========================================
# メイン
# ========================================

def main():

    prepare()

    while True:

        # 0~2列
        farm_wood()

        # 3列
        farm_sunflower()

        # 4~7列
        farm_carrots()

        # 8~11列
        farm_pumpkins()


main()
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?