-
Notifications
You must be signed in to change notification settings - Fork 0
/
squares.rb
103 lines (91 loc) · 2.82 KB
/
squares.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
103
require 'csv'
class Squares
attr_accessor :teams, :players_list
def initialize
@players_list = []
@teams = team_input
entry_type_input
squares
end
def count_error(previous_method = nil)
puts 'Wrong number of squares! Starting over.'
Squares.method_defined?(previous_method) ? public_send(previous_method) : entry_type_input
end
def team_input
puts 'Who is the AFC contender?'
afc_name = gets.strip
puts 'Who is the NFC contender?'
nfc_name = gets.strip
{ afc: afc_name, nfc: nfc_name }
end
def entry_type_input
puts 'Manual Entry or CSV? (type M or CSV)'
input = gets.strip
case input
when /M(ANUAL)?/i
@players_list = player_input
when /CS?V?/i
@players_list = import_csv('./players.csv')
else
puts 'Type either M or CSV'
end
end
# @params: players_list, {"name" => # of squares}
# returns the list of players list if there are 100 squares
# returns false if number of squares != 100
def players_check(players_list = {"name": -1}, previous_method = "entry_type_input")
# sum all values in players_list
puts "Checking player counts..."
players_list.values.inject(:+) == 100 ? players_list : count_error(previous_method)
end
# returns a hash of players or starts over if there's not enough squares
def player_input(players_list = {}, total_squares = 0)
puts 'Enter Player Name and Number of Squares'
loop do
puts "Total Squares so far: #{total_squares}"
puts 'Player Name:'
name = gets.strip
puts 'Number of Squares:'
squares = gets.strip.to_i
total_squares += squares
if squares <= 0
puts 'You need at least one square!'
else
players_list[name] = squares
break if total_squares >= 100
end
end
players_check(players_list, "player_input")
end
# @params: path: path to csv file
# returns a hash of players and their squares, or makes them start over if there's not enough squares
def import_csv(path)
players_list = {}
puts "Importing #{path}..."
CSV.read(path, headers: true).each do |row|
players_list[row['name']] = row['sqs'].to_i
end
players_check(players_list, "entry_type_input")
end
def print_csv(squares_array, teams = {afc: "AFC", nfc: "NFC"})
puts 'Printing Squares...'
nums = Array.new([*0..9])
CSV.open('squares.csv', 'w') do |csv|
csv << ["Across: #{teams[:afc]}\nDown: #{teams[:nfc]}"] + nums
count = 0
squares_array.each do |list|
csv << list.unshift(nums[count])
count += 1
end
end
end
def squares(squares_list = [])
@players_list.each do |k,v|
squares_list += Array.new(v, k)
end
squares_list.shuffle!
squares_array = squares_list.each_slice(10).to_a
print_csv(squares_array, @teams)
end
end
Squares.new