-
Notifications
You must be signed in to change notification settings - Fork 0
/
gofish_game.rb
58 lines (52 loc) · 1.11 KB
/
gofish_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
class GoFishGame < Game
attr_accessor :deck, :players, :current_player, :ui
def initialize(names)
@deck = DeckOfCards.new
@players = names.collect {|name| GF_Player.new(name, self)}
end
def deal
@deck.shuffle
@players.each { |player| 5.times {player.hand << @deck.draw} }
end
def end?
scores = @players.collect{|p| p.hand.size}
if scores.min == 0 then
ui.game_end(1)
return true
elsif @deck.number_of_cards == 0 then
ui.game_end(2)
return true
else
return false
end
end
def score
win = @players.collect{|p| p.books.size}
winner = win.find_index(win.max)
if win.count{|s| s==win.max} > 1 then
ui.winner(@players.select{|p| p.books.size == win.max})
else
ui.winner(@players[winner])
end
return @players[winner]
end
def play_full_game
deal
last = @players[0].take_turn_backup
while end? == false do
last = @players[@players.find_index(last)].take_turn_backup
end
winner = score
end
def start
@current_player = players.sample
end
def play
until end? do
@current_player = @current_player.take_turn
end
end
def end
winner = score
end
end