-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.rb
102 lines (83 loc) · 2.32 KB
/
game.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
require_relative 'hand'
require_relative 'deck'
require_relative 'card'
require_relative 'player'
require 'byebug'
class Game
attr_reader :deck, :current_player, :other_player
def initialize
@deck = Deck.new
@current_player = Player.new("John", Hand.new(deck))
@other_player = Player.new("Bob", Hand.new(deck))
end
def play
until current_player.hand.cards.empty? || other_player.hand.cards.empty?
card_choice = ask_choice
if other_player.hand.has_card?(card_choice)
give_card(card_choice)
handle_books(new_card)
else
doesnt_have_card(card_choice)
current_player.go_fish(deck)
handle_books(new_card)
switch_players
end
end
if winner.is_a?(Player)
puts "Winner is #{winner.name} with #{winner.book_count} books!"
else
puts "We have a good old fashion tie"
end
end
def winner
case current_player.book_count <=> other_player.book_count
when 1
current_player
when -1
other_player
end
end
def give_card(card_choice)
given_cards = other_player.hand.give_cards(card_choice)
current_player.hand.recieve_cards(given_cards)
puts "Other player has given you #{card_choice}(s)"
end
def doesnt_have_card(card_choice)
puts "#{other_player.name} did not have #{card_choice}"
puts "Go Fish"
end
def ask_choice
puts "#{current_player.name} you have #{current_player.book_count} book(s) "
puts "#{current_player.name} your cards are #{current_player.display_hand}"
begin
puts "What card would you like?"
choice = gets.chomp
validates_choice(choice)
rescue
puts "You can only ask for a card that you have"
retry
end
choice
end
def validates_choice(choice)
hand_values = current_player.hand.cards.map(&:value)
unless hand_values.include?(choice)
raise "Invalid" if card.value != choice
end
end
def handle_books(card_choice)
if current_player.hand.has_book?
current_player.book_count += 1
current_player.hand.drop_book
puts "#{current_player.name} has dropped his book of #{card_choice.value}s "
end
end
def switch_players
@current_player, @other_player = @other_player, @current_player
end
private
def new_card
current_player.hand.cards.last
end
end
Game.new.play