我是Rails的新手,所以我为问题的标题道歉,我不知道怎么说。随意改变它。我正在建立一个扑克游戏来学习rails,我有以下的关联......
class Game < ActiveRecord::Base
has_many :players, :dependent => :destroy
has_many :community_cards, :class_name => "Card", :dependent => :destroy, :conditions => { :is_community_card => true }
has_many :used_cards, :class_name => "Card", :dependent => :destroy, :conditions => { :is_community_card => false }
attr_accessible :pot, :name, :status
class Player < ActiveRecord::Base
belongs_to :game
has_many :cards, :dependent => :destroy
attr_accessible :chip_count, :position, :fb_id
end
class Card < ActiveRecord::Base
belongs_to :player
belongs_to :game
attr_accessible :face, :suit, :is_community_card
end
当我尝试向所有玩家发放随机牌时,所有牌都以一名玩家结束......
def deal_players_hole_cards
players.all.each do |p|
if(p.cards.count < 2)
first_card = deal_card()
second_card = deal_card()
p.cards << first_card
p.cards << second_card
end
end
end
这是交易卡方法......
def deal_card
card_was_found = false
while(!card_was_found) do
card_was_found = true
random_suit = (0..3).to_a.sample
random_face = (1..13).to_a.sample
used_cards.all.each do |used_card|
if(random_suit == used_card.suit and random_face == used_card.face)
card_was_found = false
end
end
end
new_card = Card.create(:suit => random_suit, :face => random_face, :is_community_card => false)
used_cards << new_card
end
有两个玩家,每个玩家应该有两张牌,相反,一个玩家拥有全部四张牌......
ruby-1.9.2-p290 :001 > Game.last.players.last.cards.count
Game Load (0.1ms) SELECT "games".* FROM "games" ORDER BY "games"."id" DESC LIMIT 1
Player Load (0.1ms) SELECT "players".* FROM "players" WHERE "players"."game_id" = 2 ORDER BY "players"."id" DESC LIMIT 1
(0.2ms) SELECT COUNT(*) FROM "cards" WHERE "cards"."player_id" = 6
=> 4
ruby-1.9.2-p290 :002 > Game.last.players.first.cards.count
Game Load (0.4ms) SELECT "games".* FROM "games" ORDER BY "games"."id" DESC LIMIT 1
Player Load (0.3ms) SELECT "players".* FROM "players" WHERE "players"."game_id" = 2 LIMIT 1
(0.2ms) SELECT COUNT(*) FROM "cards" WHERE "cards"."player_id" = 5
=> 0
非常感谢您的所有智慧!
答案 0 :(得分:0)
我最后通过将播放器对象传递给交易卡方法并使用构建方法将新创建的卡附加到播放器来解决此问题...
####################################################################
# Deals each player their two hole cards
####################################################################
def deal_players_hole_cards
players.all.each do |p|
if(p.cards.count < 2)
deal_card(p)
deal_card(p)
end
end
end
####################################################################
# returns a random, unused card
####################################################################
def deal_card(p)
card_was_found = false
while(!card_was_found) do
card_was_found = true
random_suit = (0..3).to_a.sample
random_face = (1..13).to_a.sample
used_cards.all.each do |used_card|
if(random_suit == used_card.suit and random_face == used_card.face)
card_was_found = false
end
end
end
new_card = p.cards.build(:suit => random_suit, :face => random_face, :is_community_card => false)
used_cards << new_card
end
结果...
ruby-1.9.2-p290 :005 > Game.last.players.first.cards.count
Game Load (0.3ms) SELECT "games".* FROM "games" ORDER BY "games"."id" DESC LIMIT 1
Player Load (0.4ms) SELECT "players".* FROM "players" WHERE "players"."game_id" = 2 LIMIT 1
(0.3ms) SELECT COUNT(*) FROM "cards" WHERE "cards"."player_id" = 5
=> 2
ruby-1.9.2-p290 :006 > Game.last.players.last.cards.count
Game Load (0.4ms) SELECT "games".* FROM "games" ORDER BY "games"."id" DESC LIMIT 1
Player Load (0.4ms) SELECT "players".* FROM "players" WHERE "players"."game_id" = 2 ORDER BY "players"."id" DESC LIMIT 1
(0.2ms) SELECT COUNT(*) FROM "cards" WHERE "cards"."player_id" = 6
=> 2
我仍然很好奇为什么我的旧代码不起作用。