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.
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;
}
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 |
| | | |
| + + | | + + |
| + | | + |
| + + | | + + |
| | | |
| + + | | + + |
| + | | + |
| + + | | + + |
|___________| |___________|
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;
}
Related
In the code attached, how do I modify it to remove Remove the trailing '+' signs.
int i,j,sum;
sum=1;
for(i=2; i<=10; i++) {
for(j=1; j<(i+1); j++) {
sum = sum + 1;
printf("%d + ",j);
}
printf(" = %d", sum);
printf("\n");
}
return EXIT_SUCCESS;
}
Here is the output:
1 + 2 + = 3
1 + 2 + 3 + = 6
1 + 2 + 3 + 4 + = 10
1 + 2 + 3 + 4 + 5 + = 15
1 + 2 + 3 + 4 + 5 + 6 + = 21
1 + 2 + 3 + 4 + 5 + 6 + 7 + = 28
1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + = 36
1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + = 45
1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 + = 55
For example you can do it the following way
for(j=1; j<(i+1); j++) {
sum = sum + 1;
if ( j != 1 ) printf( " + " );
printf("%d",j);
}
You can't 'remove' output; you have to avoid generating it.
One way is to use:
for (int i = 2; i <= 10; i++)
{
int sum = 0;
const char *pad = "";
for (int j = 1; j <= i; j++)
{
sum += j;
printf("%s%d", pad, j);
pad = " + ";
}
printf(" = %d\n", sum);
}
Note that this recalculates sum more directly, setting it to zero before the inner loop. It also minimizes the scope of the variables.
You can set and print the initial value in the outer loop. For my opinion also make it more readable.
Furthermore you can use j instead of sum+1 for the addend
for (int i = 2; i <= 10; i++) {
int sum = 1;
printf("%d", sum);
for (int j = 2; j<(i + 1); j++) {
sum += j;
printf(" + %d", j);
}
printf(" = %d", sum);
printf("\n");
}
I have created an array and some functions that print different patterns each. My problem is that they are printed inside the array in a weird way. The below is the main part of the code and just one function for the example.
int castle(int patternWidth, int doorStart, int doorEnd, int N, int i, int j, int row, int col, char** array)
{
if (N >= 3 && N <= 20)
{
for (i = 1; i <= (N + 1); i++)
{
array[row][col] = '*'; col++;
array[row][col] = ' '; col++;
}
row++;
patternWidth = (((N + 1) * 2) - 1);
doorStart = (patternWidth - 3) / 2;
doorEnd = doorStart + 3;
for (i = 0; i < N; i++)
{
for (j = 1; j <= patternWidth; j++)
{
if(N - i <= 2 && j > doorStart && j <= doorEnd)
{
array[row][col] = ' '; col++;
}
else
{
array[row][col] = '*'; col++;
}
}
row++;
}
}
return 0;
}
int main()
{
int N = 0, M = 0, i = 0, j = 0, a = 0, b = 0, s = 0, width = 0, height = 0, patternWidth = 0, doorStart = 0, doorEnd = 0, option = 0, num = 3, col = 0, row = 0;
char** array;
printf("Give height board size:");
scanf("%d", &height);
printf("Give width board size:");
scanf("%d", &width);
array = (char**)malloc(height * sizeof(char*));
for (i = 0; i < width; i++)
{
array[i] = (char*)malloc(height * sizeof(char));
}
for (i = 0; i < height; i++)
{
for (j = 0; j < width; j++)
{
array[i][j] = ' ';
}
}
while (option != 6)
{
printf("\nOption: 1-5, 6 to exit\n"
"1) Stairs and flag\n"
"2) Castle\n"
"3) Trap door\n"
"4) Platform\n"
"5) Obstacles\n"
"Option:");
scanf("%d", &option);
if (option == 1)
{
printf("Valid values 6 - 20\n");
printf("Size:");
scanf("%d", &N);
printf("Height Position:");
scanf("%d", &row);
printf("Width Position:");
scanf("%d", &col);
stairs_flag(N, i, j, b, a, num, col, row, array);
}
else if (option == 2)
{
printf("Valid values 3 - 15\n");
printf("Size:");
scanf("%d", &N);
printf("Height Position:");
scanf("%d", &row);
printf("Width Position:");
scanf("%d", &col);
castle(patternWidth, doorStart, doorEnd, N, i, j, row, col, array);
}
else if (option == 3)
{
printf("Valid values 3 - 18\n");
printf("Size of N:");
scanf("%d", &N);
printf("Height Position:");
scanf("%d", &row);
printf("Width Position:");
scanf("%d", &col);
trap_door(N, patternWidth, i, j, col, row, array);
}
else if (option == 4)
{
printf("Valid values of N 3 - 20\n");
printf("Size of N:");
scanf("%d", &N);
printf("Valid values of M 6, 8, 10, 12, 14, 16, 18, 20\n");
printf("Size of M");
scanf("%d", &M);
printf("Height Position:");
scanf("%d", &row);
printf("Width Position:");
scanf("%d", &col);
platform(N, M, i, j, col, row, array);
}
else if (option == 5)
{
printf("Valid values 2 - 10\n");
printf("Size of N:");
scanf("%d", &N);
printf("Height Position:");
scanf("%d", &row);
printf("Width Position:");
scanf("%d", &col);
obstacles(N, i, s, j, patternWidth, row, col, array);
}
print_array(array, height, width);
}
free(array);
return 0;
}
The expected result and the correct one is this :
|------------------------------------------------------------|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| * * * * * |
| ********* |
| ********* |
| *** *** |
| *** *** |
| |
| |
| |
| |
| |
|------------------------------------------------------------|
But the actual result is this:
|------------------------------------------------------------|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| * * * * * |
| ********* |
| ********* |
| *** *** |
| *** *** |
| |
| |
| |
| |
| |
|------------------------------------------------------------|
For example, in the two arrays above, the board is 60 width and 20 height and for the pattern I gave the position 10 for both height and width. Every line of the pattern should be under the previous one and print a pattern but in reality it prints every line under the previous one but further more than the previous. How can I correct this? Is there any mistake in the function code. For every function I have done the same thing. I've put array[row][col] = '*'; col++; for the asterisks and array[row][col] = ' '; col++; for the spaces and row++; to print each line in a new line.
Thank you for your time
The problem seems to be that your column index col keeps increasing throughout the castle() function. If you want the asterisks to be aligned under each other, you need to set the column index back to its original value at the start of the for-loops.
For example, you could make it
int castle(int patternWidth, int doorStart, int doorEnd, int N, int i, int j, int row, int col, char** array)
{
int column_index = col;
if (N >= 3 && N <= 20)
{
for (i = 1; i <= (N + 1); i++)
{
array[row][col] = '*'; col++;
array[row][col] = ' '; col++;
}
row++;
patternWidth = (((N + 1) * 2) - 1);
doorStart = (patternWidth - 3) / 2;
doorEnd = doorStart + 3;
for (i = 0; i < N; i++)
{
for (j = 1; j <= patternWidth; j++)
{
col = column_index;
if(N - i <= 2 && j > doorStart && j <= doorEnd)
{
array[row][col] = ' '; col++;
}
else
{
array[row][col] = '*'; col++;
}
}
row++;
}
}
return 0;
}
where I added a command int column_index = col; which stores the original value at the start of the function, and a line col = column_index; at the start of the inner for-loop, putting the stored value back in the variable.
So I am relatively new to C and have been practicing questions on it. So I came across the question, 'Where can I go?': http://www.chegg.com/homework-help/questions-and-answers/c-coding-java-c--c-coding-bouncy-string-tasked-write-program-take-parameters-command-line--q19411844
I manage to write a part of the code, but I am not sure how to get this
| | | |
| | | |
| | | |
| |X| |
I somewhat have the logic of when the counter for width = x - 1, counter for width = x + 1, counter for height = y - 1 (basically the first few steps that can be moved to from the starting position, X) the numbers placed there would be limit - 1.
Same for when it goes on
for i less than or equal to limit
the spaces between the limit - 1, will now be filled with limit - 2. So it will be like x-1 && y-1, y-2, y-1 && x+1, x+2 and so on until the last where 0 is placed.
But how do you write this and fix it in the for loop?
#include <stdio.h>
#include <stdint.h>
#include <string.h>
int main()
{
int width, height, x, y, max;
printf("Enter width: ");
scanf("%d\n", &width);
printf("Enter height: ");
scanf("%d\n", &height);
printf("Enter x: ");
scanf("%d\n", &x);
printf("Enter y: ");
scanf("%d\n", &y);
printf("Enter walking limit: ");
scanf("%d\n", &max);
int b = 0, a = 0; // initalize local variable
for (a = 0; a < height; a++) {
// fill the width
for (b = 0; b < width + 1; b++ ) {
printf("|");
if (b==x && a==y)
{
printf("X");
}else if(((b==x-1) && (a==y)) || ((b==x+1) && (a==y)) || ((a==y-1) && (b==x)))
{
printf("%d", max-1);
}else if(//i am not sure if this will be needed)
{
for(int i = 2; i <= max; i++)
{
//add max - i to spaces after C until max is over.
}
}
}
printf("\n");
}
return 0;
}
You should split your code in several functions.
I have done a quick example below.
First we need a function to display the array.
You need to create your array, see the malloc.
Then you set the max steps at the correct position.
Then you need a function to do the propagation, this is a very naive way to do.
Scan the array and set to the value minus one all the cell around.
Do it max times.
For example:
Enter width: 9
Enter height: 9
Enter x: 3
Enter y: 7
Enter walking limit: 3
| | | | | | | | | |
| | | | | | | | | |
| | | | | | | | | |
| | | | | | | | | |
| | | |0| | | | | |
| | |0|1|0| | | | |
| |0|1|2|1|0| | | |
|0|1|2|3|2|1|0| | |
| |0|1|2|1|0| | | |
The code
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
static void array_print(int *array, int width, int height)
{
for (int i = 0; i < height; i++) {
printf("|");
for (int j = 0; j < width; j++) {
int val = array[i * width + j];
if (val < 0) {
printf(" |");
} else {
printf("%d|", val);
}
}
printf("\n");
}
}
/**
*
* Update the array if x, y is inside the limit and
* if val is greater than the already set value
*
*/
static void
array_upate(int *array, int width, int height, int x, int y, int val) {
if (x >= 0 && x < width && y >= 0 && y < height) {
int prev = array[y * width + x];
if (val > prev) {
array[y * width + x] = val;
}
}
}
static void array_propagate(int *array, int width, int height)
{
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
int val = array[i * width + j];
if (val > 0) {
array_upate(array, width, height, j, i - 1, val - 1);
array_upate(array, width, height, j, i + 1, val - 1);
array_upate(array, width, height, j - 1, i, val - 1);
array_upate(array, width, height, j + 1, i, val - 1);
}
}
}
}
int main()
{
int width, height, x, y, max;
int *array;
printf("Enter width: ");
scanf("%d", &width);
printf("Enter height: ");
scanf("%d", &height);
printf("Enter x: ");
scanf("%d", &x);
printf("Enter y: ");
scanf("%d", &y);
printf("Enter walking limit: ");
scanf("%d", &max);
/* allocate the array */
array = malloc(sizeof(int) * width * height);
/* Set all to -1 */
memset(array, -1, sizeof(int) * width * height);
/* set the start to max */
array_upate(array, width, height, x, y, max);
/* do the propagation */
while (max-- > 0) {
array_propagate(array, width, height);
}
array_print(array, width, height);
free(array);
}
I'm creating a connect-4 game... I have a lot of it done; however, the way I was creating my board was static & it needed to be dynamic, so I've made a side program to fix this before implementing it in my main program. For some reason, the if & else-if conditionals in this chunk of code create a segmentation fault, and I can't figure out why...
// for the rows/columns of the board
for(row = num_rows - 1; row >= 0; row--){
printf("|");
for(col = 0; col < num_columns; col++){
if(aPtr[row][col] == '0') {
printf("| X ");
}
else if(aPtr[row][col] == '1') {
printf("| O ");
}
else {
printf("| ");
}
}
puts("||");
}
when I comment these conditionals out the board prints just fine & looks like this
------ Connect *Four ------
Connect X Command Line Game
&&===================&&
|| | | | | ||
|| | | | | ||
|| | | | | ||
|| | | | | ||
|| | | | | ||
|| | | | | ||
|| | | | | ||
|| | | | | ||
|| | | | | ||
|| | | | | ||
&&===================&&
1 2 3 4 5
the entirety of this side-program is below, any insight as to why this segmentation fault is occurring will be appreciated.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
void initialize(int num_rows, int num_cols, char **aPtr) {
int i, r, c;
// create the space for the board
aPtr = malloc(num_rows * sizeof(char*));
for (i = 0; i < num_rows; i++){
aPtr[i] = malloc(num_cols * sizeof (char));
}
// go through the board and set all values equal to -1
for (r = 0; r < num_rows; r++) {
for (c = 0; c < num_cols; c++) {
aPtr[r][c] = '9';
printf("%c", aPtr[r][c]);
}
printf("\n");
}
}
void printBoard(int num_rows, int num_columns, char **aPtr) {
int row, col;
printf("\n");
puts("------ Connect *Four ------");
puts("Connect X Command Line Game");
// for fancy top of board frame
printf("&&");
for(col = 1; col < num_columns; col++) {
printf("====");
}
printf("===");
printf("&&\n");
// for the rows/columns of the board
for(row = num_rows - 1; row >= 0; row--){
printf("|");
for(col = 0; col < num_columns; col++){
// if(aPtr[row][col] == '0') {
// printf("| X ");
// }
// else if(aPtr[row][col] == '1') {
// printf("| O ");
// }
// else {
printf("| ");
// }
}
puts("||");
}
// for fancy bottom of board frame
printf("&&");
for(col = 1; col < num_columns; col++) {
printf("====");
}
printf("===");
printf("&&\n");
printf(" ");
if (col < 100){
for(col = 0; col < num_columns; col++) {
if (col < 10) {
printf(" %d ", col + 1);
}
else {
printf("%d ", col + 1);
}
}
puts("\n");
}
}
// *******************************************************************************************************
// *******************************************************************************************************
int main (int argc, char *argv[]) {
char **aPtr;
int height = 10;
int width = 5;
int i;
initialize(height, width, aPtr);
printBoard(height, width, aPtr);
}
Here is the modification of your code, maybe it will help. Note that I'm passing &aPtr and *aPtr = (char*) malloc(...)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
void initialize(int num_rows, int num_cols, char **aPtr) {
int i, r, c;
// create the space for the board
*aPtr = (char*) malloc(num_rows * sizeof(char*));
if(*aPtr == NULL)
{
free(*aPtr);
printf("Memory allocation failed");
}
for (i = 0; i < num_rows; i++){
aPtr[i] = (char *) malloc(num_cols * sizeof (char));
}
// go through the board and set all values equal to -1
for (r = 0; r < num_rows; r++) {
for (c = 0; c < num_cols; c++) {
aPtr[r][c] = '9';
printf("%c", aPtr[r][c]);
}
printf("\n");
}
}
void printBoard(int num_rows, int num_columns, char **aPtr) {
int row, col;
printf("\n");
puts("------ Connect *Four ------");
puts("Connect X Command Line Game");
// for fancy top of board frame
printf("&&");
for(col = 1; col < num_columns; col++) {
printf("====");
}
printf("===");
printf("&&\n");
// for the rows/columns of the board
for(row = num_rows - 1; row >= 0; row--){
printf("|");
for(col = 0; col < num_columns; col++){
if(aPtr[row][col] == '0') {
printf("| X ");
}
else if(aPtr[row][col] == '1') {
printf("| O ");
}
else {
printf("| ");
}
}
puts("||");
}
// for fancy bottom of board frame
printf("&&");
for(col = 1; col < num_columns; col++) {
printf("====");
}
printf("===");
printf("&&\n");
printf(" ");
if (col < 100){
for(col = 0; col < num_columns; col++) {
if (col < 10) {
printf(" %d ", col + 1);
}
else {
printf("%d ", col + 1);
}
}
puts("\n");
}
}
// *******************************************************************************************************
// *******************************************************************************************************
int main (int argc, char *argv[]) {
char *aPtr;
int height = 10;
int width = 5;
int i;
initialize(height, width, &aPtr);
printBoard(height, width, &aPtr);
}
Note that you are doing: aPtr[r][c] = '9'; so your board is empty, but if you change it to say 0you would get something like:
------ Connect *Four ------
Connect X Command Line Game
&&===================&&
|| X | X | X | X | X ||
|| X | X | X | X | X ||
|| X | X | X | X | X ||
|| X | X | X | X | X ||
|| X | X | X | X | X ||
|| X | X | X | X | X ||
|| X | X | X | X | X ||
|| X | X | X | X | X ||
|| X | X | X | X | X ||
|| X | X | X | X | X ||
&&===================&&
1 2 3 4 5
I'm assuming that's what you expected?
I have some problem about Nested Loop on C programming
I tried to print the list like this:
| |0|1|2|3|4|5|6|7|8|9|
|0| | | | | | | | | | |
|1| | | | | | | | | | |
|2| | | | | | | | | | |
|3| | | | | | | | | | |
|4| | | | | | | | | | |
|5| | | | | | | | | | |
|6| | | | | | | | | | |
|7| | | | | | | | | | |
|8| | | | | | | | | | |
|9| | | | | | | | | | |
but there are something problem when i type my code and display:
| |0|1|2|3|4|5|6|7|8|9|
|0|0|0|0|0|0|0|0|0|0|0|
|1|0|1|1|1|1|1|1|1|1|1|
|2|0|2|2|2|2|2|2|2|2|2|
|3|0|3|3|3|3|3|3|3|3|3|
|4|0|4|4|4|4|4|4|4|4|4|
|5|0|5|5|5|5|5|5|5|5|5|
|6|0|6|6|6|6|6|6|6|6|6|
|7|0|7|7|7|7|7|7|7|7|7|
|8|0|8|8|8|8|8|8|8|8|8|
|9|0|9|9|9|9|9|9|9|9|9|
There is my code :
void displayBoard(Cell board[BOARD_HEIGHT][BOARD_WIDTH], Player * player)
{
int i, j;
printf("| ");
for (j = 0; j < BOARD_WIDTH; j++)
{
printf("|%d",j);
}
printf("|\n");
for (i = 0; i < BOARD_HEIGHT; i++)
{
for (j = 0; j < BOARD_WIDTH; j++)
{
printf("|%d",i);
if (j == 0)
{
printf("|%d",j);
}
}
printf("|\n");
}
printf("\n");
}
Have someone can help for this condition: only one row and one column, other is empty.
At no point in the body of the inner loop are you printing spaces. You're instead printing the value of i, which is the column number.
printf("|%d",i);
if (j == 0)
{
printf("|%d",j);
}
Instead, print i only on the first iteration and print the space each time:
if (j == 0) {
printf("|%d",i);
}
printf("| ");
Output:
| |0|1|2|3|4|5|6|7|8|
|0| | | | | | | | | |
|1| | | | | | | | | |
|2| | | | | | | | | |
|3| | | | | | | | | |
|4| | | | | | | | | |
|5| | | | | | | | | |
|6| | | | | | | | | |
|7| | | | | | | | | |
|8| | | | | | | | | |
The key to getting this done correct is enclosing the repeating logic (the blank cells) in the loop while confining the specialized logic to be outside the loop:
void displayBoard(int height, int width)
{
int i, j;
printf("| ");
for (j = 0; j < width; j++) {
printf("|%d", j);
}
printf("|\n");
for (i = 0; i < height; i++) {
printf("|%d", i);
for (j = 0; j < width; j++) {
printf("| ");
}
printf("|\n");
}
}
Look mom! no ifs
This is how you should code it:
void displayBoard(Cell board[BOARD_HEIGHT][BOARD_WIDTH], Player * player)
{
int i, j;
printf("| ");
for (j = 0; j < BOARD_WIDTH; j++)
{
printf("|%d",j);
}
printf("|\n");
for (i = 0; i < BOARD_HEIGHT; i++)
{
for (j = 0; j < BOARD_WIDTH; j++)
{
if (j == 0)
{
printf("|%d",i);
}
printf("| ");
}
printf("|\n");
}
printf("\n");
}
There are couple of issues with your current code:
You printf("|%d",i); in the inner loop for every j while what you really want is just to print it when j == 0 once. The rests, you want to printf("| "):
printf("| "); //you want to make this blank
if (j == 0)
{
printf("|%d",i); //you want to print i here, not j
}
between the print of number when j == 0 and the print of blank should be reversed:
if (j == 0)
{
printf("|%d",i); //you want to print i here, not j
}
printf("| "); //put the blank printing after the number
You are printing the number i in every line, when you want a blank or the array data. you also need to get rid of printing a zero every row, which is what you have coded in. Here is that part of the code corrected:
for (i = 0; i < BOARD_HEIGHT; i++)
{
for (j = 0; j < BOARD_WIDTH; j++)
{
if (j == 0)
{
printf("|%d",i);
}
printf("| "); ///or printf("|%d",board[i][j]) if you are after stored data
}
printf("|\n");
}
hope this helps.
Try this:
#define BOARD_HEIGHT 10
#define BOARD_WIDTH 10
void displayBoard()
{
int i, j;
printf("| ");
for (j = 0; j < BOARD_WIDTH; j++)
{
printf("|%d", j);
}
printf("|\n");
for (i = 0; i < BOARD_HEIGHT; i++)
{
printf("|%d", i);
for (j = 1; j < BOARD_WIDTH; j++)
{
printf("| ");
}
printf("| |\n");
}
}
BTW, there is no need to pass any arguments to this function.