2026/2/28 3:38:46
网站建设
项目流程
中国最好的网站建设有哪些,优速网站建设,自学网站建设买什么书,王占山将军是什么军衔我将为您开发一个功能完整的AI猜拳游戏系统#xff0c;包含多种博弈策略和统计分析功能。项目结构ai_rock_paper_scissors/├── main.py # 主程序入口├── game_engine.py # 游戏引擎模块├── ai_strategies.py # AI策略模块├── statistics.py # 统计分析模块├── …我将为您开发一个功能完整的AI猜拳游戏系统包含多种博弈策略和统计分析功能。项目结构ai_rock_paper_scissors/├── main.py # 主程序入口├── game_engine.py # 游戏引擎模块├── ai_strategies.py # AI策略模块├── statistics.py # 统计分析模块├── player.py # 玩家类模块├── utils.py # 工具函数模块├── data_manager.py # 数据管理模块├── config.py # 配置文件├── requirements.txt # 依赖包列表├── README.md # 项目说明├── knowledge_cards.md # 核心知识点卡片└── data/ # 数据存储目录├── game_history.json└── player_stats.json1. requirements.txtpandas1.3.0numpy1.21.0matplotlib3.5.0seaborn0.11.0tqdm4.64.0collections-extended2.0.02. config.py - 配置文件AI猜拳游戏配置文件包含游戏参数、AI策略配置、界面设置等# 游戏基础配置GAME_CONFIG {choices: [石头, 剪刀, 布],choice_symbols: {石头: ✊, 剪刀: ✌️, 布: ✋},win_rules: {石头: 剪刀, # 石头赢剪刀剪刀: 布, # 剪刀赢布布: 石头 # 布赢石头},rounds_default: 10,save_history: True,show_animation: True}# AI策略配置AI_STRATEGIES {random: {name: 随机策略,description: 完全随机选择作为基准策略,weight: 1.0},frequency: {name: 频率分析策略,description: 分析对手历史选择频率预测并克制,weight: 1.2,learning_rate: 0.1},pattern: {name: 模式识别策略,description: 识别对手的选择模式预测下一步,weight: 1.3,pattern_length: 3},counter: {name: 反击策略,description: 针对对手的优势选择进行反击,weight: 1.1},mixed: {name: 混合策略,description: 结合多种策略动态调整权重,weight: 1.4,strategies: [frequency, pattern, counter]}}# 统计分析配置STATS_CONFIG {track_detailed: True,save_interval: 10, # 每多少局保存一次数据charts_enabled: True,export_formats: [json, csv, txt]}# 难度等级配置DIFFICULTY_LEVELS {easy: {name: 简单,ai_strategy: random,win_rate_target: 0.3},normal: {name: 普通,ai_strategy: frequency,win_rate_target: 0.5},hard: {name: 困难,ai_strategy: mixed,win_rate_target: 0.7},expert: {name: 专家,ai_strategy: pattern,win_rate_target: 0.8}}3. utils.py - 工具函数模块工具函数模块提供游戏所需的各种辅助函数import randomimport timeimport jsonfrom typing import List, Dict, Tuple, Optionalfrom collections import Counter, defaultdictfrom datetime import datetimeimport numpy as npclass GameUtils:游戏工具类staticmethoddef get_choice_index(choice: str) - int:获取选择对应的索引Args:choice: 选择名称 (石头, 剪刀, 布)Returns:对应的索引 (0, 1, 2)choices [石头, 剪刀, 布]try:return choices.index(choice)except ValueError:raise ValueError(f无效选择: {choice}有效选择为: {choices})staticmethoddef get_choice_name(index: int) - str:获取索引对应的选择名称Args:index: 索引 (0, 1, 2)Returns:选择名称choices [石头, 剪刀, 布]if 0 index len(choices):return choices[index]else:raise ValueError(f无效索引: {index})staticmethoddef determine_winner(player_choice: str, ai_choice: str,win_rules: Dict[str, str]) - str:判断胜负Args:player_choice: 玩家选择ai_choice: AI选择win_rules: 胜负规则Returns:player: 玩家获胜, ai: AI获胜, tie: 平局if player_choice ai_choice:return tie# 检查玩家是否获胜if win_rules[player_choice] ai_choice:return playerelse:return aistaticmethoddef calculate_win_rate(wins: int, total_games: int) - float:计算胜率Args:wins: 获胜次数total_games: 总游戏次数Returns:胜率 (0.0-1.0)if total_games 0:return 0.0return wins / total_gamesstaticmethoddef format_percentage(value: float) - str:格式化百分比显示Args:value: 小数形式的比例Returns:格式化的百分比字符串return f{value:.1%}staticmethoddef animate_text(text: str, delay: float 0.05):文字动画效果Args:text: 要显示的文本delay: 字符间延迟时间for char in text:print(char, end, flushTrue)time.sleep(delay)print()class DataUtils:数据处理工具类staticmethoddef save_json(data: Dict, filename: str):保存数据到JSON文件Args:data: 要保存的数据filename: 文件名try:with open(filename, w, encodingutf-8) as f:json.dump(data, f, ensure_asciiFalse, indent2)except Exception as e:print(f保存文件失败: {e})staticmethoddef load_json(filename: str) - Dict:从JSON文件加载数据Args:filename: 文件名Returns:加载的数据字典try:with open(filename, r, encodingutf-8) as f:return json.load(f)except FileNotFoundError:return {}except Exception as e:print(f加载文件失败: {e})return {}staticmethoddef calculate_trend(values: List[float], window_size: int 5) - float:计算数值趋势Args:values: 数值列表window_size: 滑动窗口大小Returns:趋势值 (-1到1负值表示下降趋势正值表示上升趋势)if len(values) window_size:return 0.0recent_values values[-window_size:]x np.arange(len(recent_values))y np.array(recent_values)# 计算线性回归斜率slope np.polyfit(x, y, 1)[0]# 归一化到[-1, 1]范围normalized_slope max(-1.0, min(1.0, slope * 10))return normalized_slopeclass StrategyUtils:策略工具类staticmethoddef detect_pattern(sequence: List[str], pattern_length: int 3) - Optional[str]:检测序列中的模式Args:sequence: 选择序列pattern_length: 模式长度Returns:预测的下一项如果没有检测到模式则返回Noneif len(sequence) pattern_length * 2:return None# 查找最近的模式recent_pattern sequence[-pattern_length:]pattern_str .join(recent_pattern)# 在历史中查找相同模式后的选择occurrences []for i in range(len(sequence) - pattern_length * 2):if sequence[i:ipattern_length] recent_pattern:next_choice sequence[i pattern_length]occurrences.append(next_choice)if occurrences:# 返回最常见的后续选择counter Counter(occurrences)return counter.most_common(1)[0][0]return Nonestaticmethoddef calculate_frequency_distribution(sequence: List[str]) - Dict[str, float]:计算频率分布Args:sequence: 选择序列Returns:各选择的频率字典if not sequence:return {石头: 0.33, 剪刀: 0.33, 布: 0.34}counter Counter(sequence)total len(sequence)return {choice: count / totalfor choice, count in counter.items()}staticmethoddef get_counter_choice(target_choice: str) - str:获取克制指定选择的选择Args:target_choice: 目标选择Returns:能克制目标选择的选择counter_map {石头: 布, # 布克石头剪刀: 石头, # 石头克剪刀布: 剪刀 # 剪刀克布}return counter_map.get(target_choice, 石头)class ValidationUtils:数据验证工具类staticmethoddef validate_player_choice(choice: str) - bool:验证玩家选择是否有效Args:choice: 玩家选择Returns:是否有效valid_choices [石头, 剪刀, 布, 1, 2, 3]return choice in valid_choicesstaticmethoddef normalize_choice(choice: str) - str:标准化选择输入Args:choice: 原始输入Returns:标准化后的选择choice_map {1: 石头,2: 剪刀,3: 布,石头: 石头,剪刀: 剪刀,布: 布}return choice_map.get(choice, choice)staticmethoddef validate_game_config(config: Dict) - bool:验证游戏配置是否有效Args:config: 配置字典Returns:是否有效required_keys [choices, win_rules, rounds_default]return all(key in config for key in required_keys)4. player.py - 玩家类模块玩家类模块定义玩家和AI的行为和属性from typing import List, Dict, Optionalfrom dataclasses import dataclass, fieldfrom utils import GameUtils, StrategyUtils, ValidationUtilsdataclassclass Player:玩家基类name: strplayer_type: str human # human 或 aistats: Dict[str, int] field(default_factorylambda: {wins: 0, losses: 0, ties: 0, total_games: 0})choice_history: List[str] field(default_factorylist)win_rate_history: List[float] field(default_factorylist)def make_choice(self, **kwargs) - str:做出选择由子类实现Args:**kwargs: 额外参数Returns:选择结果raise NotImplementedError(子类必须实现此方法)def update_stats(self, result: str):更新统计数据Args:result: 游戏结果 (win, lose, tie)self.stats[total_games] 1if result win:self.stats[wins] 1elif result lose:self.stats[losses] 1elif result tie:self.stats[ties] 1# 更新胜率历史current_win_rate GameUtils.calculate_win_rate(self.stats[wins], self.stats[total_games])self.win_rate_history.append(current_win_rate)def get_current_win_rate(self) - float:获取当前胜率Returns:当前胜率return GameUtils.calculate_win_rate(self.stats[wins], self.stats[total_games])def get_choice_frequency(self) - Dict[str, float]:获取选择频率Returns:各选择的频率return StrategyUtils.calculate_frequency_distribution(self.choice_history)def add_choice_to_history(self, choice: str):添加选择到历史记录Args:choice: 选择结果self.choice_history.append(choice)# 限制历史记录长度避免内存占用过多max_history 1000if len(self.choice_history) max_history:self.choice_history self.choice_history[-max_history:]class HumanPlayer(Player):人类玩家类def __init__(self, name: str 玩家):super().__init__(namename, player_typehuman)def make_choice(self, **kwargs) - str:人类玩家做出选择Args:**kwargs: 额外参数包括可用选择列表Returns:选择结果available_choices kwargs.get(choices, [石头, 剪刀, 布])while True:try:print(f\n{self.name}请选择)for i, choice in enumerate(available_choices, 1):print(f {i}. {choice})user_input input(\n请输入选择 (1-3 或 石头/剪刀/布): ).strip()if ValidationUtils.validate_player_choice(user_input):choice ValidationUtils.normalize_choice(user_input)# 验证选择是否在可用选项中if choice in available_choices:self.add_choice_to_history(choice)return choiceelse:print(❌ 无效选择请重试)else:print(❌ 输入格式错误请重试)except KeyboardInterrupt:print(\n\n游戏被用户中断)raiseexcept Exception as e:print(f❌ 发生错误: {e}请重试)class AIPlayer(Player):AI玩家类def __init__(self, name: str AI, strategy: str random):super().__init__(namename, player_typeai)self.strategy strategyself.strategy_weights kwargs.get(strategy_weights, {})self.opponent_history []self.pattern_memory defaultdict(list)def make_choice(self, **kwargs) - str:AI根据策略做出选择Args:**kwargs: 额外参数包括对手历史、游戏规则等Returns:选择结果opponent_history kwargs.get(opponent_history, [])available_choices kwargs.get(choices, [石头, 剪刀, 布])win_rules kwargs.get(win_rules, {})# 根据策略选择if self.strategy random:choice self._random_strategy(available_choices)elif self.strategy frequency:choice self._frequency_strategy(opponent_history, available_choices)elif self.strategy pattern:choice self._pattern_strategy(opponent_history, available_choices)elif self.strategy counter:choice self._counter_strategy(opponent_history, available_choices)elif self.strategy mixed:choice self._mixed_strategy(opponent_history, available_choices, kwargs)else:choice self._random_strategy(available_choices)self.add_choice_to_history(choice)return choicedef _random_strategy(self, available_choices: List[str]) - str:随机策略return random.choice(available_choices)def _frequency_strategy(self, opponent_history: List[str],available_choices: List[str]) - str:频率分析策略分析对手历史选择预测并克制最常出现的选择if not opponent_history:return self._random_strategy(available_choices)# 计算对手选择频率freq_dist StrategyUtils.calculate_frequency_distribution(opponent_history)# 找出对手最常选择的选项most_frequent max(freq_dist.items(), keylambda x: x[1])[0]# 选择能克制对手最常选择项的选项counter_choice StrategyUtils.get_counter_choice(most_frequent)# 确保选择可用if counter_choice in available_choices:return counter_choiceelse:return self._random_strategy(available_choices)def _pattern_strategy(self, opponent_history: List[str],available_choices: List[str]) - str:模式识别策略识别对手的选择模式预测下一步if len(opponent_history) 6: # 需要足够的历史数据return self._frequency_strategy(opponent_history, available_choices)# 尝试检测模式predicted_choice StrategyUtils.detect_pattern(opponent_history, pattern_length3)if predicted_choice:# 如果检测到模式选择克制预测选择的选项counter_choice StrategyUtils.get_counter_choice(predicted_choice)if counter_choice in available_choices:return counter_choice# 如果没检测到模式回退到频率策略return self._frequency_strategy(opponent_history, available_choices)def _counter_strategy(self, opponent_history: List[str],available_choices: List[str]) - str:反击策略针对对手的优势选择进行反击if not opponent_history:return self._random_strategy(available_choices)# 分析对手的优势即经常获胜的选择# 这里简化处理直接找对手最近的选择趋势recent_choices opponent_history[-5:] if len(opponent_history) 5 else opponent_historyif recent_choices:# 选择克制对手最近选择的选项last_choice recent_choices[-1]counter_choice StrategyUtils.get_counter_choice(last_choice)if counter_choice in available_choices:return counter_choicereturn self._random_strategy(available_choices)def _mixed_strategy(self, opponent_history: List[str],available_choices: List[str],kwargs: Dict) - str:混合策略结合多种策略动态调整权重strategies [frequency, pattern, counter]weights [0.4, 0.4, 0.2] # 默认权重# 根据历史数据调整权重if len(opponent_history) 10:# 如果有足够数据增加模式识别的权重weights [0.3, 0.5, 0.2]elif len(opponent_history) 5:# 如果数据较少主要使用频率和反击策略weights [0.5, 0.2, 0.3]# 随机选择策略根据权重chosen_strategy random.choices(strategies, weightsweights)[0]# 调用对应策略if chosen_strategy frequency:return self._frequency_strategy(opponent_history, available_choices)elif chosen_strategy pattern:return self._pattern_strategy(opponent_history, available_choices)elif chosen_strategy counter:return self._counter_strategy(opponent_history, available_choices)else:return self._random_strategy(available_choices)def update_opponent_history(self, opponent_choice: str):更新对手历史记录Args:opponent_choice: 对手的选择self.opponent_history.append(opponent_choice)# 限制历史记录长度max_history 500if len(self.opponent_history) max_history:self.opponent_history self.opponent_history[-max_history:]def set_strategy(self, strategy: str):设置AI策略Args:strategy: 策略名称self.strategy strategydef get_strategy_description(self) - str:获取策略描述Returns:策略描述from config import AI_STRATEGIESstrategy_info AI_STRATEGIES.get(self.strategy, {})return strategy_info.get(description, 未知策略)5. ai_strategies.py - AI策略模块AI策略模块实现各种高级AI策略算法import numpy as npfrom typing import List, Dict, Tuple, Optionalfrom collections import defaultdict, dequefrom utils import StrategyUtils, GameUtilsimport randomclass AdvancedAIStrategies:高级AI策略类def __init__(self):self.strategy_performance defaultdict(list)self.adaptation_threshold 0.1def markov_chain_strategy(self, history: List[str], order: int 1) - Optional[str]:马尔可夫链策略基于状态转移概率预测对手下一步Args:history: 历史选择序列order: 马尔可夫链阶数Returns:预测的下一项if len(history) order 1:return None# 构建状态转移矩阵transitions defaul关注我有更多实用程序等着你