Amazon.com

www.amazon.com
Employer Engaged

Interview Question

Software Development Engineer Intern Interview Seattle, WA

Design the classes required to represent a deck of cards

  and write functions to shuffle and deal cards
Answer

Interview Answer

1 Answer

0

const int SUITS = 4; //This is the number of different suits (should be 4)
const int VALUES = 13; //This is the number of individual values that a card can have (should be 13)
const int RANDOM_SWITCHES = 25; //This is the number of times that a pair of cards is switched in the shuffle function.
enum cardSuit {SPADE, CLUB, HEART, DIAMOND};
struct card{
        cardSuit suit;
        int value;
}

#include "stdafx.h" //needed for the Windows display
#include "globals.h" //include the global constants we set
#include <cstdlib> //the standard c library
#include <time.h> //used when seeding the rand() function
#include <string>

using namespace std;
#include "card.h" //include our card structure.

//Declare any global variables (the multi-dimensional array of cards) here.
card myCardArray[SUITS][VALUES];

//Our prototypes for the functions.
void initDeck();
void getCard(char cardString[], int row, int column);
void shuffleCards();
bool compareCards(char cardString[], int row, int column);

void initDeck()
{
    //For each of the card structs in your multidimensional array,
    //You need to set it's suit and value. Initially you should organize the cards
    //by having each suit on its own row, and have the cards in order by value on each row.
    for(int i = 0; i < VALUES; i++){
        myCardArray[0][i].suit = SPADE;
        myCardArray[0][i].value = i+1;
        myCardArray[1][i].suit = CLUB;
        myCardArray[1][i].value = i+1;
        myCardArray[2][i].suit = HEART;
        myCardArray[2][i].value = i+1;
        myCardArray[3][i].suit = DIAMOND;
        myCardArray[3][i].value = i+1;
    }

    //You do not need to modify the below call. It will seed your rand() function used in the shuffleCards function with the current time.
    srand((unsigned int)time(NULL));
}

void getCard(char cardString[], int row, int column)
{

    if(myCardArray[row][column].suit == SPADE){
        cardString[0]= 'S';
    }
    else if(myCardArray[row][column].suit == CLUB){
        cardString[0]= 'C';
    }
    else if(myCardArray[row][column].suit == HEART){
        cardString[0] = 'H';
    }
    else {
        cardString[0] = 'D';
    }

    if(myCardArray[row][column].value == 1)
        cardString[1] ='A';
    else if(myCardArray[row][column].value == 2)
        cardString[1] = '2';
    else if(myCardArray[row][column].value == 3)
        cardString[1] = '3';
    else if(myCardArray[row][column].value == 4)
        cardString[1] = '4';
    else if(myCardArray[row][column].value == 5)
        cardString[1] = '5';
    else if(myCardArray[row][column].value == 6)
        cardString[1] = '6';
    else if(myCardArray[row][column].value == 7)
        cardString[1] = '7';
    else if(myCardArray[row][column].value == 8)
        cardString[1] = '8';
    else if(myCardArray[row][column].value == 9)
        cardString[1] = '9';
    else if(myCardArray[row][column].value == 10)
        cardString[1] = 'T';
    else if(myCardArray[row][column].value == 11)
        cardString[1] ='J';
    else if(myCardArray[row][column].value == 12)
        cardString[1] = 'Q';
    else if(myCardArray[row][column].value == 13)
        cardString[1] = 'K';

    cardString[2] = '\0';

    //Look at the card located at the passed in row and column and generate
    //a 2 character long character array in cardString[] that represents the card.
    //If you return a valid character array, you will see the card on the screen,
    //if not you will see the back of a card.
}

bool compareCards(char cardString[], int row, int column)
{
    //compare the card located at the passed in row and column to the
    //card represented in cardString[]. If they are the same, return true.
    //HINT: Look up the strcmp() function.
    int result;
    char myCard[20];
    getCard(myCard, row, column);
    result = strcmp(cardString,myCard);
    if(result == 0){
        return true;
    }
    else{
        return false;
    }

}

void shuffleCards()
{
    //pick 2 cards at random and swap their locations. Do this RANDOM_SWITCHES number
    //of times.

    for(int i = 0; i < RANDOM_SWITCHES; i++){
        swap(myCardArray[rand()%4][rand()%13], myCardArray[rand()%4][rand()%13]);
    }

}

Anonymous on Mar 21, 2012

Add Answers or Comments

To comment on this, Sign In or Sign Up.