﻿# -*- coding: utf-8 -*-
import random
import json
import os

SAVE_FILE = "konbini_save.json"

class City:
    def __init__(self, name, population):
        self.name = name
        self.population = population
        self.player_stores = 0
        self.rival_stores = 0
        self.personal_stores = random.randint(4, 8)

class Player:
    def __init__(self):
        self.money = 12000000
        self.stores_total = 0
        self.month = 0
        self.product_price = 480

class Rival:
    def __init__(self):
        self.stores_total = 1
        self.aggression = 1  # 後で難易度で調整

def show_status(cities, player, rival):
    print(f"\n=== 第 {player.month} 月 ===")
    print(f"資金: {player.money:,}円   あなたの総店舗: {player.stores_total}   ライバル総店舗: {rival.stores_total}")
    print("都市状況:")
    for i, city in enumerate(cities):
        total = city.player_stores + city.rival_stores + city.personal_stores
        print(f"  {i+1}. {city.name} (人口 {city.population:,}人): あなた {city.player_stores} / ライバル {city.rival_stores} / 個人 {city.personal_stores} (合計 {total})")

def main():
    print("=== 簡易コンビニ経営シミュ Ver2.0 ===")
    
    cities = [City("大都市A", 100000), City("中都市B", 50000), City("小都市C", 30000), City("町D", 20000), City("村E", 10000)]
    player = Player()
    rival = Rival()
    
    # 初期配置
    cities[0].player_stores = 1
    player.stores_total = 1
    cities[1].rival_stores = 1
    
    while player.stores_total < 30:
        show_status(cities, player, rival)
        
        print("\n行動を選択:")
        print("1. 新規出店")
        print("2. 商品価格変更")
        print("3. 情報確認")
        print("4. ターン終了")
        print("5. セーブ")
        choice = input("選択 (1-5): ").strip()
        
        if choice == "1":
            try:
                idx = int(input("出店する都市番号 (1-5): ")) - 1
                if 0 <= idx < len(cities):
                    city = cities[idx]
                    land_cost = 400000 + (city.player_stores * 150000)
                    build_cost = 600000
                    total_cost = land_cost + build_cost
                    print(f"費用: 土地 {land_cost:,}円 + 建設 {build_cost:,}円 = 合計 {total_cost:,}円")
                    if input("出店しますか？ (y/n): ").lower() == "y":
                        if player.money >= total_cost:
                            player.money -= total_cost
                            city.player_stores += 1
                            player.stores_total += 1
                            print("出店完了！")
                        else:
                            print("資金不足！")
                    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}円 に変更")
            except:
                print("入力エラー")
        
        elif choice == "3":
            show_status(cities, player, rival)
            continue
        elif choice == "4":
            player.month += 1
            # 売上計算（簡易）
            revenue = 0
            for city in cities:
                total = city.player_stores + city.rival_stores + city.personal_stores
                if total == 0 or city.player_stores == 0: continue
                per = city.population / total
                base = per * (player.product_price / 480) * 10
                scale = (city.player_stores ** 1.7) / max(1, city.player_stores)
                revenue += int(base * city.player_stores * scale)
            
            fixed = 1200000 + player.stores_total * 60000
            player.money += revenue - fixed
            print(f"売上 {revenue:,}円  経費 {fixed:,}円")
            
            # 簡易AI & 個人閉店
            if random.random() < 0.45:
                target = random.choice(cities)
                if target.player_stores + target.rival_stores < 10:
                    target.rival_stores += 1
                    rival.stores_total += 1
            for city in cities:
                if city.personal_stores > 0 and random.random() < 0.3:
                    city.personal_stores -= 1
        elif choice == "5":
            # 簡易セーブ
            data = {"month": player.month, "money": player.money, "player_stores": player.stores_total}
            with open(SAVE_FILE, "w", encoding="utf-8") as f:
                json.dump(data, f, ensure_ascii=False)
            print("セーブ完了")
            continue
        else:
            print("無効")
            continue
    
    print("ゲーム終了")

if __name__ == "__main__":
    main()