主要功能
1.用窗口界面实现2048游戏界面显示;
2. 在界面设置上下左右移动的按键;
3. 实现2048游戏功能;
4. 记录操作次数以及得分情况;
5. 通过矩阵实现。
function GUI_2048_OpeningFcn(hObject, eventdata, handles, varargin)
handles.output = hObject;
guidata(hObject, handles);
global number_matrix; % 设定4*4的矩阵
global step; % 按下按键的次数
global fraction; % 得分
global save_num; %矩阵中的最大值
number_matrix = zeros(4);
step = 0;
fraction = 0;
save_num = 0;
% 按下向上的按键
function pushbutton1_Callback(hObject, eventdata, handles)
global number_matrix;
global step;
step = step+1;
global fraction;
% 向上移动
push_up();
%¸给每个静态文本赋值
if number_matrix(1,1)~=0
set(handles.text11,'String',num2str(number_matrix(1,1)));
else
set(handles.text11,'String',' ');
end
if number_matrix(1,2)~=0
set(handles.text12,'String',num2str(number_matrix(1,2)));
else
set(handles.text12,'String',' ');
end
if number_matrix(1,3)~=0
set(handles.text13,'String',num2str(number_matrix(1,3)));
else
set(handles.text13,'String',' ');
end
if number_matrix(1,4)~=0
set(handles.text14,'String',num2str(number_matrix(1,4)));
else
set(handles.text14,'String',' ');
end
if number_matrix(2,1)~=0
set(handles.text21,'String',num2str(number_matrix(2,1)));
else
set(handles.text21,'String',' ');
end
if number_matrix(2,2)~=0
set(handles.text22,'String',num2str(number_matrix(2,2)));
else
set(handles.text22,'String',' ');
end
if number_matrix(2,3)~=0
set(handles.text23,'String',num2str(number_matrix(2,3)));
else
set(handles.text23,'String',' ');
end
if number_matrix(2,4)~=0
set(handles.text24,'String',num2str(number_matrix(2,4)));
else
set(handles.text24,'String',' ');
end
if number_matrix(3,1)~=0
set(handles.text31,'String',num2str(number_matrix(3,1)));
else
set(handles.text31,'String',' ');
end
if number_matrix(3,2)~=0
set(handles.text32,'String',num2str(number_matrix(3,2)));
else
set(handles.text32,'String',' ');
end
if number_matrix(3,3)~=0
set(handles.text33,'String',num2str(number_matrix(3,3)));
else
set(handles.text33,'String',' ');
end
if number_matrix(3,4)~=0
set(handles.text34,'String',num2str(number_matrix(3,4)));
else
set(handles.text34,'String',' ');
end
if number_matrix(4,1)~=0
set(handles.text41,'String',num2str(number_matrix(4,1)));
else
set(handles.text41,'String',' ');
end
if number_matrix(4,2)~=0
set(handles.text42,'String',num2str(number_matrix(4,2)));
else
set(handles.text42,'String',' ');
end
if number_matrix(4,3)~=0
set(handles.text43,'String',num2str(number_matrix(4,3)));
else
set(handles.text43,'String',' ');
end
if number_matrix(4,4)~=0
set(handles.text44,'String',num2str(number_matrix(4,4)));
else
set(handles.text44,'String',' ');
end
% 判断游戏是否结束
if length(find(number_matrix))==16
if if_game_over()
open('DEAD_2048.fig');
end
end
% 判断增加数字的个数
if length(find(number_matrix))<16
if length(find(number_matrix))<=14
if rem(step,2)==0
rand_times = randi([1,2]);% 随机判定增加一个或者两个
switch rand_times
case 1
while 1
rand_num_loc = randi([1,4],2,1);% 随机位置生成
if number_matrix(rand_num_loc(1),rand_num_loc(2))==0
number_matrix(rand_num_loc(1),rand_num_loc(2)) = select_2or4();% 赋值
break;
end
end
case 2
while 1
rand_num_loc1 = randi([1,4],2,1);
if number_matrix(rand_num_loc1(1),rand_num_loc1(2))==0
number_matrix(rand_num_loc1(1),rand_num_loc1(2)) = select_2or4();
break;
end
end
while 1
rand_num_loc2 = randi([1,4],2,1);
if number_matrix(rand_num_loc2(1),rand_num_loc2(2))==0
number_matrix(rand_num_loc2(1),rand_num_loc2(2)) = select_2or4();
break;
end
end
end
end
elseif length(find(number_matrix))==15
while 1
rand_num_loc = randi([1,4],2,1);
if number_matrix(rand_num_loc(1),rand_num_loc(2))==0
number_matrix(rand_num_loc(1),rand_num_loc(2)) = select_2or4();
break;
end
end
else
end
end
if number_matrix(1,1)~=0
set(handles.text11,'String',num2str(number_matrix(1,1)));
else
set(handles.text11,'String',' ');
end
if number_matrix(1,2)~=0
set(handles.text12,'String',num2str(number_matrix(1,2)));
else
set(handles.text12,'String',' ');
end
if number_matrix(1,3)~=0
set(handles.text13,'String',num2str(number_matrix(1,3)));
else
set(handles.text13,'String',' ');
end
if number_matrix(1,4)~=0
set(handles.text14,'String',num2str(number_matrix(1,4)));
else
set(handles.text14,'String',' ');
end
if number_matrix(2,1)~=0
set(handles.text21,'String',num2str(number_matrix(2,1)));
else
set(handles.text21,'String',' ');
end
if number_matrix(2,2)~=0
set(handles.text22,'String',num2str(number_matrix(2,2)));
else
set(handles.text22,'String',' ');
end
if number_matrix(2,3)~=0
set(handles.text23,'String',num2str(number_matrix(2,3)));
else
set(handles.text23,'String',' ');
end
if number_matrix(2,4)~=0
set(handles.text24,'String',num2str(number_matrix(2,4)));
else
set(handles.text24,'String',' ');
end
if number_matrix(3,1)~=0
set(handles.text31,'String',num2str(number_matrix(3,1)));
else
set(handles.text31,'String',' ');
end
if number_matrix(3,2)~=0
set(handles.text32,'String',num2str(number_matrix(3,2)));
else
set(handles.text32,'String',' ');
end
if number_matrix(3,3)~=0
set(handles.text33,'String',num2str(number_matrix(3,3)));
else
set(handles.text33,'String',' ');
end
if number_matrix(3,4)~=0
set(handles.text34,'String',num2str(number_matrix(3,4)));
else
set(handles.text34,'String',' ');
end
if number_matrix(4,1)~=0
set(handles.text41,'String',num2str(number_matrix(4,1)));
else
set(handles.text41,'String',' ');
end
if number_matrix(4,2)~=0
set(handles.text42,'String',num2str(number_matrix(4,2)));
else
set(handles.text42,'String',' ');
end
if number_matrix(4,3)~=0
set(handles.text43,'String',num2str(number_matrix(4,3)));
else
set(handles.text43,'String',' ');
end
if number_matrix(4,4)~=0
set(handles.text44,'String',num2str(number_matrix(4,4)));
else
set(handles.text44,'String',' ');
end
% 判断是否获胜
if_game_win();
set(handles.text1,'String',fraction);% 得分改变
% 按下向左的按键
function pushbutton2_Callback(hObject, eventdata, handles)
global step;
step=step+1;
global number_matrix;
global fraction;
% 向左移动
push_left();
if number_matrix(1,1)~=0
set(handles.text11,'String',num2str(number_matrix(1,1)));
else
set(handles.text11,'String',' ');
end
if number_matrix(1,2)~=0
set(handles.text12,'String',num2str(number_matrix(1,2)));
else
set(handles.text12,'String',' ');
end
if number_matrix(1,3)~=0
set(handles.text13,'String',num2str(number_matrix(1,3)));
else
set(handles.text13,'String',' ');
end
if number_matrix(1,4)~=0
set(handles.text14,'String',num2str(number_matrix(1,4)));
else
set(handles.text14,'String',' ');
end
if number_matrix(2,1)~=0
set(handles.text21,'String',num2str(number_matrix(2,1)));
else
set(handles.text21,'String',' ');
end
if number_matrix(2,2)~=0
set(handles.text22,'String',num2str(number_matrix(2,2)));
else
set(handles.text22,'String',' ');
end
if number_matrix(2,3)~=0
set(handles.text23,'String',num2str(number_matrix(2,3)));
else
set(handles.text23,'String',' ');
end
if number_matrix(2,4)~=0
set(handles.text24,'String',num2str(number_matrix(2,4)));
else
set(handles.text24,'String',' ');
end
if number_matrix(3,1)~=0
set(handles.text31,'String',num2str(number_matrix(3,1)));
else
set(handles.text31,'String',' ');
end
if number_matrix(3,2)~=0
set(handles.text32,'String',num2str(number_matrix(3,2)));
else
set(handles.text32,'String',' ');
end
if number_matrix(3,3)~=0
set(handles.text33,'String',num2str(number_matrix(3,3)));
else
set(handles.text33,'String',' ');
end
if number_matrix(3,4)~=0
set(handles.text34,'String',num2str(number_matrix(3,4)));
else
set(handles.text34,'String',' ');
end
if number_matrix(4,1)~=0
set(handles.text41,'String',num2str(number_matrix(4,1)));
else
set(handles.text41,'String',' ');
end
if number_matrix(4,2)~=0
set(handles.text42,'String',num2str(number_matrix(4,2)));
else
set(handles.text42,'String',' ');
end
if number_matrix(4,3)~=0
set(handles.text43,'String',num2str(number_matrix(4,3)));
else
set(handles.text43,'String',' ');
end
if number_matrix(4,4)~=0
set(handles.text44,'String',num2str(number_matrix(4,4)));
else
set(handles.text44,'String',' ');
end
if length(find(number_matrix))==16
if if_game_over()
open('DEAD_2048.fig');
end
end
if length(find(number_matrix))<16
if length(find(number_matrix))<=14
if rem(step,2)==0
rand_times = randi([1,2]);
switch rand_times
case 1
while 1
rand_num_loc = randi([1,4],2,1);
if number_matrix(rand_num_loc(1),rand_num_loc(2))==0
number_matrix(rand_num_loc(1),rand_num_loc(2)) = select_2or4();
break;
end
end
case 2
while 1
rand_num_loc1 = randi([1,4],2,1);
if number_matrix(rand_num_loc1(1),rand_num_loc1(2))==0
number_matrix(rand_num_loc1(1),rand_num_loc1(2)) = select_2or4();
break;
end
end
while 1
rand_num_loc2 = randi([1,4],2,1);
if number_matrix(rand_num_loc2(1),rand_num_loc2(2))==0
number_matrix(rand_num_loc2(1),rand_num_loc2(2)) = select_2or4();
break;
end
end
end
end
elseif length(find(number_matrix))==15
while 1
rand_num_loc = randi([1,4],2,1);
if number_matrix(rand_num_loc(1),rand_num_loc(2))==0
number_matrix(rand_num_loc(1),rand_num_loc(2)) = select_2or4();
break;
end
end
else
end
end
if number_matrix(1,1)~=0
set(handles.text11,'String',num2str(number_matrix(1,1)));
else
set(handles.text11,'String',' ');
end
if number_matrix(1,2)~=0
set(handles.text12,'String',num2str(number_matrix(1,2)));
else
set(handles.text12,'String',' ');
end
if number_matrix(1,3)~=0
set(handles.text13,'String',num2str(number_matrix(1,3)));
else
set(handles.text13,'String',' ');
end
if number_matrix(1,4)~=0
set(handles.text14,'String',num2str(number_matrix(1,4)));
else
set(handles.text14,'String',' ');
end
if number_matrix(2,1)~=0
set(handles.text21,'String',num2str(number_matrix(2,1)));
else
set(handles.text21,'String',' ');
end
if number_matrix(2,2)~=0
set(handles.text22,'String',num2str(number_matrix(2,2)));
else
set(handles.text22,'String',' ');
end
if number_matrix(2,3)~=0
set(handles.text23,'String',num2str(number_matrix(2,3)));
else
set(handles.text23,'String',' ');
end
if number_matrix(2,4)~=0
set(handles.text24,'String',num2str(number_matrix(2,4)));
else
set(handles.text24,'String',' ');
end
if number_matrix(3,1)~=0
set(handles.text31,'String',num2str(number_matrix(3,1)));
else
set(handles.text31,'String',' ');
end
if number_matrix(3,2)~=0
set(handles.text32,'String',num2str(number_matrix(3,2)));
else
set(handles.text32,'String',' ');
end
if number_matrix(3,3)~=0
set(handles.text33,'String',num2str(number_matrix(3,3)));
else
set(handles.text33,'String',' ');
end
if number_matrix(3,4)~=0
set(handles.text34,'String',num2str(number_matrix(3,4)));
else
set(handles.text34,'String',' ');
end
if number_matrix(4,1)~=0
set(handles.text41,'String',num2str(number_matrix(4,1)));
else
set(handles.text41,'String',' ');
end
if number_matrix(4,2)~=0
set(handles.text42,'String',num2str(number_matrix(4,2)));
else
set(handles.text42,'String',' ');
end
if number_matrix(4,3)~=0
set(handles.text43,'String',num2str(number_matrix(4,3)));
else
set(handles.text43,'String',' ');
end
if number_matrix(4,4)~=0
set(handles.text44,'String',num2str(number_matrix(4,4)));
else
set(handles.text44,'String',' ');
end
if_game_win();
set(handles.text1,'String',fraction);
% 按下向右的按键
function pushbutton3_Callback(hObject, eventdata, handles)
global step;
step=step+1;
global fraction;
global number_matrix;
push_right();
if number_matrix(1,1)~=0
set(handles.text11,'String',num2str(number_matrix(1,1)));
else
set(handles.text11,'String',' ');
end
if number_matrix(1,2)~=0
set(handles.text12,'String',num2str(number_matrix(1,2)));
else
set(handles.text12,'String',' ');
end
if number_matrix(1,3)~=0
set(handles.text13,'String',num2str(number_matrix(1,3)));
else
set(handles.text13,'String',' ');
end
if number_matrix(1,4)~=0
set(handles.text14,'String',num2str(number_matrix(1,4)));
else
set(handles.text14,'String',' ');
end
if number_matrix(2,1)~=0
set(handles.text21,'String',num2str(number_matrix(2,1)));
else
set(handles.text21,'String',' ');
end
if number_matrix(2,2)~=0
set(handles.text22,'String',num2str(number_matrix(2,2)));
else
set(handles.text22,'String',' ');
end
if number_matrix(2,3)~=0
set(handles.text23,'String',num2str(number_matrix(2,3)));
else
set(handles.text23,'String',' ');
end
if number_matrix(2,4)~=0
set(handles.text24,'String',num2str(number_matrix(2,4)));
else
set(handles.text24,'String',' ');
end
if number_matrix(3,1)~=0
set(handles.text31,'String',num2str(number_matrix(3,1)));
else
set(handles.text31,'String',' ');
end
if number_matrix(3,2)~=0
set(handles.text32,'String',num2str(number_matrix(3,2)));
else
set(handles.text32,'String',' ');
end
if number_matrix(3,3)~=0
set(handles.text33,'String',num2str(number_matrix(3,3)));
else
set(handles.text33,'String',' ');
end
if number_matrix(3,4)~=0
set(handles.text34,'String',num2str(number_matrix(3,4)));
else
set(handles.text34,'String',' ');
end
if number_matrix(4,1)~=0
set(handles.text41,'String',num2str(number_matrix(4,1)));
else
set(handles.text41,'String',' ');
end
if number_matrix(4,2)~=0
set(handles.text42,'String',num2str(number_matrix(4,2)));
else
set(handles.text42,'String',' ');
end
if number_matrix(4,3)~=0
set(handles.text43,'String',num2str(number_matrix(4,3)));
else
set(handles.text43,'String',' ');
end
if number_matrix(4,4)~=0
set(handles.text44,'String',num2str(number_matrix(4,4)));
else
set(handles.text44,'String',' ');
end
if length(find(number_matrix))==16
if if_game_over()
open('DEAD_2048.fig');
end
end
if length(find(number_matrix))<16
if length(find(number_matrix))<=14
if rem(step,2)==0
rand_times = randi([1,2]);
switch rand_times
case 1
while 1
rand_num_loc = randi([1,4],2,1);
if number_matrix(rand_num_loc(1),rand_num_loc(2))==0
number_matrix(rand_num_loc(1),rand_num_loc(2)) = select_2or4();
break;
end
end
case 2
while 1
rand_num_loc1 = randi([1,4],2,1);
if number_matrix(rand_num_loc1(1),rand_num_loc1(2))==0
number_matrix(rand_num_loc1(1),rand_num_loc1(2)) = select_2or4();
break;
end
end
while 1
rand_num_loc2 = randi([1,4],2,1);
if number_matrix(rand_num_loc2(1),rand_num_loc2(2))==0
number_matrix(rand_num_loc2(1),rand_num_loc2(2)) = select_2or4();
break;
end
end
end
end
elseif length(find(number_matrix))==15
while 1
rand_num_loc = randi([1,4],2,1);
if number_matrix(rand_num_loc(1),rand_num_loc(2))==0
number_matrix(rand_num_loc(1),rand_num_loc(2)) = select_2or4();
break;
end
end
else
end
end
if number_matrix(1,1)~=0
set(handles.text11,'String',num2str(number_matrix(1,1)));
else
set(handles.text11,'String',' ');
end
if number_matrix(1,2)~=0
set(handles.text12,'String',num2str(number_matrix(1,2)));
else
set(handles.text12,'String',' ');
end
if number_matrix(1,3)~=0
set(handles.text13,'String',num2str(number_matrix(1,3)));
else
set(handles.text13,'String',' ');
end
if number_matrix(1,4)~=0
set(handles.text14,'String',num2str(number_matrix(1,4)));
else
set(handles.text14,'String',' ');
end
if number_matrix(2,1)~=0
set(handles.text21,'String',num2str(number_matrix(2,1)));
else
set(handles.text21,'String',' ');
end
if number_matrix(2,2)~=0
set(handles.text22,'String',num2str(number_matrix(2,2)));
else
set(handles.text22,'String',' ');
end
if number_matrix(2,3)~=0
set(handles.text23,'String',num2str(number_matrix(2,3)));
else
set(handles.text23,'String',' ');
end
if number_matrix(2,4)~=0
set(handles.text24,'String',num2str(number_matrix(2,4)));
else
set(handles.text24,'String',' ');
end
if number_matrix(3,1)~=0
set(handles.text31,'String',num2str(number_matrix(3,1)));
else
set(handles.text31,'String',' ');
end
if number_matrix(3,2)~=0
set(handles.text32,'String',num2str(number_matrix(3,2)));
else
set(handles.text32,'String',' ');
end
if number_matrix(3,3)~=0
set(handles.text33,'String',num2str(number_matrix(3,3)));
else
set(handles.text33,'String',' ');
end
if number_matrix(3,4)~=0
set(handles.text34,'String',num2str(number_matrix(3,4)));
else
set(handles.text34,'String',' ');
end
if number_matrix(4,1)~=0
set(handles.text41,'String',num2str(number_matrix(4,1)));
else
set(handles.text41,'String',' ');
end
if number_matrix(4,2)~=0
set(handles.text42,'String',num2str(number_matrix(4,2)));
else
set(handles.text42,'String',' ');
end
if number_matrix(4,3)~=0
set(handles.text43,'String',num2str(number_matrix(4,3)));
else
set(handles.text43,'String',' ');
end
if number_matrix(4,4)~=0
set(handles.text44,'String',num2str(number_matrix(4,4)));
else
set(handles.text44,'String',' ');
end
if_game_win();
set(handles.text1,'String',fraction);
% 按下向下的按键
function pushbutton4_Callback(hObject, eventdata, handles)
global number_matrix;
global step;
global fraction;
step = step+1;
%向下移动
push_down();
if number_matrix(1,1)~=0
set(handles.text11,'String',num2str(number_matrix(1,1)));
else
set(handles.text11,'String',' ');
end
if number_matrix(1,2)~=0
set(handles.text12,'String',num2str(number_matrix(1,2)));
else
set(handles.text12,'String',' ');
end
if number_matrix(1,3)~=0
set(handles.text13,'String',num2str(number_matrix(1,3)));
else
set(handles.text13,'String',' ');
end
if number_matrix(1,4)~=0
set(handles.text14,'String',num2str(number_matrix(1,4)));
else
set(handles.text14,'String',' ');
end
if number_matrix(2,1)~=0
set(handles.text21,'String',num2str(number_matrix(2,1)));
else
set(handles.text21,'String',' ');
end
if number_matrix(2,2)~=0
set(handles.text22,'String',num2str(number_matrix(2,2)));
else
set(handles.text22,'String',' ');
end
if number_matrix(2,3)~=0
set(handles.text23,'String',num2str(number_matrix(2,3)));
else
set(handles.text23,'String',' ');
end
if number_matrix(2,4)~=0
set(handles.text24,'String',num2str(number_matrix(2,4)));
else
set(handles.text24,'String',' ');
end
if number_matrix(3,1)~=0
set(handles.text31,'String',num2str(number_matrix(3,1)));
else
set(handles.text31,'String',' ');
end
if number_matrix(3,2)~=0
set(handles.text32,'String',num2str(number_matrix(3,2)));
else
set(handles.text32,'String',' ');
end
if number_matrix(3,3)~=0
set(handles.text33,'String',num2str(number_matrix(3,3)));
else
set(handles.text33,'String',' ');
end
if number_matrix(3,4)~=0
set(handles.text34,'String',num2str(number_matrix(3,4)));
else
set(handles.text34,'String',' ');
end
if number_matrix(4,1)~=0
set(handles.text41,'String',num2str(number_matrix(4,1)));
else
set(handles.text41,'String',' ');
end
if number_matrix(4,2)~=0
set(handles.text42,'String',num2str(number_matrix(4,2)));
else
set(handles.text42,'String',' ');
end
if number_matrix(4,3)~=0
set(handles.text43,'String',num2str(number_matrix(4,3)));
else
set(handles.text43,'String',' ');
end
if number_matrix(4,4)~=0
set(handles.text44,'String',num2str(number_matrix(4,4)));
else
set(handles.text44,'String',' ');
end
if length(find(number_matrix))==16
if if_game_over()
open('DEAD_2048.fig');
end
end
if length(find(number_matrix))<16
if length(find(number_matrix))<=14
if rem(step,2)==0
rand_times = randi([1,2]);
switch rand_times
case 1
while 1
rand_num_loc = randi([1,4],2,1);
if number_matrix(rand_num_loc(1),rand_num_loc(2))==0
number_matrix(rand_num_loc(1),rand_num_loc(2)) = select_2or4();
break;
end
end
case 2
while 1
rand_num_loc1 = randi([1,4],2,1);
if number_matrix(rand_num_loc1(1),rand_num_loc1(2))==0
number_matrix(rand_num_loc1(1),rand_num_loc1(2)) = select_2or4();
break;
end
end
while 1
rand_num_loc2 = randi([1,4],2,1);
if number_matrix(rand_num_loc2(1),rand_num_loc2(2))==0
number_matrix(rand_num_loc2(1),rand_num_loc2(2)) = select_2or4();
break;
end
end
end
end
elseif length(find(number_matrix))==15
while 1
rand_num_loc = randi([1,4],2,1);
if number_matrix(rand_num_loc(1),rand_num_loc(2))==0
number_matrix(rand_num_loc(1),rand_num_loc(2)) = select_2or4();
break;
end
end
else
end
end
if number_matrix(1,1)~=0
set(handles.text11,'String',num2str(number_matrix(1,1)));
else
set(handles.text11,'String',' ');
end
if number_matrix(1,2)~=0
set(handles.text12,'String',num2str(number_matrix(1,2)));
else
set(handles.text12,'String',' ');
end
if number_matrix(1,3)~=0
set(handles.text13,'String',num2str(number_matrix(1,3)));
else
set(handles.text13,'String',' ');
end
if number_matrix(1,4)~=0
set(handles.text14,'String',num2str(number_matrix(1,4)));
else
set(handles.text14,'String',' ');
end
if number_matrix(2,1)~=0
set(handles.text21,'String',num2str(number_matrix(2,1)));
else
set(handles.text21,'String',' ');
end
if number_matrix(2,2)~=0
set(handles.text22,'String',num2str(number_matrix(2,2)));
else
set(handles.text22,'String',' ');
end
if number_matrix(2,3)~=0
set(handles.text23,'String',num2str(number_matrix(2,3)));
else
set(handles.text23,'String',' ');
end
if number_matrix(2,4)~=0
set(handles.text24,'String',num2str(number_matrix(2,4)));
else
set(handles.text24,'String',' ');
end
if number_matrix(3,1)~=0
set(handles.text31,'String',num2str(number_matrix(3,1)));
else
set(handles.text31,'String',' ');
end
if number_matrix(3,2)~=0
set(handles.text32,'String',num2str(number_matrix(3,2)));
else
set(handles.text32,'String',' ');
end
if number_matrix(3,3)~=0
set(handles.text33,'String',num2str(number_matrix(3,3)));
else
set(handles.text33,'String',' ');
end
if number_matrix(3,4)~=0
set(handles.text34,'String',num2str(number_matrix(3,4)));
else
set(handles.text34,'String',' ');
end
if number_matrix(4,1)~=0
set(handles.text41,'String',num2str(number_matrix(4,1)));
else
set(handles.text41,'String',' ');
end
if number_matrix(4,2)~=0
set(handles.text42,'String',num2str(number_matrix(4,2)));
else
set(handles.text42,'String',' ');
end
if number_matrix(4,3)~=0
set(handles.text43,'String',num2str(number_matrix(4,3)));
else
set(handles.text43,'String',' ');
end
if number_matrix(4,4)~=0
set(handles.text44,'String',num2str(number_matrix(4,4)));
else
set(handles.text44,'String',' ');
end
if_game_win();
set(handles.text1,'String',fraction);
% 按下start按键
function pushbutton5_Callback(hObject, eventdata, handles)
global number_matrix;
global fraction;
% 随机生成两个数字
while 1
rand_num_loc1 = randi([1,4],2,1);
if number_matrix(rand_num_loc1(1),rand_num_loc1(2))==0
number_matrix(rand_num_loc1(1),rand_num_loc1(2)) = select_2or4();
break;
end
end
while 1
rand_num_loc2 = randi([1,4],2,1);
if number_matrix(rand_num_loc2(1),rand_num_loc2(2))==0
number_matrix(rand_num_loc2(1),rand_num_loc2(2)) = select_2or4();
break;
end
end
if number_matrix(1,1)~=0
set(handles.text11,'String',num2str(number_matrix(1,1)));
end
if number_matrix(1,2)~=0
set(handles.text12,'String',num2str(number_matrix(1,2)));
end
if number_matrix(1,3)~=0
set(handles.text13,'String',num2str(number_matrix(1,3)));
end
if number_matrix(1,4)~=0
set(handles.text14,'String',num2str(number_matrix(1,4)));
end
if number_matrix(2,1)~=0
set(handles.text21,'String',num2str(number_matrix(2,1)));
end
if number_matrix(2,2)~=0
set(handles.text22,'String',num2str(number_matrix(2,2)));
end
if number_matrix(2,3)~=0
set(handles.text23,'String',num2str(number_matrix(2,3)));
end
if number_matrix(2,4)~=0
set(handles.text24,'String',num2str(number_matrix(2,4)));
end
if number_matrix(3,1)~=0
set(handles.text31,'String',num2str(number_matrix(3,1)));
end
if number_matrix(3,2)~=0
set(handles.text32,'String',num2str(number_matrix(3,2)));
end
if number_matrix(3,3)~=0
set(handles.text33,'String',num2str(number_matrix(3,3)));
end
if number_matrix(3,4)~=0
set(handles.text34,'String',num2str(number_matrix(3,4)));
end
if number_matrix(4,1)~=0
set(handles.text41,'String',num2str(number_matrix(4,1)));
end
if number_matrix(4,2)~=0
set(handles.text42,'String',num2str(number_matrix(4,2)));
end
if number_matrix(4,3)~=0
set(handles.text43,'String',num2str(number_matrix(4,3)));
end
if number_matrix(4,4)~=0
set(handles.text44,'String',num2str(number_matrix(4,4)));
end
if_game_win();
set(handles.text1,'String',fraction);
% 按下restart按键
function pushbutton6_Callback(hObject, eventdata, handles)
global number_matrix;
number_matrix=zeros(4);
global fraction;
fraction=0;
if number_matrix(1,1)~=0
set(handles.text11,'String',num2str(number_matrix(1,1)));
else
set(handles.text11,'String',' ');
end
if number_matrix(1,2)~=0
set(handles.text12,'String',num2str(number_matrix(1,2)));
else
set(handles.text12,'String',' ');
end
if number_matrix(1,3)~=0
set(handles.text13,'String',num2str(number_matrix(1,3)));
else
set(handles.text13,'String',' ');
end
if number_matrix(1,4)~=0
set(handles.text14,'String',num2str(number_matrix(1,4)));
else
set(handles.text14,'String',' ');
end
if number_matrix(2,1)~=0
set(handles.text21,'String',num2str(number_matrix(2,1)));
else
set(handles.text21,'String',' ');
end
if number_matrix(2,2)~=0
set(handles.text22,'String',num2str(number_matrix(2,2)));
else
set(handles.text22,'String',' ');
end
if number_matrix(2,3)~=0
set(handles.text23,'String',num2str(number_matrix(2,3)));
else
set(handles.text23,'String',' ');
end
if number_matrix(2,4)~=0
set(handles.text24,'String',num2str(number_matrix(2,4)));
else
set(handles.text24,'String',' ');
end
if number_matrix(3,1)~=0
set(handles.text31,'String',num2str(number_matrix(3,1)));
else
set(handles.text31,'String',' ');
end
if number_matrix(3,2)~=0
set(handles.text32,'String',num2str(number_matrix(3,2)));
else
set(handles.text32,'String',' ');
end
if number_matrix(3,3)~=0
set(handles.text33,'String',num2str(number_matrix(3,3)));
else
set(handles.text33,'String',' ');
end
if number_matrix(3,4)~=0
set(handles.text34,'String',num2str(number_matrix(3,4)));
else
set(handles.text34,'String',' ');
end
if number_matrix(4,1)~=0
set(handles.text41,'String',num2str(number_matrix(4,1)));
else
set(handles.text41,'String',' ');
end
if number_matrix(4,2)~=0
set(handles.text42,'String',num2str(number_matrix(4,2)));
else
set(handles.text42,'String',' ');
end
if number_matrix(4,3)~=0
set(handles.text43,'String',num2str(number_matrix(4,3)));
else
set(handles.text43,'String',' ');
end
if number_matrix(4,4)~=0
set(handles.text44,'String',num2str(number_matrix(4,4)));
else
set(handles.text44,'String',' ');
end
while 1
rand_num_loc1 = randi([1,4],2,1);
if number_matrix(rand_num_loc1(1),rand_num_loc1(2))==0
number_matrix(rand_num_loc1(1),rand_num_loc1(2)) = select_2or4();
break;
end
end
while 1
rand_num_loc2 = randi([1,4],2,1);
if number_matrix(rand_num_loc2(1),rand_num_loc2(2))==0
number_matrix(rand_num_loc2(1),rand_num_loc2(2)) = select_2or4();
break;
end
end
if number_matrix(1,1)~=0
set(handles.text11,'String',num2str(number_matrix(1,1)));
else
set(handles.text11,'String',' ');
end
if number_matrix(1,2)~=0
set(handles.text12,'String',num2str(number_matrix(1,2)));
else
set(handles.text12,'String',' ');
end
if number_matrix(1,3)~=0
set(handles.text13,'String',num2str(number_matrix(1,3)));
else
set(handles.text13,'String',' ');
end
if number_matrix(1,4)~=0
set(handles.text14,'String',num2str(number_matrix(1,4)));
else
set(handles.text14,'String',' ');
end
if number_matrix(2,1)~=0
set(handles.text21,'String',num2str(number_matrix(2,1)));
else
set(handles.text21,'String',' ');
end
if number_matrix(2,2)~=0
set(handles.text22,'String',num2str(number_matrix(2,2)));
else
set(handles.text22,'String',' ');
end
if number_matrix(2,3)~=0
set(handles.text23,'String',num2str(number_matrix(2,3)));
else
set(handles.text23,'String',' ');
end
if number_matrix(2,4)~=0
set(handles.text24,'String',num2str(number_matrix(2,4)));
else
set(handles.text24,'String',' ');
end
if number_matrix(3,1)~=0
set(handles.text31,'String',num2str(number_matrix(3,1)));
else
set(handles.text31,'String',' ');
end
if number_matrix(3,2)~=0
set(handles.text32,'String',num2str(number_matrix(3,2)));
else
set(handles.text32,'String',' ');
end
if number_matrix(3,3)~=0
set(handles.text33,'String',num2str(number_matrix(3,3)));
else
set(handles.text33,'String',' ');
end
if number_matrix(3,4)~=0
set(handles.text34,'String',num2str(number_matrix(3,4)));
else
set(handles.text34,'String',' ');
end
if number_matrix(4,1)~=0
set(handles.text41,'String',num2str(number_matrix(4,1)));
else
set(handles.text41,'String',' ');
end
if number_matrix(4,2)~=0
set(handles.text42,'String',num2str(number_matrix(4,2)));
else
set(handles.text42,'String',' ');
end
if number_matrix(4,3)~=0
set(handles.text43,'String',num2str(number_matrix(4,3)));
else
set(handles.text43,'String',' ');
end
if number_matrix(4,4)~=0
set(handles.text44,'String',num2str(number_matrix(4,4)));
else
set(handles.text44,'String',' ');
end
if_game_win();
set(handles.text1,'String',fraction);
% 向上移动
function push_up()
global number_matrix;
global fraction;
for i=1:4 % 遍历第一列到第四列
if number_matrix(4,i)==0 % 第四行的数字为零
if number_matrix(3,i)==0 % 第三行的数字为零
if number_matrix(2,i)==0 % 第二行的数字为零
else % 第二行的数字不为零
if number_matrix(1,i)==0
number_matrix(1,i)=number_matrix(2,i);
number_matrix(2,i)=0;
elseif number_matrix(1,i)==number_matrix(2,i)
fraction=fraction+number_matrix(1,i); % 分数随着数字合并而增大
number_matrix(1,i)=number_matrix(1,i)*2; % 相同的数字合并
number_matrix(2,i)=0;
end
end
else % 第三行的数字不为零
if number_matrix(1,i)==0
if number_matrix(2,i)==0
number_matrix(1,i)=number_matrix(3,i);
number_matrix(3,i)=0;
elseif number_matrix(2,i)==number_matrix(3,i)
fraction=fraction+number_matrix(2,i);
number_matrix(1,i)=number_matrix(2,i)*2;
number_matrix(2,i)=0;
number_matrix(3,i)=0;
else
number_matrix(1,i)=number_matrix(2,i);
number_matrix(2,i)=number_matrix(3,i);
number_matrix(3,i)=0;
end
else % 第一行的数字不为零
if number_matrix(2,i)==0
if number_matrix(3,i)==number_matrix(1,i)
fraction=fraction+number_matrix(1,i);
number_matrix(1,i)=number_matrix(1,i)*2;
number_matrix(3,i)=0;
else
number_matrix(2,i)=number_matrix(3,i);
number_matrix(3,i)=0;
end
else % 第二行的数字不为零
if number_matrix(1,i)==number_matrix(2,i)
fraction=fraction+number_matrix(1,i);
number_matrix(1,i)=number_matrix(1,i)*2;
number_matrix(2,i)=number_matrix(3,i);
number_matrix(3,i)=0;
elseif number_matrix(2,i)==number_matrix(3,i)
fraction=fraction+number_matrix(2,i);
number_matrix(2,i)=number_matrix(2,i)*2;
number_matrix(3,i)=0;
end
end
end
end
else % 第四行的数字不为零
if number_matrix(3,i)==0
if number_matrix(2,i)==0
if number_matrix(1,i)==0
number_matrix(1,i)=number_matrix(4,i);
number_matrix(4,i)=0;
elseif number_matrix(1,i)==number_matrix(4,i)
fraction=fraction+number_matrix(1,i);
number_matrix(1,i)=number_matrix(1,i)*2;
number_matrix(4,i)=0;
else
number_matrix(2,i)=number_matrix(4,i);
number_matrix(4,i)=0;
end
else % 第二行的数字不为零
if number_matrix(1,i)==0
if number_matrix(2,i)==number_matrix(4,i)
fraction=fraction+number_matrix(2,i);
number_matrix(1,i)=number_matrix(2,i)*2;
number_matrix(2,i)=0;
number_matrix(4,i)=0;
else
number_matrix(1,i)=number_matrix(2,i);
number_matrix(2,i)=number_matrix(4,i);
number_matrix(4,i)=0;
end
else % 第一行的数字不为零
if number_matrix(1,i)==number_matrix(2,i)
fraction=fraction+number_matrix(1,i);
number_matrix(1,i)=number_matrix(1,i)*2;
number_matrix(2,i)=number_matrix(4,i);
number_matrix(4,i)=0;
elseif number_matrix(2,i)==number_matrix(4,i)
fraction=fraction+number_matrix(2,i);
number_matrix(2,i)=number_matrix(2,i)*2;
number_matrix(4,i)=0;
else
number_matrix(3,i)=number_matrix(4,i);
number_matrix(4,i)=0;
end
end
end
else % 第三行的数字不为零
if number_matrix(2,i)==0
if number_matrix(1,i)==0
if number_matrix(3,i)==number_matrix(4,i)
fraction=fraction+number_matrix(3,i);
number_matrix(1,i)=number_matrix(3,i)*2;
number_matrix(3,i)=0;
number_matrix(4,i)=0;
else
number_matrix(1,i)=number_matrix(3,i);
number_matrix(2,i)=number_matrix(4,i);
number_matrix(3,i)=0;
number_matrix(4,i)=0;
end
else % 第一行的数字不为零
if number_matrix(1,i)==number_matrix(3,i)
fraction=fraction+number_matrix(1,i);
number_matrix(1,i)=number_matrix(1,i)*2;
number_matrix(2,i)=number_matrix(4,i);
number_matrix(3,i)=0;
number_matrix(4,i)=0;
elseif number_matrix(3,i)==number_matrix(4,i)
fraction=fraction+number_matrix(3,i);
number_matrix(2,i)=number_matrix(3,i)*2;
number_matrix(3,i)=0;
number_matrix(4,i)=0;
else
number_matrix(2,i)=number_matrix(3,i);
number_matrix(3,i)=number_matrix(4,i);
number_matrix(4,i)=0;
end
end
else % 第二行的数字不为零
if number_matrix(1,i)==0
if number_matrix(2,i)==number_matrix(3,i)
fraction=fraction+number_matrix(2,i);
number_matrix(1,i)=number_matrix(2,i)*2;
number_matrix(2,i)=number_matrix(4,i);
number_matrix(3,i)=0;
number_matrix(4,i)=0;
elseif number_matrix(3,i)==number_matrix(4,i)
number_matrix(1,i)=number_matrix(2,i);
fraction=fraction+number_matrix(3,i);
number_matrix(2,i)=number_matrix(3,i)*2;
number_matrix(3,i)=0;
number_matrix(4,i)=0;
else
number_matrix(1,i)=number_matrix(2,i);
number_matrix(2,i)=number_matrix(3,i);
number_matrix(3,i)=number_matrix(4,i);
number_matrix(4,i)=0;
end
else % 第一行的数字不为零
if number_matrix(1,i)==number_matrix(2,i)
fraction=fraction+number_matrix(1,i);
number_matrix(1,i)=number_matrix(1,i)*2;
number_matrix(2,i)=number_matrix(3,i);
number_matrix(3,i)=number_matrix(4,i);
number_matrix(4,i)=0;
elseif number_matrix(2,i)==number_matrix(3,i)
fraction=fraction+number_matrix(2,i);
number_matrix(2,i)=number_matrix(2,i)*2;
number_matrix(3,i)=number_matrix(4,i);
number_matrix(4,i)=0;
elseif number_matrix(3,i)==number_matrix(4,i)
fraction=fraction+number_matrix(3,i);
number_matrix(3,i)=number_matrix(3,i)*2;
number_matrix(4,i)=0;
end
end
end
end
end
end
end
% 随机出现2或4 出现2的概率大于4
% 初步设定出现2的概率为80%
function select_num = select_2or4()
hhh = randi([0,9]);
if hhh<=7
select_num = 2;
else
select_num = 4;
end
end
% 判断是否胜利 胜利条件 矩阵最大值是2048
function if_game_win()
global save_num;
global number_matrix;
save_num = max(max(number_matrix)'); %取矩阵最大值
if save_num==2048
open('WIN_2048.fig');
end
% 判断是否结束游戏 结束返回1 未结束返回0
function turn_to = if_game_over()
global number_matrix;
n = ones(2,4);
for i=1:3 % 遍历第一列
if number_matrix(i)==number_matrix(i+1)
break
end
n(1,1)=0;
end
for i=5:7 % 遍历第二列
if number_matrix(i)==number_matrix(i+1)
break
end
n(1,2)=0;
end
for i=9:11 % 遍历第三列
if number_matrix(i)==number_matrix(i+1)
break
end
n(1,3)=0;
end
for i=13:15 % 遍历第四列
if number_matrix(i)==number_matrix(i+1)
break
end
n(1,4)=0;
end
for j=1:3 % 遍历第一行
i=[1,5,9,13];
if number_matrix(i(j))==number_matrix(i(j+1))
break
end
n(2,1)=0;
end
for j=1:3 % 遍历第二行
i=[2,6,10,14];
if number_matrix(i(j))==number_matrix(i(j+1))
break
end
n(2,2)=0;
end
for j=1:3 % 遍历第三行
i=[3,7,11,15];
if number_matrix(i(j))==number_matrix(i(j+1))
break
end
n(2,3)=0;
end
for j=1:3 % 遍历第四行
i=[4,8,12,16];
if number_matrix(i(j))==number_matrix(i(j+1))
break
end
n(2,4)=0;
end
if length(find(n))==0
turn_to=1;
else
turn_to=0;
end
end