I am new to C and I would like to make a blackjack game.The problem is that I want to have the cards of the player printed on the same line. For example like this:
___________ ___________
| K K | | Q Q |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | |
|___________| |___________|
But the the code below:
#include <stdio.h>
#include <stdlib.h>
int main()
{
const char *card_k=
"\n ___________ "
"\n| K K |"
"\n| |"
"\n| |"
"\n| |"
"\n| |"
"\n| |"
"\n| |"
"\n| |"
"\n| |"
"\n|___________|";
const char *card_q=
"\n ___________ "
"\n| Q Q |"
"\n| |"
"\n| |"
"\n| |"
"\n| |"
"\n| |"
"\n| |"
"\n| |"
"\n| |"
"\n|___________|";
printf("%s",card_k);
printf("%s",card_q);
system("Pause");
return 0;
}
Obviously prints this, because of the new line characters:
___________
| K K |
| |
| |
| |
| |
| |
| |
| |
| |
|___________|
___________
| Q Q |
| |
| |
| |
| |
| |
| |
| |
| |
|___________|
I think a similar fix in python is to use end='' but I don't know how to do it in C.Any help will be appreciated.
CodePudding user response:
Don't mix up data and output formatting, those are two different things. One way to solve your problem would be to store each card as an array of character pointers, each pointing at a string literal. Then you can print that data however you fancy:
#include <stdio.h>
#include <stdlib.h>
int main()
{
const char *card_k [] =
{
" ___________ ",
"| K K |",
"| |",
"| |",
"| |",
"| |",
"| |",
"| |",
"| |",
"| |",
"|___________|",
};
const char *card_q [] =
{
" ___________ ",
"| Q Q |",
"| |",
"| |",
"| |",
"| |",
"| |",
"| |",
"| |",
"| |",
"|___________|",
};
size_t lines = sizeof (card_k) / sizeof(*card_k);
// print on the same row
for(size_t i=0; i<lines; i )
{
printf("%s %s\n", card_k[i], card_q[i]);
}
puts("");
// print on different rows
for(size_t i=0; i<lines; i )
{
printf("%s\n", card_k[i]);
}
puts("");
for(size_t i=0; i<lines; i )
{
printf("%s\n", card_q[i]);
}
return 0;
}
CodePudding user response:
You need to revise the structure of your cards so that you can (easily) print multiple cards on a single line. I recommend using a structure to hold the data for a card — it will make life a lot easier in the long run. You need all the cards to have the same size (it's easy to cheat if you can spot the difference between the cards by looking at the size). You then have a function that prints a series of cards in the form of an array of pointers to the card images. Since there is a structure, it can hold the rank (King, Queen, ..., Ace) and suit (Clubs, Diamonds, Hearts, Spades) too.
So, as I said in a comment:
- Step 1: remove the newlines from the card images, making each card an array of strings (without newlines) instead of a single string.
- Step 2: revise the code that prints card images to print N images across the page, one line at a time, with an appropriate separation between the images, and a newline at the end of the line.
More or less like this. I am not very happy with the names for the deuce (2) to the ten of a suit — choose names to suit yourself. In many ways, spelling out the number (DEUCE, THREE, FOUR, ..., TEN) might be better.
Code
#include <stdio.h>
typedef enum { CLUBS, DIAMONDS, HEARTS, SPADES } Suit;
typedef enum { ACE = 1, CARD_2, CARD_3, CARD_4, CARD_5, CARD_6, CARD_7,
CARD_8, CARD_9, CARD_10, JACK, QUEEN, KING } Rank;
enum { CARD_WIDTH = 14, CARD_HEIGHT = 11 };
typedef struct
{
Rank rank;
Suit suit;
char image[CARD_HEIGHT][CARD_WIDTH];
} Card;
static const Card card_k =
{
KING, SPADES,
{
" ___________ ",
"| K K |",
"| |",
"| |",
"| |",
"| |",
"| |",
"| |",
"| |",
"| |",
"|___________|",
}
};
static const Card card_q =
{
QUEEN, SPADES,
{
" ___________ ",
"| Q Q |",
"| |",
"| |",
"| |",
"| |",
"| |",
"| |",
"| |",
"| |",
"|___________|",
}
};
static void print_cards(size_t n_cards, const Card *cards[])
{
for (size_t line = 0; line < CARD_HEIGHT; line )
{
const char *pad = "";
for (size_t cardnum = 0; cardnum < n_cards; cardnum )
{
printf("%s%s", pad, cards[cardnum]->image[line]);
pad = " ";
}
putchar('\n');
}
}
int main(void)
{
const Card *deck[] = { &card_k, &card_q };
enum { SIZE_DECK = sizeof(deck) / sizeof(deck[0]) };
print_cards(SIZE_DECK, deck);
return 0;
}
Output
___________ ___________
| K K | | Q Q |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | |
|___________| |___________|
CodePudding user response:
Option 2: Use terminal control sequences
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define CARD_WIDTH 13
#define CARD_HEIGHT 11
typedef const char card_type [CARD_HEIGHT] [CARD_WIDTH 1];
card_type card_k =
{
" ___________ ",
"| K K |",
"| |",
"| |",
"| |",
"| |",
"| |",
"| |",
"| |",
"| |",
"|___________|",
};
card_type card_q =
{
" ___________ ",
"| Q Q |",
"| |",
"| |",
"| |",
"| |",
"| |",
"| |",
"| |",
"| |",
"|___________|",
};
void clear_screen()
{
printf("\033[2J");
}
void goto_xy(int x, int y)
{
printf("\033[%d;%dH", y 1, x 1);
}
void draw_card_at(int x, int y, card_type card)
{
for (size_t n=0; n<CARD_HEIGHT; n )
{
goto_xy(x, y );
printf("%s", card[n]);
}
}
int main()
{
clear_screen();
// print on the same row
draw_card_at(2, 2, card_k);
draw_card_at(2 CARD_WIDTH 1, 2, card_q);
goto_xy(0, CARD_HEIGHT 5);
return 0;
}