Saturday, May 22, 2010

Minesweeper in C++?

Hello, I am trying to code a MineSweeper in C++. My question is, say that I want to use Double Dim arrays, how would I actually generate random mines, and input numbers from one to ten into the array, including mines which are represented by a star?

Minesweeper in C++?
http://answers.yahoo.com/question/index;...





You want to input into the array? Loop over it, repeatedly asking for input.





EDIT:


%26gt;%26gt; I want the array to be filled out either by a loop, or from a file


Fine, so either read in the mine distribution from a file, or use some random distribution to fill up the array.





%26gt;%26gt; how would I actually randomly place them into the double dim array?


So you know that random access to an array is something like array[i][j] where array is your variable name, i,j are indices. So, there's pretty much two ways of doing this.





1) Loop over the array. Since you have a 2D array, that's two loops, right? Because for each loop over a row, you need to loop over the column. Then use some probability function to determine whether each point in the array is a mine or not.





2) Create a vector of x,y coordinates using some probability function. Then loop over this vector of coordinates. So for each coordinate, you access the appropriate point in the array and set it to be a mine.





Also, what are Double Dim Arrays? You wouldn't happen to be a VB programmer would you? There's no such thing as Dim in C++.





EDIT2:


Amanda, you realize that array[i,j] syntax is not valid in C++? Your code is wrong.
Reply:For minesweeper on a square grid, you only need numbers zero through 8. Those numbers are determined by where the mines are placed. Probably the easiest way would be to place the mines first, and then calculate and insert the numbers.





To insert the mines, use a while loop like this:





X_DIM=10; //set this to how many columns


Y_DIM=10; //set this to how many rows


NUM_MINES=10; //set this to how many mines you want


x=0;


srand(time(null)); //dont forget to include time.h


while(x%26lt;NUM_MINES){


column=rand()%X_DIM;


row=rand()%Y_DIM;


if (GRID[column,row]!='*') {


GRID[column,row]='*';


x++;


}





}





Then to calculate the numbers, you would just look at the neighbors of each grid position and count the stars, so just use two for loops to go through the grid.





for(row=0;row%26lt;Y_DIM;row++)


for(column=0;row%26lt;X_DIM;column++){


if(GRID[column,row-1]=='*') COUNT++;


if(GRID[column-1,row-1]=='*') COUNT++;


if(GRID[column+1,row-1]=='*') COUNT++;


if(GRID[column-1,row]=='*') COUNT++;


if(GRID[column+1,row]=='*') COUNT++;


if(GRID[column,row+1]=='*') COUNT++;


if(GRID[column-1,row+1]=='*') COUNT++;


if(GRID[column+1,row+1]=='*') COUNT++;


}





Note that the above loop doesn't account that some of the GRID positions checked are out of range, and will likely cause issues, so make sure you include extra checks to make sure you don't run out of bounds.





EDIT:





Ack. Its late and Im tired. Of course the code is wrong. Instead of [x,y] it should be [x][y].





Too lazy to fix.

hibiscus

No comments:

Post a Comment