-2
\$\begingroup\$

My professor gave us a project: make a minesweeper clone. However, he said that he will give bonus points for the 3 fastest algorithms for board generation. I'm currently doing the algorithm that places the numbers. How should I do it? I thought of doing something like

For every mine, increment the value of adjacent cells

or

For all cells, count the number of adjacent mines

Which is faster? Or could you recommend a faster method? Thanks

\$\endgroup\$
2
  • \$\begingroup\$ It's safer to naively parallelize the second one (the first would require synchronization). \$\endgroup\$ Commented Mar 26, 2015 at 6:44
  • \$\begingroup\$ Submitting this question required more typing than it would have taken to implement both approaches and time them. \$\endgroup\$ Commented Mar 26, 2015 at 13:05

2 Answers 2

2
\$\begingroup\$

I think the fastest method will be by not generating the numbers at board generation (I know it's almost cheating). It's most likely fastest to only generate the number when the player clicks on a square rather then during board generation.

Secondly consider how you are going to generate mines, is each place on the board going to pass a check (loop over every square)? Or are you just going to generate random positions for each mine? How do you deal with someone entering that every single tile should be a mine? That's going to slow you down with the second type but the first one can be needlessly slow as well.

\$\endgroup\$
5
  • \$\begingroup\$ I think your solution is good, but yeah it is almost cheating. I still have to confirm if that method is okay. If it is not, then what should be the fastest method to do that during board generation? On mine generation, I managed to loop only as much as the number of mines. I think it's cheap enough with respect to time complexity. \$\endgroup\$ Commented Mar 26, 2015 at 13:43
  • \$\begingroup\$ That depends and again it's possible to cheat by only "placing" a mine after a click (based on some complex statistics). Otherwise it depends on how many mines you have. If you have very many mines (say 255/256 tiles) then it's fastest to just loop over every tile and make place by a given chance, if however you only have a few mines then only looping over the mines and placing randomly (while retrying on failure) will be faster. In between there is an area where either clever data structures or placing a mine and then skipping ahead a certain number of tiles will be faster. \$\endgroup\$ Commented Mar 26, 2015 at 14:00
  • \$\begingroup\$ On a compeletely different issue, is it better to generate numbers on board generation, or generate them while the user clicks? \$\endgroup\$ Commented Mar 28, 2015 at 1:14
  • \$\begingroup\$ If it's a quick board generation then you should obviously generate them on click. It shouldn't take a noticable amount of time to do durring a click (unless you screw up or the system if very slow). \$\endgroup\$ Commented Mar 28, 2015 at 6:44
  • \$\begingroup\$ Oh. So, it's only a matter of preference? Whether to place numbers at board generation or by user clicks? Thanks! \$\endgroup\$ Commented Mar 29, 2015 at 5:57
1
\$\begingroup\$
  • Create two dimensional array
  • Randomly place mines and place mine location to list
  • Foreach mines and add +1 to adjacent cells

Mine creation

int x = random.next(min, max);
int y = random.next(min, max);
mineList.add(new XYClass(x,y))

Number calculation

Foreach(XYClass xy : mineList)
  addPlusOneToAdjacentCells(xy);

This way, you don't even have to loop through all the cells, just those that are important.

\$\endgroup\$

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.