0

This my first time using this website for help. I'm having trouble creating a tic-tac-toe board whose size is determined by the user inputting the rows and columns. The output is supposed to look like this.

enter image description here

I appreciate any who can help me with this problem.

void drawBoard(int rows, int columns)
{
    char board[rows][columns];

    for(int i = 0; i < rows; i++)
    {
        cout << "|";
        for(int j = 0; j < columns; j++)
        {
            cout << " --- ";

        }

        cout << endl;
    }
}
Olaf Dietsche
  • 72,253
  • 8
  • 102
  • 198
  • 3
    Look at loops, especially [`for loops`](http://en.cppreference.com/w/cpp/language/for). Beyond that, buy a good C++ book and learn the basics, e.g. https://stackoverflow.com/q/388242/1741542 – Olaf Dietsche Feb 11 '18 at 22:45
  • I'm using a nested for loop, but I'm having difficulty printing it correctly – Efosa Asemota Feb 11 '18 at 22:48
  • Then show what you have so far. Tell what it does, and tell what you expect instead. – Olaf Dietsche Feb 11 '18 at 22:51
  • You can edit your question and add the relevant code. – Olaf Dietsche Feb 11 '18 at 22:52
  • Ok I just edited it. And sorry, I'm new to this. – Efosa Asemota Feb 11 '18 at 22:58
  • This looks like a good start. Where do you have a problem with? If it is user input, look at [`std::cin`](http://en.cppreference.com/w/cpp/io/cin) – Olaf Dietsche Feb 11 '18 at 23:03
  • It's not user input. It's formatting it to look like how my professor wants it. To draw the board. – Efosa Asemota Feb 11 '18 at 23:05
  • And to also put the letter corresponding to each row and the number corresponding to each column. – Efosa Asemota Feb 11 '18 at 23:07
  • You told us what your assignment is. Waiting for the rest of your question. – Lightness Races in Orbit Feb 11 '18 at 23:36
  • Break the problem into smaller, easier problems. (That's one of the best high-level strategies I know.) Try writing code to print that row of numbers indicating the columns (and don't worry about getting the spacing exactly right for now). And *independently* try to write code to print the sets of three dashes ("--- --- --- ...") that form the horizontal lines. And *independently* try to write code to print the spaced vertical pipes ("| | | ...") that form the vertical lines. As these bits of code start to work, you can combine them. – Beta Feb 11 '18 at 23:47
  • It is bad to declare a fixed array at runtime. `char board[rows][columns];` Your compiler should have warned you about this `-Wvla` – Justin Randall Feb 11 '18 at 23:52

2 Answers2

1

To draw the board, you have several parts

  • Column headings
  • Board row with
    • Leading letter
    • Row with fields
    • Trailing letter
  • Column footer (which is the same as column headings)
void drawBoard(int rows, int cols)
{
    drawHeadings(cols);
    for (int i = 0; i < rows; ++i) {
        drawRow(i);
    }

    drawHeadings(cols);
}

To draw an ASCII digit, you can simply say

std::cout << static_cast<char>('0' + n);

or even simpler

std::cout << n;

An ASCII letter looks similar

std::cout << static_cast<char>('A' + n);
Olaf Dietsche
  • 72,253
  • 8
  • 102
  • 198
1

There are many ways to do this and this probably isn't the best approach but it gets the job done. You could have a helper function to print the dashed lines in between your rows, then another helper function to print the number headers for each column. The rows can be labeled like Olaf says, using ascii A plus your offset.

void printLines(int columns)
{
    cout << "   ";
    for(int i = 0; i < columns; i++)
    {
        cout << " ---";
    }
    cout << endl;
}

void printNumbers(int columns)
{
    cout << "   ";
    for(int i = 0; i < columns; i++)
    {
        cout << "  " << i+1 << " ";
    }
    cout << endl;
}

void drawBoard(int rows, int columns)
{
    printNumbers(columns);
    printLines(columns);
    for(int i = 0; i < rows; i++)
    {
        cout << " " << char('A'+i);
        for(int j = 0; j < columns; j++)
        {
            cout << " |  ";
        }
        cout << " | " << char('A'+i) << endl;
        printLines(columns);
    }
    printNumbers(columns);
}
Justin Randall
  • 2,243
  • 2
  • 16
  • 23
  • I appreciate your feedback. – Efosa Asemota Feb 12 '18 at 00:31
  • @EfosaAsemota thanks how about an upvote or accepting my answer if it helped you? The code will print out a grid exactly like your example output. – Justin Randall Feb 12 '18 at 00:33
  • I'm still new to the website, so it is not allowing me to upvote you, however I did accept that your answer did help me. This really did make it clear about how I could print the board. Thank you so much! – Efosa Asemota Feb 12 '18 at 00:39