I am making minesweeper for the standard console... So every space is represented by a unsigned char. The first four bits tell how many mines are touching the space. The 5th bit is set if it has been revealed. The 6th bit is set if it is a mine. So I have 2 "mine fields". The real one and the one the user sees. The real one just keeps track of what spaces the mines are on. The user one is the one that keeps track of how many mines each space is touching. To reveal the board, there's a function called burn. It calls itself recursively. It's supposed to only call itself if the space it's looking at isn't revealed, but it does so anyway. Or it could be that the revealed bit wasn't set, but unless I am COMPLETELY stoned, this should set the revealed bit:
Code:
#define REVEALED 16
user_field[number] |= REVEALED;
That's excactly how I set it
I don't know, I'm almost insane with frustration. I beg you to help me. Well, here's the code:
Code:
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
#define MINE 32
#define REVEALED 16
#define MINECOUNT_MASK 15
#define RM 9
#define CM 6
unsigned int field_size = 1;
unsigned int difficulty;
unsigned short field_len;
unsigned char * user_field;
unsigned char * real_field;
void initarrays()
{
int loop;
for (loop = 0; loop < field_len; loop++)
real_field[loop] = 0;
for (loop = 0; loop < field_len; loop++)
user_field[loop] = 0;
}
void draw_board(bool board)
{
unsigned short loop;
unsigned short rowcount = 0;
if (board == true)
{
for (loop = 0; loop < field_len+1; loop++)
{
if (rowcount == field_size * RM)
{
cout << endl;
rowcount = 0;
}
rowcount++;
if (user_field[loop] & REVEALED)
{
if (real_field[loop] & MINE)
cout << '*';
else
cout << (int)(real_field[loop] & MINECOUNT_MASK);
}
else
cout << "#";
}
}
if (board == false)
{
for (loop = 0; loop < field_len+1; loop++)
{
if (rowcount == field_size * RM)
{
cout << endl;
rowcount = 0;
}
rowcount++;
if (real_field[loop] & MINE)
cout << '*';
else
cout << (int)(real_field[loop] & MINECOUNT_MASK);
}
}
}
void burn(unsigned short s)
{
//for testing
if (user_field[s] & REVEALED)
{
cout << "\nrevealed\n" << endl;
return;
}
if (real_field[s] & MINE)
{
cout << "YOU LOSE" << endl;
return;
}
user_field[s] |= REVEALED;
signed short up, down, left, right;
up = -(field_size * RM);
down = field_size * RM;
left = -1;
right = 1;
char t = 0;
if (s + up >= 0)
{
cout << "up" << endl;
if (real_field[s + up] & MINE)
user_field[s] += 1;
if ((!(real_field[s + up] & MINE)) && (!(user_field[s + up] & REVEALED)))
burn(s + up);
}
if (s + down <= field_len)
{
cout << "down" << endl;
if (real_field[s + down] & MINE)
user_field[s] += 1;
if ((!(real_field[s + down] & MINE)) && (!(user_field[s + down] & REVEALED)))
burn(s + down);
}
if ((field_size * RM) % s != 0)
{
cout << "left" << endl;
if (real_field[s + left] & MINE)
user_field[s] += 1;
if ((!(real_field[s + left] & MINE)) && (!(user_field[s + right] & REVEALED)))
burn(s + left);
}
if ((field_size * RM) % (s + 1) != 0)
{
cout << "right" << endl;
if (real_field[s + right] & MINE)
user_field[s] += 1;
if ((!(real_field[s + right] & MINE)) && (!(user_field[s + right] & REVEALED)))
burn(s + right);
}
}
void scatter_mines()
{
unsigned short x, y, loop;
for (loop = 0; loop < field_size * difficulty * 3; loop++)
{
x = rand()%(field_size * RM);
y = rand()%(field_size * CM);
real_field[x + y * field_size * RM] |= MINE;
}
}
int main()
{
srand(time(0));
bool ok = false;
while (!ok)
{
cout << "Enter size (1-7)" << endl;
cin >> field_size;
if (field_size <= 7 && field_size >= 1)
ok = true;
}
field_len = (field_size*RM*field_size*CM) - 1;
ok = false;
while (!ok)
{
cout << "Enter difficulty (1-7)" << endl;
cin >> difficulty;
if (difficulty <= 7 && difficulty >= 1)
ok = true;
}
user_field = new unsigned char[field_len];
real_field = new unsigned char[field_len];
initarrays();
draw_board(true);
scatter_mines();
int burnn;
cout << "enter burn: ";
cin >> burnn;
burn(burnn);
draw_board(true);
return 0;
}
I realize all of the things I have missed, and I have noted them and will fix them as soon as I get this part to work. Thanks for your help.