Advertisement
Guest User

2048

a guest
Dec 17th, 2015
689
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 9.54 KB | None | 0 0
  1. function twos()
  2.  
  3.     clc
  4.  
  5.     n = mainmenuprint();
  6.     %Print main menu to ask user what to do
  7.    
  8.     switch n
  9.         case 1
  10.             %Play new game
  11.             game(zeros(4,4))
  12.         case 2
  13.             %Play game loaded from file
  14.             game(dbread())
  15.         case 0
  16.             %Quit
  17.             clc
  18.             clear
  19.             return
  20.     end
  21.  
  22. end
  23.  
  24. function c = mainmenuprint()
  25. %This function displays the homescreen
  26. %and asks the user what they want to do
  27.  
  28.     while 1
  29.         clc
  30.  
  31. %Bask in the glory of my ASCII art skills.
  32. %Jk. Generated at http://bigtext.org/
  33.  
  34.         fprintf('\n ____   ___  _  _    ___  ')
  35.         fprintf('\n|___ \\ / _ \\| || |  ( _ ) ')
  36.         fprintf('\n  __) | | | | || |_ / _ \\ ')
  37.         fprintf('\n / __/| |_| |__   _| (_) |')
  38.         fprintf('\n|_____|\\___/   |_|  \\___/ ')
  39.  
  40.         fprintf('\n\n-----Welcome to 2048!-----')
  41.  
  42.         fprintf('\n\n1. New game')
  43.         fprintf('\n2. Load game from file')
  44.         fprintf('\n0. Quit')
  45.  
  46.     %Takes user input. (It is taken as a string to make sure
  47.     %invalid inputs (like letters) can be ignored)
  48.  
  49.         c = str2double(input('\n\nMake a choice(0, 1 or 2) and press Enter : ','s'));
  50.  
  51.         if ~ismember(c,0:2)
  52.             continue
  53.         end
  54.  
  55.         break
  56.  
  57.     end
  58.    
  59. end
  60.  
  61. function game(board)
  62. %This function is the backbone of the game
  63.  
  64. %If the board has less than two full spaces for
  65. %any reason, it assigns either a 2 or a 4 to two
  66. %random spaces on the board
  67.  
  68.     if  sum(sum(board ~= 0))<2;
  69.         a = [randperm(4,2)',randperm(4,2)'];
  70.         b = 2.*randi(2,2,1);
  71.         a(:,3) = b;
  72.         board(a(1,1),a(1,2)) = a(1,3);
  73.         board(a(2,1),a(2,2)) = a(2,3);
  74.     end
  75.    
  76.     clc
  77.     %Prints the board
  78.     printboard(board)
  79.     %Prints the menu
  80.     c = printmenu();
  81.    
  82.     %A variable that may or may not be useful.
  83.     %It was useful in earlier versions but I am too
  84.     %afraid to take it away now.
  85.     d = 'n';
  86.  
  87.     %While the board has empty spaces, or is deemed
  88.     %playable by my lovely poss() function.
  89.     while sum(sum(board ==0))>0 || poss(board)
  90.        
  91.         if strfind('wasd',c)
  92.             %If the input was a direction, move the
  93.             %move the board pieces in that direction
  94.             [board,d] = movewhile(board,c);
  95.         elseif c == 'l'
  96.             %Save the current board to a file and
  97.             %return to the main menu
  98.             dbwrite(board)
  99.             twos()
  100.             return
  101.         elseif c == 'p'
  102.             %Return to the main menu
  103.             twos()
  104.             return
  105.         else
  106.             %If the user enters nothing, ask the
  107.             %user for an input again and restart
  108.             %the loop
  109.             clc
  110.             printboard(board)
  111.             c = printmenu();
  112.             continue
  113.         end
  114.        
  115.         %This was useful in earlier versions. Now I'm
  116.         %afraid it'll break the code if I remove it,
  117.         %so it stays.
  118.         if d ~= 'n'
  119.             c = d;
  120.             continue
  121.         end
  122.        
  123.         clc
  124.        
  125.         printboard(board)
  126.         c= printmenu();
  127.     end
  128.    
  129.     %When the while loop is over, the user is informed
  130.     %that the game is over and is returned to the main
  131.     %menu.
  132.     fprintf('\n Game Over. :(\n')
  133.     pause(5)
  134.     twos()
  135.     return
  136. end
  137.  
  138. function [board, d] = movewhile(board,k)
  139. %Moves the board pieces in the desired direction.
  140.  
  141. %This is slightly different to how the original
  142. %game works due to an oversight on my part.
  143. %It is slightly harder to code my version
  144. %anyway so I'll just go with saying
  145. %that I took it as a challenge :)
  146.  
  147. %I only explained the case where the option is to move
  148. %downwards (k == 's') because the rest are the same but with different
  149. %variables changing.
  150.  
  151. %Makes a single-layer padding of NaN-variables
  152. %around the board.
  153.     d = 'n';
  154.     first = board;
  155.     g = NaN(6,6);
  156.     g(2:5,2:5) = board;
  157.     board = g;
  158.  
  159.     if k == 's'
  160.         %down
  161.         for r = 4:-1:2
  162.             %Starts on the third row and moves upwards.
  163.             for c = 2:5
  164.                 if board(r,c) ~= 0
  165.                     %If the piece is non-zero
  166.                     if board(r,c) == board(r+1,c)
  167.                         %If the piece is the same as the one below it,
  168.                         % nullify itself and double the piece below it
  169.                         board(r+1,c) = board(r,c).*2;
  170.                         board(r,c) = 0;
  171.                     elseif board(r+1,c) == 0
  172.                         %if the piece below it is empty
  173.                         p = r;
  174.                         while board(r+1,c) == 0
  175.                             %while the piece below it is empty,
  176.                             %Move the piece to the space below it
  177.                             board(r+1,c) = board(r,c);
  178.                             board(r,c) = 0;
  179.                             r = r+1;
  180.                         end
  181.                         r = p;
  182.                     end
  183.                 end
  184.             end
  185.         end
  186.        
  187.     elseif k == 'a'
  188.         %left
  189.         for r = 2:5
  190.             for c = 3:5
  191.                 if board(r,c) ~= 0
  192.                     if board(r,c) == board(r,c-1)
  193.                         board(r,c-1) = board(r,c).*2;
  194.                         board(r,c) = 0;
  195.                     elseif board(r,c-1) == 0
  196.                         p = c;
  197.                         while board(r,c-1) == 0
  198.                             board(r,c-1) = board(r,c);
  199.                             board(r,c) = 0;
  200.                             c = c-1;
  201.                         end
  202.                         c = p;
  203.                     end
  204.                 end
  205.             end
  206.         end
  207.        
  208.     elseif k == 'd'
  209.         %right
  210.         for r = 2:5
  211.             for c = 4:-1:2
  212.                 if board(r,c) ~= 0
  213.                     if board(r,c) == board(r,c+1)
  214.                         board(r,c+1) = board(r,c).*2;
  215.                         board(r,c) = 0;
  216.                     elseif board(r,c+1) == 0
  217.                         p = c;
  218.                         while board(r,c+1) == 0
  219.                             board(r,c+1) = board(r,c);
  220.                             board(r,c) = 0;
  221.                             c = c+1;
  222.                         end
  223.                         c = p;
  224.                     end
  225.                 end
  226.             end
  227.         end
  228.            
  229.     elseif k == 'w'
  230.         %up
  231.         for r = 3:5
  232.             for c = 2:5
  233.                 if board(r,c) ~= 0
  234.                     if board(r,c) == board(r-1,c)
  235.                         board(r-1,c) = board(r,c).*2;
  236.                         board(r,c) = 0;
  237.                     elseif board(r-1,c) == 0
  238.                         p = r;
  239.                         while board(r-1,c) == 0
  240.                             board(r-1,c) = board(r,c);
  241.                             board(r,c) = 0;
  242.                             r = r-1;
  243.                         end
  244.                         r = p;
  245.                     end
  246.                 end
  247.             end
  248.         end
  249.     end
  250.    
  251.     %removes the padding of NaNs
  252.     board(1,:) = [];
  253.     board(5,:) = [];
  254.     board(:,1) = [];
  255.     board(:,5) = [];
  256.    
  257.    
  258.     %If the board is not unchanged after the move
  259.     %, a 2 or a 4 is added to a random empty
  260.     %spot on the board.
  261.     if ~isequal(board,first)
  262.         [x,y] = find(board==0);
  263.         b = 2.*randi(2);
  264.         c = randi(length(x));
  265.         board(x(c),y(c)) = b;
  266.     end
  267.  
  268. end
  269.  
  270. function res = poss(board)
  271. %Checks if the board is still playable even if the
  272. %board is full
  273.  
  274.     res = 0;
  275.    
  276. %Makes a single-layer padding of NaN-variables
  277. %around the board.
  278.     z = NaN(6,6);
  279.     z(2:5,2:5) = board;
  280.    
  281.    
  282. %If any of the board pieces has an equal and
  283. %adjacent board piece, this function returns 1.
  284.     for r = 2:5
  285.         for c = 2:5
  286.             if z(r,c) ~= 0
  287.                 if any([z(r-1,c),z(r+1,c),z(r,c+1),z(r,c-1)]==z(r,c));
  288.                     res = 1;
  289.                     return
  290.                 end
  291.             end
  292.         end
  293.     end
  294. end
  295.  
  296. function printboard(board)
  297. %Prints the board. Not much to be said.
  298.  
  299.     for i =1:4
  300.         fprintf('\n+----+----+----+----+')
  301.         fprintf('\n|%4d|%4d|%4d|%4d|',board(i,:))
  302.     end
  303.  
  304.     fprintf('\n+----+----+----+----+\n')
  305.    
  306. end
  307.  
  308. function res = printmenu()
  309. %Prints the in-game menu.
  310.  
  311.     fprintf('\n"w/a/s/d" to move')
  312.     fprintf('\n"l" to save to file')
  313.     fprintf('\n"p" to quit to main menu')
  314.    
  315.     res = input('\n\nMake your choice and press Enter: ','s');
  316.    
  317.    
  318.     %The output is the last character in
  319.     %the input string. I found that while
  320.     %playing fast, I typed in
  321.     %two or more characters at once, so this
  322.     %tries to fix that.
  323.    
  324.     if ~isempty(res)
  325.         res = res(end);
  326.     end
  327.    
  328. end
  329.  
  330. function b = dbread()
  331. %Reads and returns the contents of a
  332. %user-determined file.
  333.  
  334.     clc
  335.  
  336.     s = input('\nEnter file name to read from: ', 's');
  337.    
  338.     fid = fopen(s,'r');
  339.    
  340.     if fid == -1
  341.         error('\nFile could not be accessed.')
  342.     else
  343.         for i = 1:4
  344.             b(i,:) = str2num(fgetl(fid));
  345.         end
  346.     end
  347.    
  348.     fclose(fid);
  349.  
  350. end
  351.  
  352. function dbwrite(board)
  353. %Writes the board to a user-determined file
  354.  
  355.     clc
  356.  
  357.     s = input('\nEnter file name to save in: ', 's');
  358.     fid = fopen(s, 'w');
  359.    
  360.     if fid == -1
  361.         error('\nFile could not be accessed.')
  362.     else
  363.        
  364.         for i = 1:4
  365.             fprintf(fid,'%d %d %d %d\n', board(i,:));
  366.         end
  367.     end
  368.    
  369.     fclose(fid);
  370.  
  371. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement