.data maze: .asciiz “n##########n#*1342171#n#01#####1#n#84#19224#n####1####
.data
maze: .asciiz “n##########n#*1342171#n#01#####1#n#84#19224#n####1#####n#11#12561#n#16#####1#n#64131281#n##1#######n#12647893E#n”
win_msg: .asciiz “You win!n”
prompt: .asciiz “Enter move (w/a/s/d): ”
invalid_move: .asciiz “Invalid move!n”
current_sum_msg: .asciiz “Current Sum: ”
correct_answers_msg: .asciiz “Correct Answers: ”
time_elapsed_msg: .asciiz “Time Elapsed: ”
seconds_msg: .asciiz ” secondsn”
final_sum_msg: .asciiz “Final Sum: ”
final_correct_answers_msg: .asciiz “Total Correct Answers: ”
final_time_msg: .asciiz “Total Time: ”
newline: .asciiz “n”
debug_msg: .asciiz “Debug: Reached En”
debug_position: .asciiz “Debug: Position: ”
debug_character: .asciiz ” Character: ”
debug_reached: .asciiz ” Debug: Reached ‘E’!n”
# Maze dimensions
maze_width: .word 10
maze_height: .word 10
# Player starting position
player_x: .word 1
player_y: .word 1
# Game stats
current_sum: .word 0
correct_answers: .word 0
# Timer
start_time: .word 0
end_time: .word 0
.text
main:
# Initialize player position
la $t0, player_x
la $t1, player_y
li $t2, 1
sw $t2, 0($t0)
sw $t2, 0($t1)
# Initialize game stats
la $t3, current_sum
sw $zero, 0($t3)
la $t3, correct_answers
sw $zero, 0($t3)
# Initialize start time
li $v0, 30 # System call for time
syscall
la $t3, start_time
sw $v0, 0($t3)
# Print initial maze
la $a0, maze
li $v0, 4
syscall
# Display initial stats
jal display_stats
game_loop:
# Load player position
lw $t0, player_x
lw $t1, player_y
# Calculate player position in the maze string
la $t9, maze
li $t4, 11 # Each row is 11 characters including newline
mul $t5, $t0, $t4 # Row offset
add $t5, $t5, $t1 # Column offset
add $t5, $t5, $t9 # Final address in maze string
# Restore original maze character (if not starting position)
bne $t0, 1, not_starting_pos
bne $t1, 1, not_starting_pos
j skip_restore
not_starting_pos:
li $t6, ‘ ‘ # Assuming empty space
sb $t6, 0($t5)
skip_restore:
# Prompt for move
la $a0, prompt
li $v0, 4
syscall
# Read user input
li $v0, 12
syscall
move $t3, $v0
# Validate user input
li $t7, ‘w’
li $t8, ‘a’
li $t9, ‘s’
li $t0, ‘d’
beq $t3, $t7, process_input
beq $t3, $t8, process_input
beq $t3, $t9, process_input
beq $t3, $t0, process_input
j invalid
process_input:
# Calculate new position based on input
lw $t0, player_x
lw $t1, player_y
move $t4, $t0
move $t5, $t1
beq $t3, ‘w’, move_up
beq $t3, ‘a’, move_left
beq $t3, ‘s’, move_down
beq $t3, ‘d’, move_right
move_up:
sub $t4, $t0, 1
j validate_move
move_down:
add $t4, $t0, 1
j validate_move
move_left:
sub $t5, $t1, 1
j validate_move
move_right:
add $t5, $t1, 1
j validate_move
validate_move:
# Check boundaries
lw $t6, maze_width
lw $t7, maze_height
bltz $t4, invalid
bltz $t5, invalid
bge $t4, $t7, invalid
bge $t5, $t6, invalid
# Calculate maze index
li $t8, 11 # Each row is 11 characters including newline
mul $t8, $t8, $t4 # Row offset
add $t8, $t8, $t5 # Column offset
# Check maze value at new position
la $t9, maze
add $t9, $t9, $t8
lb $t9, 0($t9)
# Debug: Print position and character
la $a0, debug_position
li $v0, 4
syscall
move $a0, $t4
li $v0, 1
syscall
la $a0, debug_character
li $v0, 4
syscall
move $a0, $t9
li $v0, 11
syscall
la $a0, newline
li $v0, 4
syscall
# Check if move is valid
beq $t9, ‘#’, invalid
# Check if player reached the end ‘E’
li $t0, ‘E’
beq $t9, $t0, win
# Check if stepping on a number
sub $t6, $t9, ‘0’ # Convert character to number
bltz $t6, skip_update
bgt $t6, 9, skip_update
# Update current sum
lw $t7, current_sum
add $t7, $t7, $t6
sw $t7, current_sum
# Increment correct answers count
lw $t7, correct_answers
addi $t7, $t7, 1
sw $t7, correct_answers
skip_update:
# Update player position
sw $t4, player_x
sw $t5, player_y
# Calculate the new position in the maze string
la $t9, maze
lw $t0, player_x
lw $t1, player_y
li $t4, 11 # Each row is 11 characters including newline
mul $t5, $t0, $t4 # Row offset
add $t5, $t5, $t1 # Column offset
add $t5, $t5, $t9 # Final address in maze string
# Update the player’s position in the maze
li $t6, ‘*’
sb $t6, 0($t5)
# Print maze
la $a0, maze
li $v0, 4
syscall
# Display current sum and correct answers
jal display_stats
j game_loop
invalid:
la $a0, invalid_move
li $v0, 4
syscall
j game_loop
win:
# Record end time
li $v0, 30 # System call for time
syscall
la $t3, end_time
sw $v0, 0($t3)
# Debug message to indicate we have reached the win condition
la $a0, debug_reached
li $v0, 4
syscall
# Display win message
la $a0, win_msg
li $v0, 4
syscall
# Display final stats
jal display_final_stats
# End the game by exiting the program
li $v0, 10
syscall
display_stats:
# Display current sum
la $a0, current_sum_msg
li $v0, 4
syscall
lw $a0, current_sum
li $v0, 1
syscall
# Display correct answers count
la $a0, correct_answers_msg
li $v0, 4
syscall
lw $a0, correct_answers
li $v0, 1
syscall
# Display time elapsed
li $v0, 30 # System call for time
syscall
la $t4, start_time
lw $t4, 0($t4)
sub $t6, $v0, $t4
la $a0, time_elapsed_msg
li $v0, 4
syscall
move $a0, $t6
li $v0, 1
syscall
la $a0, seconds_msg
li $v0, 4
syscall
# Print newline
la $a0, newline
li $v0, 4
syscall
jr $ra
display_final_stats:
# Display final sum
la $a0, final_sum_msg
li $v0, 4
syscall
lw $a0, current_sum
li $v0, 1
syscall
# Display final correct answers count
la $a0, final_correct_answers_msg
li $v0, 4
syscall
lw $a0, correct_answers
li $v0, 1
syscall
# Calculate and display total time
la $t4, end_time
lw $t4, 0($t4)
la $t5, start_time
lw $t5, 0($t5)
sub $t6, $t4, $t5
la $a0, final_time_msg
li $v0, 4
syscall
move $a0, $t6
li $v0, 1
syscall
la $a0, seconds_msg
li $v0, 4
syscall
# Print newline
la $a0, newline
li $v0, 4
syscall
jr $ra
create a code when you go to the direction then it will ask you to enter the sum of two numbers that * is going to
like the picture