圖靈完備(Turing Complete)游戲代碼題解合集

OVERTURE架構(gòu)

加5等于幾
(hex) B1 05 82 44 9E
或者
(dec) 177 5 130 68 158
激光炮直瞄
# read radius to reg1
cp|io*8|1
# copy radius to reg2
cp|1*8|2
# add
add
# result is 2r
# copy reg3 to reg1, reg2
cp|3*8|1
cp|3*8|2
add
# result is 4r
# finally, 6r = 4r + 2r
# copy reg3 to reg1
cp|3*8|1
add
# now reg3 = 6r
cp|3*8|io
太空入侵者
# go to the frontier
5
cp|out
1
cp|out
# wait for the rat
label normal_loop
3
cp|out
label rat_loop
normal_loop # add the addr to reg0
cp|in|3 # copy input to reg3
is_zero # if input is 0, goto nloop
# if not 0: rat
5 # shoot code as immediate number
cp|out # shoot!
cp|2*8|3 # let reg3 = 0
rat_loop
is_zero # goto rloop
密碼鎖
# int guess = 63;
# output(guess);
# int fdbk = input();
# if (fdbk)
# guess--;
# else
# guess++;
# asm:
# guess a number
63
cp|1 # cp guess to reg1
1
cp|2
label lt_ans
add
label gt_ans
# output reg3
cp|3*8|out
cp|3*8|1
# read input feedback
cp|in|3
# is zero?
lt_ans
is_zero
sub
gt_ans
jmp
時間掩碼
# 397 mod 100 = 97
# (10011101)2 mod 4 = (01)2
# code:
# int num = input();
# num = (num<<6)>>6;
# return num;
cp|in|1
0b00000011
# 0000 0011
# 1011 1001
#-----------and
# 0000 0001
cp|2
and
cp|3*8|out
迷宮
# forward a step
# turn left
# if left is wall:
# turn right till not wall
# goto 1
const LEFT 0
const FORWARD 1
const RIGHT 2
const PASS 3
const INTERACT 4
const WALL 1
# code:
# output(FORWARD);
# output(LEFT);
# int front = input();
# while (front == WALL)
# output(RIGHT);
# goto start;
# asm:
label start
FORWARD
cp|out
LEFT
cp|out
label read_input
cp|in|3
start
is_zero
RIGHT
cp|out
INTERACT
cp|out
read_input
jmp

LEG架構(gòu)

除法
# read s0, s1 (s0/s1)
# let i = 1
# i*s1 >= s0 ?
# true: {
# i*s1 > s0 ?
# ? true:
# q = i - 1
# ? false:
# ? ?q = i
#}
# r = s0 - q*s1
# false: i++
# asm:
addi 0 io s0
addi 0 io s1
addij 0 1 t0
label loop
mul_l t0 s1 t1
mul_h t0 s1 t2
gt t1 s0 break
neq|j t2 0 break
eq t1 s0 equ_break
addi 1 t0 t0
goto nop nop loop
label break
sub|j t0 1 t0
label equ_break
mul_l t0 s1 t1
sub s0 t1 t1
addi 0 t0 io
addi 0 t1 io
函數(shù)
# int fib(int n) {
# ? ?if(n == 1 || n == 2) {
# ? ? ? ?return 1;
# ? ?}
# ? return fib(n - 1) + fib(n - 2);
#}
label main
addij 0 7 a0
call f_fib nop nop
addi 0 v0 io
label f_fib
# @a0: input index n
# @v0: return
addi 0 a0 s0
leq|j s0 2 ffib_1
# call fib(n-1)
push s0 0 0
push s1 0 0
push s2 0 0
subj s0 1 s0
addi 0 s0 a0
call f_fib 0 0
pop 0 0 s2
pop 0 0 s1
pop 0 0 s0
addi 0 v0 s1
# call fib(n-2)
push s0 0 0
push s1 0 0
push s2 0 0
subj s0 2 s0
addi 0 s0 a0
call f_fib 0 0
pop 0 0 s2
pop 0 0 s1
pop 0 0 s0
addi 0 v0 s2
add s1 s2 v0
ret 0 0 0
label ffib_1
addij 0 1 v0
ret 0 0 0

匯編挑戰(zhàn)
AI打牌
addij 3 0 io
addi 0 io t0
subj t0 5 t0
addi 0 t0 io
addi 0 io t0
subj t0 1 t0
addi 0 t0 io
機(jī)器賽跑
# 0r, 1d, 2l, 3u
const route1 0b00010011 # 3-0-1-0
const route2 0b11101100
const route3 0b10101100 # 0-3-2-2
const route4 0b11111001
const route5 0b01010011
const route6 0b00000110
# order_list1 1-2-3-4
# order_list2 2-1-5-6
label main
addij 0 route1 a0
call load_map 0 0
addij 0 route2 a0
call load_map 0 0
addij 0 route3 a0
call load_map 0 0
addij 0 route4 a0
call load_map 0 0
addij 0 route2 a0
call load_map 0 0
addij 0 route1 a0
call load_map 0 0
addij 0 route5 a0
call load_map 0 0
addij 0 route6 a0
call load_map 0 0
label o_loop
subj gp 1 gp
lw gp 0 io
goto 0 0 o_loop
label load_map
# @a0: map
addij 0 4 t1 # t1=4
addij 0 0b00000011 t2
label lm_sub
and a0 t2 t3 # t3 = a0&t2
addi 0 t3 io # output t3
mod|j t3 2 t4
eq|j t4 0 lm_non_rev
addj t3 2 t3
and t3 t2 t3
label lm_non_rev
sw gp t3 0
addi 1 gp gp
subj t1 1 t1 # t1--
shr|j a0 2 a0 # a0>>2
gt|j t1 0 lm_sub
ret 0 0 0
新品上市
label main
call func_goto_belt 0 0
# read input
label main_loop
addi 0 io s0
eq|j s0 92 pass_sw
addi 0 s0 a0
call func_check_if_same 0 0
eq|j v0 1 func_press_btn
sw gp s0 nop
addi 1 gp gp
label pass_sw
addij 0 3 io
goto 0 0 main_loop
label func_goto_belt
addij 0 0 io
addij 0 1 io
addij 0 0 io
addij ?0 4 t0
label fgb_loop
addij 0 1 io
subj t0 1 t0
neq|j t0 0 fgb_loop
addij 0 0 io
addij 0 1 io
addij 0 2 io
addij 0 1 io
ret 0 0 0
label func_check_if_same
# a0: input thing
addi 0 gp t0
label fcis_loop
lw t0 nop t1
eq a0 t1 fcis_has_item
subj t0 1 t0
neq|j t0 0xff fcis_loop
addij 0 0 v0
ret 0 0 0
label fcis_has_item
addij 0 1 v0
ret 0 0 0
label func_press_btn
addij 0 2 io
addij 0 4 io
ret 0 0 0
美味排序
# read input
addij 0 15 t0
label in_loop
addi 0 io t1
sw gp t1 nop
addi 1 gp gp
subj t0 1 t0
neq|j t0 0 in_loop
subj gp 1 gp
call func_sort_arr 0 0
addij 0 15 t0
label out_loop
lw gp nop t1
addi 0 t1 io
addi 1 gp gp
subj t0 1 t0
neq|j t0 0 out_loop
label func_sort_arr
label fsa_iter_loop
addij 0 0 t0
label fsa_swap_loop
lw t0 nop t2
addi 1 t0 t0
lw t0 nop t3
lt t2 t3 fsa_non_swap
# swap t2 t3
sw t0 t2 nop
subj t0 1 t2
sw t2 t3 nop
label fsa_non_swap
neq t0 gp fsa_swap_loop
subj gp 1 gp
neq|j gp 0 fsa_iter_loop
ret 0 0 0
跳舞機(jī)器
const seed 4 # $t0
const temp1 5 # $t1
const temp2 8 # $t2
addi 0 io seed
label loop
?shr|j seed 1 temp1
?xor seed temp1 temp1
?shl|j temp1 1 temp2
?xor temp1 temp2 temp2
?shr|j temp2 2 seed
?xor temp2 seed seed
?mod|j seed 4 io
goto nop nop loop
核金漢諾塔
const disk_nr s0
const source s1
const dest s2
const spare s3
const action 5 # swt the magnet
addi 0 io disk_nr
addi 0 io source
addi 0 io dest
addi 0 io spare
call func_move nop nop
label func_move
# @disk_nr, source, dest, spare
# store parameters before calling
# if disk_nr == 0: goto fm_direct
eq|j disk_nr 0 fm_direct
# else:
?# call move 01
?push disk_nr nop nop
?push source nop nop
?push dest nop nop
?push spare nop nop
?subj disk_nr 1 disk_nr
?addi 0 dest t0
?addi 0 spare dest
?addi 0 t0 spare
?call func_move nop nop
?pop nop nop spare
?pop nop nop dest
?pop nop nop source
?pop nop nop disk_nr
?# move disk from source to dest
?addi 0 source io
?addij 0 action io
?addi 0 dest io
?addij 0 action io
?# call move 02
?push disk_nr nop nop
?push source nop nop
?push dest nop nop
?push spare nop nop
?subj disk_nr 1 disk_nr
?addi 0 spare t0
?addi 0 source spare
?addi 0 t0 source
?call func_move nop nop
?pop nop nop spare
?pop nop nop dest
?pop nop nop source
?pop nop nop disk_nr
?ret nop nop nop
label fm_direct
# move disk from source to dest
addi 0 source io
addij 0 action io
addi 0 dest io
addij 0 action io
ret nop nop nop
行星之名
# Cap the first:
addi 0 io t0
subj t0 0x20 io
label main_loop
addi 0 io t0
eq|j t0 0x20 space_handle
addi 0 t0 io
goto nop nop main_loop
label space_handle
addi 0 t0 io
addi 0 io t0
subj t0 0x20 io
goto nop nop main_loop
水世界
# read input
sw gp io nop
addi 1 gp gp
neq|j gp 16 0
label main
# find global max = s0
lw t0 nop t1
lt t1 s0 fgm_non_swap
addi 0 t1 s0
label fgm_non_swap
addi 1 t0 t0
lt|j t0 16 main
label count_loop
addi 0 s0 a0
call func_sing_layer_counter 0 0
subj s0 1 s0
neq|j s0 0 count_loop
addi 0 v0 io
label func_sing_layer_counter
# @a0, layer index (starts from 1)
# @v0, water_block_counts
# find boundaries
# t1=left boundary, t2=right...
# addij 0 0 v0
addij 0 255 t1
label fslc_left_bound
addi 1 t1 t1
lw t1 nop t0
lt t0 a0 fslc_left_bound
addij 0 16 t2
label fslc_right_bound
subj t2 1 t2
lw t2 nop t0
lt t0 a0 fslc_right_bound
label fslc_counter_loop
lw t1 nop t0
geq t0 a0 fslc_non_incres
addi 1 v0 v0
label fslc_non_incres
addi 1 t1 t1
leq t1 t2 fslc_counter_loop
ret nop nop nop