EGME 205 Digital Computation Project 4
Question # 00144172
Posted By:
Updated on: 12/02/2015 01:30 AM Due on: 12/02/2015

EGME 205 Digital Computation Fall 2015
Project 4
Due: Friday Dec 11 by 6pm
• Submit your files online through the course website before the project deadline.
• Both correctness and good programming style contribute to your project score.
• You must work either on your own or with one partner. You may discuss background issues and
general solution strategies with others, but the project you submit must be the work of just you (and
your partner). Do not show your code to anyone else (besides your partner).
Objectives
Completing this project will help solidify your understanding of vectors (1D arrays) and matrices (2D
arrays). Your best strategy for this project is to decompose each problem into simpler pieces. You will also
practice problem decomposition and testing, as well as flex your creative muscles!
1 Nibbles game! (Snake/worm game)1
Program a game with a “steerable” worm! The worm starts moving from the middle of the game grid and
unless a player steers it by clicking on the plot, it will crash into a wall and die. The goal of the worm is
to eat the food and grow bigger, while avoiding crashing into the walls and itself.
-10 -5 0 5 10
-10
-8
-6
-4
-2
0
2
4
6
8
10
-10 -5 0 5 10
-10
-8
-6
-4
-2
0
2
4
6
8
10
1.1 General Specifications
• The game is played in a 20-by-20 grid, centered on the origin, in a MATLAB figure window. The
game begins with the function call Nibbles(t) where t is a positive number in the range of 0.01 to 1.
• The worm starts with 2 unit-length segments. Every segment of the worm is either oriented vertically
or horizontally. For example, the coordinates (1,1), (2,1), (2,2), (3,2), (3,1), (4,1) can specify the
shape (position) of a 5- segment worm.
• Each segment of the worm can move only horizontally or vertically and the user steers the worm by
clicking somewhere within the plot. If the horizontal distance from the worm’s head to the clicked
point is greater than the vertical distance, the worm should move horizontally (left or right, toward
the point). Otherwise, it should move vertically (up or down, toward the point).
1WARNING: There are a handful of snake games available on the mathworks exchange. Most of them involve the development
of a GUI (Graphical User Interface). Don’t do it! Just don’t look. They will lead you down a rabbit hole, from which
you may never climb out.
EGME 205 Digital Computation Fall 2015
• Each segment of the worm moves 1 unit per t seconds, i.e., pause code execution for t seconds between
updates to the worm’s position. t is the sole parameter to the game function Nibbles. See the help
file for the MATLAB function pause.
• A piece of food, represented by a marker on the plot, is randomly located within the boundaries
(cannot be on the boundary/wall) for each “level”. To beat the level the worm must be steered to
move over the food’s location thereby “eating” the food. After beating a level, the worm grows by
one unit (at the tail) and continues moving in the direction it was going before eating the food.
• The game ends when the head of the worm touches a wall, i.e., the edge of the 20-by-20 grid or
touches its own body.
1.2 MATLAB Code, Program Development, and Details
You will create a function Nibbles to implement this game. Download the file makeClickFig.m and read
the comments and given code. Function makeClickFig is completely implemented to set up a figure window
that responds to mouse clicks.2
1.2.1 Program Development
Don’t try to do everything at once! Decompose the problem into different parts, add the parts to Nibbles
one at a time, and test that the program runs after adding each part. In past projects I decomposed the
problem for you and suggested the order in which to work; this time around, in your final project, you will
do much of the problem decomposition yourself. (I still provide some help, as follows.) Use functions to
encapsulate independent tasks as necessary. One of the first subproblems that you need to solve in this
game is how to represent the worm’s position and direction in your code. Taking the time to plan first and
to test systematically after adding each detail will end up saving you time. And it will give you a good
(and fun) finished product.
1.2.2 Code layout
Start with the needed initializations, like the worm’s initial length (2 units) and its position and direction,
which are up to you. The worm’s head should not start too near to a wall though! The game proceeds
using iteration:
• For each level
– The food’s location needs to be randomly located within the boundaries (not on a wall!) The
food’s location remains the same for the entire level.
– During each level
? Detect whether there’s a mouse click and therefore any change in direction
? Calculate the worm’s new position and redraw the worm in the plot. When the worm’s head
moves in some direction one unit the body must follow. Don’t let the worm get longer (or
shorter) by accident.
? Check if the food has been eaten. Remember to eat the food the worm’s head needs to move
over the food’s location.
– A level is won by eating the food and the length of the worm is increased.
The game ends when the worm’s head touches a wall or its own body.
2This is different from using the function ginput to collect the coordinates of clicks in a window. If a program uses ginput,
it waits for the user click(s) and only after the user has completed the click(s) will the program continue executing subsequent
commands. In this game the Nibbles function continues execution whether or not the user makes a click, i.e. Nibbles does not
ask for clicks; it responds to clicks if they are made.
EGME 205 Digital Computation Fall 2015
1.2.3 Graphics
Use plot to draw the worm as a line (line segments) in the figure window. Use a marker to indicate the
worm’s head and a different marker to indicate the food.
MATLAB’s default behavior is to set the axes limits to display the data in plot in the middle of the figure.
We want the axes to have fixed limits so that the worm is seen to be moving inside a grid, instead of the
grid moving around the worm “fixed” to the center. Therefore use this command to set the axes after a call
to plot:
axis ([ - w /2 w /2 -w /2 w /2] , 'square ')
where w is the width and height (the grid is centered on the origin).
1.2.4 Detecting mouse clicks
The file makeClickFig contains the code
global click
This is a declaration to set the property of the variable named click to be “global,” which means that this
variable is accessible from any function or script file that contains this declaration. The file Nibbles should
contain the following
% Set up the clickable figure
makeClickFig (w )
global click % Global variable ; variable click is accessible in another
% function that has the same global declaration .
% click is vector of length 2 representing a position
% ( click (1) ,click (2) )
where w is the grid size (width and height). (You need to initialize w before this code!)
Once makeClickFig is called to set up a figure window, every time that a user clicks in that window the
variable click stores the coordinates of the clicked point. click is initialized as the empty vector, so to
determined whether a click has been made you can just check whether it is empty. The built-in function
isempty can be used: isempty(click) returns true (1) if click is the empty vector. Be sure to reset
click to empty after you’ve used the values of the user clicked point.
1.2.5 Bells and whistles
Show your creativity and your programming skills! Impress with interesting sound effects, graphics, even
twists in the plot (game plot, not the figure!). Being impressed comes from you demonstrating your
programming techniques—not calling built-in functions. So show us your skills and ideas. There is a 10
percent BONUS of this project’s score devoted to the “bells and whistles” that you demonstrate.
Don’t ask how much you need to do! Some guidelines though: Add sound effect for when the worm crashes
into itself or a wall? Neat! Add a sound effect that varies based on the proximity to the walls? Very
impressive! Contradict the specifications or implement a totally different game? Don’t do it as you will get
zero points for Project 4! If you add any twist to the game, you must clearly state the differences from our
original game in a comment block in Nibbles. Have fun with this project by thinking about cool things to
do, but don’t fixate on the 1 point! Have fun!
Project 4
Due: Friday Dec 11 by 6pm
• Submit your files online through the course website before the project deadline.
• Both correctness and good programming style contribute to your project score.
• You must work either on your own or with one partner. You may discuss background issues and
general solution strategies with others, but the project you submit must be the work of just you (and
your partner). Do not show your code to anyone else (besides your partner).
Objectives
Completing this project will help solidify your understanding of vectors (1D arrays) and matrices (2D
arrays). Your best strategy for this project is to decompose each problem into simpler pieces. You will also
practice problem decomposition and testing, as well as flex your creative muscles!
1 Nibbles game! (Snake/worm game)1
Program a game with a “steerable” worm! The worm starts moving from the middle of the game grid and
unless a player steers it by clicking on the plot, it will crash into a wall and die. The goal of the worm is
to eat the food and grow bigger, while avoiding crashing into the walls and itself.
-10 -5 0 5 10
-10
-8
-6
-4
-2
0
2
4
6
8
10
-10 -5 0 5 10
-10
-8
-6
-4
-2
0
2
4
6
8
10
1.1 General Specifications
• The game is played in a 20-by-20 grid, centered on the origin, in a MATLAB figure window. The
game begins with the function call Nibbles(t) where t is a positive number in the range of 0.01 to 1.
• The worm starts with 2 unit-length segments. Every segment of the worm is either oriented vertically
or horizontally. For example, the coordinates (1,1), (2,1), (2,2), (3,2), (3,1), (4,1) can specify the
shape (position) of a 5- segment worm.
• Each segment of the worm can move only horizontally or vertically and the user steers the worm by
clicking somewhere within the plot. If the horizontal distance from the worm’s head to the clicked
point is greater than the vertical distance, the worm should move horizontally (left or right, toward
the point). Otherwise, it should move vertically (up or down, toward the point).
1WARNING: There are a handful of snake games available on the mathworks exchange. Most of them involve the development
of a GUI (Graphical User Interface). Don’t do it! Just don’t look. They will lead you down a rabbit hole, from which
you may never climb out.
EGME 205 Digital Computation Fall 2015
• Each segment of the worm moves 1 unit per t seconds, i.e., pause code execution for t seconds between
updates to the worm’s position. t is the sole parameter to the game function Nibbles. See the help
file for the MATLAB function pause.
• A piece of food, represented by a marker on the plot, is randomly located within the boundaries
(cannot be on the boundary/wall) for each “level”. To beat the level the worm must be steered to
move over the food’s location thereby “eating” the food. After beating a level, the worm grows by
one unit (at the tail) and continues moving in the direction it was going before eating the food.
• The game ends when the head of the worm touches a wall, i.e., the edge of the 20-by-20 grid or
touches its own body.
1.2 MATLAB Code, Program Development, and Details
You will create a function Nibbles to implement this game. Download the file makeClickFig.m and read
the comments and given code. Function makeClickFig is completely implemented to set up a figure window
that responds to mouse clicks.2
1.2.1 Program Development
Don’t try to do everything at once! Decompose the problem into different parts, add the parts to Nibbles
one at a time, and test that the program runs after adding each part. In past projects I decomposed the
problem for you and suggested the order in which to work; this time around, in your final project, you will
do much of the problem decomposition yourself. (I still provide some help, as follows.) Use functions to
encapsulate independent tasks as necessary. One of the first subproblems that you need to solve in this
game is how to represent the worm’s position and direction in your code. Taking the time to plan first and
to test systematically after adding each detail will end up saving you time. And it will give you a good
(and fun) finished product.
1.2.2 Code layout
Start with the needed initializations, like the worm’s initial length (2 units) and its position and direction,
which are up to you. The worm’s head should not start too near to a wall though! The game proceeds
using iteration:
• For each level
– The food’s location needs to be randomly located within the boundaries (not on a wall!) The
food’s location remains the same for the entire level.
– During each level
? Detect whether there’s a mouse click and therefore any change in direction
? Calculate the worm’s new position and redraw the worm in the plot. When the worm’s head
moves in some direction one unit the body must follow. Don’t let the worm get longer (or
shorter) by accident.
? Check if the food has been eaten. Remember to eat the food the worm’s head needs to move
over the food’s location.
– A level is won by eating the food and the length of the worm is increased.
The game ends when the worm’s head touches a wall or its own body.
2This is different from using the function ginput to collect the coordinates of clicks in a window. If a program uses ginput,
it waits for the user click(s) and only after the user has completed the click(s) will the program continue executing subsequent
commands. In this game the Nibbles function continues execution whether or not the user makes a click, i.e. Nibbles does not
ask for clicks; it responds to clicks if they are made.
EGME 205 Digital Computation Fall 2015
1.2.3 Graphics
Use plot to draw the worm as a line (line segments) in the figure window. Use a marker to indicate the
worm’s head and a different marker to indicate the food.
MATLAB’s default behavior is to set the axes limits to display the data in plot in the middle of the figure.
We want the axes to have fixed limits so that the worm is seen to be moving inside a grid, instead of the
grid moving around the worm “fixed” to the center. Therefore use this command to set the axes after a call
to plot:
axis ([ - w /2 w /2 -w /2 w /2] , 'square ')
where w is the width and height (the grid is centered on the origin).
1.2.4 Detecting mouse clicks
The file makeClickFig contains the code
global click
This is a declaration to set the property of the variable named click to be “global,” which means that this
variable is accessible from any function or script file that contains this declaration. The file Nibbles should
contain the following
% Set up the clickable figure
makeClickFig (w )
global click % Global variable ; variable click is accessible in another
% function that has the same global declaration .
% click is vector of length 2 representing a position
% ( click (1) ,click (2) )
where w is the grid size (width and height). (You need to initialize w before this code!)
Once makeClickFig is called to set up a figure window, every time that a user clicks in that window the
variable click stores the coordinates of the clicked point. click is initialized as the empty vector, so to
determined whether a click has been made you can just check whether it is empty. The built-in function
isempty can be used: isempty(click) returns true (1) if click is the empty vector. Be sure to reset
click to empty after you’ve used the values of the user clicked point.
1.2.5 Bells and whistles
Show your creativity and your programming skills! Impress with interesting sound effects, graphics, even
twists in the plot (game plot, not the figure!). Being impressed comes from you demonstrating your
programming techniques—not calling built-in functions. So show us your skills and ideas. There is a 10
percent BONUS of this project’s score devoted to the “bells and whistles” that you demonstrate.
Don’t ask how much you need to do! Some guidelines though: Add sound effect for when the worm crashes
into itself or a wall? Neat! Add a sound effect that varies based on the proximity to the walls? Very
impressive! Contradict the specifications or implement a totally different game? Don’t do it as you will get
zero points for Project 4! If you add any twist to the game, you must clearly state the differences from our
original game in a comment block in Nibbles. Have fun with this project by thinking about cool things to
do, but don’t fixate on the 1 point! Have fun!

-
Rating:
5/
Solution: EGME 205 Digital Computation Project 4