Skip to content

Instantly share code, notes, and snippets.

@kmandreza
Created February 7, 2013 22:02
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save kmandreza/4734649 to your computer and use it in GitHub Desktop.
Save kmandreza/4734649 to your computer and use it in GitHub Desktop.
Ruby Flashcards 1: Single Deck Let's build a simple flashcard game in Ruby with a command-line interface. Here is an example of one possible implementation: $ ruby flashcards.rb Welcome to Ruby Flash Cards. To play, just enter the correct term for each definition. Ready? Go! Definition A file format in which values are delimited by commas. Guess…
class Flashcard #data structure
attr_accessor :term, :definition
def initialize(term,definition)
@term=term
@definition=definition
end
end
class Text_File_Parser
attr_accessor :flashcards_box
def initialize(filename)
@flashcards_box = []
File.readlines(filename).each_slice(2) do |arr|
@flashcards_box << Flashcard.new(arr[1], arr[0])
end
#enter code that parses text file into FlashC
#@flashcards_box << Flashcard.new
end
end
# flashy = Text_File_Parser.new("flashcards.txt")
# puts flashy.flashcards_box.sample
class Game
attr_accessor :game_card
def initialize
@game_card = @flashcards_box.sample #2. one card will be randomly picked from the libary
puts @game_card[0] #3. definition is shown to user, term is not.
end
def guess
loop do
if input != @game_card[1]
puts "Incorrect! Try Again"
else input == @game_card[1]
puts "Correct!" #4. If user guesses correct term, user is told "Correct!"
@game_card.delete
end
input = gets #when user enters an input, it calls gets, and assigns the string to variable input
end
end
def delete
@flashcards_box.pop # behaviour delete from data structure
end
def game_over
input = gets
until input == "\n"
if @flashcards_box == 0 #Once card library is empty, user is told
puts "Game Over! Enter 1 to Start Over or Enter to exit game."
else input == 1 #how do I have the user enter 1?
#start new game
end
input = gets
end
end
end
splashy = Game.new
puts splashy
=begin
class Game #contains game behavior
end
=begin
card is deleted from library.
5. Else user presses enter twice and will be given the option
a. to skip; next random card is presented
b. or view term; term is shown to user, card is deleted
from library
6. step 2-5 is repeated, with reduced library
7. Once card library is empty, user is told "Game Over! Enter 1
to Start Over or Enter to exit game.""
=end
=begin
learning goals: 1.how and when to use separate commas
2. create interactive command-line applications where
the data structure can be accessed and modified << ???
3. effectively manage a multiple-file application
=end
=begin
Driver Code
puts "Welcome to Ruby Flash Cards. To play, just enter the correct term
for each definition. Ready? Go!"
=end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment