2012-02-07 19 views
2

私はRubyで遊ぶための 'カードシャッフルプログラム'を書いています。私は希望の出力はここにあるruby​​ - 別の配列に基づいて配列を並べ替えるには?

:)それ、私はより多くを学ぶために自分自身を割り当て、個人の宿題考えてみましょう:

----Triple Cut Deck on 3rd and 5th cards--------- 
    -- reset 
Number: 1, Position: 3, Suit: Clubs, Card: 3 
Number: 2, Position: 4, Suit: Clubs, Card: 4 
Number: 3, Position: 1, Suit: Clubs, Card: 5 
Number: 4, Position: 2, Suit: Clubs, Card: 6 
Number: 5, Position: 5, Suit: Clubs, Card: Ace 
Number: 6, Position: 6, Suit: Clubs, Card: 2 

が、私は取得しています:

----Triple Cut Deck on 3rd and 5th cards--------- 
    -- reset 
Number: 1, Position: 3, Suit: Clubs, Card: 3 
Number: 2, Position: 4, Suit: Clubs, Card: 4 
Number: 3, Position: 5, Suit: Clubs, Card: 5 
Number: 4, Position: 5, Suit: Clubs, Card: 5 
Number: 5, Position: 6, Suit: Clubs, Card: 6 
Number: 6, Position: 6, Suit: Clubs, Card: 6 

を基本的に私がしようとしていますどのような「Ace、2、3、4、5、6」のカード順番が「1,2,3,4,5」から「5,6,3,4,1」に変更されるように、 、2 "。言い換えれば、最下位の2枚のカード(下位の順)、下の2枚の上段と中央のカードは同じままです.3ウェイ・カットのバージョンです。

この配列を正しく並べ替えるのは非常に困難です。 今、重複して、上記のようになどのソート・キーと

class Card 
    RANKS = %w(Ace 2 3 4 5 6 7 8 9 10 J Q K) 
    SUITS = ['Clubs', 'Diamonds', 'Hearts', 'Spades'] 
    SCORES = [1..54] 
    attr_accessor :rank, :suit, :card_position 

    def initialize(id, rank='', suit='', card_position=0) 
    self.card_position = id 
    self.rank = RANKS[(id % 14)-1] 
    self.suit = SUITS[(id/14)] 
    end 
end 

class Deck 
    DECK_SIZE = 6 
    attr_accessor :cards 
    def initialize 
    self.cards = (1..DECK_SIZE).to_a.collect { |id| Card.new(id) } 
    @deck = cards 
    end 

    def process_cards 

    puts "\n----Triple Cut Deck on 3rd and 5th cards---------" 
    self.triple_cut_deck(3, 5, true) 
    self.show_deck 

    end 

    def show_deck 
    @deck.sort_by!(&:card_position).each_with_index do |card, index| 
     puts 'Number: ' + (index+1).to_s + ", Position: #{card.card_position.to_s}, Suit: #{card.suit.to_s}, Card: #{card.rank.to_s}" 
    end 
    end 

    def triple_cut_deck(top_cut, bottom_cut, reset_deck=false) 
    reset_the_deck(reset_deck) 

    top_cut-= 1 
    bottom_cut-= 1 
    deck_array_size = DECK_SIZE-1 

    @new_deck = [] 
    @new_deck[0..1] = @deck[4..5] 
    @new_deck[2..3] = @deck[2..3] 
    @new_deck[4..5] = @deck[0..1] 

    DECK_SIZE.times do |card| 
     @deck[card].card_position= @new_deck[card].card_position 
     @deck[card].card_position= @new_deck[card].card_position 
     @deck[card].card_position= @new_deck[card].card_position 
    end 
    end 


    def reset_the_deck(reset_deck) 
    puts reset_deck == true ? " -- reset" : 'no-reset' 
    initialize if (true && reset_deck) 
    end 

end 
+0

あなたがデッキをシャッフルする[フィッシャーイエーツシャッフル](http://en.wikipedia.org/wiki/Knuth_shuffle)を使用して考えがありますか? – ddfreyne

+0

はい、それはプロセスをより良く学ぶのにそれを使わないほうがいいですが、ありがとう! –

+0

の可能な複製[Rubyの配列を特定の順序にソートする方法は?](http://stackoverflow.com/questions/4283295/how-to-sort-an-array-in-ruby-to-a-particular -order) –

答えて

1

これはこれでいいですか?

a = [1,2,3,4,5,6] 

n = 2 
b = a[-n, n] + a[n..-(n+1)] + a[0,n] 

p a # => [1,2,3,4,5,6] 
p b # => [5,6,3,4,1,2] 
+1

をご覧ください。シンタックスは間違っていた。ありがとう。 SO(スタックオーバーフロー)に運があります。それは素晴らしいですか? –

1

最速の解決策になる可能性は低いが、あなたは2つの配列をzipすることができます(配列まず、台無しになっているカードのランク 'とcard_position )、次のように結果をソートします。

a = [ 8, 4, 2, 7, 5 ] 
b = [ 5, 7, 0, 3, 3 ] 

a.zip(b).sort.transpose.last 
# => [0, 7, 3, 3, 5] 
+0

ジップについて知っておくと便利です。 zip-map-reduceの詳細については、http://stackoverflow.com/questions/3281/mapping-values-from-two-array-in-ruby –

0

配列の方法として、実際の位置として位置を保持しようとしています。それは多くの簿記です。また、カットとトリプルまたはフォーフォードカッティングは基本的に同じことです。

Ranks = %w(Ace 2 3 4 5 6 7 8 9 10 J Q K) 
Suits = ['Clubs', 'Diamonds', 'Hearts', 'Spades'] 
Card = Struct.new(:rank, :suit) 
deck = Suits.product(Ranks).map{|suit, rank| Card.new(rank, suit) } 
hand = deck[0, 6] 
def cut(hand, *at) #returns a new hand, multi-cutted 
    indices = (at.map{|i| i-1}+[0, hand.size]).sort 
    res = indices.each_cons(2).map{|i1,i2| hand[i1..i2-1] } 
    res.reverse.flatten 
end 
p cut(hand, 3, 5) 

出力:

[#<struct Card rank="Ace", suit="Clubs">, #<struct Card rank="2", suit="Clubs">, 
    #<struct Card rank="3", suit="Clubs">, #<struct Card rank="4", suit="Clubs">, 
    #<struct Card rank="5", suit="Clubs">, #<struct Card rank="6", suit="Clubs">] 

    [#<struct Card rank="5", suit="Clubs">, #<struct Card rank="6", suit="Clubs">, 
    #<struct Card rank="3", suit="Clubs">, #<struct Card rank="4", suit="Clubs">, 
    #<struct Card rank="Ace", suit="Clubs">, #<struct Card rank="2", suit="Clubs">] 
関連する問題