【发布时间】:2018-03-09 18:05:11
【问题描述】:
所以基本上我有一副 10 张牌,我在游戏开始时洗牌,每回合我选一张牌,没有重复,直到我用完牌。那时我会重新洗牌并重复这个过程,直到游戏结束。
void play(int &size, int &player1, int &player2, int cardPile[], int board[]);
void displayRules();
int takeTurn(int &size, int &player, int cardPile[], int board[], int &opposingPlayer);
int shuffleDeck(int &size, int cardPile[]);
int drawCard(int &size, int cardPile[]);
int main()
{
int size;
int board[] = {0, 1, 0, 0, 1, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0, 2, 0, 0, 0, 1, 0, 0, 0, 0, 0};
int cardPile[10] = {1, 1, 2, 2, 3, 3, 4, 4, 0, 5};
int player1 = 0;
int player2 = 0;
play(size, player1, player2, cardPile, board);
return 0;
}
//This is the function that plays the entire game
void play(int &size, int &player1, int &player2, int cardPile[], int board[]){
displayRules();
shuffleDeck(size, cardPile);
while(player1 < 25 && player2 < 25){
cout << "\nPlayer 1's turn!" << endl;
takeTurn(size, player1, cardPile, board, player2);
drawCard(size, cardPile);
size++;
showState(player1, player2);
if(player1 >= 25)
break;
else
cout << "\nPlayer 2's turn!" << endl,
takeTurn(size, player2, cardPile, board, player1),
drawCard(size, cardPile),
size++,
showState(player1, player2);
}
youWin(player1, player2);
}
//This function does a single turn for each player
int takeTurn(int &size, int &player, int cardPile[], int board[],int &opposingPlayer){
if(cardPile[size] == 0)
cout << "You drew a Lose a turn card! You lose a turn!" << endl;
else if(cardPile[size] == 5)
cout << "You drew a Switch Places card! You must switch places with the other player!" << endl,
switchPlaces(player, opposingPlayer);
else
cout << "You drew a " << cardPile[size] << "!";
switch(cardPile[size]){
case 1:
cout << " Move forward " << cardPile[size] << " space on the board!" << endl;
player += cardPile[size];
obstacles(player, board);
break;
case 2:
cout << " Move forward " << cardPile[size] << " spaces on the board!" << endl;
player += cardPile[size];
obstacles(player, board);
break;
case 3:
cout << " Move forward " << cardPile[size] << " spaces on the board!" << endl;
player += cardPile[size];
obstacles(player, board);
break;
case 4:
cout << " Move forward " << cardPile[size] << " spaces on the board!" << endl;
player += cardPile[size];
obstacles(player, board);
break;
}
}
//This function shuffles the deck of cards
int shuffleDeck(int &size, int cardPile[]){
srand(time(0));
for(int i = 0; i < 10; i++){
int size = rand() % 10;
int temp = cardPile[i];
cardPile[i] = cardPile[size];
cardPile[size] = temp;
}
}
int drawCard(int &size, int cardPile[]){
size++;
if(size == 10)
shuffleDeck(size, cardPile);
else
;
}
我有一个名为 size 的变量,我在 main 和 shuffleDeck 函数中声明了牌组洗牌后,我尝试将元素的数量存储在 size 变量中,然后从那里将其放入 takeTurn 函数中,这样每次播放器抽一张牌,他们会从元素中得到一个随机数。然后 drawCard 函数将 size 变量增加 1,如果它达到第 10 个元素,则它将调用 shuffleDeck 函数来洗牌。
现在我尝试在另一个程序中对此进行测试,并成功地重新排列了数组,它最终变成了一个无限循环。
#include <iostream>
using namespace std;
int shuffle(int &n, int deck[]);
void display(int &n, int deck[]);
int main()
{
int deck[10] = {2, 4, 6, 8, 12, 14, 16, 18, 20, 22};
int n;
shuffle(n, deck);
display(n, deck);
return 0;
}
int shuffle(int &n, int deck[]){
srand(time(0));
for(int i = 0; i < 10; i++){
int n = rand() % 10;
int temp = deck[i];
deck[i] = deck[n];
deck[n] = temp;
}
}
void display(int &n, int deck[]){
while(n < 10){
cout << n << " " << deck[n] << endl;
n++;
if(n == 10)
shuffle(n, deck),
n = 0;
else
;
}
}
当我编译并运行我的原始程序时,玩家会抽出成千上万张牌,最终陷入无限循环。
【问题讨论】:
-
我想是时候使用你的调试器来调试你的程序了。一方面,您应该立即看到
main中的int n;永远不会更新——它仍然未初始化。 -
我相信你。 .
-
srand(time(0));应该只调用一次(在main)。 -
还要注意std有
std::shuffle。 -
else ;没用,可以去掉。
标签: c++ arrays infinite-loop shuffle playing-cards