Having issues with Drawing a grid

i
Last edited on
You mean something like this?

        1   2   3   4   5   6   7   8   9
     -------------------------------------
   1 | 1 | 1 | 0 | 0 | 0 | 0 | 0 | 1 | 1 |
     -------------------------------------
   2 | * | 2 | 1 | 1 | 0 | 0 | 0 | 1 | * |
     -------------------------------------
   3 | 1 | 2 | * | 1 | 0 | 0 | 0 | 1 | 1 |
     -------------------------------------
   4 | 0 | 2 | 2 | 2 | 1 | 2 | 2 | 1 | 0 |
     -------------------------------------
   5 | 0 | 1 | * | 2 | 2 | * | * | 2 | 1 |
     -------------------------------------
   6 | 0 | 1 | 1 | 2 | * | 3 | 3 | * | 1 |
     -------------------------------------
   7 | 0 | 0 | 0 | 1 | 1 | 2 | 3 | 3 | 2 |
     -------------------------------------
   8 | 0 | 0 | 0 | 0 | 0 | 1 | * | * | 1 |
     -------------------------------------
   9 | 0 | 0 | 0 | 0 | 0 | 1 | 2 | 2 | 1 |
     -------------------------------------


Sort of stole the code from another post on the forum.
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include <iostream>
#include <iomanip>
#include <random>
#include <ctime>
#include <string>

int main()
{
    const unsigned int ROWS = 9;
    const unsigned int COLS = 9;
    const unsigned int MINES = 10;
    unsigned int board[ROWS][COLS];
    
    std::mt19937 gen(time(0));
    std::uniform_int_distribution<int> disR(0, ROWS - 1);
    std::uniform_int_distribution<int> disC(0, COLS - 1);
    
    for (unsigned int row = 0; row < ROWS; ++row)
        for (unsigned int col = 0; col < COLS; ++col)
            board[row][col] = 0;
    
    for (unsigned int i = 0; i < MINES; ++i)
    {        
        unsigned int row, col;
        
        do
        {
            row = disR(gen);
            col = disC(gen);
        } while (board[row][col] > 9);
        
        board[row][col] += 10;
        
        if (row > 0)
        {
            ++board[row - 1][col];
            if (col > 0)
                ++board[row - 1][col - 1];
            if (col < COLS - 1)
                ++board[row - 1][col + 1];
        }
        if (row < ROWS - 1)
        {
            ++board[row + 1][col];
            if (col > 0)
                ++board[row + 1][col - 1];
            if (col < COLS - 1)
                ++board[row + 1][col + 1];
        }
        if (col > 0)
            ++board[row][col - 1];
        if (col < COLS - 1)
            ++board[row][col + 1];
    }
    
    std::cout << "     ";
    for (unsigned int col = 0; col < COLS; ++col)
        std::cout << std::setw(4) << col + 1;
    std::cout << "\n     " + std::string(COLS * 4 + 1, '-') + '\n';
    
    for (unsigned int row = 0; row < ROWS; ++row)
    {
        std::cout << std::setw(4) << row + 1;
        for (unsigned int col = 0; col < COLS; ++col)
        {
            if (board[row][col] < 10)
                std::cout << " | " <<  board[row][col];
            else
                std::cout << " | " << '*';
        }
        std::cout << " |\n";
        std::cout << "     " + std::string(COLS * 4 + 1, '-') + '\n';
    }
    
    return 0;
}
yep thats what I mean, not sure how to get the lines to show though
Topic archived. No new replies allowed.