当前位置: 首页 > news >正文

c++ 实现狼人游戏

更多技术博客 http://vilins.top/

题目

Task

实现一个简单的“狼人游戏”通知机制

Details

角色有:村民,狼人,预言家,女巫,猎人(Uninitialized只用于默认构造函数)
法官呼叫“狼人/预言家/女巫/猎人”,相应角色进行回答
法官呼叫“村民”,全体角色回答

Hints

objA.on(objB)表示将objB作为objA的监听者(事件订阅者)
知识点:

观察者模式
类与类的关系:关联
C++ enum
类静态变量
One more step

将构造函数和初始化操作(Player();和void init(std::string name, Role role);)分离出来有何优缺点?
judge.hpp中const Player*能改为const Player&吗?为什么?
enum类型Role定义在Player类的public部分有何好处?
Role为什么定义成enum类型?定义成string数组合适吗?
kMaxCountOfPlayers为什么要作为类静态变量?如果类静态变量为double类型,还能在类定义中初始化吗?


代码实现

//main.cpp

#include "judge.hpp" #include "player.hpp" #include <cassert> #include <iostream> #include <string> using std::cin; void RunAllTests() { Judge judge; Player players[Judge::kMaxCountOfPlayers]; std::string name; Player::Role role; for (int i = 0; i < Judge::kMaxCountOfPlayers; ++i) { switch (i) { case 0: case 1: case 2: { role = Player::Villager; break; } case 3: case 4: case 5: { role = Player::Werewolf; break; } case 6: { role = Player::Seer; break; } case 7: { role = Player::Witch; break; } case 8: { role = Player::Hunter; break; } default: assert(false); } cin >> name; players[i].init(name, role); judge.on(&players[i]); } int role_index; cin >> role_index; judge.call(static_cast<Player::Role>(role_index)); } int main() { RunAllTests(); return 0; } /* Two probable tests: input0: Kathy Julie Kenneth Albert William Jack Lisa Carol Harold 2 output0: Calling: Werewolf Albert: Shh... I am a Werewolf. William: Shh... I am a Werewolf. Jack: Shh... I am a Werewolf. ========================================= input1: Lisa Albert Julie Harold Jack Carol William Kenneth Kathy 1 output1: Calling: Villager Lisa: I am the villager!! Albert: I am the villager!! Julie: I am the villager!! Harold: I am the villager!! Jack: I am the villager!! Carol: I am the villager!! William: I am the villager!! Kenneth: I am the villager!! Kathy: I am the villager!! */

//judge.hpp

#ifndef JUDGE_HPP_ #define JUDGE_HPP_ #include "player.hpp" class Judge { public: static const int kMaxCountOfPlayers = 9; Judge(); ~Judge(); void on(const Player* player); void call(Player::Role role); private: const Player* m_players[kMaxCountOfPlayers]; int m_players_count; }; #endif

//judge.cpp

#include <iostream> #include <string> #include "judge.hpp" #include "player.hpp" using namespace std; Judge::Judge() { m_players_count=0; } Judge::~Judge() { } void Judge::on(const Player* player) { m_players[m_players_count]=player; m_players_count++; } void Judge::call(Player::Role role) { if(role==Player::Werewolf) { cout << "Calling: Werewolf"<< endl; } else if(role==Player::Villager) { cout << "Calling: Villager"<< endl; } else if(role==Player::Seer){ cout << "Calling: Seer"<< endl; } else if(role==Player::Witch) { cout << "Calling: Witch"<< endl; } else if(role==Player::Hunter) { cout << "Calling: Hunter"<< endl; } if(role==Player::Villager) { for(int i=0;i<m_players_count;i++) { m_players[i]->pretend(); } return; } for(int i=0;i<m_players_count;i++) { if(role==m_players[i]->role()) { m_players[i]->answer(); } } }

//play.hpp

#ifndef PLAYER_HPP_ #define PLAYER_HPP_ #include <string> class Player { public: enum Role { Uninitialized, Villager, Werewolf, Seer, Witch, Hunter }; Player(); void init(std::string name, Role role); ~Player(); Role role() const; void answer() const; void pretend() const; private: Role m_role; std::string m_name; }; #endif

//player.cpp

#include <iostream> #include <string> #include "player.hpp" using namespace std; Player::Player() { m_role=Uninitialized; m_name=""; } void Player::init(string name, Role role) { m_name=name; m_role=role; } Player::~Player() { ; } Player::Role Player::role() const { return m_role; } void Player::answer() const { if(m_role==Werewolf) { cout << m_name << ": Shh... I am a Werewolf." << endl; } else if(m_role==Villager) { cout << m_name << ": Shh... I am the villager!!" << endl; } else if(m_role==Seer){ cout << m_name << ": Shh... I am a Seer." << endl; } else if(m_role==Witch) { cout << m_name << ": Shh... I am a Witch." << endl; } else if(m_role==Hunter) { cout << m_name << ": Shh... I am a Hunter." << endl; } } void Player::pretend() const { cout << m_name << ": I am the villager!!" << endl; }

1.

更多技术博客 http://vilins.top/


  1. Written by Vilin.↩︎

http://www.zskr.cn/news/1448923.html

相关文章:

  • 告别ifconfig!SUSE15保姆级安装与阿里云源配置全攻略
  • MATLAB相机标定一键运行包:单目/双目/鱼眼全兼容,含角点提取、畸变可视化与极线校正
  • 告别 “代码搬运工”,低代码平台如何从重复劳动中解放开发生产力
  • PE工具箱里的瑞士军刀:深度挖掘CGI增强版那些你可能不知道的隐藏功能(从ESD解密到动态磁盘)
  • Capacitated Facility Location Problem
  • 3步快速上手:Cursor Pro永久免费破解方案终极指南
  • 别再折腾了!保姆级教程:在VMware Ubuntu虚拟机里调用Windows主机摄像头(含Cheese/FFmpeg测试)
  • 基于BERT与CNN的智能交互装置:情绪分析与手势识别的软硬件实现
  • 7-Zip-zstd:当压缩工具遇见现代算法,你的文件处理体验将彻底改变
  • 告别YUV图片转换烦恼:在Ubuntu 22.04上从源码编译libjpeg-turbo 2.1.5的完整指南
  • 目标检测框回归的“进化史”:从IOU到CIOU,我们到底在优化什么?(附PyTorch实现对比)
  • 别再傻傻重做U盘了!Win10安装报错install.wim,用一条DISM命令10分钟搞定
  • 保姆级教程:在Ubuntu 20.04上管理多版本CUDA(11.0/11.4/12.1),用软链接自由切换
  • WuWa-Mod:鸣潮游戏模组全面解析与实战指南
  • Smithbox终极指南:从零开始掌握魂系游戏修改工具
  • AI工程师全景解析:岗位分类、核心职责与薪资体系
  • 3分钟掌握苹果平方字体:免费PingFangSC完整使用教程
  • 基于MOSFET的LED流水灯制作:无稳态多谐振荡器电路详解
  • 用Arduino和光敏电阻模块DIY一个天黑自动亮的小夜灯(附完整代码)
  • SMUDebugTool:如何免费解锁AMD Ryzen处理器的终极性能潜力
  • 别再乱设Content-Type了!Spring Boot接口传参失败的3个常见坑点与排查指南
  • 【超简单易懂的教程】桌面 AI 自动化 OpenClaw 2.7.8 部署实操分享(含安装包)
  • 基于ATtiny85与MAX30102的心率监测可穿戴设备开发全流程解析
  • 从‘网络打架’到‘双网协同’:手把手教你用Linux Bonding聚合双网卡(附CentOS/Ubuntu配置)
  • Android 13系统源码里给三方App“开后门”:一个Shell脚本搞定预装与静默安装
  • 3步搭建专业级跨平台音乐播放器:LX Music桌面版完全指南
  • 新手必看:用泡沫胶和热熔胶枪搞定你的第一架固定翼无人机(附详细工具清单)
  • 基于树莓派的智能称重系统:从传感器到Web全栈物联网实践
  • 用ShaderGraph给你的独立游戏加把火:低成本实现风格化火焰与篝火交互
  • 用OpenCV给图片里的形状‘体检’:紧致度、圆度、偏心率到底怎么看?附Python代码