Permalink
Browse files

play 10 chords then show how many are correct

  • Loading branch information...
1 parent 9fcade2 commit e721965257bf1af033fa39c1d91730f1a9976908 @ryanb committed Jan 25, 2011
Showing with 27 additions and 6 deletions.
  1. +19 −3 game.rb
  2. +8 −3 main_view.rb
View
22 game.rb
@@ -1,8 +1,9 @@
class Game
- attr_reader :chord, :played_notes
+ attr_reader :chord, :played_notes, :results
def initialize
@played_notes = []
+ @results = []
new_chord
end
@@ -12,12 +13,27 @@ def new_chord
def key_down(key)
@played_notes << Note.new(key)
- if @chord.notes.all? { |n| @played_notes.any? { |o| o.equal_tone?(n) } } && @played_notes.all? { |n| @chord.notes.any? { |o| o.equal_tone?(n) } }
- new_chord
+ if done?
+ if @played_notes.all? { |note| note.equal_tone? Note.new("D") }
+ @results = []
+ end
+ else
+ if @played_notes.size >= 3
+ @results << correct?
+ new_chord unless done?
+ end
end
end
def key_up(key)
@played_notes.delete_if { |n| n.equal_tone?(key) }
end
+
+ def correct?
+ @chord.notes.all? { |n| @played_notes.any? { |o| o.equal_tone?(n) } } && @played_notes.all? { |n| @chord.notes.any? { |o| o.equal_tone?(n) } }
+ end
+
+ def done?
+ @results.size >= 10
+ end
end
View
@@ -5,9 +5,14 @@ def drawRect(rect)
NSColor.whiteColor.set
NSRectFill(rect)
if @game
- draw_title("Play #{@game.chord.root_note.sharp_tone} Chord", 20)
- draw_title(@game.chord.notes.map(&:sharp_tone).join(" "), -5, 16)
- draw_title(@game.played_notes.map(&:sharp_tone).join(" "), -30, 16)
+ if @game.done?
+ draw_title("Got #{@game.results.select { |a| a }.size} out of #{@game.results.size} correct", 20)
+ draw_title("Press D to play again", -5, 16)
+ else
+ draw_title("Play #{@game.chord.root_note.sharp_tone} Chord", 20)
+ draw_title(@game.chord.notes.map(&:sharp_tone).join(" "), -5, 16)
+ draw_title(@game.played_notes.map(&:sharp_tone).join(" "), -30, 16)
+ end
else
draw_title("Loading...")
end

0 comments on commit e721965

Please sign in to comment.