From 2fa142a246be226b4779e93fef5e7d8e9803f075 Mon Sep 17 00:00:00 2001 From: Alex Scown Date: Sun, 9 Jan 2022 12:51:48 +0000 Subject: [PATCH] Remove k global --- 04_Awari/ruby/awari.rb | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/04_Awari/ruby/awari.rb b/04_Awari/ruby/awari.rb index fa149d5f..466f1c71 100644 --- a/04_Awari/ruby/awari.rb +++ b/04_Awari/ruby/awari.rb @@ -124,11 +124,11 @@ def distribute_beans(beans, start_pit, home_pit) current_pit end -def update_internals(beans) - $k = $k % 7 +def update_internals(beans, current_move) + k = current_move % 7 $c = $c + 1 - $f[$n] = $f[$n] * 6 + $k if $c < 9 + $f[$n] = $f[$n] * 6 + k if $c < 9 unless beans[0...6].find { |b| b != 0 } && beans[7...13].find { |b| b != 0 } $game_over = true @@ -141,7 +141,7 @@ end def perform_move(beans, move, home_pit) last_pit = distribute_beans(beans, move, home_pit) - update_internals(beans) + update_internals(beans, move) last_pit end @@ -210,11 +210,11 @@ def get_computer_move(beans) final_score = beans_copy[13] - beans_copy[6] - player_max_score if $c <=8 - $k = move_under_test % 7 - end + k = move_under_test % 7 - (0...$n).each do |i| - final_score = final_score - 2 if $f[$n] * 6 + $k == ((Float($f[i])/6 ** (7-$c)) + 0.1).floor + (0...$n).each do |i| + final_score = final_score - 2 if $f[$n] * 6 + k == ((Float($f[i])/6 ** (7-$c)) + 0.1).floor + end end # Choose the move if it is the best move found so far @@ -254,7 +254,6 @@ while true move = get_move("YOUR MOVE", beans) home_pit = 6 computer_home_pit = 13 - $k = move last_pit = perform_move(beans, move, home_pit) @@ -267,7 +266,6 @@ while true if home_pit == last_pit second_move = get_move("AGAIN", beans) - $k = second_move perform_move(beans, second_move, home_pit)