Mark "X" in 2D board

I managed to create a 2D board containing random numbers. My task now is how could I mark "X" in that board once any number in the board is selected by entering coordinate of x, y.

For example :

Before :
1
2
3
4
5
6
|   8   |  -12  |  18   |
 ------------------------
|   19  |  18   |  14   |
 ------------------------
|   -11 |  19   |  24    |
 ------------------------


After :
1
2
3
4
5
6
7
|   8   |  -12  |  18   |
 ------------------------
|   19  |  18   |  14   |
 ------------------------
|   -11 |  19   |  X    |
 ------------------------
Number selected = 24



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
void PrintGrid(int& BoxSize){

    cout << setw(12);
    for (int z = 1; z <= BoxSize; z++) {
            cout << z << setw(7);
    }
    cout << "\n";
   for (int y = 1; y <= BoxSize; y++) {

        cout << "\t";
        for(int i = 1;i <= BoxSize; i++)
        cout << "-------";
        cout << endl;
        cout << setw(3) << "Row " << y;
     for (int x = 1; x <= BoxSize; x++) {

//        gridRand[x][y] = rand() % 61 + -30;
        cout << setw(3) << "|" << setw(4) << gridRand[x][y] << setw(4);
        }
        cout << "|" << endl;
    }
            cout << "\t";
            for(int i = 1;i <= BoxSize; i++)
            cout << "-------";
            cout << endl;
}


Any suggestion guys?..
Last edited on
Hi azad
In my opinion, since you use PrintGrid to print the board out, I suggest you clear the sence(i.e. the console), then re-print the board, but this time you replace the selected position by the mark "X".

You could use system("cls"); to clear the console in windows.

Put a check function in the deepest inner loop in PrintGrid to place the mark.
1
2
3
4
5
6
7
8
9
10
...
for (int x = 1; x <= BoxSize; x++) {

//        gridRand[x][y] = rand() % 61 + -30;
        if (sel_x==x && sel_y==y) cout << setw(3) << "|" << setw(4) << "X" << setw(4);
        else  cout << setw(3) << "|" << setw(4) << gridRand[x][y] << setw(4);
        }
        cout << "|" << endl;
    }
...


Hope this would be helpful.
Dear Yueeng

You could use system("cls"); to clear the console in windows.


You meant the system("cls") should be put in the PrintGrid function or outside the function?

Based on what you suggested, it did mark "X" in that board but it will undo the "X" mark once I selected the next number. I want the "X" to remain in that particular board cell as the next n numbers are selected.

For example :

Before :
1
2
3
4
5
6
|   8   |  -12  |  18   |
 ------------------------
|   19  |  18   |  14   |
 ------------------------
|   -11 |  19   |  24    |
 ------------------------


After :
1
2
3
4
5
6
7
|   8   |  -12  |  18   |
 ------------------------
|   19  |  18   |  14   |
 ------------------------
|   -11 |  19   |  X    |
 ------------------------
Number selected = 24


1
2
3
4
5
6
7
|   X   |  -12  |  18   |
 ------------------------
|   19  |  18   |  14   |
 ------------------------
|   -11 |  19   |  X    |
 ------------------------
Number selected = 8


You see right how I want it to be?.. Btw thanks for the previous suggestion
I would recommend not bothering with system("cls") or indeed system("anything"). It's a bad habit (for all sorts of reasons) that you should not get into.
@Mats
Yes, you're right, that's not good, but I think the main problem here is to fix the mark,so I'd rather solve the clear scene problem as simple as possible, so we can focus on the mark. Maybe we could fix this minor drawback later. Thanks for your notation!

@azad
If you want to save the marks, maybe you should set up a stack(or simply an array of stuctrue like {int x;int y;}), then you add a instance of structure everytime you selected a position. In the check part, which is:
1
2
3
...
if (sel_x==x && sel_y==y) 
...

Replace this part with a check function, which go through your structrue array and check if this position that you're going to output is already marked. I think this will do, take your time and plan carefully, this change is not that difficult. Hope you enjoy programming(:
Topic archived. No new replies allowed.