[英]C++: array index is not increasing
我想做一個由3個玩家組成的簡單游戲,每個玩家根據隨機函數每次移動1到6個區塊,每個區塊移動一次,當第一個玩家移動之后,第二個玩家開始移動,然后第三個玩家移動。 為此,我增加了玩家完成移動的數組rach時間的索引。
我的問題是索引器似乎沒有增加,即使我增加了索引器,它也會堆積在播放器1中。 我在C#中具有完全相同的代碼,並且效果很好!
這是C ++中的代碼。
int main ()
{
string namesofplayers[] = {"one","two","three"};
int movementofplayers[] = {0,0,0}; // start position of players is
int gamesize = 32; //32 blocks-steps of game
int random;
int y = 0;
a:
y++;
if (y >= 3)
{
y = 0;
}
cout << "it's" << namesofplayers[y] << "turn to play";
int R = (rand() % 6 + 1);
cout << "player " << namesofplayers[y] << " moves to block" << R << endl;
movementofplayers[y] += random;
cout << movementofplayers[y];
if (movementofplayers[y] < gamesize)
{
goto a;
}
else
{
cout << "Player " << namesofplayers[y] << " wins the game" << endl;
}
}
在做您的工作時,我冒昧地寫了一個替代實現,該實現解決了您以前的代碼所遇到的一些問題,並產生了更具可讀性的輸出。 我還剔除了單線,因為它們使我發瘋,但這是個人喜好。 另外,我傾向於使用適當的范圍來明確限定標准庫中的符號。
擺脫goto
。 您可以出於多種原因瀏覽SO和Web,為什么不使用這樣的顯式跳轉。 只需使用一個循環
為偽隨機數生成器修復缺少的初始種子。 如果設置一個變化的種子,即通過使用某個變量值(例如time(nullptr)
)來調用它,則每次調用程序時,您將始終獲得相同的“隨機”值序列。
修正使用random
變量的問題。 您嘗試將一些random
初始化的值添加到movementofplayers[y]
。 有趣的是, g++-4.7
似乎可以確保在算術運算中使用該變量之前將其設置為1
。 但是,您需要的正確變量是R
從main()
返回一個定義明確的值。
我希望代碼仍能按照您的預期進行:
#include <string>
#include <iostream>
int main ()
{
srand(time(NULL));
std::string namesofplayers[] = {"one","two","three"};
int movementofplayers[] = {0,0,0}; // start position of players is
int gamesize = 32; //32 blocks-steps of game
int y = 1;
while(movementofplayers[y] < gamesize)
{
if (y >= 3)
{
y = 0;
}
std::cout << "it's " << namesofplayers[y] << " turn to play" << std::endl;
int R = (rand() % 6 + 1);
std::cout << "player " << namesofplayers[y] << " moves to block " << R << std::endl;
movementofplayers[y] += R;
std::cout << "movements of player " << namesofplayers[y] <<": " << movementofplayers[y] << std::endl;
y++;
}
std::cout << "Player " << namesofplayers[y] << " wins the game" << std::endl;
return 0;
}
這就是我要怎么做。
添加了隨機數生成器的種子,因此您不會每次都得到相同的游戲。
為玩家數量增加了一個常量,以消除魔術數字,並且如果需要,還可以更輕松地擴展玩家數量。
擺脫了goto。 盡管可以以合理的方式使用goto,但是它容易被誤用,使代碼更難於遵循,並使人們感到憤怒。 :)
我調整了輸出並命名了一些名稱,只是為了使其更易於測試。 這樣做時,我糾正了一個問題,該問題是說玩家移至方塊R,這是該回合的擲骰,而非他們在游戲中的實際位置。
#include <string>
#include <iostream>
#include <cstdlib>
#include <ctime>
int main()
{
std::srand(static_cast<unsigned int>(std::time(0)));
const int gamesize = 32;
const int num_players = 3;
const std::string namesofplayers[num_players] = {"1", "2", "3"};
int movementofplayers[num_players] = {0, 0, 0};
int current_player = 0;
for(;;) //Loop forever, the game logic will exit the loop when a winner is found
{
const int roll = rand() % 6 + 1;
movementofplayers[current_player] += roll;
std::cout << "Player " << namesofplayers[current_player] << " rolls a " << roll << " and moves to block " << movementofplayers[current_player] << std::endl;
//Check if they won and if so, end the game
if(movementofplayers[current_player] >= gamesize)
{
std::cout << "Player " << namesofplayers[current_player] << " wins the game!" << std::endl;
break;
}
current_player = (current_player + 1) % num_players;
}
return 0;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.