minesweeper

I need help for this project, It was possible for me to make it work but I need some help with the very last function called:

* void clearField(int A[][NCOLS], int B[][NCOLS], int x, int y){

and I also need to know how to add sound (maybe .mp3s, .wavs or something similar not computer beeps) and color (color only if possible to add it to C++ console), Thanks in advance!

this is what I have so far (hope you understand it, i see that this website removes indentation):

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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
// Mine Sweeper.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <ctime>

using namespace std;

const int NCOLS = 8;
const int NROWS = 8;
const int NBOMBS= 5;

void reset(int A[][NCOLS],int B[][NCOLS]);
void printArray(int A[][NCOLS]);
void BombPlace(int A[][NCOLS]);
void squareSetter(int A[][NCOLS]);
void reciever(int A[][NCOLS], int B[][NCOLS]);
void clearField(int A[][NCOLS],int B[][NCOLS], int x, int y);
void champ(int Metal[][NCOLS]);


int _tmain(int argc, _TCHAR* argv[])
{    srand(time(0));
    int board[NROWS][NCOLS];
    int mask[NROWS][NCOLS];
    char again;
    do{
    reset(board, mask);
    printArray(mask);
    BombPlace(board);
    squareSetter(board);
    //printArray(board);
    reciever(mask, board);
    cout << "Would you like to play again?"<<endl;
    cin >> again;
    }while(again == 'y');
    return 0;
}
void BombPlace(int A[][NCOLS]){
    int counter = 0;
    int i = 0, j = 0;

    while(counter < NBOMBS)
        {
    i = rand()%NROWS;
    j = rand()%NCOLS;
    if(A[i][j] == 0)
        {
         A[i][j] = -1;
         counter ++;
        }
    }
}

void squareSetter(int A[][NCOLS])
{
 int c;
    for(int i = 0; i< NROWS;i++)
    {
        c = 0;
        for(int j = 0; j < NCOLS; j++)
        {c=0;
            if (i < NROWS - 1)
                //process 1
            if (A[i + 1][j] == -1)
               c++;
       
         if(j < NCOLS-1){
             //process 2
            if(A[i][j+1]==-1)
                 c++;
       
          if(i<NROWS-1)
              //process 3
            if(A[i+1][j+1]==-1)
                             c++;
       
                }
             if(i>0)
            {//process 4
                if(A[i-1][j]== -1)
                                 c++;
               
              if(j<NCOLS-1)
                  //process 5
                if(A[i-1][j+1]==-1)
                  c++;
                }
          if(j>0)
            {  if(i<NROWS-1)
            //process 6
                {if(A[i+1][j-1]==-1)
                    c++;
                }
            //process 7
                 if(A[i][j-1]==-1)
                    {c++;}
            }
            if(i>0 && j>0)
            {//process 8
                if(A[i-1][j-1]==-1)
                                c++;
       
            }
            if(A[i][j] != -1)
                A[i][j]= c;
                   }

    }
   
}


void printArray(int A[][NCOLS])
{cout << endl;
    for(int i = 0; i < NROWS; i++)
    {for(int j = 0; j < NCOLS; j++)
            cout << A[i][j] << "  ";
        cout << "\n"<< endl;}
    cout << "\n\n\n" << endl;
}
void reset(int A[][NCOLS], int B[][NCOLS]){
    for(int i = 0 ;i < NROWS; i++)
        for(int j = 0 ;j < NCOLS; j++)
            A[i][j] =0;
    for(int i = 0 ;i < NROWS; i++)
        for(int j = 0 ;j < NCOLS; j++)
            B[i][j] = 9;
}

void reciever(int A[][NCOLS], int B[][NCOLS]){
    int x, y, metal =0;
   
    do
    {
    cout << "Enter coordinate" << endl;
    cin >> x >> y;
if(B[x][y] == 0)
    clearField(A, B, x, y);
if (B[x][y] == -1)
    {cout << "GAME OVER!" << endl;
     printArray(B);   
     break;   
    }
else
    {if(A[x][y] = 9)
        metal ++;
     A[x][y] = B[x][y];
     printArray(A);
    }
champ(A);
    }while(true);
}

void champ(int Metal[][NCOLS]){
    int Detector = 0;
    for(int i = 0; i < NROWS; i++)
        for(int j = 0; j < NCOLS; j++)
    if (Metal[i][j] == 9)
        Detector++;
    if (Detector == NBOMBS)
        cout << "Super Sweeping!" << endl;
}

void clearField(int A[][NCOLS], int B[][NCOLS], int x, int y){
//this needs some work. I can't figure out what's wrong. Try the puzzle thing you guys were talking about
    int i = x, j = y;
for(i; i < NROWS; i++)
{if(B[i][j] > 0)
break;
        for(j ;j < NCOLS; j++)
        {
    if(B[i][j] > 0)
    {A[i][j] = B[i][j];break;}
if(j < NCOLS-1)
    A[i][j+1] = B[i][j+1];
if(i < NROWS-1 && j < NCOLS-1)
    A[i+1][j+1] = B[i+1][j+1];
if(i < NROWS - 1)
    A[i+1][j] = B[i+1][j];
        }
}
for(i = x; i >= 0; i--)
{if(B[i][j] > 0)
break;
    for(j = y; j >= 0; j--)
        {
    if(B[i][j] > 0)
    {B[i][j] = B[i][j];break;}
    if(j > 0)
        A[i][j-1] = B[i][j-1];
    if( i > 0 && j > 0)
        A[i-1][j-1] = B[i-1][j-1];
    if(i > 0)
        A[i-1][j] = B[i-1][j];
    }
}
for(i = x; i < NROWS; i++)
{if(B[i][j] > 0)
break;
    for(j = y;j >= 0; j--)
        {
            if(B[i][j] > 0)
                {A[i][j] = B[i][j];break;}
            if(i < NROWS - 1)
                {A[i+1][j] = B[i+1][j];
                 A[i+1][j-1] = B[i+1][j-1];}
            A[i][j-1] = B[i][j-1];
        }
}
for(i = x; i > 0; i--)
{if(B[i][j] > 0)
break;
    for(j = y;j < NCOLS; j++)
        {
            if(B[i][j] > 0)
                {A[i][j] = B[i][j];break;}
            A[i-1][j] = B[i-1][j];
            if(j<NCOLS-1)   
                {A[i-1][j+1] = B[i-1][j+1];
                 A[i][j+1] = B[i][j+1];}
        }
}
}
Last edited on
closed account (D80DSL3A)
I would like to help but the issue is vague (what is the function supposed to do? What is it doing wrong? ). Also, since your code contains no comments I would have to study the entire program to understand how any portion of it works. Make it easier on us and you will get more responses.

I am attracted to your problem because I have written a minesweeper game myself (for windows, not the console) and because it is not just another of the endlessly repetitive CS101 homework problems which appear on this site.
closed account (1yvXoG1T)
My two cents: As far as I know, you can't use audio files (such as mp3, ogg, etc.) without some sort of audio library.
Get rid of #include <stdafx.h> and make sure to #include <windows.h> .
Also, your main function on line 23 should read

  int main( int argc, char* argv[] )

(but you really only need int main()).

Line 147 should read { if(A[x][y] == 9).
Line 169 should read for(; i < NROWS; i++) and likewise,
line 172 should read for(; j < NCOLS; j++).

When the game is won, it doesn't end. For example:
Enter coordinate
7 7

1  1  0  0  0  0  0  0

9  2  1  0  0  0  1  1

2  9  1  0  0  0  1  9

1  1  1  0  0  0  1  1

0  0  0  0  0  0  0  0

0  0  0  0  1  2  2  1

0  0  0  0  1  9  9  1

0  0  0  0  1  2  2  1





Super Sweeping!
Enter coordinate
This gets repeated over and over. (It printed ten times for me, but I haven't looked to see if that number has any importance.)


Minesweeper typically lets the user do one of two things to a cell: mark it as (potentially) mined, or reveal it. Your game only lets you reveal.


Reading just the numbers is a little rough on the player, and the gameboard prints badly when the "-1" mines are displayed. You can use a lookup table to print something nicer to look at:

1
2
3
4
5
6
7
const char* CellImages = "@012345678~";
void DrawCellImage( int CellValue ) {
  if ((CellValue >= -1) && (CellValue <= 9))
    cout << CellImages[ CellValue ];
  else
    cout << '?';
}


Since you are on Windows, I suggest you take a look over at
http://www.google.com/search?btnI=1&q=msdn+SetConsoleTextAttribute
(click on the Console Functions on the left side for all the console functions). These will allow you to do things like position the cursor, set colors, get single keypresses, etc.


Finally, MS has an audio library as part of the system. One of these ought to help:
http://www.google.com/search?btnI=1&q=msdn+sndPlaySound
http://www.google.com/search?btnI=1&q=msdn+PlaySound
If you use it, make sure to link with the Microsoft Multimedia library (link to libwinmm[/b]).


Hope this helps.
I did not understand about this website and code suggested to color font in console:

SetConsoleTextAttribute(GetStdHandle();

http://www.google.com/search?btnI=1&q=msdn+SetConsoleTextAttribute

how exactly do I use it and how it is possible to color only 1 character for example, my idea is to color each instance of the game ( I refer to numbers = 9 as a starting field one color, 0 as empty field another color, -1 as mines another color and 1 2 3 and 4 others colors) so the player can actually see what is going on in the game.
Can someone tell me how and where to place the code to achieve this?
THANKS for your great help!! I'm working on each of your suggestions!!
Last edited on
Topic archived. No new replies allowed.