C++ draw rectangle position - c++

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.

Related

Problem with checking deck for held card. 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();```

How to change the value of an array with a function upon a bool true parameter

My homework/project is geared towards creating a memory game with a two-dimensional array of characters A-E randomly assigned to a 4x4 array.
We must use functions to do most of the heavy lifting of the program. Once a user guesses correctly the 4x4 visible array positions guess must be changed with the corresponding correct values from 'X' to its hidden value.
I have managed to stumble and crash through the majority of the code but this part is throwing me for a loop.
I have the hidden array showing only for testing purposes. Once completed it will be deleted.
So far the only thing that seems to make sense is to use a few if/if else statements to check if the x, y values are equal to row and column values and if so then make visibleArray[row][col] = hidden[row][col].
But this seems to grab random values, sometimes those not even guessed by the input.
Below are the bool function and void function that the problem occurs.
*** UPDATE : placed entire code to help see how array are initialized.
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <stdio.h>
using namespace std;
const int PAIRS = 10;
const int ARRAY_SIZE = 4;
//BLANK HIDDEN ARRAY
void startArray(int size) {
char hiddenArray[ARRAY_SIZE][ARRAY_SIZE];
for (int row = 0; row < size; row++) {
for (int col = 0; col < size; col++) {
hiddenArray[row][col] = 'X';
}
}
}
//DISPLAYED ARRAY
void dispArray (char visibleArray[][ARRAY_SIZE], int size){
for (int row = 0; row < ARRAY_SIZE; row++){
for (int col = 0; col < ARRAY_SIZE; col++){
visibleArray[row][col] = 'X';
}
}
cout << " 1 2 3 4" << endl;
for (int i = 0; i < ARRAY_SIZE; i++){
cout << i+1 << " ";
for (int j = 0; j < ARRAY_SIZE; j++){
cout << visibleArray[i][j] << " ";
}
cout << endl;
}
}
//RANDOM LETTER GENERATION
char secretLetters(int arr) {
char secrets[PAIRS] = { 'A','A','B','B','C','C','D','D','E','E' };
for (int i = arr; i <= 9; i++) {
char temp;
temp = secrets[i];
secrets[i] = 'X';
return temp;
temp = 0;
}
}
//POPULATE ARRAY WITH RANDOM VALUES
void initArray (char hiddenArray[][ARRAY_SIZE], int size) {
for (int i = 0; i <= PAIRS - 1; i++){
int index1 = rand() % ARRAY_SIZE;
int index2 = rand() % ARRAY_SIZE;
if (hiddenArray[index1][index2] == 'X'){
hiddenArray[index1][index2] = secretLetters(i);
}
else {
--i;
}
}
}
int userChoiceX (){
int x;
cout << "Please select a row value:";
cin >> x;
return x;
}
int userChoiceY (){
int y;
cout << "Please select a column value:";
cin >> y;
return y;
}
bool check (char visibleArray[][ARRAY_SIZE], char hiddenArray [][ARRAY_SIZE], int x1, int y1, int x2, int y2){
bool result = false;
if (visibleArray[x1][y1] == hiddenArray[x2][y2]){
result = true;
}
else if (visibleArray[x1][y1] == 'X' || hiddenArray[x2][y2] == 'X'){
result = false;
}
return result;
}
void updateArray (char visibleArray[][ARRAY_SIZE], char hiddenArray [][ARRAY_SIZE], bool correct, int x1, int y1, int x2, int y2){
if (correct != 0){//Update Array
cout << " 1 2 3 4" << endl;
for(int row = 0; row < ARRAY_SIZE; row++){
cout << row + 1 << " ";
for (int col = 0; col < ARRAY_SIZE; col++){
if (row == x1 && col == y1){
visibleArray[row][col] = hiddenArray[row][col];
cout << visibleArray[row][col] << " ";
}
else if (row == x2 && col == y2){
visibleArray[row][col] = hiddenArray[row][col];
cout << visibleArray[row][col] << " ";
}
else{
cout << visibleArray[row][col] << " ";
}
}
cout << endl;
}
}
else {//Show Answers
cout << " 1 2 3 4" << endl;
for (int row = 0; row < ARRAY_SIZE; row++){
cout << row + 1 < " ";
for (int col = 0; col < ARRAY_SIZE; col++){
if(row == x1 && col == y1){
cout << hiddenArray[row][col] << " ";
}
else if (row == x2 && col == y2){
cout << hiddenArray[row][col] << " ";
}
else {
cout << visibleArray[row][col] << " ";
}
}
cout << endl;
}
}
}
int main() {
srand(time(0));
int arr[PAIRS];
int x1, x2, y1, y2;
char hiddenArray[ARRAY_SIZE][ARRAY_SIZE];
char visibleArray[ARRAY_SIZE][ARRAY_SIZE];
int guessArray[ARRAY_SIZE][ARRAY_SIZE];
for (int row = 0; row < ARRAY_SIZE; row++) {
for (int col = 0; col < ARRAY_SIZE; col++) {
hiddenArray[row][col] = 'X';
}
}
//TO SEE THE TEMP ARRAY -- DELETE BEFORE SUBMISSION
/* for (int row = 0; row < ARRAY_SIZE; row++) {
for (int col = 0; col < ARRAY_SIZE; col++) {
cout << hiddenArray[row][col] << " ";
}
cout << endl;
}
cout << endl;
*/
/* for (int i = 0; i <= PAIRS - 1; i++) {
int index1 = rand() % ARRAY_SIZE;
int index2 = rand() % ARRAY_SIZE;
if (hiddenArray[index1][index2] == 'X') {
hiddenArray[index1][index2] = secretLetters(i);
}
else {
--i;
}
}
*/
initArray(hiddenArray, ARRAY_SIZE);
//REVEAL HIDDEN AREA
for (int row = 0; row < ARRAY_SIZE; row++) {
for (int col = 0; col < ARRAY_SIZE; col++) {
cout << hiddenArray[row][col] << " ";
}
cout << endl;
}
cout << endl;
dispArray(visibleArray, ARRAY_SIZE);
int tries = 0;
bool correct = 0;
do{
int x1, y1, x2, y2;
x1 = userChoiceX();
y1 = userChoiceY();
x2 = userChoiceX();
y2 = userChoiceY();
correct = check (visibleArray, hiddenArray, x1, y1, x2, y2);
updateArray (visibleArray, hiddenArray, correct, x1, y1, x2, y2);
if (correct != 0){
cout << "Good Job! You guessed correct!" << endl;
cout << "You have " << 9 - tries << " to guess the rest!" << endl;
}
else {
cout << "Sorry! You guessed wrong." << endl;
cout << "You have " << 9 - tries << " to guess the rest!" << endl;
}
tries++;
}
while (tries != 9);
return 0;
}
After an ephiany of sorts, I figured out the answer to my own question so here it is for others to reference in the future.
the correct functions are as follows:
bool check (char hiddenArray [][ARRAY_SIZE], int x1, int y1, int x2, int y2){
bool result = false;
if (hiddenArray[x1][y1] == hiddenArray[x2][y2] && hiddenArray[x1][y1] != 'X' && hiddenArray[x2][y2]){
result = true;
}
else if (hiddenArray[x1][y1] > ARRAY_SIZE || hiddenArray[x2][y2]){
result = false;
}
return result;
}
//UPDATES ARRAY WITH CORRECT ANSWERS
void updateArray (char visibleArray[][ARRAY_SIZE], char hiddenArray [][ARRAY_SIZE], int x1, int y1){
visibleArray[x1][y1] = hiddenArray[x1][y1];
}
I also found an error that was returning strange values in my user input functions and they are corrected below as well so not to confuse the user:
//USER CHOICE FUNCTIONS
int userChoiceX (){
int x;
cout << "Please select a row value:";
cin >> x;
return x-1;
}
int userChoiceY (){
int y;
cout << "Please select a column value:";
cin >> y;
return y-1;
}

C++ Compare elements in an array and print the position

I want to compare elements for each line of a matrix, from smallest to biggest.
But I don't want to print the sorted array I want to print the original position.
0 11.80 79.34 78.23
11.80 0 65.23 45.19
79.34 65.23 0 90.27
78.23 45.19 90.27 0
In this Matrix for the first line I wanna print 1, 2, 4, 3
My Code so far:
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
int main() {
string dummy;
double myArray[4][4];
int i;
int j;
int y;
ifstream infile("dist.dat");
cout << "Open file " << "dist.dat" <<" for reading." << endl;
for (i = 0; i < 4; i++) {
for (j = 0; j < 4; j++) {
infile >> myArray[i][j];
if (!infile) {
cout << "***There was a problem trying to read element [" << i << "][" << j << "]" << endl;
return 0;
}
}
}
infile.close();
cout << "Here's the array from the file" << endl;
cout << fixed << setprecision(2);
for (i = 0; i < 4; i++) {
for (j = 0; j < 4; j++) {
cout << setw(10) << myArray[i][j];
}
cout << endl;
}
cout << endl;
int x = myArray[i][j];
for (i = 0; i < 4; i++) {
for (j = 0; j < 4; j++) {
if(myArray[i][j] >= x) {
x = j;
}
else {
x = j + 1;
}
}
cout << x << endl;
}
return 0;
}
You need to maintain another matrix which has indices of every line and then apply the same operations on it that you are applying on each line of the original matrix to sort it.
Here is the code:
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
void swap(int &x, int &y)
{
int temp = x;
x = y;
y = temp;
}
void swap(double &x, double &y)
{
double temp = x;
x = y;
y = temp;
}
int main()
{
string dummy;
double myArray[4][4];
int i;
int j;
int y;
int k;
ifstream infile("dist.dat");
cout << "Open file " << "dist.dat" <<" for reading." << endl;
for (i = 0; i < 4; i++)
{
for (j = 0; j < 4; j++)
{
infile >> myArray[i][j];
if (!infile)
{
cout << "***There was a problem trying to read element [" << i << "][" << j << "]" << endl;
return 0;
}
}
}
infile.close();
cout << "Here's the array from the file" << endl;
cout << fixed << setprecision(2);
for (i = 0; i < 4; i++)
{
for (j = 0; j < 4; j++)
{
cout << setw(10) << myArray[i][j];
}
cout << endl;
}
cout << endl;
int sortedIndices[4][4];
for (i = 0; i < 4; i++)
{
for (j = 0; j < 4; j++)
{
sortedIndices[i][j] = j+1;
}
}
int x;
for (i = 0; i < 4; i++)
{
for (j = 0; j < 4; j++)
{
x = j;
for(k = j+1; k < 4; k++)
{
if(myArray[i][k] < myArray[i][x])
{
x = k;
}
}
swap(sortedIndices[i][j], sortedIndices[i][x]);
swap(myArray[i][j], myArray[i][x]);
}
}
for (i = 0; i < 4; i++)
{
for (j = 0; j < 4; j++)
{
cout << setw(10) << sortedIndices[i][j];
}
cout<<endl;
}
return 0;
}

C++ Grid Issues

I'm printing random numbers enclosed in ascii boxes in a 6x6 grid. Having issues with printing out the grid.
Instead of having 6 columns, all my boxes and numbers are being printed out in 1 column. Been troubleshooting but cant seem to find the issue. Below is the code. Appreciate your assistance.
int main(void)
{
cout << "Magic Grid\n" << endl;
int arrayxy [6][6];
srand((unsigned)time(0));
int lowest=1111, highest=9999;
int range=(highest-lowest)+1;
// Fill array with random values
for (int i = 0; i < 6; ++i)
{
for(int j = 0; j < 6; ++j)
{
arrayxy[i][j] = lowest+int(range*rand()/(RAND_MAX + 1.0));
}
}
// Print array as grid
for (int i = 0; i < 6; ++i)
{
for(int j = 0; j < 6; ++j)
{
cout << char(218);
for (int y=0; y< 4; y++)
{
cout << char(196);
}
cout << char(191) <<endl;
cout << char(179) << arrayxy[i][j] << char(179) << endl;
cout << char(192);
for (int z=0; z< 4; z++)
{
cout << char(196);
}
cout << char(217) <<endl;
}
cout << endl;
}
cout << endl;
}
You should endl after all the columns get printed out, NOT before.
This is how I fixed your code that gives, hopefully, what you wanted to have.
For the record, I specifically changed those ASCII to * and & to make the console result more readable. You can change them back to what you want those characters to be again.
void WriteFrontLine(std::size_t count)
{
for (int i = 0; i < count; i++)
{
cout << '*';
cout << '&' << '&' << '&' << '&';
cout << '*';
}
cout << endl;
}
void WriteEndLine(std::size_t count)
{
for (int i = 0; i < count; i++)
{
cout << '*';
cout << '&' << '&' << '&' << '&';
cout << '*';
}
cout << endl;
}
int main(void)
{
cout << "Magic Grid\n" << endl;
int arrayxy[6][6];
srand((unsigned)time(0));
int lowest = 1111, highest = 9999;
int range = (highest - lowest) + 1;
// Fill array with random values
for (int i = 0; i < 6; ++i)
{
for (int j = 0; j < 6; ++j)
{
arrayxy[i][j] = lowest + int(range*rand() / (RAND_MAX + 1.0));
}
}
// Print array as grid
for (int i = 0; i < 6; ++i)
{
WriteFrontLine(6);
for (int j = 0; j < 6; ++j)
{
cout << '*' << arrayxy[i][j] << '*';
}
cout << endl;
WriteEndLine(6);
cout << endl;
}
cout << endl;
}

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;