Using variable in grid (columns/rows)

I made dots and boxes but I want the user to select the size of the board (4-10). I can do everything, but I want to make it in a for loop and not copy and paste the code 7 times for every solution of 4-10. The code that I am have trouble with is: string grid[gridSize * 2][gridSize * 3] = {code}. I tried doing it without the *2/3, but that wasn't the problem (the reason I have it is because in-between the dots are lines that I have the enter which I figured out fine). Any solution would be great. Either a way of making that line of code work so I can use the for loop, or any other way without copying and pasting 7 times. Feel free to post advanced material but if you do please explain it. I am relatively new to C++ and coding in general. Thanks in advanced.

BTW: It says "Variable lenth array of non-POD element type 'string' (aka 'basic_string<char, char_traits<char>, allocator<char>>') and I'm using XCode.

I also heard something about a dynamic allocated array or vectors, but I don't know exactly what it is or how to use it.
Last edited on
I recommend using a char array since strings are immutable. That means each time you change a string, new memory needs to be allocated on your computer, whereas with chars the memory is only changed. With a string array you will have an array of pointers to the value the string stores. The string implementation hide the pointers for your convenience. With chars the pointers are always explicit.

With a char array here's what I've got:
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
	int gridSize = 10;
	int d = (gridSize * 3) +1;
	char **grid = new char*[d];
	for( int i = 0; i < d; i++ )
		grid[i] = new char[d];

	for( int i = 0; i < d-1; i++ )
	{
		for( int j = 0; j < d; j++ )
		{
			grid[i][j] = '.';
			j++;
			grid[i][j] = '-';
		}
		grid[i][d-1] = '.';
		i++;
		for( int j = 0; j < d-1; j++ )
		{
			grid[i][j] = '|';
			j++;
			grid[i][j] = ' ';
		}
		grid[i][d-1] = '|';
	}
	for( int i = 0; i < d; i++ )
	{
		grid[d-1][i] = '-';
	}


The confusing part is the +1 to the dimensions. It add room for the bottom and right most data. In my code all boxes are filled out so that you know where the lines will be. The space character I put should always be a space character.
Last edited on
I might be able to figure it out from here, but how do you tell where the moves are if there are always lines there
Topic archived. No new replies allowed.