如何将值从一个数组移动到下一个数组

时间:2013-12-07 22:16:48

标签: c++

我正在制作一个二十一点游戏,我正试图将一张牌从我的牌组顶部移到我的玩家手上。我认为我写的代码会起作用,但它不会发送卡片。谁能解释为什么?

const int SHOESIZE = 208;
const int HANDSIZE = 15;    // definitely enough space
int shoeSize = 52;

void drawCard(int shoe[], int& position, int hand[], int& handSize, int &drawn)
{
handSize++;
hand[handSize-1] = shoe[position];
drawn = hand[handSize-1];
position++;
}

int humanHand[50];
int humanHandSize = 0;
int computerHand[50];
int drawn;
// initialize the deck
int shoe[SHOESIZE];
for(int i = 0; i < SHOESIZE; i++)
{
    shoe[i] = 1 + (i % 13);
}

// Shuffle the deck
int i;
int j;
int temp; // placeholder
int position;
for (i = 0; i < SHOESIZE; i++)
{
    j = 1 + rand() % 32;
    temp = shoe[i];
    shoe[i] = shoe[j];
    shoe[j] = temp;
}
position = 0; // sets position to the "top" of the deck
int hand[HANDSIZE];
// Draw Card
for(i = 0; i < 2; i ++)
{
drawCard(shoe, position, humanHand, humanHandSize, drawn);

2 个答案:

答案 0 :(得分:1)

这个作品我相信,我已经改变了套牌的洗牌,因为这样更有效率,我认为只要下一张牌是随机的,牌组中每张牌都有不同的数量并不重要,但如果确实如此,那么只需更改代码即可。

我同意atk的观点,你应该尝试遵循它们(我知道下面的代码不是因为我只是直接复制代码来测试和调试它。)

这几乎是一样的。

const int SHOESIZE = 208;
const int HANDSIZE = 15;    // definitely enough space
int shoeSize = 52;

void drawCard(int shoe[], int& position, int hand[], int& handSize, int &drawn)
{
    handSize++;
    hand[handSize-1] = shoe[position];
    drawn = hand[handSize-1];
    position++;
}

int main()
{

    int humanHand[50];
    int humanHandSize = 0;
    int computerHand[50];
    int drawn;

    // initialize the deck and shuffle
    int shoe[SHOESIZE];
    for(int i = 0; i < SHOESIZE; i++)
    {
        shoe[i] = 1 + ( rand() % 13 );
    }

    int position = 0; // sets position to the "top" of the deck
    int hand[HANDSIZE];
    // Draw Card
    for(int i = 0; i < 2; i ++)
    {
        drawCard(shoe, position, humanHand, humanHandSize, drawn);
    }

    return 0;
}

答案 1 :(得分:-1)

您的代码中有很多错误......我的评论是评论。数字搞砸了,因为我来回阅读代码。

/* atk 0: where is your main() function? */
/* atk 11: what are these magic numbers?  why is 15 definitely enough space?  space for what?  why could it get as large as 15? what's it space for?  */
const int SHOESIZE = 208;
const int HANDSIZE = 15;    // definitely enough space
int shoeSize = 52;

/* atk 3: no comment explaining what the function is supposed to do, what the variables mean, and what prior and post state should be. add such a description. */
/* atk 4: no return value.  How is the caller supposed to know if there's an error?  add return value.  */
void drawCard(int shoe[], int& position, int hand[], int& handSize, int &drawn)
{
/* atk 1: fix your indentation. be consistent throughout.  I've fixed for you */
/* atk 2: you have no checks to ensure that values are as expected.  what if handsize = 0?  what if hand == null?  Add safety checks */
    handSize++;                         /* atk 5: increments the pointer, not the value.  */
    hand[handSize-1] = shoe[position];  /* atk 6: position & handsize are pointers. not values.  fix. */
    drawn = hand[handSize-1];           /* atk 7: handsize is pointer, not value  fix. */
    position++; /* atk:                 /* atk 8: using pointer instead of value. fix. */
}

/* atk 9: none of these variables have comments explaining what they are  Why whould a human have a hand of 50 cards?  Is that really 50 cards?  Or is it supposed to be fingers?  or 50 hands for 50 players?  Comment for each of these. */
int humanHand[50];
int humanHandSize = 0; /* atk 10: makes no sense. size usually means number of slots used, in which case there is a maxsize, or it means the max size.  No comment makes this ambiguous.  no maxsize makes this variable appear to mean max size. */
int computerHand[50];
int drawn;

/* atk 10: why is this stuff not in an initi() function? */
// initialize the deck
int shoe[SHOESIZE];
for(int i = 0; i &lt; SHOESIZE; i++)
{
    shoe[i] = 1 + (i % 13); /* atk 17: why the magic number 13? and why are you looking for a number between 1 and 13 ?  How do you track what suit the card is?  */
}

/* atk 12: why is this not in a shuffle() function? */
// Shuffle the deck
/* atk 13: comments missing.  what are these values for? why are you doing c-style declarations instead of inline c++ style declarations? */
int i;
int j;
int temp; // placeholder /* atk 14: no $#!%sherlock.  placeholder for what? why is it needed */
int position;
for (i = 0; i &lt; SHOESIZE; i++)
{
    j = 1 + rand() % 32; /* atk 15: why 32? there are normally 52 cards in a deck... */
    temp = shoe[i];      
    shoe[i] = shoe[j];   /* atk 16: how do you know j is less than SHOESIZE? fix. */
    shoe[j] = temp;
}
position = 0; // sets position to the "top" of the deck ?8 atk 17: thank you! a good comment! */
int hand[HANDSIZE];

/* atk 18: why is this not in a draw() function? */
// Draw Card
for(i = 0; i < 2; i ++)
{
    drawCard(shoe, position, humanHand, humanHandSize, drawn); /* atk 19: does not match function footprint.  your function takes pointers, but you are passing values */

/* atk 18: where is the rest of your code? incomplete code will not run. */