If someone would be kind enough to look over this.

Hello everyone,

My first post here and only because of my inability to understand what I am doing wrong with my code. Just a little background, this is an assignment I have for my programming class and we are to build a working replica of the old game "Minesweeper" in c++. Well my design was to build two separate boards, a user board (That is displayed), and a game board (which hold the data ie. bomb locations and number of bombs surrounding certain area.) Right now I am building the Game Board that will be fed into the userboard but something is going terribly wrong with my code and it cant seem to place the appropriate numbers in the squares to define how many bombs are surrounding said squares. Take a look for yourself.
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
#include <iostream>
using namespace std;
//==============================================================================
//                                  Globals
//==============================================================================

     int X = 5; // How large the Board is going to be. (Therefore putting the number 5 will give you a 5 by 5 board)
     int NumBombs = (X*X) * (0.25); //Number Of Random Bombs on field (The lower the number the less the amount of bombs)
     
//==============================================================================
//                                  GameBoard
//==============================================================================

void GameBoard(int Row, int Col)
{
     //Variables----------------------------------------------------------------
     int Board[X][X];
     int BombRow;
     int BombCol;
     srand(time(0));
     //-------------------------------------------------------------------------
     
     //Initialize Board[R][C]---------------------------------------------------
     for(int R=0; R<X; R++)
     {
             for(int C=0; C<X; C++)
             {
                     Board[R][C] = 0;
             }
             
     }
     //-------------------------------------------------------------------------
     
     //Puts a bomb in a random square-------------------------------------------
     for (int j=0; j <= NumBombs; j++)
     {
         
         BombRow = rand() % X;
         BombCol = rand() % X;
                   //cout << BombRow << BombCol; //used for testing
         Board[BombRow][BombCol] =-1;
     }
     //-------------------------------------------------------------------------
     
     //Displays Numbers for Columns and Rows, and fills board-------------------
     cout << "\t";
     for(int L=0; L<X; L++)
     {
             cout << L << "\t";
     }
     
     cout << "\n \n" << endl;
     
     for(int R=0; R<X; R++)
     {
             cout << R << "|\t";
             for(int C=0; C<X; C++)
             {
                     //----------------------------
                     if (Board[R][C] == -1)
                     {
                            if(Board[R-1][C-1] != -1)
                            {
                                    Board[R-1][C-1] = Board[R-1][C-1]+1;
                            }
                            if(Board[R-1][C]!= -1)
                            {
                                    Board[R-1][C] = Board[R-1][C]+1;
                            }
                            if(Board[R-1][C+1]!= -1)
                            {
                                    Board[R-1][C+1]= Board[R-1][C+1] + 1;
                            }
                            if(Board[R][C-1] != -1)
                            {
                                    Board[R][C-1]=Board[R][C-1] + 1;            
                            }
                            if(Board[R][C+1]!= -1)
                            {
                                    Board[R][C+1]=Board[R][C+1]+ 1;
                            }
                            if(Board[R+1][C-1] != -1)
                            {
                                    Board[R+1][C-1]=Board[R+1][C-1]+1;
                            }
                            if(Board[R+1][C] != -1)
                            {
                                    Board[R+1][C]=Board[R+1][C]+1;
                            }
                            if(Board[R+1][C+1] != -1)
                            {
                                    Board[R+1][C+1]=Board[R+1][C+1]+1;
                            }
                     }
                     
                     //------------------------------
                     cout << Board[R][C] << "\t";
             }
             cout << "\n \n";
     }
     //-------------------------------------------------------------------------
   
}

int main()
{
    //Variables-------------------
    int Row;
    int Col;
    //----------------------------
    
    GameBoard(Row, Col);
       
 
 
 
//--------------------
    int ForTesting;
    cin >> ForTesting;
//--------------------
    return 0;
}
========================================================================


Any help would be much appreciated.
Last edited on
[code] "Please use code tags" [/code]
Instead of all those ifs, use loops.
1
2
3
for(int K=-1; K<=1; ++K)
  for(int L=-1; L<=1; ++L)
    if( grid[a+K][b+L] == bomb )


You could use sentinels so you can threat the borders of the grid in the same way
So something like this?

1
2
3
4
5
6
7
8
9
10
11
12
//----------------------------
                     for(int k=-1; k<=1; k++)
                     {
                             for(int L=-1; L<=1;L++)
                             {
                                     if( Board[R+K][C+L] == -1 )
                                     {
                                         Board[R+K][C+L]++;
                                     }
                             }
                     }
//------------------------------  


instead of the billion ifs?
This doesn't make much sense to me. How does that highlight the surrounding 8 areas and change their vaules to +1? Now i just have changing the bombs from -1s to 0s. >.< Sorry, I just don't understand.

EDIT: Okay now i see how it works, it looks at each of the eight frames surrounding the currently selected box and tests if they are a bomb. But I dont understand how to add one to the number if it isnt a bomb :/
Last edited on
Topic archived. No new replies allowed.