realestatezoqa.blogg.se

Minesweeper java
Minesweeper java













minesweeper java

The most naive implementation is to pick two random numbers (row and column) and place a mine there, but this will cause the board to have less mines than expected if the same coordinates are picked twice. For the initialization, you might choose to place mines and increment neighbors in one loop, or place mines and then iterate over all fields and count the mines in the neighborhood.Ī common error is to struggle to see the structure of the problem and make gigantic if clauses when looking at neighboring fields, instead of writing a simple for loop.Īn important part of this question is figuring out a way to place the mines. Some other important learnings from this question: What is the right way to report a problem if the requested number of mines is larger than the number of available squares? So in the extreme example above, it takes almost one try

minesweeper java minesweeper java

If mines occupy more than half of the cells, randomize empty cells instead of mines.ġ) if M NxN/2 try step 1 assuming that all cells are mined and we are trying to free up (NxN - M) spots. This question definitely points towards designing a simple class.Ĭreate an array with the specified number of mines at the beginning and use a random shuffle. The most effective solutions consider and include the following:















Minesweeper java