Class: Truco
Instance Attribute Summary collapse
Attributes inherited from Game
#player_turn
Instance Method Summary
collapse
Methods inherited from Game
#ask_player_for_card, #deal_to_everybody, #hand
Constructor Details
#initialize(players) ⇒ Truco
Returns a new instance of Truco.
53
54
55
56
57
58
59
60
61
|
# 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
|
Instance Attribute Details
#dealer ⇒ Object
Returns the value of attribute dealer.
51
52
53
|
# File 'lib/truco.rb', line 51
def dealer
@dealer
end
|
#first_hand ⇒ Object
Returns the value of attribute first_hand.
51
52
53
|
# File 'lib/truco.rb', line 51
def first_hand
@first_hand
end
|
#players ⇒ Object
Returns the value of attribute players.
51
52
53
|
# File 'lib/truco.rb', line 51
def players
@players
end
|
#second_hand ⇒ Object
Returns the value of attribute second_hand.
51
52
53
|
# File 'lib/truco.rb', line 51
def second_hand
@second_hand
end
|
#third_hand ⇒ Object
Returns the value of attribute third_hand.
51
52
53
|
# File 'lib/truco.rb', line 51
def third_hand
@third_hand
end
|
#vira ⇒ Object
Returns the value of attribute vira.
51
52
53
|
# File 'lib/truco.rb', line 51
def vira
@vira
end
|
Instance Method Details
#deal_to_players ⇒ Object
63
64
65
|
# File 'lib/truco.rb', line 63
def deal_to_players
3.times { players.each { |p| dealer.deal_to_player p } }
end
|
#first_hand_winner ⇒ Object
82
83
84
|
# File 'lib/truco.rb', line 82
def first_hand_winner
@first_hand_winner ||= winner_of_hand(@first_hand)
end
|
#first_turn_for_second_hand ⇒ Object
138
139
140
|
# File 'lib/truco.rb', line 138
def first_turn_for_second_hand
first_hand_winner
end
|
#first_turn_for_third_hand ⇒ Object
142
143
144
|
# File 'lib/truco.rb', line 142
def first_turn_for_third_hand
second_hand_winner
end
|
#other_player(p) ⇒ Object
146
147
148
|
# File 'lib/truco.rb', line 146
def other_player(p)
players.clone.reject{ |player| player == p}[0]
end
|
#play ⇒ Object
TODO: Logic for: Envido, truco, etc. AKA Apostar.
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
|
# File 'lib/truco.rb', line 108
def play
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}"
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?
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) ⇒ Object
67
68
69
70
|
# 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) ⇒ Object
72
73
74
75
|
# 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) ⇒ Object
77
78
79
80
|
# File 'lib/truco.rb', line 77
def play_third_hand(player, card)
player.delete_card(card)
@third_hand[player] = card
end
|
#second_hand_winner ⇒ Object
86
87
88
|
# File 'lib/truco.rb', line 86
def second_hand_winner
@second_hand_winner ||= winner_of_hand(@second_hand)
end
|
#third_hand_winner ⇒ Object
90
91
92
|
# File 'lib/truco.rb', line 90
def third_hand_winner
@third_hand_winner ||= winner_of_hand(@third_hand)
end
|
#to_s ⇒ Object
129
130
131
132
133
134
135
136
|
# 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 ⇒ Object
102
103
104
|
# 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) ⇒ Object
94
95
96
97
98
99
100
|
# 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
|