﻿# -*- coding: utf-8 -*-
import random
import sys

# Windows対策
if sys.platform == "win32":
    import codecs
    sys.stdout = codecs.getwriter('utf-8')(sys.stdout.buffer)

class City:
    def __init__(self, name, population):
        self.name = name
        self.population = population
        self.player_stores = 0
        self.competition = random.randint(3, 8)

class Player:
    def __init__(self):
        self.money = 8000000
        self.stores_total = 0
        self.month = 0
        self.product_price = 450

def show_status(cities, player):
    print(f"\n=== 現在の状況 (第 {player.month} 月) ===")
    print(f"資金: {player.money:,}円   総店舗数: {player.stores_total}   商品価格: {player.product_price}円")
    print("都市状況:")
    for i, city in enumerate(cities):
        total = city.player_stores + city.competition
        print(f"  {i+1}. {city.name} (人口 {city.population:,}人): あなたの店舗 {city.player_stores} / 競合 {city.competition} (合計 {total})")

def main():
    print("=== 簡易コマンド型コンビニ経営シミュ Ver1.3 ===")
    print("目標: 総店舗数30達成\n")
    
    cities = [
        City("大都市A", 100000),
        City("中都市B", 50000),
        City("小都市C", 30000),
        City("町D", 20000),
        City("村E", 10000)
    ]
    
    player = Player()
    
    while player.stores_total < 30:
        print(f"\n--- 第 {player.month} 月 ---")
        show_status(cities, player)
        
        print("\n行動を選択:")
        print("1. 新規出店")
        print("2. 商品価格変更")
        print("3. 情報確認（ターン消費なし）")
        print("4. ターン終了")
        choice = input("選択 (1-4): ").strip()
        
        if choice == "1":
            try:
                idx = int(input("出店する都市番号 (1-5): ")) - 1
                if 0 <= idx < len(cities):
                    city = cities[idx]
                    cost = 800000 + (city.player_stores * 350000)
                    if player.money >= cost:
                        player.money -= cost
                        city.player_stores += 1
                        player.stores_total += 1
                        print(f"? {city.name} に出店成功！")
                    else:
                        print("資金不足！")
                else:
                    print("無効な番号")
            except:
                print("入力エラー")
        
        elif choice == "2":
            try:
                new_price = int(input("新しい商品価格 (300-800円): "))
                if 300 <= new_price <= 800:
                    player.product_price = new_price
                    print(f"価格を {new_price}円 に設定")
                else:
                    print("範囲外です")
            except:
                print("入力エラー")
        
        elif choice == "3":
            show_status(cities, player)
            continue   # ← ここでターン消費をスキップ
        
        elif choice == "4":
            player.month += 1   # ターン終了時のみ月が進む
            # 経済計算
            total_revenue = 0
            for city in cities:
                total_stores = city.player_stores + city.competition
                if total_stores == 0:
                    continue
                per_store_pop = city.population / total_stores
                base = per_store_pop * (player.product_price / 450) * 11
                scale = (city.player_stores ** 1.7) / max(1, city.player_stores) if city.player_stores > 0 else 0
                revenue = int(base * city.player_stores * scale)
                total_revenue += revenue
            
            fixed_cost = 1200000 + (player.stores_total * 60000)
            player.money += total_revenue - fixed_cost
            
            print(f"\n今月の売上: {total_revenue:,}円  経費: {fixed_cost:,}円")
            
            if random.random() < 0.25:
                ev = random.choice(["好調！+150万", "不況！-80万", "競合増加"])
                if "好調" in ev: player.money += 1500000
                elif "不況" in ev: player.money -= 800000
                else: random.choice(cities).competition += 1
                print("イベント:", ev)
            
            if player.money < -2500000:
                print("\n資金ショート... ゲームオーバー")
                break
        else:
            print("無効な選択")
            continue
    
    if player.stores_total >= 30:
        print(f"\n勝利！ {player.month}ヶ月で30店舗達成！")

if __name__ == "__main__":
    main()