mcudm001
mcudm001

Reputation: 99

printing dots and boxes

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 2x2 to 9x9. 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!

Upvotes: 3

Views: 3097

Answers (2)

Jon Coombs
Jon Coombs

Reputation: 2226

I implemented an HTML+JS version of this game using a 2D model of just cells (each with a list of edges) plus a separate 2D view model of all four things (h,v,c,d). See here for code snippets and a link to the full code and playable game: https://stackoverflow.com/a/30387118/1593924 .

That has the benefit of separating UI state/concerns, which have to do with rendering, from the 'real data' in the underlying model, which needs to understand quite different things:

  • that each cell 'touches' four edges and shares up to two of them with other cells,

  • how a cell becomes 'filled', and

  • how to handle players and extra turns when a player fills one (or two!) cells in a given turn.

In fact, the UI can render all four kinds of elements--vertices, v-edges, h-edges, and cells--as rectangles in a single grid, though the behavior attached to them is different between cells and edges (and omitted for vertices).

Upvotes: 1

Jamie Royer
Jamie Royer

Reputation: 329

Take your edges and lay it out in a sequence (a vector).

gridsize = 2 : hh vvv hh vvv hh

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:

Your first (empty) 2x2 grid: 00 000 00 000 00
Your second 2x2 grid:        10 110 10 000 00

When I printed out the grid, I used rows and columns in for loops.

// I consider the (row, col) to be the box.
// Each row loop prints one horizontal line and (possibly) one vertical line.
// Access to the edge vector is based on this (row, col) pair.
for (int row = 0; row < size + 1; row++) /* +1 to draw the bottom line. */
{
    if (row < size) /* Bottom most horizontal row not followed by vertical. */
        for (int col = 0; col < size + 1; col++) /* +1 to draw rightmost line. */
}

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.

Horizontal edge: pos = (row * (size + size + 1)) + col

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

Vertical edge: pos = (row * (size + size + 1)) + size + col

Upvotes: 3

Related Questions