I am writing a dots and boxes program for a class and have most everything ready to go except I am having trouble printing the grid.
Note: The grid can be any size from 2×2 to 9×9. A two by two grid must print like this to standard out:
a + + + //after some moves: a +-+ +
|P|
b + + + b +-+ +
c + + + c + + +
1 2 3 1 2 3
I have a data structure for the dots, edges, and boxes. and the grid object has a one dimensional vector for each class.
ie dots is a vector of all of the points on the grid,
edges is a vector of all the edges on the grid (Each edge has two dots)
boxes is a vector of all the boxes on the grid (each box has four edges)
the boxes have a enum for who owns the box Player or Computer
and the edges have an bool for if they are taken or not, and also a bool for if they are vertical or not.
I am getting confused when I am trying to print the grid since the grid can be multiple sizes.
Since (size) edges print on the even(horizontal) rows, and (size+1) on the odd(vertical).
I hope I am explaining this clearly.
Thanks!
Take your edges and lay it out in a sequence (a vector).
This is what you noticed with horizontal = 2 and vertical = size + 1.
Now figure out the algorithm to get the different values for the different edges. Here is how I stored the edges into a vector based on your example:
When I printed out the grid, I used rows and columns in for loops.
The edge going from b1 to b2 is stored in position 5 (counting from 0, count the possible edges). How do you get this position?
You may want to stop here and figure the rest out yourself. I use pen and paper!
The edge from b1 to b2 (according to my algorithm) is part of the second row, first column (1, 0). When printing the horizontal edges, here’s how to calculate the edge’s position in the vector.
The edge from a2 to b2 is part of the first row, second column (0, 2). When printing the vertical edges you need to adjust it