In our design, the board is represented a large grid and the snake by a variable length L x 2 matrix, with L rows representing different "segments" and each segment containing a row and column value.
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
- In our design, the board is represented a large grid and the snake by a variable length L x 2 matrix, with L rows representing different "segments" and each segment containing a row and column value.
- This snake has 7 segments and would be stored as the following matrix:
snake =
2 7
2 6
2 5
2 4
3 4
4 4
4 3 - The head of the snake is stored in the first row and it proceeds sequentially from there. To get the coordinates for the i-th segment, you'd want look at the values for snake(i,1) and snake(i,2).
- The "food" that the snake will be picking up is going to be stored as a point containing the values for the row and column position of the piece.
- "Points" in the context of this homework are pairs of row/column values stored as a vector of length 2. For instance, every row of our snake matrix can be thought of as a point.
- For example in the image above, if the green square was a food piece p, it would have the values
p =
5 8
with p(1) == 5 and p(2) == 8.
- This snake has 7 segments and would be stored as the following matrix:
- You will need to download PlaySnake.m into your MATLAB directory.
- IsInSnake - One thing that's needed is a way to check collisions. That is, we need a way to determine if the snake has run into anything (boundaries, food, itself).
- To check this, create a function IsInSnake which takes two arguments: snake and p
- snake: an L x 2 matrix storing the positions of the snake in the format described above.
- p: a query point (vector of length 2) representing a position to be checked.
- The function should return true if the row and column values in p match those of any snake segment and return false otherwise.
- The function should loop over the rows of the snake matrix, checking each segment to see if it has the same row/column value as p.
- If any segment shares the same position as p the function should return true. If you reach the end of the snake and none of the segments match, return false.
- To check this, create a function IsInSnake which takes two arguments: snake and p
- GetFood - Another thing the game will have to do is generate random food piece positions.
- Create a function GetFood which takes one argument: snake
- snake: an L x 2 matrix storing the position of the snake in the format described above.
- The function should generate and return a point (vector of length 2) with the condition that it does not overlap with any segment from snake.
- We would like to the generate new positions as a random point. To create a random point, you can simply use:
p = randi([1,100],1,2); - This line of code generates a point (vector of length 2) with integers between 1 and 100 (inclusive). Feel free to test it yourself in the command window to see how it works.
- However, there's nothing ensuring that this point will not be in a position that is already occupied by the snake!
- How can we account for this? The simple way is to just generate a random point, check if that point overlaps with our snake, and if it does, discard it and generate a new one.
- Your function should operate as follows:
- Generate a random point (using the line above)
- Check if this point overlaps with snake (using IsInSnake)
- If it does, generate a new point and go back to step 2
- Create a function GetFood which takes one argument: snake
- MoveHead - Lastly, we need a function for moving our snake along, based on the position of the head of the snake and the direction it's moving.
- Create a function MoveHead that takes two arguments: p and direction
- p: a point (vector of length 2) denoting the position of the head of our snake.
- direction: a string taking on the values 'up', 'down', 'left', or 'right' denoting the direction the snake is currently moving.
- The function should return a new point immediately next to p in the direction of movement. (i.e. if the direction was 'up' you would return the point adjacent and above p)
- NOTE: you do not need to check for the boundary conditions of p. You may assume that is it always valid.
- Your function should set up conditionals based on the value of direction.
- Then, depending on the direction, return a new point that is adjacent to p in that direction.
- Create a function MoveHead that takes two arguments: p and direction
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
7 years ago
Purchase the answer to view it

- answer.docx