Hi there! I'm new here and I'm quite new to programming. I'm in a programming class here at my school, and we have an individual assignment, and I'm making a card game (well, at least trying to :P).
I've written the basics, but I'm trying to give a priority this way " "Hearts", "Spades", "Diamonds", "Clubs", and then sorting them out in a number priority. I know how to create the number priority (I thought that I'd give each number and color a number before them, what I mean by this is, give for example "2 of hearts", I'd put 80 as hearts, and then it writes the "2", so it becomes "802", and it reads it as hearts of 2. However, what I need help with is sorting out the colors in the order I wrote previously.
Thanks in advance, Marrcus.
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 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
|
// Prog1_Kortlek_1.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <list>
#include <fstream>
#include <cstdlib> // Used for function rand()
#include <time.h>
#include <string>
using namespace std;
struct Card
{
string color;
int value;
};
Card cardDeck[52];
list<Card> hand;
fstream cardFile;
int randSeed;
int ReadDeck(const char * fileName)
{
cardFile.open(fileName, ios_base::in);
for (int i = 0; i < 52; i++)
{
if (cardFile.eof())
return 1;
cardFile >> cardDeck[i].color;
cardFile >> cardDeck[i].value;
}
cardFile.close();
return 0;
}
int WriteDeck(const char * fileName)
{
cardFile.open(fileName, ios_base::out);
for (int i = 0; i < 52; i++)
{
cardFile << cardDeck[i].color << " " << cardDeck[i].value << endl;
}
cardFile.close();
return 0;
}
void MixDeck()
{
string color;
int value;
int randIndex;
for (int i = 0; i < 52; i++)
{
srand (time(NULL));
randIndex = i + (rand() % (52 - i));
color = cardDeck[i].color;
value = cardDeck[i].value;
cardDeck[i].color = cardDeck[randIndex].color;
cardDeck[i].value = cardDeck[randIndex].value;
cardDeck[randIndex].color = color;
cardDeck[randIndex].value = value;
}
}
void PickHand(list<Card> & aHand, int nbrOfcards)
{
for (int i = 0; i < nbrOfcards; i++)
{
/*
struct Card myCard;
myCard.color = "Diamond";
myCard.value = 5;
*/
aHand.push_back(cardDeck[i]);
}
}
void SortHand(list<Card> & aHand, int nbrOfcards)
{
for (int i = 0; 1 < nbrOfcards; i++)
{
/*
*/
/*aHand.sort_cards(cardDeck[i]);*/
}
}
int main(int argc, char* argv[])
{
int result;
if ((argc < 2) || (argc > 3))
return 1;
const char* cardDeckFileName = argv[1];
cardDeckFileName = argv[1];
result = ReadDeck(cardDeckFileName);
if (result != 0)
return result;
cout << "First card is: " << cardDeck[0].color << " " << cardDeck[0].value << endl;
MixDeck();
cout << "Mixing card deck." << endl;
cout << "First card is now: " << cardDeck[0].color << " " << cardDeck[0].value << "\n\n";
WriteDeck(cardDeckFileName);
PickHand(hand, 5);
for (auto it = hand.begin(); it != hand.end(); it++)
{
cout << "\n\ncolor:" << it->color << "\nvalue: " << it->value << endl;
}
//cout << "\nkort-färg: " << hand.front().color << "kot-värde: " << hand.front().value << endl;
return 0;
}
|