编程家庭作业:在C中装配一副牌

时间:2016-04-19 05:31:11

标签: c playing-cards

我有这项任务,我一直在努力。我应该有一副牌,并将它均匀地分配给四名玩家。

每张卡片都有自己的值(两个= 1,三个= 2,......,King = 12,Ace = 13)并且套装也有自己的值(Clubs = 1,Diamonds = 2,Hearts = 3 ,黑桃= 4)

我应该让经销商(玩家P)在75%的时间内通过换掉牌或其他东西赢得胜利。 我编写的程序通过从0到51的单个数组来模拟卡片。程序运行,但命令提示符说“按任意键继续......”

任何人都可以查看我的代码并告诉我是否有任何可能导致此问题的原因?提前谢谢。

#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <algorithm>

#define deck 52
#define CARDS 52
#define SUITS 4
#define FACES 13

int Deck[deck];
void distributeCards(int i);
void swap(int i, int j);
void shuffle(unsigned int wDeck[][FACES]);

int main(void)
{

    int Deck[deck] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51 };

}

void swap(int i, int j)
{
    int temp = Deck[i];
    Deck[j] = Deck[i];
    Deck[i] = temp;
}
int max(int i, int j, int k, int l)
{
    int tempMax = Deck[i];
    int maxIndex = i;

    if (Deck[j] > tempMax)
    {
        tempMax = Deck[j];
        maxIndex = j;
    }
    if (Deck[k] > tempMax)
    {
        tempMax = Deck[k];
        maxIndex = k;
    }
    if (Deck[l] > tempMax)
    {
        tempMax = Deck[l];
        maxIndex = l;
    }
    return maxIndex;
}

int determineFace(int i)
{
    if (i % 4 == 0)
        return 3; // 3 = Hearts
    if (i % 4 == 1)
        return 2; // 2 = Diamonds
    if (i % 4 == 2)
        return 4; // 4 = Spades
    else
        return 1; // 1 = Clubs
}

int determineValue(int i)
{
    if (i / 4 == 0) return 1; // 1 = Two
    if (i / 4 == 1) return 2; // 2 = Three
    if (i / 4 == 2) return 3; // 3 = Four
    if (i / 4 == 3) return 4; // 4 = Five
    if (i / 4 == 4) return 5; // 5 = Six
    if (i / 4 == 5) return 6; // 6 = Seven
    if (i / 4 == 6) return 7; // 7 = Eight
    if (i / 4 == 7) return 8; // 8 = Nine 
    if (i / 4 == 8) return 9; // 9 = Ten
    if (i / 4 == 9) return 10; // 10 = Jack
    if (i / 4 == 10) return 11; // 11 = Queen
    if (i / 4 == 11) return 12; // 12 = King
    else return 13; // 13 = Ace
}

void distributeCards()
{
    int house[13]; // house = Player P
    int p1[13]; // p1 = Player Q
    int p2[13]; // p2 = Player R
    int p3[13]; // p3 = Player S



    //picking house's cards
    printf("\n\nPlayer P's Cards: \n");
    for (int i = 0; i < 13; i++)
    {
        int rand1 = (rand() % (51 - i)) + i;
        int rand2 = (rand() % (51 - i)) + i;
        int rand3 = (rand() % (51 - i)) + i;
        int rand4 = (rand() % (51 - i)) + i;
        int rand = max(rand1, rand2, rand3, rand4);
        house[i] = Deck[rand];
        swap(rand, i);
    } 

    //picking p1's cards
    printf("\n\nPlayer Q's Cards: \n");
    for (int i = 13; i < 26; i++)
    {
        int rand5 = (rand() % (51 - i)) + i;
        p1[i - 13] = Deck[rand()];
        swap(rand(), i);
    }
    //picking p2's cards
    printf("\n\nPlayer R's Cards: \n");
    for (int i = 26; i < 39; i++)
    {
        int rand6 = (rand() % (51 - i)) + i;
        p2[i - 13] = Deck[rand()];
        swap(rand(), i);
    }
    //picking p3's cards
    printf("\n\nPlayer S's Cards: \n");
    for (int i = 39; i < 51; i++)
    {
        int rand7 = (rand() % (51 - i)) + i;
        p3[i - 13] = Deck[rand()];
        swap(rand(), i);
    }
}

1 个答案:

答案 0 :(得分:3)

行为完全正常。你已经写了这个主要功能:

int main(void)
{    
    int Deck[deck] = {....};
}

除了定义array之外什么都不做。没有输入,没有输出,没有。所以&#34;按任意键继续&#34;意味着您的阵列已正确定义并正确销毁,并且程序正确运行并终止。

修改

我认为你需要这样的东西:

int main(void)
{
  int Deck_temp[deck] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
                     17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30,
                     31, 32, 33, 34, 35,  36, 37, 38, 39, 40, 41, 42,
                     43, 44, 45, 46, 47, 48, 49, 50, 51 };

   memcpy(Deck, Deck_temp, 52*sizeof(int));
   distributeCards();
}