Description
Register Conventions
Y m f ll he egi e c n en i n a gh in lec e and e ie ed in eci a i n. Fail e f ll hem ill e l in l f c edi hen e g ade k. He e i a b ief mma f he egi e
c n en i n and h e f hem ill im ac g ading:
I i he callee e n ibili a e an $ egi e i e i e b a ing c ie f h e egi e n he ack and e ing hem bef e e ning.
If a f nc i n call a ec nda f nc i n, he calle m a e $ a bef e calling he callee. In addi i n, if he calle an a a ic la $a, $ $ egi e al e be e e ed ac he ec nda f nc i n call, he be ac ice ld be lace a c f ha egi e in an $
egi e bef e making he f nc i n call.
A f nc i n hich all ca e ack ace b adj ing $ m e e $ i iginal al e bef e e ning.
Regi e $f and $g a e ea ed a e e ed egi e f he e f hi c e. If a f nc i n m difie ne b h, he f nc i n m e e hem bef e e ning he calle . The e eall i n ea n f c de ch he $g egi e , lea e i al ne.
The f ll ing ac ice ill e l in l f c edi :
| B e-f ce a ing f all $ egi e in a f nc i n he i e a ing $ egi e e i en b a f nc i n.
Callee- a ing f $a, $ $ egi e a a mean f hel ing he calle . |
ha a e n |
| Hiding al e in he $k, $f and $a egi e ing al e in main mem b | a f ff e |
| $g . Thi i ba icall chea ing a be , a f m f la ine , d n d i . We an ch c de e find. | ill c mmen |
How to Test Your Functions
T e im lemen ed f nc i n , en he ided main file in MARS. Ne , a emble he main file and n i . MARS ill incl de he c n en f an .a m file efe enced i h he .incl de di ec i e( ) a he end f he file and hen a end he c n en f h k3.a m file bef e a embling he g am.
Each main file call a ingle f nc i n i h ne f he am le e ca e and in an e n al e( ). Y ill need change he a g men a ed he f nc i n e f nc i n i h he he ca e . T e each f f nc i n h ghl , c ea e n e ca e in h e main file . Y bmi i n ill n be g aded ing he e am le ided in hi d c men ing he ided main
file( ). D n bmi main file i h h k3.a m file e ill dele e hem. Al lea e n e
ha he e e ing main file ill n be ed in g ading.
Again, an m difica i n he main file ill n be g aded. Y ill bmi nl h k3.a m f g ading. Make e ha all c de e i ed f im lemen ing f nc i n i incl ded in he h k3.a m file. T make e ha c de i elf-c n ained, a embling h k3.a m file b i elf in MARS.
If ge an e ( ch a a mi ing label), hi mean ha h k3.a m file i a em ing efe ence label in he main file, hich ill n ha e he ame name d ing g ading!
A Final Reminder on How Your Work Will be Graded
I i im e a i e (c cial, e en ial, nece a , c i icall im an ) ha im lemen he f nc i n bel e ac l a ecified. D n de ia e f m he ecifica i n , e en if hink a e im lemen ing he g am in a be e a . M dif he c n en f mem nl a de c ibed in he f nc i n ecifica i n !
Preliminaries
F hi a ignmen ill be im lemen ing a e i n f he cla ic game f S nake in MIPS. B iefl , he nake c n i f e e al b d egmen ( e e en ed b ASCII cha ac e ) ha can m e a nd
a 2D game b a d, ea ing a le . Each ime he nake ea an a le, i leng h inc ea e b ne. In he iginal game, he game end hen he nake n in a all, n in i elf, n f b nd . We ill im lemen he ame ba ic l gic in e i n.
The head f he nake i e e en ed b he ASCII cha ac e 1 , i h each f ll ing egmen in he b d gi en b c n ec i e digi cha ac e (i.e., 2 , 3 , 4 , e c.) Af e 9 , b d egmen a e den ed
| i h e ca e le e , a ing i h A and g ing all he a h gh he al habe Z . Th , he
nake can be a m 9 + 26 = 35 b d egmen in leng h.
The game b a d c n i f a 2D g id f ASCII cha ac e ( l ), i h he e i d, . , e e en ing an |
||||||
| em l ; a nd ign, # , e e en ing a all; he l e ca e le e a e e en ing an a | le, and he | |||||
| digi and e ca e le e e e en ing a f he nake. Bel | i an e am le f an in- | g e | ||||
| game n a 15 30 g id (15 , 30 c l mn ).
………………………… ………………………… ……######……######…… ……#…………….#…… ……#…………….#…… ……#…………….#…… ………………………… ….a……….123………… ……………..4………… ……#……….56….#…… ……#…………….#…… ……#…………….#…… ……######……######…… ………………………… …………………………
|
||||||
| The la e c e in a he/ he m e he nake ab | he game g id and ea | a | le . Sc | ing i | ||
e lained la e .
Data Structures
T cha ac e i e he a e f he game e ack e e al iece f da a in he f m f a s r c, hich i like an bjec i h da a field nl (n me h d ). We ill call hi da a e G ameState h gh hi d c men .
num_rows: he n mbe f in he g id (an n igned b e) num_cols: he n mbe f c l mn in he g id (an n igned b e) head_row: he f he nake head (an n igned b e) head_col: he c l mn f he nake head (an n igned b e) length: he leng h f he nake (an n igned b e) grid: he c n en f he game b a d (a n ll- e mina ed ing ha e he b a d in -maj de )
The m f he g id i #0, and he lef m c l mn i c l mn #0. Simila l , he b m-m i n um_rows-1, and he igh m c l mn i n um_cols-1. The ma im m g id i e i 99 99.
A an e am le, he game b a d h n ab e ld be e e en ed in MIPS a f ll :
.align 2 # ensures that the data structure is on a word-aligned boundary state: # name of the data structure (game state)
.byte 15 # num_rows (byte #0)
.byte 30 # num_cols (byte #1)
.byte 7 # head_row (byte #2)
.byte 15 # head_col (byte #3)
.byte 6 # length (byte #4)
# Game grid: (bytes #5-end)
.ascii
“…………………………………………………………######.
…..######…………#…………….#…………#…………….#…
………#…………….#………………………………….a……
….123………………………..4………………#……….56….#.
………..#…………….#…………#…………….#…………### ###……######…………………………………………………..
…….”
| T acce he field f he c , e need be ided i | a ing add e (i.e., he add e f he |
| num_rows field). F m he e e can e l bu i h a incl ding he c n en f he g id. | ia e ff e acce he field al e , |
Part 1: Load a Game Board from Disk
int, int load_game(GameState* state, string filename)
The l oad_game f nc i n ead he c n en f a file ha define a game b a d and ini iali e he efe enced G ameState da a c e. The n a i n G ameState* indica e ha s tate i he a ing
add e f a G ameState c . Y ma a me ha s tate in a bl ck f mem la ge en gh e he c e e en ed in ide he file. If he file e i , ma a me ha he file c n en a e alid. If he file d e n e i , he f nc i n im l e n -1, -1.
The file f ma i e im le:
# of rows in the game grid # of columns in the game grid
contents of grid in which each row is on its own line
Im an n e: n Mic f Wind , a line end i h he cha ac e c mbina i n \ r\n, he ea n
Mac and he Uni -like e a ing em like Lin , a line end nl i h \ n. Y c de m be able handle b h line-ending le . All line f he file, incl ding he final f he g id, a e g a an eed end i h \ n \r\n. A an e am le, he ab e game g id ld be a ed in a Mic f Wind -gene a ed file a :
15\r\n
30\r\n
…………………………\r\n
…………………………\r\n
……######……######……\r\n
……#…………….#……\r\n
……#…………….#……\r\n
……#…………….#……\r\n
…………………………\r\n
….a……….123…………\r\n
……………..4…………\r\n
……#……….56….#……\r\n
……#…………….#……\r\n
……#…………….#……\r\n
……######……######……\r\n
…………………………\r\n
…………………………\r\n
Again, c de m be able c n end i h \ r\n line-ending
and i h \ n line-ending .
T a i i h eading and i ing file , MARS ide e e al em call :
| Service | Code
in $v0 |
Arguments | Results | |||
| en file | 13 | $a0 = add e
$a1 = flag $a2 = m de |
f n ll- e mina ed filename | ing | $ 0 c n ain file
de c i (nega i e if e ) |
|
| ead f m file | 14 | $a0 = file de c i
$a1 = add e f in b ffe $a2 = ma im m # f cha ac e ead
|
$ 0 c n ain # f cha ac e ead (0 if end- f-file, nega i e if
e ) |
|||
| i e file | 15 | $a0 = file de c i
$a1 = add e f b ffe (nega i e if e $a2 = ma im m # f cha ac e i e |
) | $ 0 c n ain cha ac e
|
# f i en | |
| cl e file | 16 | $a0 = file de c i | ||||
Se ice 13: MARS im lemen h ee flag al e : 0 f ead- nl , 1 f i e- nl i h c ea e, and 9 f i e- nl i h c ea e and a end. I ign e m de. The e ned f ile de c i ill be nega i e if he e a i n failed. MARS main ain file de c i in e nall and all ca e hem a ing i h 3. File de c i 0, 1 and 2 a e al a en f eading f m anda d in , i ing anda d , and
| i ing anda d e , e ec i el . An e am le f h e he e
MARS call eb age.
|
call can be f nd n he |
| S me ad ice: ead he c n en f he file ne cha ac e a a ime ing | em call #14. Thi em |
| call e i e a mem b ffe h ld he cha ac e ead f m he di k. Y | h ld all ca e f b e f |
mem n he ack (b adj ing $ ) e ha b e em a il . Di ca d ne line cha ac e (b h
\r and \ n) a ead hem and d n e hem in he G ameState c . Finall , emembe e e $ nce ha e fini hed eading he file c n en and cl e he file i h em call #16.
MARS can be a li le b gg hen i c me ening file . The ef e, ei he : all .a m and . game file in he ame di ec a he MARS .ja file,
e ab l e a h name hen gi ing he filename in e ing main .
The f nc i n l oad_game ake he f ll ing a g men , in hi de :
state: a p oin er (i.e., a ing mem add e f) an nini iali ed G ameState c la ge
en gh h ld he c n en f he game e e en ed b file be l aded. A me ha he
c n en f he c a e filled i h and m ga bage.
filename: a ing c n aining he filename en and ead he c n en f
Re n in $ 0:
-1 if he in file d e n e i ; he i e: 1 if a l e ca e le e a e e en ing an a le a
f nd in he file; 0, if n
Re n in $ 1:
-1 if he in file d e n e i ; he i e: he n mbe f all cha ac e , # , f nd in he file
Addi i nal e i emen :
The f nc i n m n i e an change main mem e ce a needed. If he file d e n
e i , n change ma be i en s tate.
E am le #1:
filename = game01.txt
Re n al e in $ 0: 1
Re n al e in $ 1: 6
C n en f s tate m be da ed eflec he da a c e defined in he file.
E am le #2:
filename = game02.txt
Re n al e in $ 0: 0
Re n al e in $ 1: 16
C n en f s tate m be da ed eflec he da a c e defined in he file.
E am le #3:
filename = junk.txt (n n-e i en file)
Re n al e in $ 0: -1
Re n al e in $ 1: -1
C n en f s tate m be lef nchanged.
P art 2: Get the Character Stored in the Game Grid
int get_slot(GameState* state, byte row, byte col)
The f nc i n g et_slot e n he cha ac e ed in he l a state.grid[row][col]. A ec mmenda i n: e l bu ead ile al e f m he game b a d. If r ow i ide he alid ange
col i ide he alid ange f he gi en G ameState, he f nc i n e n -1. F in ance, he alid
ange f he r ow a g men i [0, board.num_rows-1].
The f nc i n ake he f ll ing a g men , in hi de :
state: a in e a alid G ameState c row: he f he g rid a a f m he e e an ead a al e. Thi i an 8-bi , c m lemen al e.
col: he c l mn f he g rid a a f m he e e an ead a al e. Thi i an 8-bi , c m lemen al e.
Re n in $ 0:
he cha ac e l ca ed a s tate.grid[row][col],
-1 f he e c ndi i n e lained ab e
Addi i nal e i emen :
The f nc i n m n i e an change main mem .
E am le :
In he e am le bel , imagine ha e ini iali ed a e b l ading i i h g ame01.txt:
5
12
…………
.a.#.1..#…
…#.2..#…
…#.3..#…
…..4567…
Re n al e he han -1 a e he ASCII c de f he e ned cha ac e .
| o | col | Return Value ($v0) |
| 1 | 5 | 49 |
| 2 | 3 | 35 |
| 3 | 16 | -1 |
| -3 | 5 | -1 |
Part 3: Set the Character Stored in the Game Grid
int set_slot(GameState* state, byte row, byte col, char ch)
The f nc i n s et_slot e he al e ed in he l a s tate.grid[row][col] c h. A ec mmenda i n: e s b i e a cha ac e in he game g id. If r ow i ide he alid ange col
i ide he alid ange f he gi en G ameState, he f nc i n e n -1. O he i e, he f nc i n
e n c h. The f nc i n d e n need alida e ha c h i a alid cha ac e f he Snake game.
The f nc i n ake he f ll ing a g men , in hi de :
state: a in e a alid G ameState c row: he f he g rid a a a hich e an i e a cha ac e . Thi i an 8-bi , c m lemen al e.
col: he c l mn f he grid a a a hich e an i e a cha ac e . Thi i an 8-bi , c m lemen al e.
ch: he cha ac e i e a s tate.grid[row][col]
Re n in $ 0:
ch, ided ha r ow and c ol a e b h alid,
-1 f he e c ndi i n e lained ab e
Addi i nal e i emen :
The f nc i n m n i e an change main mem e ce a nece a .
E am le :
In he e am le bel , imagine ha e ini iali ed a e b l ading i i h g ame01.txt.
5
12
…………
.a.#.1..#…
…#.2..#…
…#.3..#…
…..4567…
E am le #1:
row = 0 col = 5 ch = Q
Re n al e in $ 0: 81 U da ed game a e:
…..Q……
.a.#.1..#…
…#.2..#…
…#.3..#…
…..4567…
E am le #2: row = 2 col = 11 ch = Z
Re n al e in $ 0: 90 U da ed game a e:
…………
.a.#.1..#…
…#.2..#..Z …#.3..#…
…..4567…
E am le #3: row = 3 col = 7 ch = ?
Re n al e in $ 0: 63 U da ed game a e:
…………
.a.#.1..#…
…#.2..#…
…#.3.?#…
…..4567…
E am le #4: row = -5 col = 2 ch = *
Re n al e in $ 0: -1
The game a e h ld be nchanged.
Part 4: Place an Apple in the Game Grid
int, int place_next_apple(GameState* state, byte[] apples, int apples_length)
The f nc i n p lace_next_apple ea che he a pples a a f a alid a le lace in he game g id and lace i . The a pples a a c n i f a pples_length ai f in ege ed in -maj de ha e e en ( row, col) c dina e he e an a le ha n e been laced in he game g id. The ai ( -1, -1) in he a a indica e ha an a le ha al ead been laced a ha i i n in he g id. F e am le, e ha ha a le a laced d ing a i call p lace_next_apple.
A an e am le, e a pples begin [4, 2, 7, 3, 1, 9, -1, -1, …]. Thi indica e
ha en ial l ca i n in he game g id he e an a le c ld be laced incl de (4, 2), (7, 3), (1, 9),
am ng he . T find an a le lace in he g id, he f nc i n ill a i h (4, 2) and ee if ha i i n in he g id i em (i.e., s tate.grid[4][2] c n ain . ). If , s tate.grid[4][2] i
changed a i h s et_slot, and hen a pples[0] and a pples[1] a e b h changed -1. On he he hand, e s tate.grid[4][2] c n ain a all a a f he nake an he a le. In
hi ca e, p lace_next_apple ki e he (4, 2) ai and ie (7, 3). The ea ch c n in e in hi
fa hi n n il a alid a le can be laced. When he ai (-1, -1) i enc n e ed in apples,
place_next_apple ki e i and c n in e he ne ai . Y ma afel a me ha place_next_apple ill al a be able find a alid a le lace in he game g id.
The f nc i n ake he f ll ing a g men , in hi de :
state: a in e a alid G ameState c apples: an a a f a pples_length ai f 8-bi , c m apples_length: he n mbe f ai f in ege in a pples
|
Re n in $ 0: he f he a le ha a laced
Re n in $ 1: he c l mn f he a le ha a laced
Addi i nal e i emen : |
|
| The f nc i n m n i e an change main mem
The f nc i n m call g et_slot and s et_slot.
|
e ce |
lemen in ege
a nece a .
E am le :
In he e am le bel , imagine ha ini iali ed a e b l ading i i h g ame01.txt:
5
12
…………
.a.#.1..#…
…#.2..#…
…#.3..#…
…..4567…
E am le #1:
apples = [3, 2, 1, 4, 4, 3] apples_length = 3 Re n al e in $ 0: 3
Re n al e in $ 1: 2
U da ed a pples: [-1, -1, 1, 4, 4, 3] U da ed game a e:
…………
.a.#.1..#…
…#.2..#…
..a#.3..#…
…..4567…
E am le #2:
apples = [2, 5, 3, 5, 1, 4, 4, 3, 2, 9] apples_length = 5 Re n al e in $ 0: 1
Re n al e in $ 1: 4
U da ed a pples: [2, 5, 3, 5, -1, -1, 4, 3, 2, 9] U da ed game a e:
…………
.a.#a1..#…
…#.2..#…
…#.3..#…
…..4567…
E am le #3:
apples = [2, 5, 3, 5, -1, -1, -1, -1, 4, 1, 4, 3, 2, 9] apples_length = 7 Re n al e in $ 0: 4
Re n al e in $ 1: 1
U da ed a pples: [2, 5, 3, 5, -1, -1, -1, -1, -1, -1, 4, 3, 2, 9] U da ed game a e:
…………
.a.#.1..#…
…#.2..#…
…#.3..#…
.a…4567…
Part 5: Find the Next Body Segment of the Snake
int, int find_next_body_part(GameState* state, byte row, byte col, char target_part)
The f nc i n f ind_next_body_part in ec he f g id l , d n, he lef and he igh f s tate.grid[row][col] f he cha ac e t arget_part. If t arget_part i f nd in ne f
h e l ca i n , hen he c dina e f t arget_part a e e ned. O he i e, he f nc i n e n -1, -1. The f nc i n al e n -1, -1 if r ow c ol i ide i alid ange f s tate.grid. Thi f nc i n i called b he f nc i n in ide f a l find all f he b d egmen f he nake.
The f nc i n ake he f ll ing a g men , in hi de : state: a in e a alid G ameState c
| row: he f he g rid a a l a nd hich e ea ch f t arget_part. Thi i an 8-bi , c m lemen al e.
col: he c l mn f he g rid a a l a nd hich e ea ch f t arget_part. Thi i an |
||
| 8-bi , c m lemen al e. | ||
| target_part: he a f he nake b d egmen e a e l | king f | |
| Re
|
n in $ 0:
he f t arget_part, if i a f nd; -1, he i e |
|
| Re
|
n in $ 1:
he c l mn f t arget_part, if i a f nd; -1, he i e |
|
| Addi i nal e i emen :
The f nc i n m n i e an change main mem . The f nc i n m call g et_slot.
|
||
E am le :
In he e am le bel
, imagine ha ini iali ed a e b l ading i i h g ame01.txt:
5
12
…………
.a.#.1..#…
…#.2..#…
…#.3..#…
…..4567…
| o | col | a ge | _ a | Return Values |
| 3 | 5 | 4 | 4, 5 | |
| 4 | 7 | 7 | 4, 8 | |
| 3 | 5 | 9 | -1, -1 |
Part 6: Slide (Slither) the Snake Forward One Slot
int slide_body(GameState* state, byte head_row_delta, byte head_col_delta, byte[] apples, int apples_length)
The f nc i n s lide_body a em lide ( li he ) he nake ne lace f a d in he di ec i n
(head_row_delta, head_col_delta):
(1, 0): m e he nake d n ne , a ing f m he head and i h he b d f ll ing, ne egmen a a ime
(-1, 0): m e he nake ne
(0, 1): m e he nake igh ne c l mn
(0, -1): m e he nake lef ne c l mn
Thi f nc i n d e n me el lide he en i e nake , d n, lef igh . Ra he , he ai (head_row_delta, head_col_delta) indica e h he nake head h ld l ead he m emen . T he field s tate.head_row and s tate.head_row m be da ed, a a ia e.
| F in ance, e he nake i in he c nfig a i n bel : | |||||
|
………. .a…….. …..1…. …..234.. …….56.
|
|||||
| and e call s lide_body i h (0, -1) f | ( head_row_delta, head_col_delta). The nake i | ||||
| n i i ned a f ll :
………. .a…….. ….12…. …..345.. …….6..
|
|||||
| S e e call s lide_body ime . N e ha e hi :
………. .a…….. ..1234…. …..56… ……….
|
i h (0, -1) f | ( head_row_delta, head_col_delta) | m | e | |
| N ice h he b d li he al ng i h he head leading he a .
The f nc i n ake he f ll ing a g men , in hi de : |
|||||
state: a in e a alid G ameState c head_row_delta: he e ical di ec i n in hich m e he nake head. Thi i an 8-bi , c m lemen al e. head_col_delta: he h i n al di ec i n in hich m e he nake head. Thi i an 8-bi , c m lemen al e.
| apples: an a a f a pples_length ai f 8-bi , c m lemen in ege apples_length: he n mbe f ai f in ege in a pples
|
|||||
| Y ma a me ha he ai ( head_row_delta, head_col_delta) i ne f he f c mbina i n f -1, 0, 1 li ed ab e.
|
|||||
| The f nc i n e n a c de in $ 0 hich indica e ha ha ened d ing he a em ed m | e: | ||||
| 0 if he nake head m ed n an em l (i.e., . ) and li he ed f a d i h
ble |
an | ||||
| 1 if he nake head m ed n a l c n aining an a le (i.e., a ) and li he ed f i h an ble | a d | ||||
| -1 if he nake c ld n m | e f | a d beca e d ing ld ake i ide he game b a d, | |||
| ld in e ec a all l , | ld in e ec i h a b d a f he nake. Re n -1 i h | ||||
| making an change he | a | f he game g id. | |||
T make he nake li he f a d, fi m e he head ne l f a d in he de i ed di ec i n. Then, call f ind_next_body_part e ea edl in a l i e a i el m e each he a f a d ne lace. S ecificall , b d egmen 2 ill ake he lace f 1 ( he head), 3 ill ake he lace f 2 , 4
ill ake he lace f 3 , and n, n il he final b d egmen f he nake ha m ed.
In he ca e ha he nake i ab m e n an a le, call p lace_next_apple bef e m ing an egmen f he nake f a d. Then ha e he en i e nake li he f a d in he de i ed di ec i n,
i h he head e lacing he a le. N e ha s lide_body d e n inc ea e he leng h f he nake hen he nake c n me an a le. Tha a k i handled b a diffe en f nc i n
| (i ncrease_snake_length) .
Addi i nal e i emen : |
|
| The f nc i n m n i e an change main mem e ce a nece a
The f nc i n m call g et_slot, s et_slot, p lace_next_apple and find_next_body_part.
E am le :
|
. |
| In he e am le bel , imagine ha e ini iali ed a e b l ading i i h g ame03.txt: | |
8
14
…………..
……##……
…………..
..#..a…..#..
..#..1234..#..
……..56…E
……##.7..CD ………89AB.
E am le #1:
head_row_delta = 0 head_col_delta = -1 apples = [1, 7, 3, 2, 1, 4, 4, 3] apples_length = 4
Re n al e in $ 0: 0
E ec ed a pples: [1, 7, 3, 2, 1, 4, 4, 3] U da ed game a e:
…………..
……##……
…………..
..#..a…..#..
..#.12345..#..
……..67….
……##.8..DE
………9ABC.
E am le #2:
head_row_delta = -1 head_col_delta = 0 apples = [1, 7, 3, 2, 1, 4, 4, 3] apples_length = 4
Re n al e in $ 0: 1
U da ed a pples: [1, 7, 3, 2, -1, -1, 4, 3] U da ed game a e:
…………..
….a.##……
…………..
..#..1…..#..
..#..2345..#..
……..67….
……##.8..DE
………9ABC.
E am le #3:
head_row_delta = 0 head_col_delta = 1 apples = [1, 7, 3, 2, 1, 4, 4, 3] apples_length = 4 Re n al e in $ 0: -1 apples emain nchanged.
Game a e emain nchanged.
Part 7: Add a New Body Segment to the Tail of the Snake
int add_tail_segment(GameState* state, char direction, byte tail_row, byte tail_col)
The f nc i n a dd_tail_segment ake he c dina e f he nake ail a ( tail_row, int tail_col) and a em add a ne egmen he ail f he nake in he gi en di ec i n (c har
direction) . N e ha hi f nc i n i e en iall a hel e f nc i n f increase_snake_length. F e am le, e ha d irection = U and t ail_row = 3, t ail_col = 6. The e a g men indica e ha he f nc i n h ld add a ail egmen a inde s tate.grid[2][6].
Y ma a me ha s tate.grid[tail_row][tail_col] i he alid, ac al ail f he nake.
| The ne ail egmen ma nl be laced n an em g id l , den ed b he e i d cha ac e , . . If he ne ail egmen i cce f ll added, he l ength field f he s tate bjec m be inc emen ed
b 1. Tha ne leng h i e ned b a dd_tail_segment. If he ail egmen cann be added ( e.g., d e a c lli i n beca e he nake leng h i al ead 35), he f nc i n make n change main mem and e n -1.
The f nc i n ake he f ll ing a g men , in hi de : |
|
| state: a in e a alid G ameState c direction: he di ec i n ela i e he ail f he nake a end a ne egmen . M be
ne f U , D , L R . F e am le, D indica e ha he ne ail egmen h ld be laced in he immedia el bel he c en ail egmen (if ible). A an he e am le, R indica e ha he ne ail egmen h ld be laced in he c l mn immedia el he igh f he c en ail egmen (if ible). tail_row: he n mbe f he final egmen in he ail. Y ma a me ha hi al e i alid. Thi i an 8-bi , c m lemen al e. tail_col: he c l mn n mbe f he final egmen in he ail. Y ma a me ha hi al e i alid. Thi i an 8-bi , c m lemen al e. |
|
| Re
|
n in $ 0:
The da ed al e f s tate.length if he ne ail egmen a laced. If he ail egmen c ld n be laced if d irection i n ne f U , D , L R , hen $ 0 i -1. If d irection i in alid, he f nc i n m n make an change mem and e n -1. |
| Addi i nal e i emen :
The f nc i n m n i e an change main mem e ce a nece a . The f nc i n m call g et_slot and s et_slot.
E am le :
In he e am le bel , imagine ha e ini iali ed a e b l ading i i h g ame01.txt: |
|
5
12
…………
.a.#.1..#…
…#.2..#…
…#.3..#…
…..4567…
E am le #1:
tail_row = 4 tail_col = 8 direction = R Re n al e in $ 0: 8 U da ed game a e:
…………
.a.#.1..#…
…#.2..#…
…#.3..#…
…..45678..
E am le #2:
tail_row = 4 tail_col = 8 direction = D
Re n al e in $ 0: -1
Game a e emain nchanged.
E am le #3:
tail_row = 4 tail_col = 8 direction = U
Re n al e in $ 0: -1
Game a e emain nchanged.
Part 8: Increase the Length of the Snake’s Body
int increase_snake_length(GameState* state, char direction)
The f nc i n i ncrease_snake_length call n f ind_next_body_part (i e a i el ) and hen add_tail_segment find he ail f he nake in he game and a end a ne ail egmen in he opposi e di ec i n ha he nake head i m ing. F e am le, if d irection = L , hen increase_snake_length ill a em add a ne egmen he r igh f he c en ail. Like i e, if d irection = D , hen increase_snake_length ill a em add a ne egmen a bo e
he c en ail egmen . If hi a em i n cce f l, he f nc i n ill ea ch he cell in he
neighb h d f he ail in c n e cl ck i e de find a i i n f he ne ail egmen . Thi a ec f he f nc i n ill be made clea i h a e f e am le bel . N e ha increase_snake_length d e n change he al e f he l ength field in state.
The f nc i n ake he f ll ing a g men , in hi de : state: a in e a alid G ameState c direction: he di ec i n ha he nake head i m ing. M be ne f U , D , L R .
Re n in $ 0: state.length if a ne ail egmen a cce f ll added b a dd_tail_segment, -1 if n .
If d irection i in alid, he f nc i n m n make an change mem and e n -1.
Addi i nal e i emen :
The f nc i n m n i e an change he main mem i elf. An change i en mem m be made ia a dd_tail_segment.
The f nc i n m call f ind_next_body_part and a dd_tail_segment .
E am le #1: The nake head i m ing he lef (d irection = L ) , increase_snake_length call a dd_tail_segment a ending a egmen he righ end f
he c en ail. a dd_tail_segment e n 8, indica ing cce .
The game a e ha been ini iali ed b l ading i i h g ame01.txt:
5
12
…………
.a.#.1..#…
…#.2..#…
…#.3..#…
…..4567…
direction = L Re n al e in $ 0: 8 U da ed game a e:
…………
.a.#.1..#…
…#.2..#…
…#.3..#…
…..45678..
E am le #2: The nake head i m ing he (d irection = U ) , increase_snake_length call a dd_tail_segment a ending a egmen b elo he c en ail. a dd_tail_segment e n -1 beca e a b d egmen i in he a . S ,
increase_snake_length call a dd_tail_segment a ending a egmen he r igh f he c en ail. a dd_tail_segment nce again e n -1 beca e ha l i f b nd .
increase_snake_length c n in e b calling a dd_tail_segment a ending a egmen
| he a bo e he end f he c en ail. a dd_tail_segment e n 15, indica ing
The game a e ha been ini iali ed b l ading i i h g ame04.txt: |
cce | . |
8
14
…………..
……##……
………….. ..#……..#..
a.#..1234..#.. ……..56…E
……##.7..CD
………89AB.
direction = U Re n al e in $ 0: 15 U da ed game a e:
…………..
……##……
………….. ..#……..#..
a.#..1234..#.F ……..56…E
……##.7..CD ………89AB.
E am le #3: The nake head i m ing d n (d irection = D ) , i ncrease_snake_length call a dd_tail_segment a ending a egmen a bo e he c en ail. a dd_tail_segment
| a ending a egmen l ef f he c en ail. add_tail_segment e
The game a e ha been ini iali ed b l ading i i h g ame05.txt: |
n 14, indica ing | cce | . |
e n -1 beca e an a le i in he a . S , i ncrease_snake_length call a dd_tail_segment
8 14
…………..
……##……
…………..
..#……..#..
..#..1234..#a.
……..56..D.
……##.7..C.
………89AB. direction = D Re n al e in $ 0: 14 U da ed game a e:
…………..
……##……
…………..
..#……..#..
..#..1234..#a.
……..56.ED.
……##.7..C.
………89AB.
E am le #4: The nake head i m ing (d irection = U ) , i ncrease_snake_length call a dd_tail_segment a ending a egmen bel he c en ail. a dd_tail_segment e n -1, indica ing fail e. Ne , increase_snake_length ill a dd_tail_segment h ee m e
ime , a em ing a end ail egmen he igh , ab e and he lef f he c en ail egmen . In each ca e, a dd_tail_segment e n -1, indica ing fail e. Beca e in e e ca e add_tail_segment e ned -1, i ncrease_snake_length e n -1.
The game a e ha been ini iali ed b l ading i i h g ame06.txt:
5
5
….a ..1..
.#2#.
.#3#.
.###.
direction = U
Re n al e in $ 0: -1
The g id af e call i ncrease_snake_length emain nchanged.
Part 9: Move the Snake Forward One Slot
int, int move_snake(GameState* state, char direction, byte[] apples, int apples_length)
| The f nc i n m ove_snake i a -le el f nc i n f he game ha m | e he nake h gh he game |
| g id ne l a a ime. Gi en he al e f direction, i make an a an a em m e he nake f a d ne l in he g id. | ia e call s lide_body in |
If he e n al e f s lide_body i -1, hen m ove_snake e n (0, -1) indica e,
e ec i el , ha n in e e c ed d ing hi call m ove_snake and ha l ima el he m emen a n cce f l.
On he he hand, if s lide_body e ned 1, hi mean he nake a e an a le. In e n e, move_snake call i ncrease_snake_length a em inc ea e he leng h f he nake,
ing he di ec i n f he head m emen a he al e f d irection. The call
increase_snake_length e n ei he -1, indica ing fail e inc ea e he leng h f he b d , a al e he han -1 indica e cce . If i ncrease_snake_length failed, hen move_snake e n (0, -1). O he i e, i e n (100, 1) indica ing ha 100 in e e
c ed.
The la ibili f s lide_body i ha i e ned 0, indica ing ha he nake head (and
| b d ) m ed f a d cce f ll and he head m ed in an em l . In hi ca e, | |
| move_snake e n (0, 1) indica e ha n in e e c ed, b ha he nake
cce f ll m ed f a d.
N e ha if d irection i in alid, m ove_snake e n (0, -1).
The f nc i n ake he f ll ing a g men , in hi de : state: a in e a alid G ameState c |
|
| direction: he di ec i n ha he nake head i m ing. M be ne f U , D , L apples: an a a f a pples_length ai f ( igned) in ege apples_length: he n mbe f ai f in ege in a pples
Re n in $ 0: The n mbe f in c ed d ing hi f nc i n call. M be 0 100.
Re n in $ 1: |
R . |
| 1 if i ncrease_snake_length cce f ll added a ne b d egmen he ail f he nake he nake im l m ed f a d in an em l ;
-1 if i ncrease_snake_length d e n add a egmen if d irection i in alid.
Addi i nal e i emen : The f nc i n m n i e an change he main mem i elf. An change i en mem m be made ia s lide_body and i ncrease_snake_length. The f nc i n m call s lide_body and increase_snake_length.
E am le #1:
S e ha e ini iali ed a e b l ading i i h g ame03.txt: |
|
8
14
…………..
……##……
…………..
..#..a…..#..
..#..1234..#..
……..56…E
……##.7..CD ………89AB.
direction = U apples = [1, 2, 2, 9, 0, 5, 1, 7, 6, 10, 3, 10, 3, 11, 2, 10, 2, 1, 2, 4,
2, 5, 1, 13] apples_length = 12 Re n al e in $ 0: 100
Re n al e in $ 1: 1
U da ed a pples: [-1, -1, 2, 9, 0, 5, 1, 7, 6, 10, 3, 10, 3, 11, 2, 10, 2, 1,
2, 4, 2, 5, 1, 13] U da ed game a e:
…………..
..a…##……
…………..
..#..1…..#..
..#..2345..#..
……..67….
……##.8..DE
………9ABCF
E am le #2:
S e ha e ini iali ed a e b l ading i i h g ame03.txt:
8
14
…………..
……##……
…………..
..#..a…..#..
..#..1234..#..
……..56…E
……##.7..CD ………89AB.
direction = D apples = [1, 2, 2, 9, 0, 5, 1, 7, 6, 10, 3, 10, 3, 11, 2, 10, 2, 1, 2, 4,
2, 5, 1, 13] apples_length = 12 Re n al e in $ 0: 0 Re n al e in $ 1: 1 apples emain nchanged. U da ed game a e: …………..
……##……
…………..
..#..a…..#..
..#..2345..#..
…..1..67….
……##.8..DE ………9ABC.
E am le #3:
S e ha e ini iali ed a e b l ading i i h g ame07.txt:
5
12
…………
.a.#….#…
…#12..#…
…#.3..#…
…..4567…
direction = L apples = [4, 4, 2, 7, 3, 5, 1, 8, 1, 7, 3, 11, 1, 11, 0, 4] apples_length = 8 Re n al e in $ 0: 0 Re n al e in $ 1: -1 apples emain nchanged.
Game a e emain nchanged.
E am le #4:
S e ha e ini iali ed a e b l ading i i h g ame07.txt:
5
12
…………
.a.#….#…
…#12..#…
…#.3..#…
…..4567…
direction = Z apples = [4, 4, 2, 7, 3, 5, 1, 8, 1, 7, 3, 11, 1, 11, 0, 4] apples_length = 8 Re n al e in $ 0: 0 Re n al e in $ 1: -1 apples emain nchanged.
Game a e emain nchanged.
Part 10: Simulate a Game of Snake
int, int simulate_game(GameState* state, string filename, string directions, int num_moves_to_execute, byte[] apples, int apples_length)
The f nc i n s imulate_game im la e a m n um_moves_to_execute m e f a game f Snake. The ini ial c nfig a i n f he game g id i gi en b he c n en f he file i h name filename. The n ll- e mina ed ing d irections c n i f he e ca e le e L , R , U and
D in an de and c mbina i n and e e en he a em ed m e f he la e . The a a apples c n ain he i i n f en ial a le ha can be laced d ing he im la i n. The im la i n alg i hm ha m im lemen i a f ll :
- Call l oad_game a em ini iali e he nini iali ed s tate c i h he c n en f filename.
- If l oad_game fail ini iali e he c , s iulate_game e n -1, -1 and make n change main mem . O he i e, e c n in e n he ne e .
- If l oad_game did n find an a le hile ini iali ing he s tate c , call place_next_apple i h a ia e a g men .
- Ini ial a a iable e he al c e
- While n um_moves_to_execute i n e 0 a nd he nake leng h i ill le han 35 a nd he la e ha n l (e.g., b nning in a all in e ec ing he nake i elf) a nd he e a e
ill di ec i n lef c n ide :
- Ge he ne di ec i n m e f m d irections.
- Call m ove_snake i h a ia e a g men .
- If m ove_snake e ned a s core f 100, hen add s core * (state.length – 1) he al c e.
- On he he hand, if m ove_snake e ned a e l f -1, indica ing fail e m e he nake, immedia el b eak f he l .
- Dec emen n um_moves_to_execute b 1.
- Re n he n mbe f e ec ed m e in $ 0 and he al c e in $ 1.
The f nc i n ake he f ll ing a g men , in hi de :
state: a p oin er (i.e., a ing mem add e f) an nini iali ed G ameState c la ge en gh h ld he c n en f he game e e en ed b file be l aded. A me ha he c n en f he c a e filled i h and m ga bage.
filename: a ing c n aining he filename en and ead he c n en f
directions: a n ll- e mina ed ing f cha ac e e e en ing di ec i n m e he nake. Each cha ac e i g a an eed be ne f U , D , L R . Y d n need alida e he c n en f d irections. num_moves_to_execute: he ma im m n mbe f cha ac e ead f m d irections apples: an a a f a pples_length ai f 8-bi , c m lemen in ege A ailable a
| 0($ ). | ||
| apples_length: he n mbe f ai f in ege in a pples. A ailable a 4($
Re n in $ 0: The n mbe f m e e ec ed.
Re n in $ 1: The al c e a he end f he im la ed game.
Addi i nal e i emen : The f nc i n m n i e an change main mem . The f nc i n m call l oad_game, place_next_apple and m ove_snake
E am le :
|
). | |
| D e he c m le i f he f nc i n a g men , e am le f s imulate_game a e | ided in | |
| simulate_game_test1.txt, s imulate_game_test2.txt, e c., al ng i h f nc i n a g men
Academic Honesty Policy
Academic h ne i aken e e i l in hi c e. B bmi ing k f g ading indica e nde anding f, and ag eemen i h, he f ll ing Academic H ne S a emen : |
. | |
- I nde and ha e e en ing an he e n k a m n i academicall di h ne .
- I nde and ha c ing, e en i h m difica i n , a l i n f m an he ce ( ch a he eb an he e n) a a a f m an e c n i e lagia i m.
- I nde and ha ha ing a f m h me k l i n ( e i e- , chema ic , c de, elec nic ha d-c ) i academic di h ne and hel he lagia i e m k.
- I nde and ha ec ing m k f m ible lagia i m i m e n ibili . I nde and he im ance f a ing m k ch ha i i i ible nl
- I nde and ha a ing inf ma i n ha i ele an a h me k/e am he in he c e (ei he lec e e en in he f e!) f hei i a e e c n i e academic di h ne . I ill nl di c ma e ial ha I am illing enl n he di c i n b a d.
- I nde and ha academic di h ne i ea ed e e i l in hi c e. I nde and ha he in c ill e an inciden f academic di h ne he Uni e i Academic J dicia .
- I nde and ha he enal f academic di h ne migh n be immedia el admini e ed. F in ance, chea ing n a h me k a ignmen ma be di c e ed and enali ed af e he g ade f ha h me k ha e been ec ded.
- I nde and ha b ing a ing an he en i f an c de, a ial in i en i e , and bmi ing i a m n k i c n ide ed academic di h ne .
- I nde and ha he e a e n e en a ing ci c m ance f academic di h ne .
How to Submit Your Work for Grading
T bmi h k3.a m file f g ading:
- G he c e eb i e.
- Click he S bmi link f hi a ignmen .
- T e SBU ID# n he line
- P e he b n ma ked A dd file and f ll
- Hi S bmi bmi file g ading.
he di ec i n a ach file.
Oops, I messed up and I need to resubmit a file!
N ie ! J f ll he e again. We ill g ade nl la bmi i n.






