MATLAB自动扫雷(2)——排雷插旗

2019-04-30 18:07:12 浏览数 (1)

前面已经实现了雷区的扫描,现在就是要根据已知情况计算哪个方块底下是雷、哪个方块底下是空白,在初级、中级难度下只要计算每个方块周围的8个方块就好。

code

如果周围8个方块和当中数字判断周围有雷

% 插旗

function banner()

global map

global blocks_x

global blocks_y

global left

global top

global block_width

global block_height

showmap()

for y=1:blocks_y

for x=1:blocks_x

if (1 <= map(x,y)) && (map(x,y) <= 8)

boom_number = map(x,y);

block_white = 0;

block_qi = 0;

for yy=(y - 1):(y 1)

for xx=(x - 1):(x 1)

if (1 <= yy) && (1 <= xx) && (yy <= blocks_y) && (xx <= blocks_x)

if ~((yy == y) && (xx == x))

if map(xx,yy) == 0

block_white = block_white 1;

elseif map(xx,yy) == -4

block_qi = block_qi 1;

end

end

end

end

end

if boom_number == block_white block_qi

for yy=(y - 1):(y 1)

for xx=(x - 1):(x 1)

if (1 <= yy) && (1 <= xx) && (yy <= blocks_y) && (xx <= blocks_x)

if ~((yy == y) && (xx == x))

if map(xx,yy) == 0

disp("插旗")

win.setMouse(left (xx-1) * block_width 5,...

top (yy-1) * block_height 5);

win.rightClick;

showmap();

end

end

end

end

end

end

end

end

end

end

code

如果周围8个方块和当中数字判断周围有空白方块

% 点击白块

function click_blank()

global map

global blocks_x

global blocks_y

global left

global top

global block_width

global block_height

showmap();

iscluck = 0;

for y=1:blocks_y

for x=1:blocks_x

if (1 <= map(x,y)) && (map(x,y) <= 8)

boom_number = map(x,y);

block_white = 0;

block_qi = 0;

for yy=(y - 1):(y 1)

for xx=(x - 1):(x 1)

if (1 <= yy) && (1 <= xx) && (yy <= blocks_y) && (xx <= blocks_x)

if ~((yy == y) && (xx == x))

if map(xx,yy) == 0

block_white = block_white 1;

elseif map(xx,yy) == -4

block_qi = block_qi 1;

end

end

end

end

end

if boom_number == block_qi && block_white > 0

for yy=(y - 1):(y 1)

for xx=(x - 1):(x 1)

if (1 <= yy) && (1 <= xx) && (yy <= blocks_y) && (xx <= blocks_x)

if ~((yy == y) && (xx == x))

if map(xx,yy) == 0

disp("点开")

win.setMouse(left (xx-1) * block_width 5,...

top (yy-1) * block_height 5);

win.leftClick;

iscluck = 1;

end

end

end

end

end

end

end

end

end

if iscluck == 0 && ~all(all(map~=0))

luck();

end

win.setMouse(50,50);

end

0 人点赞