Draw 2d grid with blank cells

Hi im trying to draw a 2d grid array for a battleships game. I need the grid to have a border around each cell does anyone know how to do this. so far my code is:

#include<iostream>

using namespace std;

int main(){
int grid[5][5] = {{0}};
int x, y;

for(x=0;x<5;x++){
for(y=0;y<5;y++){
cout<<grid[x][y] == 0;
}
cout<<endl;
}
return 0;
}

You mean something like that?
┌─┬─┬─┬─┬─┬─┬─┬─┬─┬─┐
│ │ │ │*│ │ │ │ │ │ │
├─┼─┼─┼─┼─┼─┼─┼─┼─┼─┤
│ │█│█│█│▒│ │ │ │ │ │
├─┼─┼─┼─┼─┼─┼─┼─┼─┼─┤
│ │ │ │ │ │ │ │ │ │ │
├─┼─┼─┼─┼─┼─┼─┼─┼─┼─┤
│ │ │ │ │ │ │ │ │ │ │
~~~~~~~~~~~~~~~~~~~~~
* — missed shot
█ — ship
▒ — hit ship
Graphics are not part of C++ and are OS and hardware dependent.

If you're running in a Windows console window, you can try the following:

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
37
38
39
40
41
42
43
44
45
46
47
48
49
    static const unsigned char DBL_UL_CORNER = 201;
    static const unsigned char DBL_HORIZ_LINE = 205;
    static const unsigned char DBL_TOP_TEE = 209;
    static const unsigned char DBL_BOTTOM_TEE = 207;
    static const unsigned char DBL_LEFT_TEE = 199;
    static const unsigned char DBL_RIGHT_TEE = 182;
    static const unsigned char DBL_UR_CORNER = 187;    
    static const unsigned char DBL_LL_CORNER = 200;    
    static const unsigned char DBL_LR_CORNER = 188;    
    static const unsigned char DBL_VERT_LINE = 186;    
    static const unsigned char SINGLE_HORIZ_LINE = 196;
    static const unsigned char SINGLE_VERT_LINE = 179;        
    static const unsigned char SINGLE_CROSS = 197;  

void DisplayUpperLine (int len) 
{    putc (DBL_UL_CORNER);
    for (int i=0;i<len; i++)
    {   putc (DBL_HORIZ_LINE);
         if (i == len-1)
	putc (DBL_UR_CORNER);
        else
            putc (DBL_TOP_TEE);
    }	        
    cout << endl;
}

void DisplayMiddleLine (int len) 
{   putc (DBL_LEFT_TEE);
     for (int i=0;i<len; i++)
    {   putc (SINGLE_HORIZ_LINE);
         if (i == len-1)
	putc (DBL_RIGHT_TEE);
        else
              putc (SINGLE_CROSS);
    }	        
    cout << endl;
}

void DisplayBottomLine (int len) 
{   putc (DBL_LL_CORNER);
    for (int i=0;i<len; i++)
    {   putc (DBL_HORIZ_LINE);
        if (i == len-1)
            putc (DBL_LR_CORNER);
        else
            putc (DBL_BOTTOM_TEE);
    }	        
    cout << endl;
}


PLEASE USE CODE TAGS (the <> formatting button) when posting code. It makes it easier to read your post and also to reply to it.
Last edited on
I'd say that you should stay away from attempting to make graphical applications in the console. I recommend using a graphic library such as SFML or SDL (versions 2.x regarding both). It's much easier to just use them instead of hacking the console to do something it isn't supposed to do.
I figured it out, I used the extended ASCII table to border each cell

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
37
38
39
40
41
42
43
44
45
46
47
48
#include<iostream>
#include<ctime>

void draw_grid();		//declare function

//Name: Cian Feehan
//Project: Battleships Game

using namespace std; 

char grid[5][5] = {{255}};

int main(){

		draw_grid();
  

	
	return 0;
}

//-----------------------------------------------------------------------------------------------------------------------
void draw_grid(){
 	int x,y;
  char topl[25]={201,205,205,205,203,205,205,205,203,205,205,205,203,205,205,205,203,205,205,205,187};
  char midl[25]={204,205,205,205,206,205,205,205,206,205,205,205,206,205,205,205,206,205,205,205,185};
  char bottoml[25]={200,205,205,205,202,205,205,205,202,205,205,205,202,205,205,205,202,205,205,205,188};
  //clrscr();
  cout<<endl;
  cout<<topl<<endl;
  for(x=0;x<5;x++)
   {
   	for(y=0;y<5;y++)
   	 {
   	    if(y==0)
   	     cout<<char(186);
		cout<<char(32)<<grid[x][y];
		if (grid[x][y] <10)
		cout<<char(32)<<char(186);
	    else
	    cout<<char(186);
     }
     cout<<endl;
    if (x<4)
     cout<<midl<<endl;
   }
   cout<<bottoml<<endl;
  }



#include<iostream>
#include<ctime>

void draw_grid();		//declare function

//Name: Cian Feehan
//Project: Battleships Game

using namespace std; 

char grid[5][5] = {{255}};

int main(){

		draw_grid();
  

	
	return 0;
}

//-----------------------------------------------------------------------------------------------------------------------
void draw_grid(){
 	int x,y;
  char topl[25]={201,205,205,205,203,205,205,205,203,205,205,205,203,205,205,205,203,205,205,205,187};
  char midl[25]={204,205,205,205,206,205,205,205,206,205,205,205,206,205,205,205,206,205,205,205,185};
  char bottoml[25]={200,205,205,205,202,205,205,205,202,205,205,205,202,205,205,205,202,205,205,205,188};
  //clrscr();
  cout<<endl;
  cout<<topl<<endl;
  for(x=0;x<5;x++)
   {
   	for(y=0;y<5;y++)
   	 {
   	    if(y==0)
   	     cout<<char(186);
		cout<<char(32)<<grid[x][y];
		if (grid[x][y] <10)
		cout<<char(32)<<char(186);
	    else
	    cout<<char(186);
     }
     cout<<endl;
    if (x<4)
     cout<<midl<<endl;
   }
   cout<<bottoml<<endl;
  }


I need to generate 5 random co-ordinates on the grid. Does anyone know what would be the best way of doing this? Any tips or useful links would be appreciated
Topic archived. No new replies allowed.