USING MATLAB Given various screenshots of people playing 2048, identify which ti
ID: 3869544 • Letter: U
Question
USING MATLAB
Given various screenshots of people playing 2048, identify which tiles are currently on the board!
Represent the game board as a 2D matrix of numeric values; each entry in the matrix corresponds
to a tile in the game. If a tile is empty, use 0 for that location; otherwise use the tile’s numeric value.
Create the following function:
•AnalyzeScreenshot
–Summary: Given the name of image, find which tiles are currently on the board
–Inputs: filename (string)
–Outputs: board (4x4 matrix)
–Example: board = AnalyzeScreenshot(‘Screenshot_Alpha.png’);
-2048 4 16 32 16 4 64 32 8 4 ft indows 95 4Explanation / Answer
function twos()
clc
n = mainmenuprint();
%Print main menu to ask user what to do
switch n
case 1
%Play new game
game(zeros(4,4))
case 2
%Play game loaded from file
f = dbread();
if f == -1
twos()
return
else
game(f)
end
case 0
%Quit
clc
clear
return
end
end
function c = mainmenuprint()
%This function displays the homescreen
%and asks the user what they want to do
while 1
clc
% Bask in the glory of my ASCII art skills.
% Jk. Generated at http://bigtext.org/
fprintf(' ____ ___ _ _ ___ ')
fprintf(' |___ \ / _ \| || | ( _ ) ')
fprintf(' __) | | | | || |_ / _ \ ')
fprintf(' / __/| |_| |__ _| (_) |')
fprintf(' |_____|\___/ |_| \___/ ')
fprintf(' -----Welcome to 2048!-----')
fprintf(' 1. New game')
fprintf(' 2. Load game from file')
fprintf(' 0. Quit')
%Takes user input. (It is taken as a string to make sure
%invalid inputs (like letters) can be ignored)
c = str2double(input(' Make a choice(0, 1 or 2) and press Enter : ','s'));
if ~ismember(c,0:2)
continue
end
break
end
end
function game(board)
%This function is the backbone of the game
%If the board has less than two full spaces for
%any reason, it assigns either a 2 or a 4 to two
%random spaces on the board
if sum(sum(board ~= 0))<2;
a = [randperm(4,2)',randperm(4,2)'];
b = 2.*randi(2,2,1);
a(:,3) = b;
board(a(1,1),a(1,2)) = a(1,3);
board(a(2,1),a(2,2)) = a(2,3);
end
clc
%Prints the board
printboard(board)
%Prints the menu
c = printmenu();
%A variable that may or may not be useful.
%It was useful in earlier versions but I am too
%afraid to take it away now.
d = 'n';
%While the board has empty spaces, or is deemed
%playable by my lovely poss() function.
while sum(sum(board ==0))>0 || poss(board)
if strfind('wasd',c)
%If the input was a direction, move the
%move the board pieces in that direction
[board,d] = movenew(board,c);
elseif c == 'l'
%Save the current board to a file and
%return to the main menu
b = dbwrite(board);
if b == 0
twos()
return
end
elseif c == 'p'
%Return to the main menu
twos()
return
else
%If the user enters nothing, ask the
%user for an input again and restart
%the loop
clc
printboard(board)
c = printmenu();
continue
end
%This was useful in earlier versions. Now I'm
%afraid it'll break the code if I remove it,
%so it stays.
if d ~= 'n'
c = d;
continue
end
clc
printboard(board)
c= printmenu();
end
%When the while loop is over, the user is informed
%that the game is over and is returned to the main
%menu.
fprintf(' Game Over. :( ')
pause(3)
twos()
return
end
function [board, d] = movenew(board,k)
%Moves the board pieces in the desired direction.
%I only explained the case where the option is to move
%downwards (k == 's') because the rest are the same but with different
%variables changing.
%Makes a single-layer padding of NaN-variables
%around the board.
d = 'n';
first = board;
g = NaN(6,6);
g(2:5,2:5) = board;
board = g;
if k == 's'
%down
for r = 4:-1:2
%Starts on the third row and works upwards.
for c = 2:5
%if the piece is non-zero
if board(r,c) ~= 0
p = r;
%while the piece below it is zero or the same
while board(r+1,c) == 0 || board(r,c) == board(r+1,c)
if board(r,c) == board(r+1,c)
%If the piece is the same as the one below it,
%nullify itself and double the piece below it
%and break the loop.
board(r+1,c) = board(r,c).*2;
board(r,c) = 0;
break
elseif board(r+1,c) == 0
%if the piece below it is empty
%while the piece below it is empty,
%Move the piece to the space below it
board(r+1,c) = board(r,c);
board(r,c) = 0;
r = r+1;
end
end
r = p;
end
end
end
elseif k == 'a'
%left
for r = 2:5
for c = 3:5
if board(r,c) ~= 0
p = c;
while board(r,c-1) == 0 || board(r,c) == board(r,c-1)
if board(r,c) == board(r,c-1)
board(r,c-1) = board(r,c).*2;
board(r,c) = 0;
break
elseif board(r,c-1) == 0
board(r,c-1) = board(r,c);
board(r,c) = 0;
c = c-1;
end
end
c = p;
end
end
end
elseif k == 'd'
%right
for r = 2:5
for c = 4:-1:2
if board(r,c) ~= 0
p = c;
while board(r,c+1) == 0 || board(r,c) == board(r,c+1)
if board(r,c) == board(r,c+1)
board(r,c+1) = board(r,c).*2;
board(r,c) = 0;
break
elseif board(r,c+1) == 0
board(r,c+1) = board(r,c);
board(r,c) = 0;
c = c+1;
end
end
c = p;
end
end
end
elseif k == 'w'
%up
for r = 3:5
for c = 2:5
if board(r,c) ~= 0
p = r;
while board(r-1,c) == 0 || board(r,c) == board(r-1,c)
if board(r,c) == board(r-1,c)
board(r-1,c) = board(r,c).*2;
board(r,c) = 0;
break
elseif board(r-1,c) == 0
board(r-1,c) = board(r,c);
board(r,c) = 0;
r = r-1;
end
end
r = p;
end
end
end
end
%removes the padding of NaNs
board = board(2:5,2:5);
%If the board is not unchanged after the move,
% a 2 or a 4 is added to a random empty
%spot on the board.
if ~isequal(board,first)
[x,y] = find(board==0);
b = 2.*randi(2);
c = randi(length(x));
board(x(c),y(c)) = b;
end
end
function res = poss(board)
%Checks if the board is still playable even if the
%board is full
res = 0;
%Makes a single-layer padding of NaN-variables
%around the board.
z = NaN(6,6);
z(2:5,2:5) = board;
%If any of the board pieces has an equal and
%adjacent board piece, this function returns 1.
for r = 2:5
for c = 2:5
if z(r,c) ~= 0
if any([z(r-1,c),z(r+1,c),z(r,c+1),z(r,c-1)]==z(r,c));
res = 1;
return
end
end
end
end
end
function printboard(board)
%Prints the board. Not much to be said.
for i =1:4
fprintf(' +----+----+----+----+')
fprintf(' |%4d|%4d|%4d|%4d|',board(i,:))
end
fprintf(' +----+----+----+----+ ')
end
function res = printmenu()
%Prints the in-game menu.
fprintf(' "w/a/s/d" to move')
fprintf(' "l" to save to file')
fprintf(' "p" to quit to main menu')
res = input(' Make your choice and press Enter: ','s');
%The output is the last character in
%the input string. I found that while
%playing fast, I typed in
%two or more characters at once, so this
%tries to fix that.
if ~isempty(res)
res = res(end);
end
end
function b = dbread()
%Reads and returns the contents of a
%user-determined file.
clc
s = input(' Enter file name to read from: ', 's');
fid = fopen(s,'r');
if fid == -1
b = fid;
fprintf(' File could not be accessed. Returning to main menu')
pause(1)
pause(0.7)
fprintf('.')
pause(0.7)
fprintf('.')
pause(0.7)
fprintf('.')
pause(0.7)
else
for i = 1:4
b(i,:) = str2num(fgetl(fid));
end
fclose(fid);
end
end
function b = dbwrite(board)
%Writes the board to a user-determined file
clc
s = input(' Enter file name to save in: ', 's');
fid = fopen(s, 'w');
if fid == -1
b = fid;
fprintf(' File could not be accessed.')
pause(2)
else
b=0;
for i = 1:4
fprintf(fid,'%d %d %d %d ', board(i,:));
end
end
fclose(fid);
end
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.