-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwordle.asm
331 lines (297 loc) · 6.92 KB
/
wordle.asm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
.data
# Message types
debug: .word 0
welcome_message: .asciiz "Welcome to my simple Wordle game, let's play it!\n"
menu_message: .asciiz "What do you want to do?\n (1) Play\n (2) Quit\n"
line_separator: .asciiz "="
new_line: .asciiz "\n"
wrong_choice: .asciiz "\nWrong choice, please choose again.\n"
make_guess: .asciiz "\nMake your guess:"
congratulations: .asciiz "\nCongratulations! You win! The word was indeed: "
failure_message: .asciiz "\nWhoops, it seems you could not guess :(\n The word was: "
debug_mode_message: .asciiz "\nYou are in debug mode, the word system chose is:"
left_bracket: .asciiz "["
right_bracket: .asciiz "]"
left_parenthesis: .asciiz "("
right_parenthesis: .asciiz ")"
option_play: .asciiz "1"
option_quit: .asciiz "2"
seed: .word 42
# Data types
char_array: .space 5
player_input: .space 5
buffer: .space 2
# Word list
word_list:
.asciiz "apple"
.asciiz "baker"
.asciiz "champ"
.asciiz "darts"
.asciiz "elope"
.asciiz "flask"
.asciiz "glint"
.asciiz "hymns"
.asciiz "inbox"
.asciiz "jumps"
.asciiz "knots"
.asciiz "liver"
.asciiz "mirth"
.asciiz "nymph"
.asciiz "oasis"
.asciiz "pupil"
.asciiz "quiet"
.asciiz "ruler"
.asciiz "slime"
.asciiz "train"
.asciiz "unity"
.asciiz "vowel"
.asciiz "wrist"
.asciiz "xenon"
.asciiz "youth"
.asciiz "zebra"
.asciiz "amber"
.asciiz "blush"
.asciiz "chime"
.asciiz "demon"
.asciiz "ember"
.asciiz "frost"
.asciiz "gloom"
.asciiz "haste"
.asciiz "inlet"
.asciiz "joust"
.asciiz "kiosk"
.asciiz "ledge"
.asciiz "mango"
.asciiz "nexus"
.asciiz "oasis"
.asciiz "pupil"
.asciiz "quota"
.asciiz "ranch"
.asciiz "scold"
.asciiz "tweet"
.asciiz "usual"
.asciiz "venom"
.asciiz "whisk"
.asciiz "yodel"
.asciiz "grape"
.text
.globl main
main:
lw $a0, seed # Generate a random number
li $a1, 100
li $v0, 42
syscall
li $v0, 42
syscall
move $t0, $v0
mul $t0, $t0, 5
game_loop:
# Print welcome screen
jal print_welcome
# Decide to play or quit
jal decide_play_or_quit
# 5 guesses
jal choose_word
li $t2, 0
addi $t0, $t0, 5 # Find the position of the next word
guess_loop:
li $t1, 0
la $a0, make_guess
li $v0, 4
syscall
li $v0, 8
la $a0, player_input # Get input
li $a1, 6
syscall
jal check # Check if the player's guess is correct
addi $t2, $t2, 1
beq $t1, 5, success
beq $t2, 5, failure
beq $zero, 0, guess_loop
failure: # Print failure message
la $a0, failure_message
li $v0, 4
syscall
li $s0, 0
la $s1, char_array
s_loop:
lb $s2, ($s1)
move $a0, $s2
li $v0, 11
syscall
addi $s0, $s0, 1
addi $s1, $s1, 1
blt $s0, 5, s_loop
beq $zero, 0, game_loop
success: # Print success message
la $a0, congratulations
li $v0, 4
syscall
li $s0, 0
la $s1, char_array
ss_loop:
lb $s2, ($s1)
move $a0, $s2
li $v0, 11
syscall
addi $s0, $s0, 1
addi $s1, $s1, 1
blt $s0, 5, ss_loop
beq $zero, 0, game_loop
check:
addi $sp, $sp, -4
sw $ra, 0($sp)
la $a0, new_line # New line
li $v0, 4
syscall
la $s0, char_array
la $s1, player_input
li $s2, 0 # Counter for the first loop
loop1:
beq $s2, 5, end_loop1
lb $s3, ($s0)
lb $s4, ($s1)
bne $s3, $s4, check2
addi $t1, $t1, 1 # Not entering check2 means same position, same letter
la $a0, left_bracket
li $v0, 4
syscall
move $a0, $s4
li $v0, 11
syscall
la $a0, right_bracket
li $v0, 4
syscall
addi $s0, $s0, 1
addi $s1, $s1, 1
addi $s2, $s2, 1
blt $s2, 5, loop1
end_loop1:
lw $ra, 0($sp)
addi $sp, $sp, 4
jr $ra
check2:
li $t7, 0 # Check if this letter also appears in the system's chosen word
la $s6, char_array
loop2: # This loop repeats 5 times, meaning checking each letter one by one
lb $s7, ($s6)
beq $s4, $s7, right1 # This letter also appears in the system's chosen word, go to right1
addi $t7, $t7, 1
addi $s6, $s6, 1
blt $t7, 5, loop2
move $a0, $s4
li $v0, 11
syscall
addi $s0, $s0, 1
addi $s1, $s1, 1
addi $s2, $s2, 1
beq $zero, 0, loop1
right1:
la $a0, left_parenthesis
li $v0, 4
syscall
move $a0, $s4
li $v0, 11
syscall
la $a0, right_parenthesis
li $v0, 4
syscall
addi $s0, $s0, 1
addi $s1, $s1, 1
addi $s2, $s2, 1
beq $zero, 0, loop1
choose_word: # Choose a word from the word list
addi $sp, $sp, -4
sw $ra, 0($sp)
la $a1, word_list
la $a2, char_array
move $s0, $t0 # Use the number in $t0 to determine the position
addi $s1, $s0, 5
li $s3, 0
lw $s4, debug
beq $s4, 1, debug_mode
loop_for_choose_word:
lb $s2 ($a1)
addi $a1, $a1, 1 # Point to the next character
addi $s3, $s3, 1
ble $s3, $s0, loop_for_choose_word
sb $s2, ($a2)
beq $s4, 1, out
back:
addi $a2, $a2, 1
bge $s3, $s1, end_loop
beq $zero, 0, loop_for_choose_word
end_loop:
lw $ra, 0($sp)
addi $sp, $sp, 4
jr $ra
out:
move $a0, $s2
li $v0, 11
syscall
beq $zero, 0, back
debug_mode:
la $a0, debug_mode_message
li $v0, 4
syscall
beq $zero, 0, loop_for_choose_word
decide_play_or_quit: # Check if the player wants to continue playing
addi $sp, $sp, -4
sw $ra, 0($sp)
li $v0, 8
la $a0, buffer # Load the address of the buffer into $a0
li $a1, 2 # Read one character
syscall
la $s0, option_play
la $s1, option_quit
lb $a0, 0($s0) # Load the first character into $a0
lb $a1, 0($s1)
la $s3, buffer
lb $s4, 0($s3)
beq $s4, $a0, continue
beq $s4, $a1, over
la $a0, wrong_choice
li $v0, 4
syscall
beq $zero, 0, decide_play_or_quit
over: # Don't want to play
li $v0, 10
syscall
continue: # Want to play
lw $ra, 0($sp)
addi $sp, $sp, 4
jr $ra
print_welcome: # Print welcome screen
addi $sp, $sp, -4
sw $ra, 0($sp)
la $a0, new_line
li $v0, 4
syscall
la $a0, welcome_message
li $v0, 4
syscall
# Print line "==="
jal print_line
la $a0, menu_message
li $v0, 4
syscall
lw $ra, 0($sp)
addi $sp, $sp, 4
jr $ra
print_line: # Print line on welcome screen
addi $sp, $sp, -4
sw $ra, 0($sp)
li $s0, 0
li $s1, 50
loop_start:
la $a0, line_separator
li $v0, 4
syscall
addi $s0, $s0, 1
bne $s0, $s1, loop_start
la $a0, new_line
li $v0, 4
syscall
lw $ra, 0($sp)
addi $sp, $sp, 4
jr $ra