帮我写一个2048游戏能在MATLAB里运行的代码
时间: 2023-12-20 20:03:25 浏览: 85
game2048_2048_matlab做2048_matlab_
5星 · 资源好评率100%
好的,这是一个简单的2048游戏MATLAB代码:
```matlab
function play_2048()
% 2048 game
% by Aneesh Komanduri
% Initialize game
board = zeros(4,4);
board = add_tile(board);
board = add_tile(board);
disp_board(board)
% Play game until game over
while ~game_over(board)
% Get user input
direction = input('Enter direction: ','s');
switch direction
case 'w'
board = move_up(board);
case 'a'
board = move_left(board);
case 's'
board = move_down(board);
case 'd'
board = move_right(board);
otherwise
disp('Invalid direction!')
end
% Add new tile and display board
board = add_tile(board);
disp_board(board)
end
% Game over message
disp('Game over!')
end
function disp_board(board)
% Display board
disp('----------------')
for i = 1:size(board,1)
fprintf('|')
for j = 1:size(board,2)
if board(i,j) == 0
fprintf(' ')
else
fprintf('%4d',board(i,j))
end
fprintf('|')
end
fprintf('\n----------------\n')
end
end
function board = add_tile(board)
% Add new tile to board (either 2 or 4)
if rand < 0.9
tile_value = 2;
else
tile_value = 4;
end
[i,j] = find(board == 0);
random_index = randi(length(i));
board(i(random_index),j(random_index)) = tile_value;
end
function board = move_up(board)
% Move tiles up
for j = 1:size(board,2)
column = board(:,j);
nonzeros = column(column ~= 0);
zeros = length(column) - length(nonzeros);
column = [nonzeros; zeros*0];
for i = 1:length(column)-1
if column(i) == column(i+1) && column(i) ~= 0
column(i) = 2*column(i);
column(i+1) = 0;
end
end
nonzeros = column(column ~= 0);
zeros = length(column) - length(nonzeros);
column = [nonzeros; zeros*0];
board(:,j) = column;
end
end
function board = move_down(board)
% Move tiles down
for j = 1:size(board,2)
column = board(:,j);
nonzeros = column(column ~= 0);
zeros = length(column) - length(nonzeros);
column = [zeros*0; nonzeros];
for i = length(column):-1:2
if column(i) == column(i-1) && column(i) ~= 0
column(i) = 2*column(i);
column(i-1) = 0;
end
end
nonzeros = column(column ~= 0);
zeros = length(column) - length(nonzeros);
column = [zeros*0; nonzeros];
board(:,j) = column;
end
end
function board = move_left(board)
% Move tiles left
for i = 1:size(board,1)
row = board(i,:);
nonzeros = row(row ~= 0);
zeros = length(row) - length(nonzeros);
row = [nonzeros, zeros*0];
for j = 1:length(row)-1
if row(j) == row(j+1) && row(j) ~= 0
row(j) = 2*row(j);
row(j+1) = 0;
end
end
nonzeros = row(row ~= 0);
zeros = length(row) - length(nonzeros);
row = [nonzeros, zeros*0];
board(i,:) = row;
end
end
function board = move_right(board)
% Move tiles right
for i = 1:size(board,1)
row = board(i,:);
nonzeros = row(row ~= 0);
zeros = length(row) - length(nonzeros);
row = [zeros*0, nonzeros];
for j = length(row):-1:2
if row(j) == row(j-1) && row(j) ~= 0
row(j) = 2*row(j);
row(j-1) = 0;
end
end
nonzeros = row(row ~= 0);
zeros = length(row) - length(nonzeros);
row = [zeros*0, nonzeros];
board(i,:) = row;
end
end
function game_over_flag = game_over(board)
% Check if game is over
game_over_flag = true;
for i = 1:size(board,1)
for j = 1:size(board,2)
if board(i,j) == 0
game_over_flag = false;
return
end
if i > 1 && board(i,j) == board(i-1,j)
game_over_flag = false;
return
end
if i < size(board,1) && board(i,j) == board(i+1,j)
game_over_flag = false;
return
end
if j > 1 && board(i,j) == board(i,j-1)
game_over_flag = false;
return
end
if j < size(board,2) && board(i,j) == board(i,j+1)
game_over_flag = false;
return
end
end
end
end
```
运行这个函数,你就可以玩2048游戏了!
阅读全文