class Truco
Attributes
dealer[R]
first_hand[R]
players[R]
second_hand[R]
third_hand[R]
vira[R]
Public Class Methods
new(players)
click to toggle source
# File lib/truco.rb, line 53 def initialize(players) @players = players @dealer = Dealer.new @vira = Vira.new @dealer.deal_to_player(vira) @first_hand = Hash.new @second_hand = Hash.new @third_hand = Hash.new end
Public Instance Methods
deal_to_players()
click to toggle source
# File lib/truco.rb, line 63 def deal_to_players 3.times { players.each { |p| dealer.deal_to_player p } } end
first_hand_winner()
click to toggle source
# File lib/truco.rb, line 82 def first_hand_winner @first_hand_winner ||= winner_of_hand(@first_hand) end
first_turn_for_second_hand()
click to toggle source
# File lib/truco.rb, line 138 def first_turn_for_second_hand first_hand_winner end
first_turn_for_third_hand()
click to toggle source
# File lib/truco.rb, line 142 def first_turn_for_third_hand second_hand_winner end
other_player(p)
click to toggle source
# File lib/truco.rb, line 146 def other_player(p) players.clone.reject{ |player| player == p}[0] end
play()
click to toggle source
TODO: Logic for: Envido, truco, etc. AKA Apostar.
# File lib/truco.rb, line 108 def play # playing the first hand players.each { |player| ask_for_user_input(player, "play_first_hand") } self.to_s puts " the winner of the first hand #{first_hand_winner.nickname}" #playing the second hand ask_for_user_input(first_hand_winner, "play_second_hand") ask_for_user_input(other_player(first_hand_winner), "play_second_hand") self.to_s puts " the winner of the second hand #{second_hand_winner.nickname}" return if player_won_first_two_hands? #playing the third hand ask_for_user_input(second_hand_winner, "play_third_hand") ask_for_user_input(other_player(second_hand_winner), "play_third_hand") self.to_s end
play_first_hand(player, card)
click to toggle source
# File lib/truco.rb, line 67 def play_first_hand(player, card) player.delete_card(card) @first_hand[player] = card end
play_second_hand(player, card)
click to toggle source
# File lib/truco.rb, line 72 def play_second_hand(player, card) player.delete_card(card) @second_hand[player] = card end
play_third_hand(player, card)
click to toggle source
# File lib/truco.rb, line 77 def play_third_hand(player, card) player.delete_card(card) @third_hand[player] = card end
second_hand_winner()
click to toggle source
# File lib/truco.rb, line 86 def second_hand_winner @second_hand_winner ||= winner_of_hand(@second_hand) end
third_hand_winner()
click to toggle source
# File lib/truco.rb, line 90 def third_hand_winner @third_hand_winner ||= winner_of_hand(@third_hand) end
to_s()
click to toggle source
# File lib/truco.rb, line 129 def to_s puts "First Hand:" display_hand(@first_hand) puts "Second Hand:" display_hand(@second_hand) puts "Third Hand:" display_hand(@third_hand) end
winner()
click to toggle source
# File lib/truco.rb, line 102 def winner player_won_first_two_hands? ? first_hand_winner : player_that_won_most_matches end
winner_of_hand(hand)
click to toggle source
# File lib/truco.rb, line 94 def winner_of_hand(hand) cards = @players.map{|p| hand[p] } judge = Judge.new(cards) judge.vira = vira.card winning_card = judge.winner (winning_card == hand[@players[0]]) ? @players[0] : @players[1] end
Private Instance Methods
ask_for_user_input(player, method)
click to toggle source
# File lib/truco.rb, line 168 def ask_for_user_input(player, method) puts "La vira: #{vira.card.to_s}" puts "What are you going to Play #{player.nickname}?" puts player.display_posible_moves p = gets card = player.play p.to_i self.send(method, player, card) 100.times{ puts nil } end
display_hand(hand)
click to toggle source
# File lib/truco.rb, line 152 def display_hand(hand) return if hand == {} puts "#" * 20 hand.each do |k,v| puts "#{k.nickname}: #{v.to_s}" end end
player_that_won_most_matches()
click to toggle source
# File lib/truco.rb, line 160 def player_that_won_most_matches [ first_hand_winner, second_hand_winner, third_hand_winner].group_by { |d| d }.keys.first end
player_won_first_two_hands?()
click to toggle source
# File lib/truco.rb, line 164 def player_won_first_two_hands? first_hand_winner == second_hand_winner end