Problem with checking deck for held card. C++ - c++

I'm very new and attempting to create a deck that will be filled at random from cardpool, permterm will also be filled with the same cards. This will give you duplicates of some cards and singles of others, as intended. Then you can use a card or draw a card and it will sort hand() and deckArr() correctly.
The problem comes when you draw and use a card back to back. When a card is in your hand, the corresponding trigger in cardActivation will turn to 1. (Meaning it's in your hand and cannot be drawn again) So if you draw, it handles the activation for that one card and shifts the deck accordingly. If you use the card, it is the opposite. It will turn the trigger to 0 (Meaning its available to draw and inside the deck) and shift the hand accordingly.
It seems that it randomly decides not to run the if statement within draw() [if (cardActivation[x] == 0 && hand[handSize-1] == permterm[x])] and will not trigger the activation, thus throwing off the rest of the program, but the next iteration of the loop correctly runs the if statement. When I get to the end of all the testing the activation is off by 1 because of the one time it missed the if statement. (There should be nine 1s instead of eight.)
I've fiddled and changed my code 100000 times but can't figure out why randomly it decided to skip the if statement, as far as I can tell it should still be true.
I've left my notes and tests within my code so hopefully it can help others understand my thinking process, sorry it's such a mess. It was MUCH cleaner before I started bug fixing. The HP bar information can be ignored, it's apart of something else. Hopefully I explained well enough for some help, I'm losing my mind. Thank you in advance!
#include <iostream>
#include <string>
#include <ctime>
using namespace std;
class healthBar
{
private:
int hp;
public:
healthBar()
{
hp = 100;
}
int damage(int dmg)
{
hp-=dmg;
if (hp == 0)
{
cout << "Youve Died " << endl;
resetHP();
return 0;
}
else
{
printHP();
return 1;
}
}
void heal(int regen)
{
hp+=regen;
}
void printHP()
{
cout << "HP is: " << hp << endl;
}
void resetHP()
{
hp = 100;
}
};
class myDeck
{
private:
int deckArr[30];
int tempDeck[30];
int permterm[30];
int hand[10] = {0,0,0,0,0,0,0,0,0,0};
int handSize = 0;
int cardPool[25] = {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};
int cardActivation[30];
public:
myDeck()
{
//Attaches Deck
int x;
srand(time(0));
for (int y = 0; y < 30; y++)
{
x = rand()%25;
deckArr[y] = cardPool[x];
cout << deckArr[y] << endl;
}
cout << "Permterm:" << endl;
for (int z = 0; z < 30; z++)
{
permterm[z] = deckArr[z];
cardActivation[z] = 0;
cout << permterm[z] << endl;
}
/*If you wanted duplicates of cards but
also be able to reject if that specific card has
been drawn already, you'd need an
activation array lined up with the permterm. 0=Not Active (Dont Skip) 1= In Hand (Skip)
It will need to search for the number.*/
}
void shuffle()
{
int z = 0;
for (int x = 0; x<30; x++)
{
tempDeck[x] = deckArr[x];
}
srand(time(0));
int y;
for (int x = 0; x<30; x++)
{
y = rand()%30;
if (y != z)
{
deckArr[x] = tempDeck[y];
}
z = y;
}
cout << "\nShuffled!" << endl;
}
void drawHand()
{
handSize=7;
for (int x = 0; x < 7; x++)
{
hand[x] = deckArr[x];
}
for (int x = 0; x<30; x++)
{
deckArr[x] = deckArr[x+7];
}
for (int x = 23; x < 30; x++)
{
deckArr[x] = 0;
}
//Switch On and Search
for (int y = 0; y < handSize; y++)
{
for (int x = 0; x < 30; x++)
{
if (hand[y] == permterm[x] && cardActivation[x] == 0)
{
cardActivation[x] = 1;
break;
}
}
}
cout << "\nHand Drawn!" << endl;
}
void viewDeck()
{
for (int x = 0; x<30; x++)
{
cout << permterm[x] << " " << cardActivation[x] << endl;
}
}
void viewHand()
{
for (int x = 0; x<handSize; x++)
{
cout << hand[x] << endl;
}
}
void resetDeck()
{
int resetCard;
int handTemp = handSize;
for (int y = 0; y < 30-handTemp; y++)
{
for (int x = 0; x<30;x++)
{
if (cardActivation[x] != 1)
{
deckArr[y] = permterm[x];
break;
}
}
}
cout << "Reset!"<< endl;
}
void draw()
{
if (handSize < 10)
{
cout << "\nDrawing..." << endl;
hand[handSize] = deckArr[0];
cout << "You drew: " << deckArr[0] << endl;
handSize++;
for (int x = 0; x < 30; x++)
{
deckArr[x] = deckArr[x+1];
}
deckArr[29] = 0;
//Activation
for (int x = 0; x < 30; x++)
{
if (cardActivation[x] == 0 && hand[handSize-1] == permterm[x])
{
cardActivation[x] = 1;
viewDeck();
break;
}
}
}
else
{
cout << "\nYour hand is full!" << endl;
}
//Test to see if deck needs to be reset
int sum = 0;
for (int x = 0; x < 30; x++)
{
sum+=deckArr[x];
}
//Checks to see if sending to resetDeck.
if (sum == 0)
{
viewDeck();
resetDeck();
}
}
void useCard()
{
int input;
cout << "\nWhich card would you like to use?" << endl;
for (int y = 0; y < handSize; y++)
{
cout << y+1 << ". " << hand[y] << endl;
}
cin >> input;
cout << "\nYou used: " << hand[input-1] << endl;
//Deactivate
for (int x = 0; x < 30; x++)
{
if (cardActivation[x] == 1 && hand[input-1] == permterm[x])
{
cardActivation[x] = 0;
break;
}
}
viewDeck();
for (int y = input-1; y<handSize; y++)
{
if (y+1 == 10)
{
hand[y] = 0;
}
else
hand[y] = hand[y+1];
}
handSize--;
}
};
int main()
{
/*Objective: Create a game where you draw from a deck of cards, to have 7 cards in hand.*/
healthBar health;
myDeck deck;
deck.viewDeck();
cout << "\nShuffling..." << endl;
deck.shuffle();
deck.viewDeck();
cout << "\nDrawing Hand..." << endl;
deck.drawHand();
deck.viewHand();
cout << "\nIs the Deck missing 7 cards?" << endl;
deck.viewDeck();
cout << "Can you draw? Hand:" << endl;
deck.draw();
deck.viewHand();
cout << "\nIs the deck missing 8 cards?" << endl; deck.viewDeck();
cout << "\nCan you draw until hand is full?" << endl;
deck.draw();
deck.viewHand();
deck.draw();
deck.viewHand();
deck.draw();
deck.viewHand();
cout << "\nCan you use a card?" << endl;
deck.useCard();
deck.draw();
deck.useCard();
deck.draw();
deck.useCard();
deck.draw();
deck.useCard();
for (int x = 0; x <10; x++)
{
deck.draw();
deck.useCard();
}
cout << "\nDoes resetting exclude your hand?" << endl;
cout << "Hand: " << endl;
deck.viewHand();
cout << "Deck: " << endl;
deck.viewDeck();```

Related

C++ roman numeral converter using class

I am having issues with trying to figure out how to declare and call my functions. I had this working without using a class, but my assignment calls for a class, romanType, to be used. Can someone please help me figure out my issues?
Here are my errors:
Severity Code Description Project File Line Suppression State
Error (active) E0020 identifier "getRoman" is undefined Garant_Project9 C:\Users\Andre\OneDrive\Desktop\C++\Garant_Project9\Garant_Project9\Garant_Project9.cpp 119
Severity Code Description Project File Line Suppression State
Warning C6001 Using uninitialized memory 'sum'. Garant_Project9 C:\Users\Andre\OneDrive\Desktop\C++\Garant_Project9\Garant_Project9\Garant_Project9.cpp 81
Severity Code Description Project File Line Suppression State
Error C3861 'getRoman': identifier not found Garant_Project9 C:\Users\Andre\OneDrive\Desktop\C++\Garant_Project9\Garant_Project9\Garant_Project9.cpp 119
Here is my code:
#include<iostream>
#include<iomanip>
#include<iostream>
#include<iomanip>
#include<string>
using namespace std;
class romanType
{
public:
void printValue(char numeral[15]);
void Menu();
void getRoman();
private:
};
void romanType::printValue(char numeral[15])
{
int RomanValues[7] = { 1000,500,100,50,10,5,1 };
char Roman[7] = { 'M','D','C','L','X','V','I' };
int number = 0, ck[15] = { 0 }, len = 0;
bool ok = false;
for (int x = 0; x < 15; x++)
if (numeral[x] != '\0')
len++; //Finding out how long the input is
else
x = 15;
for (int x = 0; x < len; x++)
{
ok = false;
for (int y = 0; y < 7; y++)
{
if (numeral[x] == Roman[y])
ok = true; // Making sure the input is all Roman numerals
}
if (!ok)
{
cout << "Sorry, the '" << numeral[x] << "' is NOT a Roman Numeral." << endl << "Try
again.." << endl << endl;
return;
}
}
for (int x = 0; x < len; x++)
{
for (int y = 0; y < 7; y++)
{
if (numeral[x] == Roman[y])
{
ck[x] = RomanValues[y];// Fill array ck with decimal value of inputted Roman #
}
}
}
for (int x = 0; x < len; x++)
{
if ((ck[x + 1] > ck[x]))
{
number += (ck[x + 1] - ck[x]); // If number is lower than next number, subtract
// So IV is 4 and VI is 6
x++;
}
else
number += ck[x]; // else add
}
cout << number << endl << endl;
}
void romanType::Menu()
{
cout << "\tRoman Numeral Values" << endl << endl;
cout << "\t\tM = 1000" << endl;
cout << "\t\tD = 500" << endl;
cout << "\t\tC = 100" << endl;
cout << "\t\tL = 50" << endl;
cout << "\t\tX = 10" << endl;
cout << "\t\tV = 5" << endl;
cout << "\t\tI = 1" << endl;
}
void romanType::getRoman()
{
char numeral[15];
do
{
Menu();
cout << endl << "Please enter a Roman Numeral.\n(Enter 'Q', to end program)" << endl;
cin >> numeral;
for (int x = 0; x < 15; x++)
numeral[x] = toupper(numeral[x]);
if (numeral[0] == 'Q')
cout << "Program terminating!" << endl;
else
printValue(numeral);
} while (numeral[0] != 'Q');
}
int main()
{
getRoman();
return 0;
}

Problems with a c++ class

I am working on this university project and I have 2-3 issues. I want to get them done 1 by 1.
My first question is about one of my getter/setter functions.
Here are the class's attributes:
I got getters and setters for each attribute of the class, and a view function that shows the attributes of a class object
class Game{
private:
string gameName;
float gamePrice;
int gameNoPlatforms;
string *gamePlatforms;
int gameNoSitesRating;
int *gameRatings;
int gameSales[19];
string gameLaunchers[5];
}
Those 2 are my get/set function for the atribute * string gamePlatforms:
void setGamePlatforms(string *newGamePlatforms, int newGameNoPlatforms)
{
if (this->gamePlatforms != NULL)
{
delete[] this->gamePlatforms;
}
if (newGameNoPlatforms > 0 )
{
this->gameNoPlatforms = newGameNoPlatforms;
this->gamePlatforms = new string[this->gameNoPlatforms];
for (int i = 0; i < this->gameNoPlatforms; i++)
{
this->gamePlatforms[i] = newGamePlatforms[i];
}
}
else cout << "The number of new game platforms can't be negative. "<< endl;
}
string *getGamePlatforms()
{
return this->gamePlatforms;
}
And this is the input that i tried to test on:
int main(){
Game g1;
int noPlatforms = 10;
string*model = new string[noPlatforms];
for (int i = 0; i < noPlatforms; i++)
{
model[i] = "Platform " + to_string(i+10);
}
g1.setGamePlatforms(model, noPlatforms); //-> not working
g1.setGamePlatforms(model, noPlatforms);
cout << g1.getGamePlatforms();
return 0;
}
And for me it is returning a weird value. I think it is an address. What have I done wrong?
Edit: the entire class:
class Game{
private: string gameName;
float gamePrice;
int gameNoPlatforms;
string *gamePlatforms;
int gameNoSitesRating;
int *gameRatings;
int gameSales[19];
string gameLaunchers[5];
public:
// Constructor1 fara parametrii pt game
Game():gameReleaseYear(2000)
{
this->gameName = "Counter-Strike";
this->gamePrice = 20;
this->gameNoPlatforms = 10;
this->gamePlatforms = new string[this->gameNoPlatforms];
for (int i = 0; i < this->gameNoPlatforms; i++)
{
this->gamePlatforms[i] = "Platform" + to_string(i+1);
}
this->gameNoSitesRating = 5;
this->gameRatings = new int[this->gameNoSitesRating];
for (int i = 0; i < this->gameNoSitesRating; i++)
this->gameRatings[i] = i + 1;
for (int i = 1; i < 19; i++)
{
this->gameSales[i] = i + 2;
}
for (int i = 1; i < 5; i++)
{
this->gameLaunchers[i] = "Launcher" + to_string(i);
}
this->noGames++; //incrementare nr games
}
// Constructor cu parametrii pt game
Game(string gameNameP, float gamePriceP, int gameNoPlatformsP, string *gamePlatformsP, int gameNoSitesRatingP, int *gameRatingsP, int gameSalesP[19], string gameLaunchersP[5]) :gameReleaseYear(2005)
{
this->gameName = gameNameP;
this->gamePrice = gamePriceP;
this->gameNoPlatforms = gameNoPlatformsP;
this->gamePlatforms = new string[this->gameNoPlatforms];
for (int i = 0; i < this->gameNoPlatforms; i++)
{
gamePlatforms[i] = gamePlatformsP[i];
}
this->gameNoSitesRating = gameNoSitesRatingP;
this->gameRatings = new int[this->gameNoSitesRating];
for (int i = 0; i < this->gameNoSitesRating; i++)
{
gameRatings[i] = gameRatingsP[i];
}
for (int i = 0; i < 19; i++)
{
gameSales[i] = gameSalesP[i];
}
for (int i = 0; i < 5; i++)
{
gameLaunchers[i] = gameLaunchersP[i];
}
}
// Destructor pt game
~Game()
{
if (this->gamePlatforms != NULL)
delete[] this->gamePlatforms;
if (this->gameRatings != NULL)
delete[] this->gameRatings;
noGames--; // decrementare nr games
}
// Functie de afisare pentru game
void view()
{
cout << "For the game: " <<' '<< this->gameName << " we have the following details: " << endl;
cout << endl;
cout << "The release year for the game was: " << this->gameReleaseYear << endl;
cout << endl;
cout << "The game is sold at a full price of: " << this->gamePrice << " euroes" << endl;
cout << endl;
cout << "The number of sites that are rating this game is: " << this -> gameNoSitesRating << " and the ratings are: " << endl;
cout << endl;
for (int i = 0; i < this->gameNoSitesRating; i++)
{ if(this->gameRatings[i]+i >10)
cout << "The no " << i + 1 << " site is rating the game as " << 10 << " stars out of 10 " << endl;
else
cout << "The no " << i+1 << " site is rating the game as " << this->gameRatings[i] << " stars out of 10 " << endl;
}
cout << endl;
cout << "The sales of the game from the release year since now are: " << endl;
for (int i = 1; i < 19; i++)
{
cout << "For the year " << i << " the sales estimate at " << gameSales[i] << " millions" << endl;
}
cout << endl;
cout << "The launchers that support the game are: " << endl;
for (int i = 1; i < 5; i++)
{
cout << gameLaunchers[i] << ' ' << endl;
}
cout << "The game is currently running on " << this->gameNoPlatforms << " number of platforms, and those platforms are: " << endl;
cout << endl;
for (int i = 0; i < this->gameNoPlatforms; i++)
{
cout << this->gamePlatforms[i] << endl;
}
}
// functiile accesor getters
string getGameName()
{
return this->gameName;
}
float getGamePrice()
{
return this->gamePrice;
}
int getGameNoPlatforms()
{
return this->gameNoPlatforms;
}
int getNoSitesRating()
{
return this->gameNoSitesRating;
}
string *getGamePlatforms()
{
return this->gamePlatforms;
}
int *getGameRatings()
{
return this->gameRatings;
}
string *getGameLaunchers()
{
return this->gameLaunchers;
}
//functiile accesor setters
void setGameName(string newGameName) //testat pt input valid si invalid
{ if(newGameName != gameName)
this->gameName = newGameName;
else cout << "This is the actual name of the game, no modify is required.";
}
void setGamePrice(float newPrice) //testat pt input valid si invalid;
{ if(newPrice>0)
this->gamePrice = newPrice;
else cout << "The price can't be negative." << endl;
}
void setGameNoPlatforms(int newNoPlatforms) //testat pentru input valid, input mai mare ca 5 si negativ
{ if(newNoPlatforms>0 && newNoPlatforms <5)
this->gameNoPlatforms = newNoPlatforms;
else cout << "The number of platforms can't be negative and must be less than 5, since this is the maximum number of existing platforms." << endl;
}
void setGameNoSitesRating(int newNoSites) //testat pentru input valid, input negativ, input mai mare decat 15
{ if(newNoSites>0 && newNoSites<15)
this->gameNoSitesRating = newNoSites;
else cout << "The number of sites can't be negative nor greater than 15 since this is the maximum number of sites that our game is rated on." << endl;
}
void setGamePlatforms(string *newGamePlatforms, int newGameNoPlatforms) //testat pt input valid, returneaza adresa
{
if (this->gamePlatforms != NULL)
{
delete[] this->gamePlatforms;
}
if (newGameNoPlatforms > 0 ){
this->gameNoPlatforms = newGameNoPlatforms;
this->gamePlatforms = new string[this->gameNoPlatforms];
for (int i = 0; i < this->gameNoPlatforms; i++)
{
this->gamePlatforms[i] = newGamePlatforms[i];
}
}
else cout << "The number of new game platforms can't be negative. "<< endl;
}
void setGameRatings(int *newGameRatings, int newNoRatings) //testat pt valori valide, testat pt neNoRatings negativ;
{
if (newNoRatings < 0)
cout << "The new number of sites that are rating the game can't be negative";
else {
if (this->gameRatings != NULL)
{
delete[] this->gameRatings;
}
this->gameNoSitesRating = newNoRatings;
this->gameRatings = new int[this->gameNoSitesRating];
for (int i = 0; i < this->gameNoSitesRating; i++)
{
this->gameRatings[i] = newGameRatings[i];
}
}
}
void setGamesSales(int *newSales)
{
if (newSales != NULL)
{
for (int i = 0; i < 19; i++)
{
gameSales[i] = newSales[i];
}
}
else cout << "The sales can't be null." << endl;
}
void setLaucnhers(string *newLaunchers)
{
if (newLaunchers != NULL)
{
for (int i = 0; i < 5; i++)
{
gameLaunchers[i] = newLaunchers[i];
}
}
else cout << "The name of the new launchers can't be null." << endl;
}
//variabile static/const
const int gameReleaseYear;
static int noGames;
};
int Game::noGames = 0;
Try this:
int Game::getNumberOfPlatforms() const
{
return gameNoPlatforms;
}
And print them:
for(int i = 0; i < g1.getNumberOfPlatforms(); ++i)
cout << g1.getGamePlatforms()[i] << endl;

Multidimensional array not changing elements from function for some reason

The function player_attack() changes the elements of the multi-dimensional array pc_board but when i reprint it in main, the array prints unchanged.
I removed all unnecessary code.
I tried to pass is at as a parameter to the function but i got an error for using a multidimensional array in the parameter.
$
bool game_won = false;
string board[5][5];
string pc_board[5][5];
void initialize_player_board() {
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
board[i][j] = "-";
}
}
}
void print_map() {
for (int i = 0; i < 5; i++) {
cout << setw(5);
cout << i << setw(5);
for (int j = 0; j < 5; j++) {
cout << board[i][j] << setw(5);
}
cout << setw(10);
for (int j = 0; j < 5; j++) {
cout << pc_board[i][j] << setw(5);
}
cout << endl;
}
}
void pc_add_battleship() {
int x = 0;
int y = 0;
int choice_generator = 0;
char choice;
x = rand() % 4 + 1;
y = rand() % 4 + 1;
choice_generator = rand() % 2;
if (choice_generator == 0) {
choice = 'h';
}
else {
choice = 'v';
}
if (choice == 'h') {
pc_board[y - 1][x] = 'O';
pc_board[y][x] = 'O';
pc_board[y + 1][x] = 'O';
}
if (choice == 'v') {
pc_board[y][x - 1] = 'O';
pc_board[y][x] = 'O';
pc_board[y][x + 1] = 'O';
}
}
void player_attack() {
int x = 0;
int y = 0;
cout << "Choose an x coordinate to attack: " << endl;
cin >> x;
cout << "Choose a y coordinate to attack: " << endl;
cin >> y;
if (pc_board[y][x] == "O") {
cout << "HIT!" << endl;
pc_board[y][x] == "H";
}
else {
cout << "Miss." << endl;
pc_board[y][x] == "M";
}
}
int main()
{
srand(time(0));
initialize_player_board();
initialize_pc_board();
cout << "Welcome to the battleship game." << endl;
print_map();
Add_battleship();
pc_add_battleship();
while (!game_won) {
print_map();
player_attack();
}
return 0;
}
$
I expected for the multidimensional array to change its elements due to the function
In your function player_attack you use wrong operator:
if (pc_board[y][x] == "O") {
cout << "HIT!" << endl;
pc_board[y][x] == "H"; // here
}
else {
cout << "Miss." << endl;
pc_board[y][x] == "M"; // and here
}
instead of == that is a comparison operator you should use = that is the assignment operator.
Using operator == in this context is still valid C++ syntax, which produces a boolean value, however it does not modify the arguments (which are left and right side of comparison), which is probably what you want to do in most cases. Enabling compiler flags like -Wall or Wextra along with Werror helps to avoid this kind of bugs.

How do i draw a hollow diamond in C++ using two fuctions

So me and my group have been working on this project for a few days we can get the top part too draw when we step into it but it does not run and we are not sure why it wont.
The first function is to draw the top and the second is to draw the bottom.
The top seems to draw fine i am not sure why the code will not even run.
#include<iostream>
using namespace std;
void drawTopPart(int userNum);
void drawBottomPart(int userNum);
int main() {
//declarations
int userNum;
//get user input
cout << "Enter an odd number from 1 to 15: ";
cin >> userNum;
//output
drawTopPart(userNum);
drawBottomPart(userNum);
cout << endl;
system("pause");
return 0;
}
void drawTopPart(int userNumPar) {
int z = 1;
int size;
cin >> size;
for (int i = 0; i <= size; i++) {
for (int j = size; j>i; j--) {
cout << " ";
}
cout << "*";
if (i>0) {
for (int k = 1; k <= z; k++) {
cout << " ";
} z += 2; cout << "*";
}
cout << endl;
}
}
void drawBottomPart(int userNumPar){
int z -= 4;
int size;
cin >> size;
for (int i = 0; i <= size - 1; i++) {
for (int j = 0; j <= i; j++) {
cout << " ";
}
cout << "*";
for (int k = 1; k <= z; k++) {
cout << " ";
}
z -= 2;
if (i != size - 1) {
cout << "*";
}
}
You have two mainly errors.
First,
void drawBottomPart(int userNumPar){
int z -= 4;
int size;
int z -= 4; have syntax error.You can change it int z = 17;
Second,you forget endl here.
if (i != size - 1) {
cout << "*";
}
cout << endl;

C++ draw rectangle position

I have my class:
class Rectangle : public TwoDim
{
public:
void fun() {};
void printShape();
Rectangle(int x1, int y1, int height1, int width1)
{
x = x1;
y = y1;
height = height1;
width = width1;
}
};
And the function to print it:
void Rectangle::printShape()
{
{
cout << "+";
for (int i = 0; i < width - 2; i++)
{
cout << "-";
}
cout << "+\n";
for (int i = 0; i < height - 2; i++)
{
cout << "|";
for (int j = 0; j < +width - 2; j++)
{
cout << " ";
}
cout << "|\n";
}
cout << "+";
for (int i = 0; i < width - 2; i++)
{
cout << "-";
}
cout << "+\n";
}
}
How do I change my function such that I will draw the rectangle starting from the point (x, y)?
Thanks a lot
I'd start by outputting y std::endls before the actual printing, and then outputting x " " before effectively printing each row.