rubyで条件分岐が上手く設定できていない

ruby

1# 数列の組み合わせを定義2$combinations_1 = [3 [1, 1], [1, 2], [1, 3], [1, 4], [1, 5], [1, 6], [1, 7],4 [2, 1], [2, 2], [2, 3], [2, 4], [2, 5], [2, 6], [2, 7], 5 [3, 1], [3, 2], [3, 3], [3, 4], [3, 5], [3, 6], [3, 7],6 [4, 1], [4, 2], [4, 3], [4, 4], [4, 5], [4, 6], [4, 7],7 [5, 1], [5, 2], [5, 3], [5, 4], [5, 5], [5, 6], [5, 7],8 [6, 1], [6, 2], [6, 3], [6, 4], [6, 5], [6, 6], [6, 7],9 [7, 1], [7, 2], [7, 3], [7, 4], [7, 5], [7, 6], [7, 7]10 ]11 12$combinations_1_DBM = [13 [1, 7],14 [2, 6],15 [3, 5],16 [4, 4],17 [5, 3],18 [6, 2],19 [7, 1]20 ]21 22$combinations_2 = [23 [57, 13], [57, 24], [57, 35], [57, 46], [57, 15], [57, 26], [57, 37], [57, 17], [57, 12], [57, 57],24 [57, 14], [57, 25], [57, 36], [57, 47], [57, 23], [57, 34], [57, 45], [57, 67], [57, 16], [57, 27],25 [46, 13], [46, 24], [46, 35], [46, 46], [46, 15], [46, 26], [46, 37], [46, 17], [46, 12], [46, 57],26 [46, 14], [46, 25], [46, 36], [46, 47], [46, 23], [46, 34], [46, 45], [46, 67], [46, 16], [46, 27],27 [35, 13], [35, 24], [35, 35], [35, 46], [35, 15], [35, 26], [35, 37], [35, 17], [35, 12], [35, 57],28 [35, 14], [35, 25], [35, 36], [35, 47], [35, 23], [35, 34], [35, 45], [35, 67], [35, 16], [35, 27],29 [24, 13], [24, 24], [24, 35], [24, 46], [24, 15], [24, 26], [24, 37], [24, 17], [24, 12], [24, 57],30 [24, 14], [24, 25], [24, 36], [24, 47], [24, 23], [24, 34], [24, 45], [24, 67], [24, 16], [24, 27],31 [37, 13], [37, 24], [37, 35], [37, 46], [37, 15], [37, 26], [37, 37], [37, 17], [37, 12], [37, 57],32 [37, 14], [37, 25], [37, 36], [37, 47], [37, 23], [37, 34], [37, 45], [37, 67], [37, 16], [37, 27],33 [26, 13], [26, 24], [26, 35], [26, 46], [26, 15], [26, 26], [26, 37], [26, 17], [26, 12], [26, 57],34 [26, 14], [26, 25], [26, 36], [26, 47], [26, 23], [26, 34], [26, 45], [26, 67], [26, 16], [26, 27],35 [15, 13], [15, 24], [15, 35], [15, 46], [15, 15], [15, 26], [15, 37], [15, 17], [15, 12], [15, 57],36 [15, 14], [15, 25], [15, 36], [15, 47], [15, 23], [15, 34], [15, 45], [15, 67], [15, 16], [15, 27],37 [17, 13], [17, 24], [17, 35], [17, 46], [17, 15], [17, 26], [17, 37], [17, 17], [17, 12], [17, 57],38 [17, 14], [17, 25], [17, 36], [17, 47], [17, 23], [17, 34], [17, 45], [17, 67], [17, 16], [17, 27],39 [67, 13], [67, 24], [67, 35], [67, 46], [67, 15], [67, 26], [67, 37], [67, 17], [67, 12], [67, 57], 40 [67, 14], [67, 25], [67, 36], [67, 47], [67, 23], [67, 34], [67, 45], [67, 67], [67, 16], [67, 27],41 [13, 13], [13, 24], [13, 35], [13, 46], [13, 15], [13, 26], [13, 37], [13, 17], [13, 12], [13, 57],42 [13, 14], [13, 25], [13, 36], [13, 47], [13, 23], [13, 34], [13, 45], [13, 67], [13, 16], [13, 27],43 [47, 13], [47, 24], [47, 35], [47, 46], [47, 15], [47, 26], [47, 37], [47, 17], [47, 12], [47, 57],44 [47, 14], [47, 25], [47, 36], [47, 47], [47, 23], [47, 34], [47, 45], [47, 67], [47, 16], [47, 27],45 [36, 13], [36, 24], [36, 35], [36, 46], [36, 15], [36, 26], [36, 37], [36, 17], [36, 12], [36, 57],46 [36, 14], [36, 25], [36, 36], [36, 47], [36, 23], [36, 34], [36, 45], [36, 67], [36, 16], [36, 27],47 [25, 13], [25, 24], [25, 35], [25, 46], [25, 15], [25, 26], [25, 37], [25, 17], [25, 12], [25, 57],48 [25, 14], [25, 25], [25, 36], [25, 47], [25, 23], [25, 34], [25, 45], [25, 67], [25, 16], [25, 27],49 [14, 13], [14, 24], [14, 35], [14, 46], [14, 15], [14, 26], [14, 37], [14, 17], [14, 12], [14, 57],50 [14, 14], [14, 25], [14, 36], [14, 47], [14, 23], [14, 34], [14, 45], [14, 67], [14, 16], [14, 27],51 [56, 13], [56, 24], [56, 35], [56, 46], [56, 15], [56, 26], [56, 37], [56, 17], [56, 12], [56, 57],52 [56, 14], [56, 25], [56, 36], [56, 47], [56, 23], [56, 34], [56, 45], [56, 67], [56, 16], [56, 27],53 [45, 13], [45, 24], [45, 35], [45, 46], [45, 15], [45, 26], [45, 37], [45, 17], [45, 12], [45, 57],54 [45, 14], [45, 25], [45, 36], [45, 47], [45, 23], [45, 34], [45, 45], [45, 67], [45, 16], [45, 27],55 [34, 13], [34, 24], [34, 35], [34, 46], [34, 15], [34, 26], [34, 37], [34, 17], [34, 12], [34, 57],56 [34, 14], [34, 25], [34, 36], [34, 47], [34, 23], [34, 34], [34, 45], [34, 67], [34, 16], [34, 27],57 [12, 13], [12, 24], [12, 35], [12, 46], [12, 15], [12, 26], [12, 37], [12, 17], [12, 12], [12, 57],58 [12, 14], [12, 25], [12, 36], [12, 47], [12, 23], [12, 34], [12, 45], [12, 67], [12, 16], [12, 27],59 [27, 13],[27, 24],[27, 35],[27, 46],[27, 15],[27, 26],[27, 37],[27, 17],[27, 12],[27, 57],60 [27, 14],[27, 25],[27, 36],[27, 47],[27, 23],[27, 34],[27, 45],[27, 67],[27, 16],[27, 27],61 [16, 13],[16, 24],[16, 35],[16, 46],[16, 15],[16, 26],[16, 37],[16, 17],[16, 12],[16, 57],62 [16, 14],[16, 25],[16, 36],[16, 47],[16, 23],[16, 34],[16, 45],[16, 67],[16, 16],[16, 27]63]64 65$combinations_2_DBM = [66 [13, 57],67 [24, 46],68 [35, 35],69 [46, 24],70 [15, 37],71 [26, 26],72 [37, 15],73 [17, 17],74 [12, 67],75 [57, 13],76 [14, 47],77 [25, 36],78 [36, 25],79 [47, 14],80 [23, 56],81 [34, 45],82 [45, 34],83 [67, 12],84 [16, 27],85 [27, 16]86]87 88 # 数列をランダムにシャッフルし、条件に基づいて選択するメソッド89 def select_combinations(combinations)90 previous = nil91 selected = []92 93 while combinations.any?94 combination = combinations.sample 95 if valid_combination?(combination, previous)96 selected << combination 97 previous = combination 98 combinations.delete(combination)99 end100 end101 102 selected 103 end104 105 # 組み合わせが有効かどうかを判断するメソッド106 def valid_combination?(current, previous)107 return true unless previous 108 109 # 縦連のチェック110 return false if current[0] == previous[0] || current[1] == previous[1]111 112 # 特定のキーの連続使用を避ける113 prev_left, prev_right = previous 114 curr_left, curr_right = current 115 116 # 左手のルール117 return false if (prev_left == 2 && curr_left == 3) || (prev_left == 3 && curr_left == 2)118 return false if (prev_left == 6 && curr_left == 5) || (prev_left == 5 && curr_left == 7)119 return false if (prev_left == 7 && curr_left == 5) || (prev_left == 5 && curr_left == 6)120 121 # 右手のルール122 return false if (prev_right == 6 && curr_right == 5) || (prev_right == 5 && curr_right == 6)123 return false if (prev_right == 2 && curr_right == 3) || (prev_right == 3 && curr_right == 1)124 return false if (prev_right == 1 && curr_right == 3) || (prev_right == 3 && curr_right == 2)125 126 true127 end128 129 # 数列を置き換えて出力するメソッド130 def replace_and_output(arr, code_thickness)131 left_mapping = {132 1 => "11:ZZ", 2 => "12:ZZ", 3 => "13:ZZ", 4 => "14:ZZ", 5 => "15:ZZ", 6 => "18:ZZ", 7 => "19:ZZ"133 }134 right_mapping = {135 1 => "21:ZZ", 2 => "22:ZZ", 3 => "23:ZZ", 4 => "24:ZZ", 5 => "25:ZZ", 6 => "28:ZZ", 7 => "29:ZZ"136 }137 138 left_str = arr[0].to_s.chars.map { |digit| left_mapping[digit.to_i] }.join("\n")139 right_str = arr[1].to_s.chars.map { |digit| right_mapping[digit.to_i] }.join("\n")140 141 output = left_str + "\n" + right_str + "\n\n"142 output 143 end144 145 # ユーザーに表示される部分146 # 処理を実行するメインメソッド147 def generate_output148 puts "出力したい同時押しを構成するノーツの片側あたりの数を入力してください"149 puts "現在は「1」のみ受け付けています"150 # puts "1、2、3、4、5のみ受け付けています"にいずれ書き換えたい151 code_thickness = gets.to_i 152 153 # DBMモードを尋ねるメソッド154 def asking_dbm155 while true156 puts "DBM(左右対称配置)の形で生成しますか?"157 puts "[1]:はい"158 puts "[2]:いいえ"159 input = gets.to_i 160 161 case input 162 when 1163 puts "DBM配置で生成します"164 return "on"165 when 2166 puts "左右バラバラに生成します"167 return "off"168 else169 puts "もう一度入力をお願いします"170 end171 end172 end173 174 dbm_mode = asking_dbm 175 176 combinations = case code_thickness 177 when 1178 dbm_mode == "on" ? $combinations_1_DBM : $combinations_1179 when 2180 dbm_mode == "on" ? $combinations_2_DBM : $combinations_2181 when 3182 dbm_mode == "on" ? $combinations_3_DBM : $combinations_3183 when 4184 dbm_mode == "on" ? $combinations_4_DBM : $combinations_4185 when 5186 dbm_mode == "on" ? $combinations_5_DBM : $combinations_5187 else188 puts "プログラム終了!"189 exit 190 end191 192 selected_combinations = select_combinations(combinations.dup)193 194 puts "処理を実行する回数を入力してください: "195 if dbm_mode == "on"196 puts "#{code_thickness}個同時押しは全#{combinations.size}パターン"197 else198 puts "#{code_thickness}個同時押しの組み合わせは全#{combinations.size}パターン"199 end200 201 output_count = gets.chomp.to_i 202 203 File.open("#{code_thickness}重ホムポジ乱打.txt", "w") do |file|204 output_count.times do205 selected_combinations.each do |combination|206 output = replace_and_output(combination, code_thickness)207 file.write(output)208 end209 end210 end211 212 puts "結果は #{code_thickness}重ホムポジ乱打.txt に保存されました。"213 end214 215 generate_output

コメントを投稿

0 コメント