Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Question is BOLDED, need a MATLAB coding that makes this work. Supposely simple,

ID: 3559925 • Letter: Q

Question

Question is BOLDED, need a MATLAB coding that makes this work.
Supposely simple, but having several errors preventing me from getting it to work .

The description of the project is

Inside the PlaySnake.m file are the codings

function PlaySnake
%PlaySnake Plays a game of SNAKE! on a 100 x 100 board
% The player controls a long, thin creature, resembling a snake, which
% roams around on a bordered plane, picking up food (or some other item),
% trying to avoid hitting its own tail or the "walls" that surround the
% playing area. Each time the snake eats a piece of food, its tail grows
% longer, making the game increasingly difficult.

if (~exist('IsInSnake.m','file'))
error('Cannot find IsInSnake.m. Implement it before calling this function.');
end
if (~exist('GetFood.m','file'))
error('Cannot find GetFood.m. Implement it before calling this function.');
end
if (~exist('MoveHead.m','file'))
error('Cannot find MoveHead.m. Implement it before calling this function.');
end

snake = [5,5]; % starting position
c = 14; % starting size
d = {'right'}; % starting direction

rng('shuffle'); % random seed
p = GetFood(snake); % get a food position

% create a figure to draw the board in
f = figure('NumberTitle','off','Menubar','none','Name','SNAKE!','KeyPressFcn',@key);

% loop as long as (1) the figure is open (2) the snake hasn't hit itself
% and (3) the snake hasn't hit any of the boundaries
while ishandle(f) && ~IsInSnake(snake(2:end,:),snake(1,:)) && ...
snake(1,1) >= 1 && snake(1,1) <= 100 && snake(1,2) >= 1 && snake(1,2) <= 100

draw(snake,p); % draw the board
  
if size(d,2) > 1 % move queue
d = d(2:end);
end
snake = [MoveHead(snake(1,:),d{1});snake]; % move snake head

if IsInSnake(snake(1,:),p) % check if it hit food
p = GetFood(snake); % get a new food point
  
c = c + 5; % growth rate of snake
end

if c ~= 0 % update snake tail
c = c - 1;
else
snake(end,:) = [];
end

pause(0.01); % game delay (smaller = faster)
end

if ishandle(f) % close the figure
close(f);
end

display(sprintf('The length of your snake was %d!',size(snake,1)));

% this gets called every time a key is hit (nested function!)
% changes the movement direction based on what key was hit
function key(~, event)

switch event.Key % checks what key was hit
case 'uparrow'
if ~strcmp(d{1},'down')
d = [d 'up'];
end
case 'downarrow'
if ~strcmp(d{1},'up')
d = [d 'down'];
end
case 'leftarrow'
if ~strcmp(d{1},'right')
d = [d 'left'];
end
case 'rightarrow'
if ~strcmp(d{1},'left')
d = [d 'right'];
end
end

end

end

% draws the board with the snake and food piece
function draw(snake, p)

board = zeros(100); % create black board

for i = 1:size(snake,1) % loop over snake
board(snake(i,1),snake(i,2)) = 1; % add each segment
end

board(p(1),p(2)) = 1; % add the food piece

imshow(board,'InitialMagnification',500); % draw the board
drawnow;

end

Explanation / Answer

function draw_snake(snake,food) plot(snake(:,1),snake(:,2), 'ws',food(1,1),food(1,2), 'rs')%creates the vectors for the food and snake and plots them whitebg([0 0 0])%creates black background axis([0, 25, 0, 25])%creates the axis for gameplay end function checkpoint3() global x y a b n x =randi([10 20],1);%generates random x starting point for snake y =randi([10 20],1);%generates random y starting point for snake d =randi([1,4]);% generates random direction to start in for snake a =randi([1 24],1);%generates random x coordinate for food b =randi([1 24],1);%generates random y coordinate for food snake=[x y];%defines the snake for x and y coordinates food=[a b];%defines food for a and b coordinates figure('KeyPressFcn',@my_callback); function my_callback(splooge,event)%callback function for movement switch event.Character case 30%says if this arrow key is pressed d = 1;%assign this value to that arrow key case 31%says if this arrow key is pressed d = 2;%assign this value to that arrow key case 29 %says if this arrow key is pressed d = 3;%assign this value to that arrow key case 28 %says if this arrow key is pressed d = 4;%assign this value to that arrow key end end while (snake(1)>=0 && snake(2)>=0 && snake(1)
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote