Basic allmost functional Mastermind game
[TD_IML.git] / project / mips1.asm
index 9294487f6353e091a7396ed3f981f0a0a024cec0..690aca808a2abc420b5a7b08dca485f4d14368b0 100644 (file)
-    .data
+.data
 
-Welcome1:
-    .ascii  "\n Hello! you are about to play the mastermind"
-    .ascii  " guessing and logic game,Bulls & Cows!\n"
-    .ascii  "The Computer will generate a secret number made of 4 unique"
-    .ascii  " integer numbers. You have to guess the number!\n"
-    .ascii  "Using the number of Bulls and Cows you get to find out what"
-    .asciiz " the secret number is!\n"
+welcome:              .asciiz "Bienvenue dans le jeu de Mastermind.\nUne sequence de quatre chiffres de 1 à 8 a ete générée.\nA vous de la deviner !"
+play:                .asciiz "Commencer à jouer:"
+lost_msg:             .asciiz "Vous avez perdu !" 
+win_msg:              .asciiz "Vous avez gagné !"
+temp_msg:             .asciiz " bien placé" 
+exclamation_point:    .asciiz "! "  
+interrogation_point:  .asciiz "? "
+newline:             .asciiz "\n"
 
-Welcome2:
-    .ascii  "\nEvery digit you enter that is both correct and in the right"
-    .ascii  " location is a BULL. When you get 4 BULLS, YOU WIN!\n\n"
-    .ascii  "Every digit you enter that is correct, but not in the right"
-    .asciiz " location is a COW!\n"
+O:                    .ascii  "O"
+X:                   .ascii  "X"
+tiret:               .ascii  "-"
 
-confirm:
-    .ascii  "\n Select \n"
-    .ascii  "YES - if you are ready to guess\n"
-    .ascii  "NO - to see the rules again\n"
-    .asciiz "Cancel - to exit the Game\n"
+compare_buffer:       .space  5
+input_value:         .byte   0:5
+guess_value:         .byte   0:5
 
-msg_asknum:
-    .asciiz "\nEnter your game choice\n"
-
-    .text
+.text
 
 main:
 
-    # show intro and rules
-main_welcome:
-    la      $a0,Welcome1
-    li      $a1,1
-    li      $v0,55
+# show intro and rules
+display_welcome:
+    la      $a0, newline
+    li      $v0, 4
     syscall
-
-    la      $a0,Welcome2
-    li      $v0,55
+    la      $a0, welcome
+    li      $v0, 4
     syscall
-
-    # ask user to select an action (i.e. enter data, reread rules, exit program)
-main_confirm:
-    la      $a0,confirm
-    li      $v0,50
+    la      $a0, newline
+    li      $v0, 4
     syscall
-
-    # the return is in a0: 0=yes, 1=no, 2=cancel
-    beq     $a0,0,main_asknum
-    beq     $a0,2,main_exit
-    b       main_welcome
-
-    # ask user for the next game input
-    # NOTE: this is a prompt for an integer number
-main_asknum:
-    li      $v0,51
-    la      $a0,msg_asknum
+    la      $a0, play
+    li      $v0, 4
     syscall
-
-    beq     $a1,-1,main_asknum      # syntax error
-    beq     $a1,-2,main_confirm     # cancel
-    beq     $a1,-3,main_asknum      # ok, but no data
-
-    jal     do_something            # do something with the number in a0 ...
-
-    j       main_asknum
-
-main_exit:
-    li      $v0,10
+    la      $a0, newline
+    li      $v0, 4
     syscall
-
-# do_something -- process user's input
-#
-# arguments:
-#   a0 -- number the user entered
-do_something:
-    jr      $ra                     # return
\ No newline at end of file
+   
+generate_sequence:
+    la      $t0, guess_value
+        
+    li             $a1, 8
+    li      $v0, 42
+    syscall
+    addi    $a0, $a0, 1             # Put the integer into the [1-8] range
+    
+    sb      $a0, 0($t0)
+    
+    li             $a1, 8
+    li      $v0, 42
+    syscall
+    addi    $a0, $a0, 1             # Put the integer into the [1-8] range
+        
+    sb      $a0, 1($t0)
+   
+    li             $a1, 8
+    li      $v0, 42
+    syscall
+    addi    $a0, $a0, 1             # Put the integer into the [1-8] range
+        
+    sb      $a0, 2($t0)
+    
+    li             $a1, 8
+    li      $v0, 42
+    syscall
+    addi    $a0, $a0, 1             # Put the integer into the [1-8] range
+        
+    sb      $a0, 3($t0)
+              
+generate_sequence_loop_end:
+
+    la      $s0, ($zero)            # Initialize loop counter to zero
+    la      $s1, compare_buffer
+loop:
+    beq     $s0, 10, lost
+# ask user for the next input
+input:
+    la      $a0, newline
+    li      $v0, 4
+    syscall
+    
+    la      $a0, ($s0)
+    li      $v0, 1
+    syscall
+    la      $a0, interrogation_point
+    li      $v0, 4
+    syscall
+    
+    la             $a0, input_value        # Store the answer
+    li             $a1, 5
+    li     $v0, 8                  # Load syscall read_string
+    syscall                        # Make the syscall  
+
+# check that each input values are in the right range 
+check_input:
+    la             $t7, input_value
+    
+    lb             $t5, 0($t7)
+    andi    $t5, $t5, 0x0F          # ASCII code to integer
+    beq     $t5, $zero, input
+    addi    $t5, $t5, -9
+    bgez    $t5, input
+    
+    lb             $t5, 1($t7)
+    andi    $t5, $t5, 0x0F          # ASCII code to integer
+    beq     $t5, $zero, input
+    addi    $t5, $t5, -9
+    bgez    $t5, input
+       
+    lb             $t5, 2($t7)
+    andi    $t5, $t5, 0x0F          # ASCII code to integer
+    beq     $t5, $zero, input
+    addi    $t5, $t5, -9
+    bgez    $t5, input 
+    
+    lb             $t5, 3($t7)
+    andi    $t5, $t5, 0x0F          # ASCII code to integer
+    beq     $t5, $zero, input
+    addi    $t5, $t5, -9
+    bgez    $t5, input    
+       
+   la      $s2, ($zero)            # Same counter
+   la      $s3  ($zero) 
+compare_input:
+    la     $t7, input_value
+    la      $t6, guess_value
+    la      $t5, O
+    la      $t4, X
+    la      $t3, tiret   
+    
+    lb      $t0, 0($t7)
+    andi    $t0, $t0, 0x0F          # ASCII code to integer
+    lb      $t1, 0($t6)
+    beq            $t0, $t1, same1
+    j      different1
+same1:
+    sb      $0, 0($t7)
+    sb      $t5, 0($s1)
+    addi    $s2, $s2, 1    
+different1:    
+    lb      $t0, 1($t7)
+    andi    $t0, $t0, 0x0F          # ASCII code to integer
+    lb      $t1, 1($t6)
+    beq            $t0, $t1, same2
+    j      different2
+same2:
+    sb      $zero, 1($t7)
+    sb      $t5, 1($s1)
+    addi    $s2, $s2, 1
+different2:
+    lb      $t0, 2($t7)
+    andi    $t0, $t0, 0x0F          # ASCII code to integer
+    lb      $t1, 2($t6)
+    beq            $t0, $t1, same3
+    j       different3
+same3:
+    sb      $zero, 2($t7)
+    sb      $t5, 2($s1)
+    addi    $s2, $s2, 1
+different3:         
+    lb      $t0, 3($t7)
+    andi    $t0, $t0, 0x0F          # ASCII code to integer
+    lb      $t1, 3($t6)
+    beq            $t0, $t1, same4
+    j       different4 
+same4: 
+    sb      $zero, 3($t7)
+    sb      $t5, 3($s1)
+    addi    $s2, $s2, 1
+different4:
+    beq     $s2, 4, won   
+    
+   
+print_comp_result:       
+    la      $a0, newline
+    li      $v0, 4
+    syscall
+    la      $a0, ($s0)
+    li      $v0, 1
+    syscall
+    la      $a0, exclamation_point
+    li      $v0, 4
+    syscall
+    la      $a0, ($s2)
+    li      $v0, 1
+    syscall
+    la      $a0, temp_msg
+    li      $v0, 4
+    syscall
+    
+    addi    $s0, $s0, 1
+    j       loop                    
+                                                                                                                                              
+won:
+    la      $a0, newline
+    li      $v0, 4
+    syscall
+    la      $a0, win_msg
+    li      $v0, 4
+    syscall   
+    
+    j       exit
+lost:
+    la      $a0, newline
+    li      $v0, 4
+    syscall                                                
+    la      $a0, lost_msg
+    li      $v0, 4
+    syscall 
+      
+    j       exit
+exit:
+    li      $v0, 10
+    syscall
\ No newline at end of file