#Using ShowCards method
1 messages · Page 1 of 1 (latest)
can you send the code?
Yes I have different files do you want both the .cpps and the .h
this is what entire code
std::vector<Card> WarGame::_cards;
WarGame::WarGame(std::string cardsFile)
{
// Method for loadcards is being called
LoadCards(cardsFile);
}
std::vectorstd::string split(const std::string& s, char delimiter) {
std::vectorstd::string tokens;
std::istringstream tokenStream(s);
std::string token;
while (std::getline(tokenStream, token, delimiter))
{
tokens.push_back(token);
}
return tokens;
}
void WarGame::LoadCards(std::string fPath) {
std::ifstream file(fPath);
if (!file.is_open())
{
std::cerr << "Unable to load file " << fPath << std::endl;
return;
}
std::string stLine, rkLine;
std::getline(file, stLine);
std::getline(file, rkLine);
std::vector<std::string> suits = split(stLine, '?');
std::vector<std::string> ranks = split(rkLine, '?');
for (const auto& suit: suits)
{
for (const auto& rank : ranks)
{
_cards.emplace_back(suit, rank);
}
}
file.close();
}
void WarGame::shuffle()
{
int ndx1, ndx2;
srand((unsigned int)time(NULL));
for (size_t i = 0; i < 52; i++)
{
ndx1 = rand() % 52;
ndx2 = rand() % 52;
Card temp = _cards[ndx1];
_cards[ndx1] = _cards[ndx2];
_cards[ndx2] = temp;
}
}
void WarGame::ShowCards() {
// Looping over the list of cards and displaying on a new line.
for (const Card& card : _cards)
{
if (card.GetSuit() == "HEARTS")
{
std::cout << '\u2665';
}
else if (card.GetSuit() == "SPADES")
{
std::cout << '\u2660';
}
else if (card.GetSuit() == "CLUBS")
{
std::cout << '\u2663';
}
else if (card.GetSuit() == "DIAMONDS")
{
std::cout << '\u2666';
}
std::cout << card.GetFace() << card.GetSuit() << std::endl;
}
}
#include "WarGame.h"
#include "Card.h"
#include <time.h>
#include <iostream>
#include <fstream>
#include <sstream>
#include <iomanip>
#include "Input.h"
is this c++?
Yes
have you ever used a debugger before?
i tired and it only worked in the output area where i'm calling the code.
Would you be able to show your code for GetSuit() ? Also, you can add syntax highlighting to a discord message by creating a code block in this format for cpp:
```cpp
// code goes here
```
Also, this may be outside the scope of your question, but have you gone over enums yet in class? Your if-else sequence may be more efficient as a switch-case, but cpp doesn't allow strings in case statements and so enums come to mind as a better way around that.