在Ruby中使用过多的堆栈内存

在Ruby中使用过多的堆栈内存,ruby,stack-memory,Ruby,Stack Memory,可能重复: 这是我制作的一个21点模拟器,用来检查在不同策略下一个人获胜的百分比。守则: one_suit = [2,3,4,5,6,7,8,9,10,10,10,10,11]; #the value of the cards for blackjack $full_deck = one_suit*4; #clubs, diamonds, hearts and spades $deck = $full_deck; #start off the game with a full deck cl

可能重复:

这是我制作的一个21点模拟器,用来检查在不同策略下一个人获胜的百分比。守则:

one_suit = [2,3,4,5,6,7,8,9,10,10,10,10,11]; #the value of the cards for blackjack
$full_deck = one_suit*4; #clubs, diamonds, hearts and spades
$deck = $full_deck; #start off the game with a full deck

class Player
  attr_accessor :ace_count
  attr_accessor :hand_value

  def initialize(ace_count,hand_value)
    @ace_count  = ace_count;
    @hand_value = hand_value;
  end

  def hit #instance method, vs. self.hit = class method
    choice_of_card = rand($deck.length); #choose a random card out of the deck
    drawn_card = $deck[choice_of_card]; #draw that random card from the deck
    if drawn_card != 0 #if there is a card there 
     $deck[choice_of_card] = 0; #remove that card from the deck by making the space blank
     if drawn_card == 11 #if you draw an ace
      self.ace_count += 1;
     end 
     self.hand_value += drawn_card ;
    else hit; #if there is no card at that space then redraw (recursion)
    end
  end

  def flip_aces
    while self.hand_value > 21 && ace_count > 0 #repeat until hand is below 21 or aces are all flipped
     self.ace_count -= 1 #ace gets flipped to a 1
     self.hand_value -= 10 #ace goes from 11 to 1
    end
  end

end


def oneplayergame
 $deck = $full_deck; #start a new game with a full deck 
 #generate the house and the player 
 house = Player.new(0,0);
 player1 = Player.new(0,0);
 #both the house and the player draw two cards
 house.hit; house.hit; player1.hit; player1.hit;
 while player1.hand_value <= 16 #PLAYER STRATEGY: ON WHAT CARD DOES THE PLAYER STAND
  player1.hit;
  if player1.hand_value > 21
   player1.flip_aces; 
  end
 end
 while house.hand_value <= player1.hand_value && player1.hand_value <=21 #HOUSE DRAWS CARDS IF UNDER PLAYER VALUE AND PLAYER IS NOT BUST
  house.hit;
  if house.hand_value > 21
   house.flip_aces; 
  end
 end
 #outcome
 #puts player1.hand_value;
 #puts house.hand_value;
 if player1.hand_value < house.hand_value
  return -1;
 elsif player1.hand_value > house.hand_value
  return 1;
 else return 0;
 end
end

#the simulation
wins = 0;
losses = 0;
rounds=5;

for i in 1..rounds
 if oneplayergame >0 
  wins +=1;
 elsif oneplayergame <0
  losses +=1
 end
end

print wins/(wins+losses).round(3)*100;
one_suit=[2,3,4,5,6,7,8,9,10,10,10,11]#21点牌的价值
$full_甲板=一套西装*4#梅花、钻石、红桃和黑桃
$deck=$full_deck#以一副完整的牌开始游戏
职业选手
属性存取器:ace\U计数
属性存取器:手动值
def初始化(ace_计数、手动值)
@ace_计数=ace_计数;
@hand_值=hand_值;
结束
def hit#instance方法与self.hit=class方法
卡片的选择=兰特($deck.length)#从牌组中随机选择一张牌
抽出的卡片=$CACK[卡片的选择]#从牌组中随机抽出那张牌
如果使用U卡!=0#如果那里有卡片
$deck[choice_of_card]=0#通过将空格留空,将该卡从牌组中移除
如果抽到牌=11#如果你抽到A
self.ace_计数+=1;
结束
self.hand\u value+=提款卡;
否则命中#如果该空间没有卡,则重新绘制(递归)
结束
结束
def flip_aces
当self.hand_value>21&&ace_count>0时,重复此操作,直到hand低于21或ace全部翻转
self.ace_count-=1#ace被翻转为1
self.hand_值-=10#ace从11变为1
结束
结束
结束
def oneplayergame
$deck=$full_deck#开始一场全新的全套游戏
#生成房子和玩家
豪斯=玩家。新(0,0);
player1=Player.new(0,0);
#房子和玩家都抽了两张牌
house.hit;house.hit;player1.hit;player1.hit;
whileplayer1.hand_值21
玩家1.翻牌;
结束
结束
而house.hand_值house.hand_值
返回1;
否则返回0;
结束
结束
#模拟
wins=0;
损失=0;
轮数=5;
我在1..轮
如果oneplayergame>0
wins+=1;

elsif oneplayergame我不懂这个游戏,但这是我对你的代码的重构。你自己检查一下差异,想想为什么我会这样改变它

$full_deck = [2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 11] * 4

class Player
    attr_reader :ace_count, :hand_value
    def initialize(ace_count, hand_value)
        @ace_count, @hand_value = ace_count, hand_value
    end
    def hit
        drawn_card = $deck.shift
        @ace_count += 1 if drawn_card == 11
        @hand_value += drawn_card
    end
    def flip_aces
        while @hand_value > 21 and @ace_count > 0
            @ace_count -= 1
            @hand_value -= 10
        end
    end
end

def oneplayergame
    $deck = $full_deck.dup.shuffle!
    house, player1 = Player.new(0,0), Player.new(0,0)
    [house, house, player1, player1].each{|player| player.hit}
    while player1.hand_value <= 16
        player1.hit
        player1.flip_aces
    end
    while house.hand_value <= player1.hand_value and player1.hand_value <=21
        house.hit
        house.flip_aces
    end
    player1.hand_value <=> house.hand_value
end

wins = 0.0
losses = 0.0
rounds = 5

rounds.times do
    result = oneplayergame
    wins +=1 if result > 0 
    losses +=1 if result < 0
end
puts wins/(wins+losses).round(3)*100
$full_deck=[2,3,4,5,6,7,8,9,10,10,10,11]*4
职业选手
属性读取器:ace计数,:手值
def初始化(ace_计数、手动值)
@ace_计数,@hand_值=ace_计数,hand_值
结束
def命中
提款卡=$deck.shift
@如果提款卡=11,则ace计数+=1
@手牌价值+=提款卡
结束
def flip_aces
而@hand_value>21和@ace_count>0
@ace_计数-=1
@手动_值-=10
结束
结束
结束
def oneplayergame
$deck=$full_deck.dup.shuffle!
豪斯,玩家1=玩家。新(0,0),玩家。新(0,0)
[house,house,player1,player1]。每个{玩家{玩家。击中}
whileplayer1.hand_值