資源簡介
實現一個游戲:N個玩家圍成一個圓圈,玩家序號分別是1~N,順勢針排列;選一個報數值M。從玩家1開始按順時針方向自1開始順序報數,報到M時停止報數,報M的人被淘汰出列,然后從他順時針方向上的下一個人開始重新報數,如此下去,直到圓圈中只剩下一個人,這個幸存者就是游戲的勝利者。輸出游戲中玩家出局的順序(包括游戲勝利者)。
例子:如有6個玩家,報數值為3,則玩家出局順序為3-6-4-2-5-1
----------------------------------------------
僅供參考

代碼片段和文件信息
//?Game.cpp:?implementation?of?the?Game?class.
//
//////////////////////////////////////////////////////////////////////
#include?“stdafx.h“
#include?“Game.h“
#include?“JosephGameDlg.h“
#include?“Resource.h“
//////////////////////////////////////////////////////////////////////
//?Construction/Destruction
//////////////////////////////////////////////////////////////////////
struct?clink
{
int?num;
struct?clink?*next;
};
struct?clink?*head;
Game::Game(CJosephGameDlg*?_dlgint?aint?b)
{
dlg?=?_dlg;
playernum?=?a;
distance?=?b;
}
int?Game::GetNum()
{
return?playernum;
}
int?Game::GetDis()
{
return?distance;
}
void?Game::Init()??????????//使用帶表頭附加結點的循環單鏈表
{
struct?clink?*p;
int?i;
head?=?(struct?clink?*)malloc(sizeof(struct?clink));
head->next?=?head;
for(i=playernum;?i>=1;?i--)??//生成含有n個結點的,結點值依次為1-n的帶表頭附加結點的循環表
{
p?=?(struct?clink?*)malloc(sizeof(struct?clink));???//生成新結點
p->num?=?i;??????
p->next=head->next;????//把新結點插入到表頭
head->next=p;
}
}
void?Game::Joseph()
{
int?i;
char?st[5];
CString?s;
CEdit?*p;
struct?clink?*cur?=?head;
while(cur->next!=cur)
{
for(i=1;?i {
cur=cur->next;
if(cur==head)
{
cur=cur->next;
}
}
if(cur->next?==?head)
cur=cur->next;
p?=?(CEdit?*)dlg->GetDlgItem(IDC_EDIT_OUT);???//感慨良多
p->GetWindowText(s);
_itoa(cur->next->numst10);
s?=?s?+?“?“?+?st;
p->SetWindowText(s);
{
struct?clink?*temp;
temp=cur->next;
cur->next=cur->next->next;
free(temp);
}
}
}
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件???????7596??2010-08-12?16:24??JosephGame\Debug\Game.obj
?????文件??????????0??2010-08-12?16:24??JosephGame\Debug\Game.sbr
?????文件????5088256??2010-08-12?19:34??JosephGame\Debug\JosephGame.bsc
?????文件?????110667??2010-08-12?19:34??JosephGame\Debug\JosephGame.exe
?????文件?????204412??2010-08-12?19:34??JosephGame\Debug\JosephGame.ilk
?????文件??????13950??2010-08-12?16:19??JosephGame\Debug\JosephGame.obj
?????文件????7075400??2010-08-12?10:21??JosephGame\Debug\JosephGame.pch
?????文件?????386048??2010-08-12?19:34??JosephGame\Debug\JosephGame.pdb
?????文件???????2760??2010-08-12?16:19??JosephGame\Debug\JosephGame.res
?????文件??????????0??2010-08-12?16:19??JosephGame\Debug\JosephGame.sbr
?????文件??????26564??2010-08-12?19:34??JosephGame\Debug\JosephGameDlg.obj
?????文件??????????0??2010-08-12?19:34??JosephGame\Debug\JosephGameDlg.sbr
?????文件?????105927??2010-08-12?10:21??JosephGame\Debug\StdAfx.obj
?????文件????1370190??2010-08-12?10:21??JosephGame\Debug\StdAfx.sbr
?????文件?????214016??2010-08-12?19:34??JosephGame\Debug\vc60.idb
?????文件?????364544??2010-08-12?19:34??JosephGame\Debug\vc60.pdb
?????文件???????1657??2010-08-12?16:23??JosephGame\Game.cpp
?????文件????????646??2010-08-12?12:38??JosephGame\Game.h
?????文件??????21176??2010-08-12?19:31??JosephGame\JosephGame.aps
?????文件???????1379??2010-08-12?19:35??JosephGame\JosephGame.clw
?????文件???????2119??2010-08-11?16:58??JosephGame\JosephGame.cpp
?????文件???????4352??2010-08-12?11:04??JosephGame\JosephGame.dsp
?????文件????????526??2010-08-11?16:58??JosephGame\JosephGame.dsw
?????文件???????1368??2010-08-11?16:58??JosephGame\JosephGame.h
?????文件??????66560??2010-08-12?19:35??JosephGame\JosephGame.ncb
?????文件??????53760??2010-08-12?19:35??JosephGame\JosephGame.opt
?????文件????????944??2010-08-12?19:34??JosephGame\JosephGame.plg
?????文件???????5619??2010-08-12?15:04??JosephGame\JosephGame.rc
?????文件???????4420??2010-08-12?19:34??JosephGame\JosephGameDlg.cpp
?????文件???????1460??2010-08-12?15:04??JosephGame\JosephGameDlg.h
............此處省略12個文件信息
- 上一篇:利用MFC編寫一個簡單的帶界面的數據庫
- 下一篇:漢字點陣字庫建立與提取
評論
共有 條評論