【農家は Replace() されました】第11回 リスト|ギャルから学ぶドローン農業【超初心者向け攻略】

当記事はChatGPTが出力した文章を使用しています。

 

このゲームの関連記事

www.himajin-block30.com

 

前回

www.himajin-block30.com

 

リストってなに?

  • 複数の値を1つにまとめて持てる入れ物。順番があるよ。

  • つくる:lanes = [Entities.Carrot, Entities.Bush, None]
    空:empty = []

  • 取り出す:lanes[0](最初は 0 番!)

  • ぜんぶ回す:

    for x in lanes:
        print(x)   # quick_printでもOK
    

よく使う操作(このゲーム目線)

  • append(x) …末尾に追加

  • remove(x) …最初に見つかった x を削除

  • insert(i, x) …i 位置に差し込み

  • pop() / pop(i) …末尾 or i 番目を取り出しつつ削除

  • len(lst) …長さ(要素数)


dirs = [North, East, South, West]
dirs.append(North) で巡回パターンを足す、とかね。


実戦レシピ①:列ごとの作物をリストで決める

1列目=ニンジン、2列目=茂み、3列目=草(植え直しなし)…をコードを書き換えず差し替え可能に。

lanes = [Entities.Carrot, Entities.Bush, None]   # None は「草レーン」
size = get_world_size()
going_right = True
going_up    = True

while True:
	x = get_pos_x()
	y = get_pos_y()
	lane_kind = lanes[x % len(lanes)]   # ← 今の列の役割

	# ===== このマスの仕事 =====
	if lane_kind == Entities.Carrot:
		if can_harvest():
			harvest()
			if get_ground_type() == Grounds.Grassland:
				till()  # 必要なときだけ土にする(トグル事故回避)
			if num_items(Items.Hay) >= 1 and num_items(Items.Wood) >= 1:
				plant(Entities.Carrot)

	elif lane_kind == Entities.Bush:
		if can_harvest():
			harvest()
			plant(Entities.Bush)
		else:
			if get_entity_type() == None:
				plant(Entities.Bush)

	else:  # None → 草レーン
		if can_harvest():
			harvest()

	# ===== 端ガード付きの縦スネーク移動 =====
	if going_up:
		if y == size - 1:
			if going_right:
				if x == size - 1:
					going_right = False
					move(West)
				else:
					move(East)
			else:
				if x == 0:
					going_right = True
					move(East)
				else:
					move(West)
			going_up = False
		else:
			move(North)
	else:
		if y == 0:
			if going_right:
				if x == size - 1:
					going_right = False
					move(West)
				else:
					move(East)
			else:
				if x == 0:
					going_right = True
					move(East)
				else:
					move(West)
			going_up = True
		else:
			move(South)

ここが便利✨

  • 列配分をリスト1行で変更できる:

    • 例)木材不足 → lanes = [Entities.Carrot, Entities.Bush, Entities.Bush]

    • 例)干し草不足 → lanes = [Entities.Carrot, None, None]

  • len(lanes) を使ってるので、何列パターンでも対応できる(4列循環も可)。


実戦レシピ②:方向のパターンをリストで回す

“N→E→S→W”を繰り返したい、など。

dirs = [North, East, South, West]
i = 0
while True:
    move(dirs[i])
    i = (i + 1) % len(dirs)   # 末尾まで行ったら先頭に戻る

実戦レシピ③:しきい値や設定をリストでまとめる

まだ辞書がないから、“位置で意味を決める”のがコツ。

# [必要Hay, 必要Wood, 待ちtick]
cfg = [1, 1, 0]
need_hay = cfg[0]; need_wood = cfg[1]; wait = cfg[2]

if not can_harvest():
    for _ in range(wait):
        do_a_flip()

実戦レシピ④:簡単なキュー/スタック

  • 末尾に積む:queue.append(task)

  • 先頭から取り出す(キューっぽく):task = queue.pop(0)

  • 末尾から取り出す(スタック):task = queue.pop()

例:リカバリすべき座標を溜める、みたいな用途に。


注意ポイント💣(つまずき潰し)

  • インデックスは0始まりlanes[3] は4番目→範囲外に注意。

  • a = b = [1,2] みたいに同じリストを指すと、片方の変更がもう片方にも効く(エイリアス)。
    別物が欲しい時は:

    src = [1,2,3]
    dst = []
    for v in src: dst.append(v)   # 手作業コピー(安全)
    
  • remove(x)最初の1個だけ消える。全部消したいなら for で何度もやる。

  • pop(0) は重め。回数が多いなら、方針を“末尾から”に変えるか運用を見直す。


 

続き

www.himajin-block30.com

 

 

このゲームの関連記事

www.himajin-block30.com