simple textual minesweeper game


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
// MINESWEEPER
void main()
{
           int b;
           do
           {
           cout << "Enter number of squares per side (2 - 10)" << endl;
           cin >> b;
           }while(b < 1 && b >= 10);
           int board[b][b];             //0 - 8 = # of mines, 9 is mine
           int revealed[b][b];  //1 is revealed
           int i = 0;                   
           int j = 0;
           int x = 0;                   
           int y = 0;
           int z;                               //number of mines
           int q;
           int t = 0;                           //game over input
           int dead = 0;
           do
           {
                         cout << "How many mines? (1 - " << ((b*b)-1) << ")" << endl;
                         cin >> z;
           }while(z <= 0 && z >= ((b*b)-1));
           for(i=0;i<b;i++)
                         for(j=0;j<b;j++)
                                    board[i][j] = 0;
           i = random(i, b);
           j = random(j, b);
           cout << "Generating board..." << endl;
           do
           {
                         i+=3;
                         j+=6;
                         x = random(i, b);
                         y = random(j, b);
                         if(board[y][x] != 9)
                         {
                                    board[y][x] = 9;
                                    z--;
                                    if((y-1) >= 0)
                                    if(board[y-1][x] != 9)
                                                  board[y-1][x]++;
                                    if((y-1) >= 0 && (x-1) >= 0)
                                    if(board[y-1][x-1] != 9)
                                                  board[y-1][x-1]++;
                                    if((x-1) >= 0)
                                    if(board[y][x-1] != 9)
                                                  board[y][x-1]++;
                                    if((y+1) < b)
                                    if(board[y+1][x] != 9)
                                                  board[y+1][x]++;
                                    if((y+1) < b && (x+1) < b)
                                    if(board[y+1][x+1] != 9)
                                                  board[y+1][x+1]++;
                                    if((x+1) < b)
                                    if(board[y][x+1] != 9)
                                                  board[y][x+1]++;
                                    if((y-1) >= 0 && (x+1) < b)
                                    if(board[y-1][x+1] != 9)
                                                  board[y-1][x+1]++;
                                    if((y+1) < b && (x-1) >= 0)
                                    if(board[y+1][x-1] != 9)
                                                  board[y+1][x-1]++;
                         }
           }while(z>0);
           for(i = 0; i < b; i++)
                         for(j=0;j<b;j++)
                                    revealed[i][j]=0;
           //board creation end
           //ask for input/check squares loop
           do
           {
                         system ("clear");
                         q = 0;
                         z = 0;
                         cout << "   ";
                         for(i=0;i<b;i++)
                                    cout << i << " ";
                         cout << endl;
                         for(i=0;i<b;i++)
                         {
                                    for(j=0;j<b;j++)
                                    {
                                                  if(j==0)
                                                                cout << i << " |";
                                                  if(revealed[i][j]==1)
                                                                reveal(board[i][j]);
                                                  else
                                                                cout << "_|";
                                                  if(j==(b-1))
                                                                cout << endl;
                                                  if(board[i][j]!=9 && revealed[i][j]==1)
                                                                q++;
                                                  if(board[i][j] == 9)
                                                                z++;
                                    }
                         }
                         if(q >= ((b*b) - z))
                         {
                                    cout << "You win!" << endl;
                                    dead = 1;
                         }
                         if(dead == 0)
                         {
                         cout << "X: ";
                         cin >> x;
                         cout << "Y: ";
                         cin >> y;
                         }
                         if(board[y][x] == 9)
                         {
                                    system ("clear");
                                    cout << "You hit a mine!" << endl;
                                    cout << "   ";
                                    for(i=0;i<b;i++)
                                                  cout << i << " ";
                                    cout << endl;
                                    dead = 1;
                                    for(i=0; i<b; i++)
                                    {
                                                  for(j=0;j<b;j++)
                                                  {
                                                                if(j==0)
                                                                           cout << i << " |";
                                                                if(board[i][j]==9)
                                                                           revealed[i][j]=1;
                                                                if(revealed[i][j]==1)
                                                                           reveal(board[i][j]);
                                                                else
                                                                           cout << "_|";
                                                                if(j==(b-1))
                                                                           cout << endl;
                                                  }
                                    }

                         }
                         if(board[y][x]==0)
                         {
                                    revealed[y][x] = 1;
                                    for(i=0;i<b;i++)
                                    {
                                                  for(j=0;j<b;j++)
                                                  {
                                                                if(i>(y-2)&&i<(y+2))
                                                                           if(j>(x-2)&&j<(x+2))
                                                                                         if(board[i][j]!=9)
                                                                                           revealed[i][j]=1;
                                                  }
                                    }
                         }
                         if(board[y][x]>0 && board[y][x]<9)
                                    revealed[y][x] = 1;
           } while(dead == 0);
           if (dead == 1)
                         replay();
}

void replay()
{
           char a;
           cout << "1) Replay 2) Quit" << endl;
           cin >> a;
           switch(a)
           {
                         case '1':
                                    main();
                                    break;
                         case '2':
                                    cout << "Quit" << endl;
                                    break;
                         default:
                                    cout << "Invalid input" << endl;
                                    replay();
                                    break;
           }
}

void reveal(int x)
{
           if(x == 0)
                         cout << "o|";
           else if(x == 9)
                         cout << "x|";
           else
                         cout << x << "|";
}

int random(int i, int b)
{
           long ran;
           int t = time(0);
           int s;
           srand(t);
           s = ran;
           ran = rand();
           ran >>= ran / (ran * i) + (i * 1337);
           ran = ran % b;
           return ran;
}
My friend wanted to improve his minesweeper. Please don't ask me questions on this because its not my program. Any help would be appreciated, and please keep the code at high school level. >.> thanks
Had to chop off the headers for it to fit, so here it is:

#include <iostream.h>
#include <time.h>
#include <stdlib.h>

void replay();
int random(int, int);
void reveal(int);
void main()
Does he have a problem with it, or is he only looking for suggestions?

One very important one would be don't call main(). He should move all (well, most of) that stuff in main() to a function and call that function again if the user indicates that he would like to play again.

BTW, main() returns int, also.

Other than that, my (admittedly tired) eye doesn't see any egregious errors. If he plans to distribute this at some point, he'll want to get rid of the system() call and #include the proper headers (no .hes):
1
2
3
4
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;


Does this help?
sorry, he was looking for suggestions like shortening, or organizing etc. And well, thanks for the suggestion :p. He'll be looking at this thread.
I thought you couldn't have an array of a variable size?
Like
int board[b][b]
because b is a variable.
You would need a linked list to have a dynamically sized object.
But somehow this works...
Last edited on
Topic archived. No new replies allowed.