用matlab写一个扫雷小游戏
时间: 2023-12-29 15:16:53 浏览: 254
好的,我将为您提供一个简单的扫雷小游戏的 MATLAB 代码。该代码基于 MATLAB 的 GUI 界面,使用了一些基本的图形组件,如 pushbutton、axes 和 uicontrol。
```matlab
function Minesweeper
% Create the main figure window and set its properties
fig = figure('Name', 'Minesweeper', ...
'NumberTitle', 'off', ...
'MenuBar', 'none', ...
'Toolbar', 'none', ...
'Resize', 'off');
% Define the size of the game board
boardSize = [10, 10];
numMines = 10;
% Generate the game board
board = generateBoard(boardSize, numMines);
% Set the colors for the board
colors = {'w', 'k', 'b', 'g', 'r', 'c', 'm', 'y'};
% Initialize the GUI components
boardAxes = axes('Parent', fig, ...
'Units', 'normalized', ...
'Position', [0.1, 0.2, 0.8, 0.7]);
mineCountText = uicontrol('Parent', fig, ...
'Style', 'text', ...
'Units', 'normalized', ...
'Position', [0.1, 0.1, 0.3, 0.05], ...
'String', ['Mines remaining: ', num2str(numMines)]);
restartButton = uicontrol('Parent', fig, ...
'Style', 'pushbutton', ...
'Units', 'normalized', ...
'Position', [0.6, 0.1, 0.3, 0.05], ...
'String', 'Restart', ...
'Callback', @restartGame);
% Draw the game board
drawBoard();
% Define the function to generate the game board
function board = generateBoard(boardSize, numMines)
% Initialize the board with all zeroes
board = zeros(boardSize);
% Randomly place the mines on the board
mineLocations = randperm(boardSize(1)*boardSize(2), numMines);
[mineRows, mineCols] = ind2sub(boardSize, mineLocations);
for i = 1:numMines
board(mineRows(i), mineCols(i)) = -1;
end
% Calculate the number of mines adjacent to each cell
for i = 1:boardSize(1)
for j = 1:boardSize(2)
if board(i,j) ~= -1
adjacentMines = 0;
for k = max(1,i-1):min(boardSize(1),i+1)
for l = max(1,j-1):min(boardSize(2),j+1)
if board(k,l) == -1
adjacentMines = adjacentMines + 1;
end
end
end
board(i,j) = adjacentMines;
end
end
end
end
% Define the function to draw the game board
function drawBoard()
% Clear the axes
cla(boardAxes);
% Draw each cell on the board
for i = 1:boardSize(1)
for j = 1:boardSize(2)
% Determine the color of the cell
if board(i,j) == -1
cellColor = 'k';
elseif board(i,j) == 0
cellColor = 'w';
else
cellColor = colors{board(i,j)};
end
% Create the pushbutton for the cell
cellButton = uicontrol('Parent', fig, ...
'Style', 'pushbutton', ...
'Units', 'normalized', ...
'Position', [(j-1)/boardSize(2), (boardSize(1)-i)/boardSize(1), 1/boardSize(2), 1/boardSize(1)], ...
'BackgroundColor', cellColor, ...
'Callback', {@clickCell, i, j});
end
end
end
% Define the function to handle cell clicks
function clickCell(~, ~, row, col)
% Determine the value of the clicked cell
cellValue = board(row, col);
% Check if the cell has already been clicked
if cellValue == -2
return;
end
% Update the cell color and value
set(findobj('Parent', fig, 'Type', 'uicontrol', 'Style', 'pushbutton', 'UserData', row, 'UserData', col), 'BackgroundColor', 'g');
if cellValue == -1
% Game over if a mine is clicked
set(mineCountText, 'String', 'Game over!');
board(row, col) = -2;
drawBoard();
elseif cellValue == 0
% Recursively click all adjacent cells if the clicked cell is empty
board(row, col) = -2;
for i = max(1,row-1):min(boardSize(1),row+1)
for j = max(1,col-1):min(boardSize(2),col+1)
clickCell([], [], i, j);
end
end
else
board(row, col) = -2;
end
% Check if the game has been won
if sum(sum(board == -2)) == boardSize(1)*boardSize(2) - numMines
set(mineCountText, 'String', 'You win!');
end
end
% Define the function to restart the game
function restartGame(~, ~)
board = generateBoard(boardSize, numMines);
drawBoard();
set(mineCountText, 'String', ['Mines remaining: ', num2str(numMines)]);
end
end
```
此代码可以生成一个简单的扫雷小游戏,玩家可以通过单击每个方格来揭示其内容。如果单击到地雷,游戏结束。如果单击空方格,则会揭示周围的所有方格。游戏胜利的条件是标记所有地雷并揭示所有非地雷方格。
阅读全文