Programming Exercise II: Playing a Snakes Game


Absolutely no copying others’ works
(Unlike other languages, using others’ assembly code is obvious.)

Development Requirements
When start developing the exercise, follow the two requirements below:

Due Date and Submission Method
On or before Thursday, April 02, 2026 and upload the source code (no documentation needed) to the section of “COVID-19 Exams, Homeworks, & Programming Exercises” of Blackboard.



Objective
Design and implement a MIPS assembly program, which has the user play a 6×6 snakes game against the computer. The final program is less than 400 lines without counting the declarations of data structures. The purpose of this exercise is to make students practice assembly language programming including various control instructions, functions, and the runtime stack.


The first player, Blue (or top), can not move, and so Red (or bottom) wins.



Rules of a Snakes Game
It is a board game for two players who take turns in drawing segments of a snake. The last player able to move wins. The game is played on a grid; 6×6 is a good size. The above game is played on a matrix of 5×5 dots. The first player, Blue (top), starts in the second row and column, and the other player, Red (bottom), starts in the last but one row and column, as shown by the blue and red squares. The players take turns in growing a snake, extending it a segment at a time by drawing a horizontal or vertical line from the previous dot to an adjacent dot. Each player must avoid touching either his/her opponent’s snake or his/her own snake. The first player unable to move loses.



Requirements
The 6×6 snakes game includes the following requirements:

Programming Hints
The four essential components of software are (i) algorithms, (ii) data structures, (iii) programming languages, and (iv) code, where algorithms are the most critical one. In addition, using appropriate data structures could save a great deal of coding work, especially for assembly coding. The following hints are from the instructor and you do not necessarily have to use them:

Examples
The following list shows some execution examples, where the white, italic text with a black background color is entered by users:

Examples of Exercise II Execution

Start Playing a Snakes Game.

    . . . . . .      0 1 2 3 4 5
    . . . . . .      6 7 8 9 a b
    . . . . . .      c d e f g h
    . . . . . .      i j k l m n
    . . . . . .      o p q r s t
    . . . . . .      u v w x y z

You are first. Pick a piece: (X|O)  X 
Enter your move: (0..z)  0 

    X . . . . .      0 1 2 3 4 5
    . . . . . .      6 7 8 9 a b
    . . . . . .      c d e f g h
    . . . . . .      i j k l m n
    . . . . . .      o p q r s t
    . . . . . .      u v w x y z

Continue? (y/n)  y 
Hit the spacebar to start the System’s next move.

    X O . . . .      0 1 2 3 4 5
    . . . . . .      6 7 8 9 a b
    . . . . . .      c d e f g h
    . . . . . .      i j k l m n
    . . . . . .      o p q r s t
    . . . . . .      u v w x y z

Continue? (y/n)  y 
Enter your move: (0..z)  6 

    $ O . . . .      0 1 2 3 4 5
    X . . . . .      6 7 8 9 a b
    . . . . . .      c d e f g h
    . . . . . .      i j k l m n
    . . . . . .      o p q r s t
    . . . . . .      u v w x y z

Continue? (y/n)  y 
Hit the spacebar to start the System’s next move.

    $ + . . . .      0 1 2 3 4 5
    X O . . . .      6 7 8 9 a b
    . . . . . .      c d e f g h
    . . . . . .      i j k l m n
    . . . . . .      o p q r s t
    . . . . . .      u v w x y z

Continue? (y/n)  y 
Enter your move: (0..z)  c 

    $ + . . . .      0 1 2 3 4 5
    $ O . . . .      6 7 8 9 a b
    X . . . . .      c d e f g h
    . . . . . .      i j k l m n
    . . . . . .      o p q r s t
    . . . . . .      u v w x y z

Continue? (y/n)  y 
Hit the spacebar to start the System’s next move.

    $ + . . . .      0 1 2 3 4 5
    $ + . . . .      6 7 8 9 a b
    X O . . . .      c d e f g h
    . . . . . .      i j k l m n
    . . . . . .      o p q r s t
    . . . . . .      u v w x y z

Continue? (y/n)  y 
Enter your move: (0..z)  i 

    $ + . . . .      0 1 2 3 4 5
    $ + . . . .      6 7 8 9 a b
    $ O . . . .      c d e f g h
    X . . . . .      i j k l m n
    . . . . . .      o p q r s t
    . . . . . .      u v w x y z

Continue? (y/n)  y 
Hit the spacebar to start the System’s next move.

    $ + . . . .      0 1 2 3 4 5
    $ + . . . .      6 7 8 9 a b
    $ + . . . .      c d e f g h
    X O . . . .      i j k l m n
    . . . . . .      o p q r s t
    . . . . . .      u v w x y z

Continue? (y/n)  y 
Enter your move: (0..z)  o 

    $ + . . . .      0 1 2 3 4 5
    $ + . . . .      6 7 8 9 a b
    $ + . . . .      c d e f g h
    $ O . . . .      i j k l m n
    X . . . . .      o p q r s t
    . . . . . .      u v w x y z

Continue? (y/n)  y 
Hit the spacebar to start the System’s next move.

    $ + . . . .      0 1 2 3 4 5
    $ + . . . .      6 7 8 9 a b
    $ + . . . .      c d e f g h
    $ + . . . .      i j k l m n
    X O . . . .      o p q r s t
    . . . . . .      u v w x y z

Continue? (y/n)  y 
Enter your move: (0..z)  u 

    $ + . . . .      0 1 2 3 4 5
    $ + . . . .      6 7 8 9 a b
    $ + . . . .      c d e f g h
    $ + . . . .      i j k l m n
    $ O . . . .      o p q r s t
    X . . . . .      u v w x y z

Continue? (y/n)  y 
Hit the spacebar to start the System’s next move.

    $ + . . . .      0 1 2 3 4 5
    $ + . . . .      6 7 8 9 a b
    $ + . . . .      c d e f g h
    $ + . . . .      i j k l m n
    $ + . . . .      o p q r s t
    X O . . . .      u v w x y z

******* The System (O) wins! *******

New game? (y/n)  y 

Start Playing a Snakes Game.

    . . . . . .      0 1 2 3 4 5
    . . . . . .      6 7 8 9 a b
    . . . . . .      c d e f g h
    . . . . . .      i j k l m n
    . . . . . .      o p q r s t
    . . . . . .      u v w x y z

I (the System) am first and I pick X.
Hit the spacebar to start the System’s next move.

    . . . . . .      0 1 2 3 4 5
    . . . . X .      6 7 8 9 a b
    . . . . . .      c d e f g h
    . . . . . .      i j k l m n
    . . . . . .      o p q r s t
    . . . . . .      u v w x y z

Continue? (y/n)  y 
Enter your move: (0..z)  p 

    . . . . . .      0 1 2 3 4 5
    . . . O X .      6 7 8 9 a b
    . . . . . .      c d e f g h
    . . . . . .      i j k l m n
    . . . . . .      o p q r s t
    . . . . . .      u v w x y z

Continue? (y/n)  y 
Hit the spacebar to start the System’s next move.

    . . . . X .      0 1 2 3 4 5
    . . . O $ .      6 7 8 9 a b
    . . . . . .      c d e f g h
    . . . . . .      i j k l m n
    . . . . . .      o p q r s t
    . . . . . .      u v w x y z

Continue? (y/n)  y 
Enter your move: (0..z)  3 

    . . . O X .      0 1 2 3 4 5
    . . . + $ .      6 7 8 9 a b
    . . . . . .      c d e f g h
    . . . . . .      i j k l m n
    . . . . . .      o p q r s t
    . . . . . .      u v w x y z

Continue? (y/n)  y 
Hit the spacebar to start the System’s next move.

    . . . O $ X      0 1 2 3 4 5
    . . . + $ .      6 7 8 9 a b
    . . . . . .      c d e f g h
    . . . . . .      i j k l m n
    . . . . . .      o p q r s t
    . . . . . .      u v w x y z

Continue? (y/n)  n 
New game? (y/n)  y 

Start Playing a Snakes Game.

    . . . . . .      0 1 2 3 4 5
    . . . . . .      6 7 8 9 a b
    . . . . . .      c d e f g h
    . . . . . .      i j k l m n
    . . . . . .      o p q r s t
    . . . . . .      u v w x y z

You are first. Pick a piece: (X|O)  O 
Enter your move: (0..z)  s 

    . . . . . .      0 1 2 3 4 5
    . . . . . .      6 7 8 9 a b
    . . . . . .      c d e f g h
    . . . . . .      i j k l m n
    . . . . O .      o p q r s t
    . . . . . .      u v w x y z

Continue? (y/n)  y 
Hit the spacebar to start the System’s next move.

    . . . . . .      0 1 2 3 4 5
    . . . . . .      6 7 8 9 a b
    . . . . . .      c d e f g h
    . . . . X .      i j k l m n
    . . . . O .      o p q r s t
    . . . . . .      u v w x y z

Continue? (y/n)  y 
Enter your move: (0..z)  6 

    . . . . . .      0 1 2 3 4 5
    . . . . . .      6 7 8 9 a b
    . . . . . .      c d e f g h
    . . . . X .      i j k l m n
    . . . . + .      o p q r s t
    . . . . O .      u v w x y z

Continue? (y/n)  y 
Hit the spacebar to start the System’s next move.

    . . . . . .      0 1 2 3 4 5
    . . . . . .      6 7 8 9 a b
    . . . . X .      c d e f g h
    . . . . $ .      i j k l m n
    . . . . + .      o p q r s t
    . . . . O .      u v w x y z

Continue? (y/n)  y 
Enter your move: (0..z)  z 

    . . . . . .      0 1 2 3 4 5
    . . . . . .      6 7 8 9 a b
    . . . . X .      c d e f g h
    . . . . $ .      i j k l m n
    . . . . + .      o p q r s t
    . . . . + O      u v w x y z

Continue? (y/n)  y 
Hit the spacebar to start the System’s next move.

    . . . . . .      0 1 2 3 4 5
    . . . . . .      6 7 8 9 a b
    . . . . $ X      c d e f g h
    . . . . $ .      i j k l m n
    . . . . + .      o p q r s t
    . . . . + O      u v w x y z

Continue? (y/n)  y 
Enter your move: (0..z)  t 

    . . . . . .      0 1 2 3 4 5
    . . . . . .      6 7 8 9 a b
    . . . . $ X      c d e f g h
    . . . . $ .      i j k l m n
    . . . . + O      o p q r s t
    . . . . + +      u v w x y z

Continue? (y/n)  y 
Hit the spacebar to start the System’s next move.

    . . . . . .      0 1 2 3 4 5
    . . . . . .      6 7 8 9 a b
    . . . . $ $      c d e f g h
    . . . . $ X      i j k l m n
    . . . . + O      o p q r s t
    . . . . + +      u v w x y z

******* The System (X) wins! *******

New game? (y/n)  n 
-- program is finished running --



Possible Instructions to Be Used
The following directives and instructions may be used in this exercise, but you are not limited to them. For instruction syntax, check
MIPS Instruction Reference.

No. Directive Description
1 .ascii "string" Allocating space for string
2 .asciiz "string" Allocating space for string, NULL terminated
3 .byte Allocating space for a byte
4 .data Beginning of data section
5 .globl name Making the following name be a global symbol
6 .half Allocating space for a half word (two bytes)
7 .space n Allocating n bytes of space
8 .text Beginning of text section
No. Instruction Operation Description
1 add rd, rs, rt rd = rs + rt Add;
rd: destination register, rs: first source register, and rt: second source register or immediate value.
Check MIPS Registers and Usage Convention.
2 addu rd, rs, rt rd = rs + rt (no overflow) Add unsigned
3 beq rs, rt, label if rs==rt then goto label Branch if equal to
4 beqz rs label if rs==0 then goto label Branch if equal to zero
5 bgt rs, rt, label if rs>rt then goto label Branch if greater than
6 ble rs, rt, label if rs≤rt then goto label Branch if less than or equal to
7 blt rs, rt, label if rs<rt then goto label Branch if less than
8 bltz rs, label if rs<0 then goto label Branch if less than zero
9 bne rs, rt, label if rs≠rt then goto label Branch if not equal to
10 j label jump to label Jump
11 jal label jump to label and save the return address in $31 or $ra Jump and link
12 jr rs jump to [rs]; [ ]: contents of Jump and link
13 la rd, mem rd = address( mem ) Load address
14 lb rd, mem rd = mem Load byte
15 lh rd, mem rd = mem Load half word
16 li rd, imm rd = imm Load immediate
17 lw rd, mem rd = mem Load word
18 move rd, rs rd = rs Move register
19 mul rd, rs, rt rd = rs × rt Multiply
20 neg rd, rs rd = -rs Negate
21 sb rs, mem mem = rs Store byte
22 sub rd, rs, rt rd = rs - rt Subtract
23 subu rd, rs, rt rd = rs - rt (no overflow) Subtract unsigned
24 sw rs, mem mem = rs Store word
25 syscall   System call; check System Services.
26 xor rd, rs, rt rd = rs xor rt Bitwise exclusive or

The following table lists some System Services provided by the MARS:

Service Code in $v0 Arguments Result
print_int 1 $a0 = integer to be printed  
print_float 2 $f12 = float to be printed  
print_double 3 $f12 = double to be printed  
print_string 4 $a0 = address of string in memory  
read_int 5   integer returned in $v0
read_float 6   float returned in $v0
read_double 7   double returned in $v0
read_string 8 $a0 = address of string input buffer
$a1 = length of string buffer (n)
 
malloc 9 $a0 = amount address in $v0
exit 10    
print character 11 $a0 = character to be printed  
read character 12   character returned in $v0
random int range 42 $a0 = i.d. of pseudorandom number generator (any int).
$a1 = upper bound of range of returned values.
$a0 contains pseudorandom, uniformly distributed int value in the range 0 ≤ [int] < [upper bound], drawn from this random number generator’s sequence.



Evaluations
The following features will be considered when grading: