diff --git a/scene/little_game/书架/书架.gd b/scene/little_game/书架/书架.gd index 93ed8aef..de1417e8 100644 --- a/scene/little_game/书架/书架.gd +++ b/scene/little_game/书架/书架.gd @@ -48,6 +48,20 @@ func _ready() -> void: _measure_width_by_row() # shuffle at the end _shuffle_books() +# # test +# _test() + + +# func _test(): +# var timer = Timer.new() +# timer.autostart = true +# timer.wait_time = 0.5 +# timer.one_shot = false +# timer.timeout.connect(func(): +# print("[test] reload") +# get_tree().reload_current_scene() +# ) +# get_tree().root.add_child.call_deferred(timer) func _enter_tree() -> void: @@ -92,30 +106,39 @@ func _shuffle_books() -> void: rand_from_seed(Time.get_ticks_usec()) # 打乱偶数次有可能复原,需要打乱奇数次;为降低难度,仅打乱一次 var shuffle_times = 1 - var r_size = current_answer[0].size() + var r_size := current_answer[0].size() as int for _i in range(shuffle_times): - var col_1 = randi() % r_size - var col_2 = randi() % r_size + var col_1 := randi() % r_size + var col_2 := _rand_exclusive(col_1, r_size) selected_book = [0, col_1] _interchange_book(0, col_2, false) _relocate_books(0) r_size = current_answer[1].size() for _i in range(shuffle_times): - var col_1 = randi() % r_size - var col_2 = randi() % r_size + var col_1 := randi() % r_size + var col_2 := _rand_exclusive(col_1, r_size) selected_book = [1, col_1] _interchange_book(1, col_2, false) _relocate_books(1) r_size = current_answer[2].size() for _i in range(shuffle_times): - var col_1 = randi() % r_size - var col_2 = randi() % r_size + var col_1 := randi() % r_size + var col_2 := _rand_exclusive(col_1, r_size) selected_book = [2, col_1] _interchange_book(2, col_2, false) _relocate_books(2) - # turn off initilazing after shuffle suffling = false + _check_answer() + + +func _rand_exclusive(exclude_id: int, size: int) -> int: + if size <= 1: + return 0 + var id = randi() % size + if id == exclude_id: + return _rand_exclusive(exclude_id, size) + return id var press_mutex := Mutex.new() @@ -186,8 +209,7 @@ func _interchange_book(row: int, col: int, relocate := true) -> void: # relocate after reset selected_book if relocate: _relocate_books(row) - # check answer - if relocate: + # check answer after relocation _check_answer()