Permalink
Browse files

moving note logic into note class

  • Loading branch information...
1 parent a1bf073 commit 6300fe19818e8440158d0fc11b02ad82c9516499 @ryanb committed Sep 4, 2010
Showing with 55 additions and 11 deletions.
  1. +4 −0 Maestro.xcodeproj/project.pbxproj
  2. +6 −10 game.rb
  3. +1 −1 main_view.rb
  4. +44 −0 note.rb
@@ -50,6 +50,7 @@
8D11072D0486CEB800E47090 /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 29B97316FDCFA39411CA2CEA /* main.m */; settings = {ATTRIBUTES = (); }; };
8D11072F0486CEB800E47090 /* Cocoa.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 1058C7A1FEA54F0111CA2CBB /* Cocoa.framework */; };
DC3DEDB41232C37000B1A3D0 /* game.rb in Resources */ = {isa = PBXBuildFile; fileRef = DC3DEDB31232C37000B1A3D0 /* game.rb */; };
+ DC3DEDDD1232E63B00B1A3D0 /* note.rb in Resources */ = {isa = PBXBuildFile; fileRef = DC3DEDDC1232E63B00B1A3D0 /* note.rb */; };
DC5285C71183C62700A4D471 /* main_controller.rb in Resources */ = {isa = PBXBuildFile; fileRef = DC5285C61183C62700A4D471 /* main_controller.rb */; };
DC5285D01183DA7F00A4D471 /* CoreMIDI.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = DC5285CF1183DA7F00A4D471 /* CoreMIDI.framework */; };
DC5286001183E32100A4D471 /* MIDIUtility.m in Sources */ = {isa = PBXBuildFile; fileRef = DC5285D91183DAF600A4D471 /* MIDIUtility.m */; };
@@ -83,6 +84,7 @@
8D1107310486CEB800E47090 /* Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
8D1107320486CEB800E47090 /* Maestro.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = Maestro.app; sourceTree = BUILT_PRODUCTS_DIR; };
DC3DEDB31232C37000B1A3D0 /* game.rb */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.script.ruby; path = game.rb; sourceTree = "<group>"; };
+ DC3DEDDC1232E63B00B1A3D0 /* note.rb */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.script.ruby; path = note.rb; sourceTree = "<group>"; };
DC5285C61183C62700A4D471 /* main_controller.rb */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.script.ruby; path = main_controller.rb; sourceTree = "<group>"; };
DC5285CF1183DA7F00A4D471 /* CoreMIDI.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CoreMIDI.framework; path = System/Library/Frameworks/CoreMIDI.framework; sourceTree = SDKROOT; };
DC5285D81183DAF600A4D471 /* MIDIUtility.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MIDIUtility.h; sourceTree = "<group>"; };
@@ -110,6 +112,7 @@
DC5285C61183C62700A4D471 /* main_controller.rb */,
DC52869A1183ED3300A4D471 /* main_view.rb */,
DC3DEDB31232C37000B1A3D0 /* game.rb */,
+ DC3DEDDC1232E63B00B1A3D0 /* note.rb */,
);
name = Classes;
sourceTree = "<group>";
@@ -255,6 +258,7 @@
DC5285C71183C62700A4D471 /* main_controller.rb in Resources */,
DC52869B1183ED3300A4D471 /* main_view.rb in Resources */,
DC3DEDB41232C37000B1A3D0 /* game.rb in Resources */,
+ DC3DEDDD1232E63B00B1A3D0 /* note.rb in Resources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
View
@@ -1,21 +1,17 @@
class Game
- attr_reader :goal
+ attr_reader :target_note
def initialize
- new_goal
+ new_target
end
- def new_goal
- @goal = notes[rand(notes.size)]
- end
-
- def notes
- %w[C C# D Eb E F F# G G# A Bb B]
+ def new_target
+ @target_note = Note.random
end
def key_down(key)
- if notes[key % 12] == @goal
- new_goal
+ if @target_note.equal_tone?(key)
+ new_target
end
end
View
@@ -5,7 +5,7 @@ def drawRect(rect)
NSColor.whiteColor.set
NSRectFill(rect)
if @game
- draw_title("Play a #{@game.goal}")
+ draw_title("Play #{@game.target_note.sharp_tone}")
else
draw_title("Loading...")
end
View
@@ -0,0 +1,44 @@
+class Note
+ attr_reader :value
+
+ def self.value_from_tone(tone)
+ (sharp_tones.index(tone) || flat_tones.index(tone)) + 60 # default to 4th octive
+ end
+
+ def self.sharp_tones
+ %w[C C# D D# E F F# G G# A A# B]
+ end
+
+ def self.flat_tones
+ %w[C Db D Eb E F Gb G Ab A Bb B]
+ end
+
+ def self.random
+ new(rand(12))
+ end
+
+ def initialize(value)
+ if value.kind_of? String
+ @value = self.class.value_from_tone(value)
+ else
+ @value = value
+ end
+ end
+
+ def equal_tone?(note)
+ note = Note.new(note) unless note.kind_of? Note
+ note.tone_value == tone_value
+ end
+
+ def tone_value
+ @value % 12
+ end
+
+ def sharp_tone
+ self.class.sharp_tones[tone_value]
+ end
+
+ def flat_tone
+ self.class.flat_tones[tone_value]
+ end
+end

0 comments on commit 6300fe1

Please sign in to comment.