-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.rb
92 lines (79 loc) · 2.31 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
module Guesser
class Game
include MessagesDictionary
attr_reader :options, :output
attr_accessor :winner, :players
def initialize(output, argv)
@output, @options, @players = output, GameOptions.new(argv), []
self.class.class_exec do
has_messages_dictionary file: 'info.yml', dir: 'lib/guesser/messages',
output: output
end
end
def start
welcome
initialize_players
play
show_winner
print_winner_to_file
end
private
def play
until winner do
players.each do |player|
sleep 1
pretty_output 'players.turn', name: player.name
player.generate_number_to_guess!(options.limit)
player.waiting_times << measure_time do
pretty_output 'guesses.enter'
if player.guessed?
pretty_output 'guesses.guessed'
player.guessed!
else
pretty_output 'guesses.wrong'
end
end
if player.won?(self)
self.winner = player
break
end
end
show_players_statistics
end
end
def welcome
pretty_output 'welcome.intro'
pretty_output 'welcome.rules', limit: options.limit - 1
pretty_output 'welcome.rules2', points: options.points
end
def initialize_players
pretty_output 'players.count', players_count: options.players
options.players.times do |i|
pretty_output 'players.enter_name', num: i + 1
players << Player.new($stdin.gets.strip)
end
end
def show_players_statistics
players.each { |p| output.puts p.show_player_statistics }
end
def show_winner
pretty_output 'winner.announce'
pretty_output 'winner.name', name: winner.name
pretty_output 'winner.outro'
end
def print_winner_to_file
writer = FileManager::Writer.new('game-')
writer << [pretty_output('winner.won_at', name: winner.name, time: Time.now) {|msg| msg},
winner.show_player_statistics]
end
def measure_time
if block_given?
time_before = Time.now
yield
waited_for = Time.now - time_before
pretty_output 'guesses.time_passed', time: waited_for
waited_for
end
end
end
end