c++ 实现狼人游戏

题目

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 
#include 
#include 
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(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 
#include 
#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;ipretend();
		}
		return;
	}
	for(int i=0;irole()) {
			m_players[i]->answer();
		}
	}
}

//play.hpp

#ifndef PLAYER_HPP_
#define PLAYER_HPP_
#include 

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 
#include 
#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.

更多技术博客:https://vilin.club/


  1. Written by Vilin. ↩︎

你可能感兴趣的:(c++)