maze game project

fix the code this code runs a maze game on mips assembly language and the code runs on MARS program. the problem in the game is when a player plays maze game * goes to the number and when it ask you for the sum of the number is going to and you enter the sum it tells you its invalid input. even though you put the right answer. SEE THE PICTURE AND FIX THE CODE DONT DO NEW CODE FIX THAT ONE.

.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”

prompt_sum_part1: .asciiz “Enter the sum of the number “

prompt_sum_part2: .asciiz ” in the maze: “

invalid_input: .asciiz “Invalid input! Please enter an integer.n”

maze_width: .word 10

maze_height: .word 10

player_x: .word 1

player_y: .word 1

current_sum: .word 0

correct_answers: .word 0

start_time: .word 0

end_time: .word 0

input_buffer: .space 8 # Buffer for reading input as a string

.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

# Prompt for sum

move $t2, $t6 # Save the number to $t2 for sum check

jal prompt_for_sum

# 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

prompt_for_sum:

# Display the current number

la $a0, prompt_sum_part1

li $v0, 4

syscall

# Display the number to sum

move $a1, $t2 # Move the number to $a1 for display

addi $a0, $a1, ‘0’ # Convert number to ASCII

li $v0, 11 # Print character

syscall

# Display the rest of the prompt

la $a0, prompt_sum_part2

li $v0, 4

syscall

# Read user input as a string

li $v0, 8 # Read string

la $a0, input_buffer

li $a1, 8 # Buffer size

syscall

# Ensure input is null-terminated

la $t0, input_buffer

li $t1, 0

sb $t1, 7($t0) # Null-terminate the input

# Print the input string for debugging

la $a0, input_buffer

li $v0, 4

syscall

# Convert string to integer

la $a0, input_buffer

jal str_to_int

# Check if the conversion was successful

bltz $v0, invalid # If conversion failed, go to invalid

# Calculate expected sum

lw $t1, current_sum

add $t1, $t1, $t2 # $t2 contains the number from the maze

# Check if the sum is correct

beq $v0, $t1, correct_sum

j invalid

correct_sum:

# If the sum is correct, return

jr $ra

str_to_int:

# Convert a string in $a0 to an integer in $v0

li $v0, 0

li $t0, 0

str_to_int_loop:

lb $t1, 0($a0) # Load byte from string

beqz $t1, str_to_int_end # If null terminator, end

sub $t1, $t1, ‘0’ # Convert char to integer

blt $t1, 0, str_to_int_skip # If char < ‘0’, skip

bgt $t1, 9, str_to_int_skip # If char > ‘9’, skip

mul $v0, $v0, 10 # Multiply current result by 10

add $v0, $v0, $t1 # Add the digit

str_to_int_skip:

addi $a0, $a0, 1 # Move to the next char

j str_to_int_loop

str_to_int_end:

jr $ra

str_to_int_error:

la $a0, invalid_input

li $v0, 4

syscall

li $v0, -1

jr $ra