俄罗斯轮盘赌游戏

/*
游戏的道具是一把左轮手枪,其规则也很简单:在左轮手枪中的 6 个弹槽中随意放入一颗或者多颗子弹,在任意旋转转轮之后,关上转轮。游戏的参加者轮流把手枪对着自己,扣动扳机:中枪或是怯场,即为输的一方;坚持到最后的即为胜者。
*/
#include <iostream> #include <ctime> #include <cstdlib> #define random(a,b) (rand()%(b-a+1)+a) using namespace std; struct Link { int data; Link* next; }; Link* InitLink(Link*, int); void ShootP(Link*); void Display(Link*); int main() { int n; cout << "请输入总人数:" << endl; cin >> n; Link* head = NULL; head = InitLink(head, n); ShootP(head); system("PAUSE"); return 0; } Link* InitLink(Link* head, int n) { head = new Link; head->data = 1; head->next = NULL; Link* temp = head;//head存储首元节点,temp存储head中存储的结构体地址,相当于head的一个分身 for (int i = 2; i <= n; ++i) { Link* list = new Link; list->data = i; list->next = NULL; temp->next = list; temp = temp->next; } temp->next = head; return head; } void ShootP(Link* head) { Link* temp = head; srand((int)time(0)); while (temp->next != head) { temp = temp->next; } while (temp->next != temp) { int num = random(1, 6); cout << "下一次在开第" << num << "枪时,biubiubiu" << endl; while(--num) { temp = temp->next; } cout << "序号为:" << temp->next->data << "被射杀" << endl; Link* temp2 = temp->next;//error point 存储当前节点状态,用于下面判断首元节点是否有效 temp->next = temp->next->next; cout << "剩余人为:" << endl; if (temp2 == head)//判断是否首元节点被删除 head = head->next;//更新首元节点 Display(head); /*error point Display(head) 此时首元节点改变 */ } cout << "序号为:" << temp->data << "存活" << endl; } void Display(Link* head) { Link* temp = head; if (temp) { while (temp->next != head) { cout << temp->data << "->"; temp = temp->next; } } cout << temp->data; cout << endl; }

猜你喜欢

转载自www.cnblogs.com/Mayfly-nymph/p/10155331.html