質問編集履歴
1
全体のコードを書きましたが、エラーが発生してしまいました。
test
CHANGED
File without changes
|
test
CHANGED
@@ -20,12 +20,28 @@
|
|
20
20
|
|
21
21
|
```ruby
|
22
22
|
|
23
|
+
|
24
|
+
|
25
|
+
|
26
|
+
|
27
|
+
|
28
|
+
|
29
|
+
|
30
|
+
|
23
|
-
#役の配列をつくる
|
31
|
+
#役の配列をつくる。インデックスの大小で役の強弱を表す。
|
24
32
|
|
25
33
|
YAKU = ["High_Cards", "One_Pair", "Two_Pair", "Three_of_a_Kind", "Straight", "Flush", "Fullhouse", "Four_of_a_Kind", "Straight_Flush", "Royal_Straight_Flush"]
|
26
34
|
|
27
35
|
|
28
36
|
|
37
|
+
|
38
|
+
|
39
|
+
#役の判断。大きい順に行う。
|
40
|
+
|
41
|
+
|
42
|
+
|
43
|
+
|
44
|
+
|
29
45
|
def eval_hand
|
30
46
|
|
31
47
|
|
@@ -36,47 +52,147 @@
|
|
36
52
|
|
37
53
|
hand = gets.split
|
38
54
|
|
39
|
-
|
40
|
-
|
41
|
-
i
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
e
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
e
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
e
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
e
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
e
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
55
|
+
#それそれのカードから、スートの部分だけ、数字の部分だけを取り出した配列を別に作る。
|
56
|
+
|
57
|
+
suits = Array.new
|
58
|
+
|
59
|
+
numbers = Array.new
|
60
|
+
|
61
|
+
|
62
|
+
|
63
|
+
hand.each do |c|
|
64
|
+
|
65
|
+
suits.push c[0]
|
66
|
+
|
67
|
+
numbers.push c[1]
|
68
|
+
|
69
|
+
end
|
70
|
+
|
71
|
+
|
72
|
+
|
73
|
+
def same_number_counts
|
74
|
+
|
75
|
+
same_numbers = numbers.group_by{|r|r}.map(&:size).sort.reverse
|
76
|
+
|
77
|
+
end
|
78
|
+
|
79
|
+
|
80
|
+
|
81
|
+
def straight?
|
82
|
+
|
83
|
+
steps = numbers.sort.map{|r|r-numbers[0]}
|
84
|
+
|
85
|
+
steps == [0,1,2,3,4] || steps == [0,9,10,11,12]
|
86
|
+
|
87
|
+
end
|
88
|
+
|
89
|
+
|
90
|
+
|
91
|
+
def flush?
|
92
|
+
|
93
|
+
suits.uniq.size == 1
|
94
|
+
|
95
|
+
end
|
96
|
+
|
97
|
+
|
98
|
+
|
99
|
+
case same_number_counts
|
100
|
+
|
101
|
+
when [2,1,1,1]
|
102
|
+
|
103
|
+
yaku = YAKU[1]
|
104
|
+
|
105
|
+
when [2,2,1]
|
106
|
+
|
107
|
+
yaku = YAKU[2]
|
108
|
+
|
109
|
+
when [3,1,1]
|
110
|
+
|
111
|
+
yaku = YAKU[3]
|
112
|
+
|
113
|
+
when [3,2]
|
114
|
+
|
115
|
+
yaku = YAKU[6]
|
116
|
+
|
117
|
+
when [4,1]
|
118
|
+
|
119
|
+
yaku = YAKU[7]
|
120
|
+
|
121
|
+
else
|
122
|
+
|
123
|
+
case [straight?, flush?]
|
124
|
+
|
125
|
+
when [true, false]
|
126
|
+
|
127
|
+
yaku = YAKU[4]
|
128
|
+
|
129
|
+
when [false, true]
|
130
|
+
|
131
|
+
yaku = YAKU[5]
|
132
|
+
|
133
|
+
when [true, true]
|
134
|
+
|
135
|
+
if numbers == [1,10,11,12,13]
|
136
|
+
|
137
|
+
yaku = YAKU[8]
|
138
|
+
|
139
|
+
else
|
140
|
+
|
141
|
+
yaku = YAKU[9]
|
142
|
+
|
143
|
+
end
|
144
|
+
|
145
|
+
else
|
146
|
+
|
147
|
+
yaku = YAKU[0]
|
148
|
+
|
149
|
+
end
|
150
|
+
|
151
|
+
end
|
152
|
+
|
153
|
+
end
|
154
|
+
|
155
|
+
|
156
|
+
|
157
|
+
def end_program
|
158
|
+
|
159
|
+
exit
|
160
|
+
|
161
|
+
end
|
162
|
+
|
163
|
+
|
164
|
+
|
165
|
+
def exception
|
166
|
+
|
167
|
+
puts "入力された値は無効です"
|
168
|
+
|
169
|
+
end
|
170
|
+
|
171
|
+
|
172
|
+
|
173
|
+
#メニューの表示
|
174
|
+
|
175
|
+
while true do
|
176
|
+
|
177
|
+
puts "[0]役を判定する"
|
178
|
+
|
179
|
+
puts "[1]アプリを終了する"
|
180
|
+
|
181
|
+
input = gets.to_i
|
182
|
+
|
183
|
+
|
184
|
+
|
185
|
+
if input == 0 then
|
186
|
+
|
187
|
+
eval_hand
|
188
|
+
|
189
|
+
elsif input == 1 then
|
190
|
+
|
191
|
+
end_program
|
192
|
+
|
193
|
+
else
|
194
|
+
|
195
|
+
exception
|
80
196
|
|
81
197
|
end
|
82
198
|
|
@@ -84,8 +200,16 @@
|
|
84
200
|
|
85
201
|
```
|
86
202
|
|
87
|
-
上記の
|
203
|
+
上記のファイルpoker.rbをターミナル上で実行したところ、以下のようなエラーが発生してしまいました。
|
204
|
+
|
205
|
+
|
206
|
+
|
88
|
-
|
207
|
+
:in `same_number_counts': undefined local variable or method `numbers' for main:Object (NameError)
|
208
|
+
|
209
|
+
from poker.rb:38:in `eval_hand'
|
210
|
+
|
89
|
-
|
211
|
+
from poker.rb:82:in `<main>'
|
90
|
-
|
212
|
+
|
213
|
+
|
214
|
+
|
91
|
-
|
215
|
+
numbersの定義の部分に問題があるようなのですが…どうすれば解決できるでしょうか。ご助言いただけると嬉しいです。
|