Need help with mines in the minesweeper game

So, my minesweeper Java game is presented as int [] [], where -1 is a mine. When I initialize my game, I need to randomly place x number of mines.

What is an elegant way to do this? I was thinking of using an ArrayList with the coordinates of each cell, randomly selecting it, changing the state of int [] [], and then deleting this Point. This ensures that no points are selected twice.

Is there a more elegant way to do this?

+3
source share
4 answers

I would do it the same way, but in a slightly different way. Use a map processing algorithm.

. ([0,0], [0,1] .. [0,max], [1,0] .. [max, max]). " ", . .

+4

. , , .

+1

, ?

, :

, 4x4.

, 4- :

2 ^ 4 = 16 = > 0... 15

"":

1,16,0,3

:

0001
1111
0000
0011
0

This is in python. But it prints the (x, y) min coordinates. The result is sorted, but it does not matter.

from random import sample
import sys

def randomSquares(n, m, mines):
  return sorted([ (mine % m, mine // n) for mine in sample(xrange(n * m), mines)])

if __name__ == '__main__':

  print randomSquares(int(sys.argv[1]), int(sys.argv[2]), int(sys.argv[3]))
0
source

Source: https://habr.com/ru/post/1784575/


All Articles