为散列中的数组中的单个元素赋值
Assigning a Value to a Single Element in an Array Within a Hash
我正在尝试创建一个 "Connect Four," 游戏来练习使用 Ruby。
我的问题在于更换单曲,"square," 在黑板上。我的 play_piece
方法更改了上面我想要播放的位置的整个列。
未删节的代码片段:
class Connect_four
def initialize
row = Array.new(7, '- ')
@board = {first: row, second: row, third: row,
fourth: row, fifth: row, sixth: row}
end
def display
puts @board[:sixth].join
puts @board[:fifth].join
puts @board[:fourth].join
puts @board[:third].join
puts @board[:second].join
puts @board[:first].join
puts "1 2 3 4 5 6 7"
end
def play_piece
input = get_input
if @board[:first][input] == '- '
@board[:first][input] = 'P '
elsif @board[:second][input] == '- '
@board[:second][input] = 'P '
elsif @board[:third][input] == '- '
@board[:third][input] = 'P '
elsif @board[:fourth][input] == '- '
@board[:fourth][input] = 'P '
elsif @board[:fifth][input] == '- '
@board[:fifth][input] = 'P '
elsif @board[:sixth][input] == '- '
@board[:sixth][input] = 'P '
end
end
def get_input
begin
puts "Enter the column # you wish to play in"
input = gets.chomp
puts "Invalid input!" unless input =~ /[1-7]/
end while (!input =~ /[1-7]/)
input = (input.to_i) - 1 #return array adjusted number
end
end
game = Connect_four.new
game.display
game.play_piece
game.display
gets
和不希望的结果:
- - P - - - -
- - P - - - -
- - P - - - -
- - P - - - -
- - P - - - -
- - P - - - -
1 2 3 4 5 6 7
检查后,只有if
/elsif
/else
语句之一被触发。我还尝试将值分配给较低的行,但它使下面的值保持不变。
例如:如果我将第三行更改为 X
,这会将第四、第五和第六行更改为 P
。
您使用相同的 row
七次。 Object#dup
来救援:
def initialize
row = Array.new(7, '- ')
@board = {first: row.dup, second: row.dup, third: row.dup,
fourth: row.dup, fifth: row.dup, sixth: row.dup}
end
使用 Ruby 惯用的语法:
def initialize
@board = %i|first second third fourth fifth sixth|.zip(
6.times.map { ['- '] * 7 }
).to_h
end
我正在尝试创建一个 "Connect Four," 游戏来练习使用 Ruby。
我的问题在于更换单曲,"square," 在黑板上。我的 play_piece
方法更改了上面我想要播放的位置的整个列。
未删节的代码片段:
class Connect_four
def initialize
row = Array.new(7, '- ')
@board = {first: row, second: row, third: row,
fourth: row, fifth: row, sixth: row}
end
def display
puts @board[:sixth].join
puts @board[:fifth].join
puts @board[:fourth].join
puts @board[:third].join
puts @board[:second].join
puts @board[:first].join
puts "1 2 3 4 5 6 7"
end
def play_piece
input = get_input
if @board[:first][input] == '- '
@board[:first][input] = 'P '
elsif @board[:second][input] == '- '
@board[:second][input] = 'P '
elsif @board[:third][input] == '- '
@board[:third][input] = 'P '
elsif @board[:fourth][input] == '- '
@board[:fourth][input] = 'P '
elsif @board[:fifth][input] == '- '
@board[:fifth][input] = 'P '
elsif @board[:sixth][input] == '- '
@board[:sixth][input] = 'P '
end
end
def get_input
begin
puts "Enter the column # you wish to play in"
input = gets.chomp
puts "Invalid input!" unless input =~ /[1-7]/
end while (!input =~ /[1-7]/)
input = (input.to_i) - 1 #return array adjusted number
end
end
game = Connect_four.new
game.display
game.play_piece
game.display
gets
和不希望的结果:
- - P - - - -
- - P - - - -
- - P - - - -
- - P - - - -
- - P - - - -
- - P - - - -
1 2 3 4 5 6 7
检查后,只有if
/elsif
/else
语句之一被触发。我还尝试将值分配给较低的行,但它使下面的值保持不变。
例如:如果我将第三行更改为 X
,这会将第四、第五和第六行更改为 P
。
您使用相同的 row
七次。 Object#dup
来救援:
def initialize
row = Array.new(7, '- ')
@board = {first: row.dup, second: row.dup, third: row.dup,
fourth: row.dup, fifth: row.dup, sixth: row.dup}
end
使用 Ruby 惯用的语法:
def initialize
@board = %i|first second third fourth fifth sixth|.zip(
6.times.map { ['- '] * 7 }
).to_h
end