-
Notifications
You must be signed in to change notification settings - Fork 0
/
flashcards.rb
74 lines (62 loc) · 1.15 KB
/
flashcards.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
class Card
attr_reader :front, :back
def initialize(front, back)
@front = front
@back = back
end
def play
puts "Guess the answer to #{front}:"
correct_guess = false
3.times do
print "> "
guess = gets.strip
if correct_guess?(guess)
correct_guess = true
break
end
end
if correct_guess
puts "You are correct!"
else
puts "Sorry, it was actually #{back}"
end
end
def correct_guess?(guess)
guess.downcase == @back.downcase
end
end
class Deck
attr_reader :cards
def initialize(filename)
@cards = []
file = File.open(filename, "r")
card_count = file.gets.to_i
card_count.times do
front = file.gets.strip
back = file.gets.strip
@cards << Card.new(front, back)
end
end
def play
cards.each do |card|
card.play
end
end
end
class Game
def initialize(deck)
@deck = deck
end
def play
print_welcome_message
@deck.play
end
private
def print_welcome_message
puts "Welcome to the flashcard app!"
puts "-" * 28
end
end
deck = Deck.new("decks")
game = Game.new(deck)
game.play