Draw board on console

Hi everyone,
Ok, so I have this assignment to create a tetris game using C++. I'm trying to draw the board and the logic isn't working. I just want to put a border around the board. But, what I end up with is something like this:
*********** * *
* * * *
* * * *
* * * *

When it should be like this:
* *
* *
* *
* *
* *
* *
***********

What am I missing here> Here's my code:

void drawBoard(char board[30][20]){
system("CLS");
for (int i=0;i<30;i++){
for (int j=0;j<20;j++){
board[0][j]='*';
board[30][j]='*';
board[i][20]='*';
}
}


for (int i=0;i<30;i++){
for (int j=0;j<20;j++){
cout<<board[i][j];
}
}
Sorry, the drawing of the board didn't display here very well. But what I'm looking for is to have the asterik's print down the left and right side of the board and then the bottom row all the way accross. I'm getting a line of asterik's accross and then 4 columns of astericks.
You're putting an asterisk in each column of row 0 and 30 and an asterisk in each row of column 20. What's in the remaining positions of your array?

Try initializing the rest of you array with spaces and it should display better.

Also, you can't store in row[30] or column[20]. Arrays in C/C++ start at 0 and go to n-1.

I changed it to 29 and 19, and filled the rest in with ' ' blanks using an if statement, if ='*' then ' '. But still getting weird results....multiple columns, when there should only be two filled in with *'s.
If you try typing text, you'll see that it goes from left to right. If you press enter, you go one line down, and start typing again, from left to right.
Try to execute your code by yourself. Simply do what it says. You will find the mistake quickly:)

Actually your loop doesn't work really well. 30 times your program will do following instructions:

- set board[0][0-19] to '*'(for example, it's gonna set board[0][0] to '*' 30 times)
- set board [30][0-19] to '*' 30 times
- set board [0-29][20] to '*' 30 times

What you actually wanna do is:
- Set top row to '*'(once :) )
- Set most left column and most right column to '*'
- Set bottom row to '*'

To accomplish this, using for loop, I'd do something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
for(int i = 0; i < 30; i++)
{
    for(int j = 0; j < 20; j++)
    {
        if(i == 0 || i == 29 || j == 0 || j == 19)
//if we are filling first or last row, or first or last column, put there '*'
            board[i][j] = '*';
        else
                board[i][j] = ' ';
    }
}
for(int i = 0; i < 30; i++)
{
    for(int j = 0; j < 20; j++)
        cout<<board[i][j];
cout<<endl;


I believe it's what you aimed at. If you want to do it other way, like you want to have multiple rows, you have to change code.

Hope it helps.
Last edited on
That makes sense. Sometimes i overthink things...Thanks!!
I tried your code and still getting weird results. I really don't want to have to do this long hand. Any other suggestions? Is there a bug with visual studio 10 I don't know about?
@newbe25

Try this. It passes the board array to the function, fills the edges with asterisks, and prints them. I removed the System("CLS"); call since it's a bad habit to get into. Check out this site, and you'll find a bunch of other options for clearing the screen.

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
26
27
28
29
30
31
32
33
34
35
36
// Asterisk Box.cpp : main project file.

#include <iostream>

using namespace std;
void drawBoard(char board[30][20]);

int main(array<System::String ^> ^args)
{
	char board[30][20] ={' '}; // Fill array with spaces, or you'll get weird output
	drawBoard(board); // Send board array to function
	return 0;
}

void drawBoard(char board[][20])
{
	for (int i=0;i<30;i++)
	{
		for (int j=0;j<20;j++)
		{
			board[0][j]='*';
			board[29][j]='*'; // Goes from 0 to 29
			board[i][19]='*'; // and 0 to 19
			board[i][0]='*';
		}
	}

	for (int i=0;i<30;i++)
	{
		for (int j=0;j<20;j++)
		{
			cout << board[i][j];
		}
		cout << endl; // A new line after each row printed
	}
} 
That one worked! Thank you so much!! I will check out the link on other ways to clear screen.
Topic archived. No new replies allowed.