Class: Truco

Inherits:
Game show all
Defined in:
lib/truco.rb

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

#dealerObject (readonly)

Returns the value of attribute dealer.



51
52
53
# File 'lib/truco.rb', line 51

def dealer
  @dealer
end

#first_handObject (readonly)

Returns the value of attribute first_hand.



51
52
53
# File 'lib/truco.rb', line 51

def first_hand
  @first_hand
end

#playersObject (readonly)

Returns the value of attribute players.



51
52
53
# File 'lib/truco.rb', line 51

def players
  @players
end

#second_handObject (readonly)

Returns the value of attribute second_hand.



51
52
53
# File 'lib/truco.rb', line 51

def second_hand
  @second_hand
end

#third_handObject (readonly)

Returns the value of attribute third_hand.



51
52
53
# File 'lib/truco.rb', line 51

def third_hand
  @third_hand
end

#viraObject (readonly)

Returns the value of attribute vira.



51
52
53
# File 'lib/truco.rb', line 51

def vira
  @vira
end

Instance Method Details

#deal_to_playersObject



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_winnerObject



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_handObject



138
139
140
# File 'lib/truco.rb', line 138

def first_turn_for_second_hand
  first_hand_winner
end

#first_turn_for_third_handObject



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

#playObject

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
  # 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) ⇒ 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_winnerObject



86
87
88
# File 'lib/truco.rb', line 86

def second_hand_winner
  @second_hand_winner ||= winner_of_hand(@second_hand)
end

#third_hand_winnerObject



90
91
92
# File 'lib/truco.rb', line 90

def third_hand_winner
  @third_hand_winner ||= winner_of_hand(@third_hand)
end

#to_sObject



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

#winnerObject



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