solve minesweeper c++ in an OOP way

Hi, I am trying to solve a minesweeper game where i need to show how many mines are there around each box. Given a m x n array and the indexes where there are mines, how can I solve this in an object oriented way? I can do everything in a procedural way in my main class but that's not good. What sort of classes should I have?

Thanks a lot.

Sample input:

4 4

....

..*.

.*..

....

Sample output:

0111

12*1

1*21

1110
Class Board. Contains a board of Fields with method IsMine, or booleans(true means bomb, false means no bomb).

Board has method called IsMine(x,y) (<- this is optional), and method SurroundingMineCount(x,y), which simply checks adjacent fields for mines.
Finally, you can create method Print() that will print the whole board in the way you want.

Is that okay?
closed account (48bpfSEw)
An other approach is to define the mines as objects
which knows their neighboars and count them by itself.

The question in OOP is : what is defined as an object? In this case: the field or the mine or both? And the other view is: how about the efficence? Programming with threads running on differend CPUs is faster as procedural or oop. So: what is the focus of the programmer?
@MatthewRock,
I thought of the things you said but I wasn't sure if it was an OOP like approach. Thanks.
closed account (D80DSL3A)
Modeling an individual field space as an object can be quite useful.
I get good OOP mileage there.

However, a minefield object would be (in my model) a simple owner and manager of a 2D array of mineField objects.
There would be just 1 minefield in the program, so Singleton? This is an "anti-pattern" property.
It may be useful to enapsulate the fieldSpace array in a minefield class, but I just wrote global createField(), displayField(), destroyField(), etc.. functions.
I suppose that in a mineField class these would be mineField(), display() and ~mineField() respectively.

I made a debatable design choice for fieldSpace objects. I allow the exclusive nature of some properties to be reflected in a single char data member.
For example, a fieldSpace (FS) is either a mine OR it has a count. Not both.
Hence 1 variable to reflect this char cnt;// = 'M' or '0'-'9'
Member functions may be then:
1
2
3
4
5
6
7
bool fieldSpace::isMine()const { return cnt == 'M'; }

int fieldSpace::getCount()const 
{ 
    if( cnt == 'M' ) return -1;// I rest my case
    return (int)(cnt-'0');
}

Are you treating a space as only revealed or not revealed?
Are there no other states like marked with a '?' or flag?
If so a bool revealed data member will do.

Otherwise, I went with a 2nd char data member to record which of 4 states a fieldSpace could be in.
char st;// state = 'r'(revealed) or covered: '-', '?', 'f'
Two of those states are clearable: '-' (unmarked) and '?'.
The other 2 are unclearable: 'f'(locked) and 'r' = already cleared
Hence:
bool fieldSpace::isClearable()const { return (st == '-' || st == '?'); }
Otherwise just:
bool fieldSpace::isClearable()const { return !revealed; }
Constructor?
fieldSpace::fieldSpace( bool mined, char Count='0' ): st('-'), cnt(mined ? 'M' : Count) {}
Drawing?
1
2
3
4
5
void fieldSpace::show()const
{
    if( st == 'r' ) cout << cnt;  
    else cout << st;
}

Hope this gives you some useful ideas.

edit: corrections
Last edited on
This was truly great and very detailed. Thanks a lot. I shall go with this. @fun2code
@fun2code, a question for you.
So if I have 50x50 matrix, then I should create 100 fieldspace objects and store them in a 2-d vector for easy access?
closed account (D80DSL3A)
Not quite. There would be 50x50 = 2,500 fieldSpace objects. Only 100 of them have .cnt == 'M'.
Topic archived. No new replies allowed.