Create Minesweeper Like Game

조회 수: 22 (최근 30일)
Krish Desai
Krish Desai 2015년 12월 1일
댓글: Guillaume 2015년 12월 1일
I create this board with my code:
1 2 3 4 5
1x x x x x
2x x x x x
3x k x x x
4x x x k x
5x x x x x
I want to replace the board above with the board below, that is all the 'k'='*' and any values surrounding k have a counter.
1 2 3 4 5
10 0 0 0 0
21 1 1 0 0
31 k 2 1 1
41 1 2 k 1
50 0 1 1 1
How would I go about doing this?
  댓글 수: 2
Walter Roberson
Walter Roberson 2015년 12월 1일
I do not understand about 'k'='*', and it is not clear why the x became 0 in places that are not surrounding k.
Image Analyst
Image Analyst 2015년 12월 1일
k is where a bomb resides. The other numbers are the number of 8-connected bombs that are next to that location. For example, row 3, column 3 is next to 2 bombs, so it's a 2. Row 3, column 4 is next to only one k, so it has a value of 1. Same as in the minesweeper game.

댓글을 달려면 로그인하십시오.

채택된 답변

Image Analyst
Image Analyst 2015년 12월 1일
Just use conv2() to count the bombs:
gameBoard = [...
'0' '0' '0' '0' '0'
'1' '1' '1' '0' '0'
'1' 'k' '2' '1' '1'
'1' '1' '2' 'k' '1'
'0' '0' '1' '1' '1']
% Find the bombs.
bombLocations = gameBoard == 'k'
% Sum up the bombs in a surrounding 3x3 window around each pixel.
nearbyBombs = conv2(double(bombLocations), ones(3), 'same')
% Create a new board that is characters and instead of numbers.
newBoard = num2str(nearbyBombs, '%1d')
% Replace the bomb locations with the letter k.
newBoard(bombLocations) = 'k'
You'll see:
gameBoard =
00000
11100
1k211
112k1
00111
bombLocations =
0 0 0 0 0
0 0 0 0 0
0 1 0 0 0
0 0 0 1 0
0 0 0 0 0
nearbyBombs =
0 0 0 0 0
1 1 1 0 0
1 1 2 1 1
1 1 2 1 1
0 0 1 1 1
newBoard =
00000
11100
11211
11211
00111
newBoard =
00000
11100
1k211
112k1
00111

추가 답변 (1개)

Guillaume
Guillaume 2015년 12월 1일
Please, use valid matlab syntax in your question, so we don't have to guess if the input is a cell array, a char array, or something else, and whether the spaces and headers are embedded in the matrix or not
Possibly:
in = ['xxxxx';
'xxxxx';
'xkxxx';
'xxxkx';
'xxxxx'];
%step 1: convert the input matrix into an array of 0 and 1, 1 where k is present
%if your input matrix is a different format, you'll have to figure how to do it on your own
inaslogical = in == 'k';
%step2: convolve the array of 0 and 1 with an array of 1:
out = conv2(double(inaslogical), ones(3), 'same')
  댓글 수: 2
Krish Desai
Krish Desai 2015년 12월 1일
Cell array, spaces and headers are embedded I believe
function board = makeboard(size)
board = cell(size+1,size+1);
% initialize the board
for i=1:size+1
for j=1:size+1
if i==1
if j<=size
board{i,j+1} = j;
board{j+1,i} = j;
end
elseif j==1
elseif rand < 1/5
board{i,j}='k';
else
board{i,j}= 'x';
end
end
end
end
Guillaume
Guillaume 2015년 12월 1일
Note: don't use the name size for a variable as you won't be able to use the extremely useful size function.
A more efficient way to generate your board:
function board = makeboard(boardsize)
symbols = 'xk';
playarea = num2cell(symbols(1 + (rand(boardsize) < 1/5)));
header = num2cell(1:boardsize);
board = [{[]}, header; header', playarea];
end
The gist of my answer (and IA's answer) still stands. You need to convert your play area into a logical array, where 1 stands for a mine. Then convolve with a square of 1 to get the number of neighbours.
With the given format:
ismine = cell2mat(board(2:end, 2:end)) == 'k';
neighbourcount = conv2(double(ismine), ones(3), 'same');
neighbourcount = num2cell(neighbourcount);
neighbourcount(ismine) = {'k'};
newboard = board;
newboard(2:end, 2:end) = neighbourcount;

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Strategy & Logic에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by