Everything is working, Win / loss detection, already selected / flagged square detection, square flagging, menu, board etc.
But, Im using fixed arrays.
Ive been trying to make a Random Array but just cant get it to work. Here is my Test code for random array for a 24 x 24 grid (I want mines set to 99 but cant work out how to do that either so just set srand to 6 as that gives around 100 mines.
Here is my code, The Random mine placement is working fine its just increasing the surrounding squares count that I cant get working. any help would be great Thanks
Here is your program, with the array, boardm, zeroed out, meaning no mines. Then, 99 mines are placed in random locations on the board. Wasn't really sure what the last part of your program above, did, so I left it out.
// Mine Sweeper.cpp : main project file.
#include "stdafx.h"
#include <iostream>
#include <windows.h>
#include <string>
#include <cmath>
#include <stdio.h>
#include <crtdbg.h>
#include <limits>
#include <time.h>
#include <iomanip>
usingnamespace std;
int main()
{
int mines=0, X, Y;
int boardm[24][24] = {0}; // Set whole array to zeroes
srand((unsigned)time(0));
//------------------------------- Random mine placement, seems to work.
cout << "Placing Mines.." << endl;
do
{
do
{
X = rand()%24; // get a random row
Y = rand()%24; // get a random column
}while (boardm[X][Y] !=0 ); // do again if a mine is located there already
boardm[X][Y] = 1; // place a mine at location
mines++; // increase mine count
} while (mines < 99); // Keep doing this until we have 99 mines
for(X=0; X<24; X++)
{
for( Y=0; Y<24; Y++)
{
cout << boardm[X][Y] << " "; // Print out Mine Sweeper board
}
cout << endl; // After print a row, do a new line, and start a new row
}
cout << "There are " << mines << " Mines\n\n";
}
between his do while loop and his for loop (line 35).
The reason that it wasn't working was probably because you were changing some of the spaces AFTER printing them. It would shown up wrong during at least that print.
As for the random placement, the reason why rand%6 was giving you ~100 mines is because you were giving each of 576 spaces a 1/6 chance of having a mine. This gives you about 96 mines per generation (576/6). In order to get exactly 100 mines in random locations you need to take 100 mines and randomly determine a position for them, rather than giving each spot a chance to randomly have a mine. This is what whitenite1's code does.
[Edit] oops, his code makes mines as "1"s. For my code to work you have to make the mines as -1 by making line 32 of whitenite's code =-1 instead of =1