Class: Quiz::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/quiz/cli.rb

Constant Summary collapse

@@points =
[]

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCLI

Returns a new instance of CLI.



4
5
# File 'lib/quiz/cli.rb', line 4

def initialize
end

Class Method Details

.clearObject



153
154
155
# File 'lib/quiz/cli.rb', line 153

def self.clear
    @@points.clear
end

.pointsObject



149
150
151
# File 'lib/quiz/cli.rb', line 149

def self.points
    @@points
end

Instance Method Details

#all_question(team, year) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/quiz/cli.rb', line 95

def all_question(team, year)
    ["Who won the world cup in #{year}?",
     "Who was the host in #{year} world cup?",
     "#{team} won the World cup final in #{year}. Who was the runner-up?",
     "How many world cup #{team} has won?", 
     "Who won the champion league in #{year}?",
     "#{team} won the Champion league final in #{year}. Who was the runner-up?",
     "Who hosted champion league final in #{year}?",
     "Who won the balon d'Or in #{year}?",
     "How many balon de'or #{team} has won?",
     "How many Champion league #{team} has won?"         
    ]
end

#callObject



6
7
8
9
# File 'lib/quiz/cli.rb', line 6

def call
    puts "Welcome to football soccer quiz." 
    start_quiz
end

#multiple_choice(answer, winners) ⇒ Object



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
# File 'lib/quiz/cli.rb', line 29

def multiple_choice(answer,winners)
    multiple_choice = []
    multiple_choice << answer
    until multiple_choice.size == 4   
        multiple_choice << winners.sample
        multiple_choice = multiple_choice.uniq
    end
    multiple_choice = multiple_choice.shuffle
    multiple_choice.each_with_index{|item,index| puts "#{index + 1}: #{item}"}
    input = ""
    loop do
        input = gets  
        if !(1..4).include?(input.to_i)
             puts "Invalid choice, enter a number from 1 to 4"
        else break    
        end         
    end
    if multiple_choice.index(answer) == input.to_i - 1 
        @@points << 1
        puts "Right"
    else
        puts "Wrong. Right answer is #{multiple_choice.index(answer) + 1}: #{answer}." 
    end
    puts "Press enter to continue"
    gets
end

#question_processorObject



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/quiz/cli.rb', line 110

def question_processor
    Quiz::CHAMPIONLEAGUE.champion_league_files(Quiz::Scraper.champion_league)
    Quiz::Worldcup.world_cup_file(Quiz::Scraper.world_cup)
    Quiz::BALONDOR.balon_d_or(Quiz::Scraper.balon_d_or_players)
    champion_league = Quiz::CHAMPIONLEAGUE.all
    world_cup = Quiz::Worldcup.all
    balon_d_or = Quiz::BALONDOR.all
    last_20_champions = []
    counter = 44
    while counter < champion_league.size
       last_20_champions << champion_league[counter]
       counter += 1
    end
    question_selector(world_cup,0)
    question_selector(world_cup,1)
    question_selector(world_cup,2)
    question_selector(world_cup,3) 
    question_selector(last_20_champions,4) 
    question_selector(last_20_champions,5)
    question_selector(last_20_champions,6)
    question_selector(balon_d_or,7)                
    question_selector(balon_d_or,8)              
    question_selector(champion_league,9)  
    points = 0
    @@points.each{|i|points += i }
    if points > 5
        puts "Congratulation you score #{points} of 10 points."
    else puts "Sorry you failed. you score #{points} of 10 points."
    end
    Quiz::CLI.clear
    Quiz::CHAMPIONLEAGUE.all.clear
    Quiz::Worldcup.all.clear
    Quiz::BALONDOR.all.clear

end

#question_selector(objects, question) ⇒ Object



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
# File 'lib/quiz/cli.rb', line 56

def question_selector(objects,question) 
    years = objects.map{|item| item.year} 
    winners = objects.map{|item|item.winner}
    hosts = objects.map{|item|item.host}
    runner_ups = objects.map{|item|item.runner_up}
    random_year = years.sample
    random_winner = winners.sample

    instace_for_answers = objects.select{|item|item.year == random_year}
    
    answer_for_how_many = objects.select{|item|item.winner == random_winner}.size
    answer_for_how_many
    winner = instace_for_answers[0].winner
    
    host = instace_for_answers[0].host
    runner_up = instace_for_answers[0].runner_up
    
    if all_question("none",random_year)[question].include?("host")
        puts all_question(host,random_year)[question]
        multiple_choice(host,hosts)
    elsif all_question("none",random_year)[question].include?("runner-up")
        runner_ups = runner_ups.reject{|item| item == winner}
        puts all_question(winner,random_year)[question]
        multiple_choice(runner_up,runner_ups)
   elsif all_question("none",random_year)[question].include?("How many")
        puts all_question(random_winner,'none')[question]
        obtions = []
        counter = 1
        while obtions.size < 13
            obtions << counter
            counter += 1
        end
    multiple_choice(answer_for_how_many,obtions)
    else 
        puts all_question(winner,random_year)[question]
        multiple_choice(winner,winners)
    end
end

#start_quizObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/quiz/cli.rb', line 11

def start_quiz
    puts "*************************************************************************"
    puts "Take this 10 question quiz to find out how much you know about soccer."
    puts "*************************************************************************"
    puts "You pass the test if you score 6 or more points."
    puts "*************************************************************************"
    puts "To take the quiz press enter, to exit press 1 and enter."
    input = gets


    while input.to_i != 1
        question_processor
        puts "To take the quiz one more time press enter. to exit press 1 and enter."
        input = gets   
    end
    puts "Thanks for playing Soccer quiz."
end