我觉得我错过了一些非常非常基本的东西,所以,为了避免扯掉更多的头发,我来这里问为什么 p1、p1t 和 p1t2 会被修改为“hand = newgame.view p1”。我什至不知道为什么 p1 会被修改,所以是的,我在这里完全感到困惑。任何帮助将不胜感激。
# encoding: utf-8
class Cards
def view hand
x = 0
hand.each do |card|
if card[0] == 's'
card[0] = '♠'
elsif card[0] == 'd'
card[0] = '♦'
elsif card[0] == 'h'
card[0] = '♥'
elsif card[0] == 'c'
card[0] = '♣'
else
#nil
end
hand[x] = card
x = x + 1
end
prettyhand = ''
hand.each do |card|
prettyhand = prettyhand + card[0] + card[1] + ' '
end
return prettyhand
end#view
end#Cards
###########
deck = 0
up = 0
p1 = 0
p2 = 0
newgame = Cards.new
p1 = [["c", "4"], ["s", "4"], ["d", "q"], ["c", "8"], ["s", "5"], ["s", "q"], ["h", "3"], ["c", "10"]]
p1t = []
p1t2 = []
hand = ''
p1.each do |card|
p1t.push card
end
p1t.each do |card|
p1t2.push card
end
p '----------------------------------'
p 'fresh p1:'
p p1
p p1t
p p1t2
hand = newgame.view p1
p 'unfresh p1:'#why is this changing??
p p1
p p1t
p p1t2
p '----------------------------------'
输出:
"----------------------------------"
"fresh p1:"
[["c", "4"], ["s", "4"], ["d", "q"], ["c", "8"], ["s", "5"], ["s", "q"], ["h", "3"], ["c", "10"]]
[["c", "4"], ["s", "4"], ["d", "q"], ["c", "8"], ["s", "5"], ["s", "q"], ["h", "3"], ["c", "10"]]
[["c", "4"], ["s", "4"], ["d", "q"], ["c", "8"], ["s", "5"], ["s", "q"], ["h", "3"], ["c", "10"]]
"unfresh p1:"
[["♣", "4"], ["♠", "4"], ["♦", "q"], ["♣", "8"], ["♠", "5"], ["♠", "q"], ["♥", "3"], ["♣", "10"]]
[["♣", "4"], ["♠", "4"], ["♦", "q"], ["♣", "8"], ["♠", "5"], ["♠", "q"], ["♥", "3"], ["♣", "10"]]
[["♣", "4"], ["♠", "4"], ["♦", "q"], ["♣", "8"], ["♠", "5"], ["♠", "q"], ["♥", "3"], ["♣", "10"]]
"----------------------------------"