So I've been a little pressed for time the past couple days and have this somewhat large assignment due in 2 days. But I can't even figure out how to get started! I've been trying to do part F of the project which is "A function that initializes a given deck array with the 52 standard playing cards". We are NOT allowed to use classes for this project and i've been struggling with structs so this is what I have so far:
card.h:
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
|
#ifndef CARD_H
#define CARD_H
#include <iostream>
#include <cassert>
#include <fstream>
#include <string>
using namespace std;
const unsigned int DECK_SIZE = 52;
const unsigned int MAX_RANK = 13;
const unsigned int MAX_SUIT = 4;
const string ranks[MAX_RANK] =
{
"Ace", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight",
"Nine", "Ten", "Jack", "Queen", "King"
};
const string suits[MAX_SUIT] =
{
"Spades", "Hearts", "Diamonds", "Clubs"
};
struct Deck
{
string ranks;
string suits;
};
Deck initializeDeck();
|
card.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
|
#include "card.h"
Deck initializeDeck()
{
int i, j;
int k = 0;
struct Deck card[DECK_SIZE];
for (i = 0; i < MAX_RANK; i++)
{
for (j = 0; i < MAX_SUIT; i++)
{
card[k].ranks = ranks[i];
card[k].suits = suits[j];
k++;
}
}
}
|
main.cpp
1 2 3 4 5 6 7 8 9
|
#include "card.h"
int main()
{
cout << "Now running card.cpp..." << endl;
initializeDeck();
return 0;
}
|
I have no idea what I've actually done here as I was just following a bunch of different examples. Compiling and running this just results in a segmentation error and I've been stuck and frustrated for the past four hours now.
P.S. if anyone is curious, here are the other tasks of the project:
a. a record type (i.e., struct) for modeling a card
b. a function that returns the string representation of a given card
c. a function that compares if one card is more valuable than another card
Ranks are ordered, from higher to lower, as follows: ace, king, queen, jack, 10, 9, ..., 2. We follow the convention of Contract Bridge, and consider Spades the most valuable, followed by Hearts, and then Diamonds, and lastly Clubs.
d. a function that tests if two given cards are the same in suit and in rank
e. an array type, in the form of a typedef, for representing a deck of cards
f. a function that initializes a given deck array with the 52 standard playing cards
g. a function that randomly shuffles a given deck of cards
h. a function that prints a given deck of playing cards to an output file stream
i. a function that reads a given deck of playing cards from an input file stream
j. Develop a C++ program, shuffle, that prints a randomly shuffled deck of playing cards to file output. The output of the program shuffle should follow the format specified in part h of the previous question.
k. Develop a C++ filter, game, that reads from file input a deck of playing cards (assumed to have been generated by the program shuffle) and simulates the following game using the input deck. *Picture of example game*