设为首页 加入收藏

TOP

算法设计:约瑟夫环(二)
2013-07-23 09:05:50 来源: 作者: 【 】 浏览:273
Tags:算法 设计 约瑟夫
 

  数据结构

  在这个游戏中,假定每个人都是一个节点,这样有利于程序的理解。

  [cpp] view plaincopyprint

  template

  struct Node{

  List_entry code; // 存储每个人手中 密码

  List_entry iposition; // 存储每个人所处的 位置

  Node *next;

  Node();

  Node(List_entry a, List_entry b, Node *link=NULL);

  };

  template

  class Joeph_list{

  public:

  Joeph_list();

  int size() const;

  bool empty() const;

  void clear();

  Error_code retrieve(int position, List_entry &x, List_entry &y) const;

  Error_code replace(int position, const List_entry &x, const List_entry &y);

  Error_code remove(int position, List_entry &x, List_entry &y);

  Error_code insert(int position, const List_entry &x, const List_entry &y);

  ~Joeph_list();

  protected:

  int count;

  void Init(); // 初始化线性表

  Node *head;

  Node *set_position(int position) const;

  // 返回指向第position个结点的指针

  };

  template

  struct Node{

  List_entry code; // 存储每个人手中 密码

  List_entry iposition; // 存储每个人所处的 位置

  Node *next;

  Node();

  Node(List_entry a, List_entry b, Node *link=NULL);

  };

  template

  class Joeph_list{

  public:

  Joeph_list();

  int size() const;

  bool empty() const;

  void clear();

  Error_code retrieve(int position, List_entry &x, List_entry &y) const;

  Error_code replace(int position, const List_entry &x, const List_entry &y);

  Error_code remove(int position, List_entry &x, List_entry &y);

  Error_code insert(int position, const List_entry &x, const List_entry &y);

  ~Joeph_list();

  protected:

  int count;

  void Init(); // 初始化线性表

  Node *head;

  Node *set_position(int position) const;

  // 返回指向第position个结点的指针

  };

  Node结构:表示实现Joeph_list以及List表的结点

  Joeph_list类:储存游戏中玩家座位、密码等信息的数据结构

  List类:以链表的方式存储图片等数据结构

  全局对象game:SimpleWidow的窗口输出游戏过程

  Listtu;Listshu;Listpeople;分别存储游戏参与者报数、所持密码、和游戏参与者的图片。

  全局函数:

  void Baoshu(int p,int s); 用以显示游戏参与者报数的效果

  void Yizou(int p,int m); 用以移走报到数的游戏参与者

  void Code(int m); 用以更新密码信息

  void Jieshu(); 结束游戏

  项目测试

  1、游戏开始,初始m为6,从第一个玩家开始自动报数,报到数的人出列

  2、以出列人手中的密码为密码(不大于6)继续游戏

  3、直到所有人出列,游戏结束

  项目演示

      

首页 上一页 1 2 下一页 尾页 2/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇N阶楼梯上楼问题 下一篇用最小路径覆盖求得二分图

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: