diff --git a/.gitignore b/.gitignore index 18d7cf1..7af12fb 100644 --- a/.gitignore +++ b/.gitignore @@ -11,6 +11,7 @@ spec/reports test/tmp test/version_tmp tmp +.idea *.*~ *~ diff --git a/week1/exercises/rspec_spec.rb b/week1/exercises/rspec_spec.rb index f4c2f0b..b1c20ae 100644 --- a/week1/exercises/rspec_spec.rb +++ b/week1/exercises/rspec_spec.rb @@ -43,11 +43,13 @@ # When this example fails, # it will show "expected" as 2, and "actual" as 1 - 1.should eq 2 + 1.should eq 1 end - it "supports placeholder examples that lack code (like this one)" + it "supports placeholder examples that lack code (like this one)" do + "true".should eq "true" + end it "requires that examples use expectations (like #should) to work properly" do diff --git a/week1/homework/questions.txt b/week1/homework/questions.txt index 0adfd69..035af7d 100644 --- a/week1/homework/questions.txt +++ b/week1/homework/questions.txt @@ -3,22 +3,22 @@ Chapter 3 Classes, Objects, and Variables p.90-94 Strings 1. What is an object? -An object is a representation in memory of a specific concept or thing that the Ruby interpreter knows about. + Anything that is manipulated in Ruby 2. What is a variable? -A variable is a name for a location in memory. It can contain, or point to, any type of object. + A reference to an object 3. What is the difference between an object and a class? -An object is an instance of a class, or a specific thing of that class's type in memory. The class is the specifics that are common to all things of that type. The classification of a concept or a thing is a class. A specific thing or concept of a class's type in memory is an object. For example: All books have titles (Class). This book's title is "Harry Potter and the Goblet of Fire" (Object). + classes create all objects 4. What is a String? -A string is how Ruby understands text. It is a collection of characters (Bytes), and can be created by making an instance of the String class (String.new) or as a string literal ("",'', %Q[]). + It's a sequence of characters 5. What are three messages that I can send to a string object? Hint: think methods -chomp! - removes newline characters, or the specified characters, from the end of a string -strip! - removes leading or trailing whitespace from a string -split - returns an array of strings made up of the original string separated on whitespace or the specified characters or regexp + .chomp + .split + .squeeze 6. What are two ways of defining a String literal? Bonus: What is the difference between the two? -Single quotes ex: '' and Double quotes ex: "". The single qoutes allow for 2 escape characters: \' and \\ . The double qouted string literal allows for many different escaped special characters (like \n is a line break) and allows for string interpolation, or the injection of evaluated Ruby code into the string ex: "Hello #{my_name}". The single qouted string takes up much less memory than a doulbe qouted string with interpolation. Without interpolation, both are about the same. + with single quotes(') or double quotes("). There are many more escape sequences and code substitution with " diff --git a/week1/homework/strings_and_rspec_spec.rb b/week1/homework/strings_and_rspec_spec.rb index 2f188f6..d9df149 100644 --- a/week1/homework/strings_and_rspec_spec.rb +++ b/week1/homework/strings_and_rspec_spec.rb @@ -12,10 +12,10 @@ before(:all) do @my_string = "Renée is a fun teacher. Ruby is a really cool programming language" end - it "should be able to count the charaters" do - @my_string.should have(@my_string.size).characters - end - it "should be able to split on the . charater" do + it "should be able to count the characters" do + @my_string.length.should eq 66 + end + it "should be able to split on the . character" do result = @my_string.split('.') result.should have(2).items end diff --git a/week2/exercises/book.rb b/week2/exercises/book.rb index 50bc054..717834d 100644 --- a/week2/exercises/book.rb +++ b/week2/exercises/book.rb @@ -1,13 +1,10 @@ class Book - - attr_accessor :title, :pages - - def initialize(title, pages) - @title = title - @pages = pages - end - - def page_count - "Page count is #{@pages}" - end + attr_accessor :title + def initialize(title, page_count) + @title = title + @page_count = page_count + end + def page_count + "Page count is " + @page_count.to_s + end end diff --git a/week2/homework/questions.txt b/week2/homework/questions.txt index 4cfc0a3..3376500 100644 --- a/week2/homework/questions.txt +++ b/week2/homework/questions.txt @@ -3,16 +3,16 @@ Containers, Blocks, and Iterators Sharing Functionality: Inheritance, Modules, and Mixins 1. What is the difference between a Hash and an Array? -An array is an ordered list of items that are referenced by their index (order), a hash is a collection of items that can be referenced by a key and have no order. - + And Array is indexed with positive integers and a hash is a key value pair indexed by the key object 2. When would you use an Array over a Hash and vice versa? -When the items have an inherent order I would use an array, when I want to reference the items in my collection by a name or key and their order does not matter I would use a hash. - -3. What is a module? Enumerable is a built in Ruby module, what is it? -A module is a way to group code that you can use across multiple classes. Enumerable is a Ruby module that provides collection functionality; iteration, searching, and sorting. It requires an implementation of the each method. - -4. Can you inherit more than one thing in Ruby? How could you get around this problem? -No, multiple inheritance is not allowed in Ruby. You can include multiple modules if you wanted to mix-in different functionality into your code. Code that is related with a hierarchical nature should be subclassed (inherited). A class can only have 1 direct parent, but can have lots of ancestors. - + You'd want to use arrays when you need to iterate over the integer index. Hashes are better suited for storing and looking objects up with a key +3. What is a module? + A module is a set of code that is separated by a namespace. Useful to avoid naming clashes +Enumerable is a built in Ruby module, what is it? + It's a mixin that defines many methods for iteration +4. Can you inherit more than one thing in Ruby? + No, you can only inherit from one base class +How could you get around this problem? + You can include Mixins 5. What is the difference between a Module and a Class? -A class can be instantiated into an object, a module cannot. A module is code that can be used across many classes. + A module is a namespace and isn't instantiated. It can define classes within it however. A class is instantiated diff --git a/week2/homework/simon_says.rb b/week2/homework/simon_says.rb index fa35ccd..840177c 100644 --- a/week2/homework/simon_says.rb +++ b/week2/homework/simon_says.rb @@ -1,22 +1,22 @@ module SimonSays - def echo(st) - st - end - - def shout(st) - st.upcase - end + def echo(textToEcho) + textToEcho + end - def first_word(st) - st.split.first - end + def shout(textToShout) + textToShout.upcase + end - def start_of_word(st,i) - st[0...i] - end - - def repeat(st, t=2) - return "Go Away!" if t==0 - ([st]*t).join(' ') - end + def repeat(textToRepeat, count=2) + Array.new(count, textToRepeat + " ").inject(:+).chomp(" ") + end + + def start_of_word(word, count) + word[0...count] + end + + def first_word(sentence) + sentence.scan(/[\w']+/)[0] + end end + diff --git a/week3/homework/calculator.rb b/week3/homework/calculator.rb index f8916e3..d1c2425 100644 --- a/week3/homework/calculator.rb +++ b/week3/homework/calculator.rb @@ -1,24 +1,17 @@ class Calculator - def sum(array) - array.inject(0){|sum, x| sum +x} - end + def sum(numberArray) + numberArray.count == 0 ? 0 : numberArray.inject(:+) + end - def multiply(*numbers) - puts numbers.inspect - numbers.flatten.inject(:*) - end + def multiply(*numberArray) + numberArray.flatten().count == 0 ? 0 : numberArray.flatten().inject(:*) + end - def pow(base, p) - #(1...p).to_a.inject(base){|r,v| r *= base} - pow_fac(base, p) - end + def power(baseNumber, exponent) + baseNumber**exponent + end - def fac(n) - #(1..n).to_a.inject(1){|f,v| f *= v} - pow_fac(n) - end -private - def pow_fac(base=nil, p) - (1..p).to_a.inject(1){|f,v| f *= base || v} - end + def factorial(number) + number == 0 ? 1 : Math.gamma(number + 1) + end end diff --git a/week3/homework/calculator_spec.rb b/week3/homework/calculator_spec.rb index 5a418ed..936b7f8 100644 --- a/week3/homework/calculator_spec.rb +++ b/week3/homework/calculator_spec.rb @@ -26,43 +26,39 @@ # Once the above tests pass, # write tests and code for the following: - describe "#multiply" do - it "multiplies two numbers" do - @calculator.multiply(2,2).should eq 4 - end + it "multiplies two numbers" do + @calculator.multiply(2,2).should == 4 + end - it "multiplies an array of numbers" do - @calculator.multiply([2,2]).should eq 4 - end + it "multiplies an array of numbers" do + @calculator.multiply([2,2,6]).should == 24 end it "raises one number to the power of another number" do - p = 1 - 32.times{ p *= 2 } - @calculator.pow(2,32).should eq p + @calculator.power(4,2).should == 16 end # http://en.wikipedia.org/wiki/Factorial describe "#factorial" do it "computes the factorial of 0" do - @calculator.fac(0).should eq 1 + @calculator.factorial(0).should == 1 end + it "computes the factorial of 1" do - @calculator.fac(1).should eq 1 + @calculator.factorial(1).should == 1 end it "computes the factorial of 2" do - @calculator.fac(2).should eq 2 + @calculator.factorial(2).should == 2 end it "computes the factorial of 5" do - @calculator.fac(5).should eq 120 + @calculator.factorial(5).should == 120 end it "computes the factorial of 10" do - @calculator.fac(10).should eq 3628800 + @calculator.factorial(10).should == 3628800 end - end end diff --git a/week3/homework/questions.txt b/week3/homework/questions.txt index 08067b8..fc1712b 100644 --- a/week3/homework/questions.txt +++ b/week3/homework/questions.txt @@ -5,24 +5,12 @@ Please Read: - Chapter 22 The Ruby Language: basic types (symbols), variables and constants 1. What is a symbol? -A symbol is a static name or identifier. - + A symbol is an identifier that doesn't change in memory 2. What is the difference between a symbol and a string? -A string is a collection of characters whereas a symbol is a static identifier. A string is not static no matter what the contents of the string are. So the strings "hello" and "hello" are two different ojects, whereas the symbol :hello and :hello are the exact same object. If you think of 1 as a FixNum or fixed number, you can think of the symbol :hello as the "FixStr" or fixed string :hello. - + Two identical strings will create new instances in memory whereas two symbols will have one 3. What is a block and how do I call a block? -A block is an anonymous function, or some code snipt that you can define and then call at a later time. To call a block you can use the yield keyword. - + A block is a chunk of code between {} or do end. It's called as a argument to a method or yielded inside a method 4. How do I pass a block to a method? What is the method signature? -To pass a block to a method you define the block after the method call with either the curly bracket enclosure {} or the do/end syntax. An example of passing a block to the each method of an array: - -my_array.each {|a| puts a} - -Any method in Ruby can take a block. You can explicitly add a block to a method by putting an ampersand & before the variable name in the method definition. An example of this would be: - -def my_method(&my_block) - my_block.call -end - + It's passed as an argument after the method call: doSomething {puts 'Block'} 5. Where would you use regular expressions? -Regular expressions are used for pattern matching and replacement with strings. An example would be if I wanted to write a syntax checker for some text that checked if each sentance ended with a period, started with a space and then a capital letter. + Many places. It's used for string matching, modification and replacement diff --git a/week4/class_materials/timer.rb b/week4/class_materials/timer.rb index 67b7681..1e48bc2 100644 --- a/week4/class_materials/timer.rb +++ b/week4/class_materials/timer.rb @@ -1,8 +1,8 @@ class Timer - def self.time_code(n=1) - start_time = Time.now - n.times{yield} - end_time = Time.now - (end_time - start_time) / n.to_f - end + def self.time_code(n=1) + start_time = Time.now + n.times{yield} + end_time = Time.now + (end_time - start_time) / n.to_f + end end \ No newline at end of file diff --git a/week4/class_materials/timer_spec.rb b/week4/class_materials/timer_spec.rb index f71414a..ba59ff5 100644 --- a/week4/class_materials/timer_spec.rb +++ b/week4/class_materials/timer_spec.rb @@ -17,16 +17,15 @@ flag.should be_true end - it "should run our code multiple times" do - counter = 0 - result = Timer.time_code(17) {counter += 1} - counter.should equal 17 - end - - it "should give the average time" do - Time.stub(:now).and_return(0,10) - result = Timer.time_code(10) { } - result.should be_within(0.1).of(1) - end + it "should run our code multiple times" do + counter = 0 + result = Timer.time_code(17){counter += 1} + counter.should eq 17 + end + it "should give the average time" do + Time.stub(:now).and_return(0,1) + result = Timer.time_code(10) { } + result.should equal 10 + end end diff --git a/week4/exercises/worker.rb b/week4/exercises/worker.rb index fad4e2f..9161023 100644 --- a/week4/exercises/worker.rb +++ b/week4/exercises/worker.rb @@ -1,5 +1,7 @@ class Worker - def self.work(n=1) - n.times.inject(nil){yield} - end + def self.work(n=1) + result = nil + n.times{result = yield} + result + end end \ No newline at end of file diff --git a/week4/homework/questions.txt b/week4/homework/questions.txt index bc1ab7c..3617124 100644 --- a/week4/homework/questions.txt +++ b/week4/homework/questions.txt @@ -3,7 +3,14 @@ Chapter 10 Basic Input and Output The Rake Gem: http://rake.rubyforge.org/ 1. How does Ruby read files? + It reads files using the objects in the IO class. Specifically the File subclass. (File.open(...)) 2. How would you output "Hello World!" to a file called my_output.txt? + file = File.new("my_output.txt", "w") + file.puts("Hello World!") + file.close 3. What is the Directory class and what is it used for? + The Directory class is used for handling file system directories. creating, deleting, etc... 4. What is an IO object? + An IO object is an object that encompasses all input and output related tasks such as reading from the command line and outputting to a file to name a couple examples. 5. What is rake and what is it used for? What is a rake task? + Rake stands for Ruby Make which is used to do build and deployment tasks. \ No newline at end of file diff --git a/week5/exercises/Rakefile b/week5/exercises/Rakefile index ce332af..8dea588 100644 --- a/week5/exercises/Rakefile +++ b/week5/exercises/Rakefile @@ -2,9 +2,26 @@ task :test do puts "Hello World!" end -task :make_class_dir do - Dir.mkdir("class") +task :names do + getNamesFromFile.each do |name| + puts name + end end -task :output +task :createclassdir do + Dir.mkdir(:RubyClass) unless Dir.exists?(:RubyClass) +end + +task :createnamedir => [:createclassdir] do +end + +def getNamesFromFile(file="names") + store_names = [] + f = File.open("names", "r") + f.each do |name| + store_names << name + end + f.close + return store_names +end diff --git a/week5/exercises/skier.rb b/week5/exercises/skier.rb new file mode 100644 index 0000000..e5ac76c --- /dev/null +++ b/week5/exercises/skier.rb @@ -0,0 +1,11 @@ +class Skier + attr_accessor :events + + def initialize(events=[:gs, :downhill]) + @events = events + end + + def ski(event) + put "This skier is skiing #{event}" + end +end \ No newline at end of file diff --git a/week6/hologem/.rspec b/week6/hologem/.rspec new file mode 100644 index 0000000..b36b4b5 --- /dev/null +++ b/week6/hologem/.rspec @@ -0,0 +1,2 @@ +--color +--format nested \ No newline at end of file diff --git a/week6/hologem/Rakefile b/week6/hologem/Rakefile new file mode 100644 index 0000000..cc1ffb1 --- /dev/null +++ b/week6/hologem/Rakefile @@ -0,0 +1,5 @@ +require 'rspec/core/rake_task' + +RSpec::Core::RakeTask.new('test') + +task :default => :test diff --git a/week6/hologem/bin/hologem b/week6/hologem/bin/hologem new file mode 100644 index 0000000..044c5e3 --- /dev/null +++ b/week6/hologem/bin/hologem @@ -0,0 +1,4 @@ +#!/usr/bin/env ruby + +puts "Hello There!" +puts ARGV[0] \ No newline at end of file diff --git a/week6/hologem/hologem.gemspec b/week6/hologem/hologem.gemspec new file mode 100644 index 0000000..7d54afd --- /dev/null +++ b/week6/hologem/hologem.gemspec @@ -0,0 +1,12 @@ +Gem::Specification.new do |s| + s.name = 'hologem' + s.version = '0.0.2' + s.date = '2012-11-13' + s.summary = "Tutorial Gem" + s.description = "A gem to learn how to make gems" + s.authors = ["Steven Curtiss"] + s.email = 'stevencurtiss@hotmail.com' + s.homepage = 'http://rubygems.org/gems/hologem' + s.files = ["lib/hologem.rb"] + s.executables << "hologem" +end \ No newline at end of file diff --git a/week6/hologem/lib/hologem.rb b/week6/hologem/lib/hologem.rb new file mode 100644 index 0000000..1bd5f23 --- /dev/null +++ b/week6/hologem/lib/hologem.rb @@ -0,0 +1,3 @@ +puts "Hello World!" +class Hello +end \ No newline at end of file diff --git a/week6/hologem/spec/hologem_spec.rb b/week6/hologem/spec/hologem_spec.rb new file mode 100644 index 0000000..12831d5 --- /dev/null +++ b/week6/hologem/spec/hologem_spec.rb @@ -0,0 +1,9 @@ +require 'hologem' + +describe "hologem" do + it "Should make a new Hello" do + h = Hello.new + h.should be_a Hello + end + +end \ No newline at end of file diff --git a/week7/exercises/features/converter.feature b/week7/exercises/converter/features/converter.feature similarity index 100% rename from week7/exercises/features/converter.feature rename to week7/exercises/converter/features/converter.feature diff --git a/week7/exercises/converter/features/step_definitions/converter.rb b/week7/exercises/converter/features/step_definitions/converter.rb new file mode 100644 index 0000000..25849d7 --- /dev/null +++ b/week7/exercises/converter/features/step_definitions/converter.rb @@ -0,0 +1,21 @@ +class Converter + def initialize(value) + @value = value.to_f + end + + def type=(type) + @type = type + end + + def convert + self.send("#{@type}_conversion") + end + + def Celsius_conversion + (@value * (9.0/5.0) + 32.0).round(1) + end + + def Fahrenheit_conversion + ((@value - 32) * (5.0/9.0)).round(1) + end +end \ No newline at end of file diff --git a/week7/exercises/converter/features/step_definitions/converter_steps.rb b/week7/exercises/converter/features/step_definitions/converter_steps.rb new file mode 100644 index 0000000..1dc9ed2 --- /dev/null +++ b/week7/exercises/converter/features/step_definitions/converter_steps.rb @@ -0,0 +1,16 @@ +Given /^I have entered (\d+) into the converter$/ do |arg1| + @converter = Converter.new(arg1) +end + +Given /^I set the type to Fahrenheit$/ do + @converter.type = "Fahrenheit" +end + +When /^I press convert$/ do + @result = @converter.convert +end + +Then /^the result returned should be (\d+)\.(\d+)$/ do |arg1, arg2| + @result.should eq "#{arg1}.#{arg2}".to_f +end + diff --git a/week7/exercises/features/puppy.feature b/week7/exercises/puppy/features/puppy.feature similarity index 100% rename from week7/exercises/features/puppy.feature rename to week7/exercises/puppy/features/puppy.feature diff --git a/week7/exercises/puppy/features/step_definitions/puppy.rb b/week7/exercises/puppy/features/step_definitions/puppy.rb new file mode 100644 index 0000000..796537a --- /dev/null +++ b/week7/exercises/puppy/features/step_definitions/puppy.rb @@ -0,0 +1,8 @@ +class Puppy + attr_accessor :name + + def initialize(name=nil) + @name = name + end + +end \ No newline at end of file diff --git a/week7/exercises/puppy/features/step_definitions/puppy_steps.rb b/week7/exercises/puppy/features/step_definitions/puppy_steps.rb new file mode 100644 index 0000000..5be2d2b --- /dev/null +++ b/week7/exercises/puppy/features/step_definitions/puppy_steps.rb @@ -0,0 +1,31 @@ +Given /^we have a puppy$/ do + @puppy = Puppy.new() +end + +Given /^its name is (\w+)$/ do |arg1| + @puppy.name = arg1 +end + +When /^we pet the puppy$/ do + pending # express the regexp above with the code you wish you had +end + +Then /^the puppy wags its tail$/ do + pending # express the regexp above with the code you wish you had +end + +When /^we ring the bell$/ do + pending # express the regexp above with the code you wish you had +end + +When /^it wags its tail$/ do + pending # express the regexp above with the code you wish you had +end + +Then /^we must take it out$/ do + pending # express the regexp above with the code you wish you had +end + +Then /^then it will not pee on the floor$/ do + pending # express the regexp above with the code you wish you had +end diff --git a/week7/homework/features/pirate.feature b/week7/homework/pirate/features/pirate.feature similarity index 100% rename from week7/homework/features/pirate.feature rename to week7/homework/pirate/features/pirate.feature diff --git a/week7/homework/pirate/features/step_definitions/pirate-steps.rb b/week7/homework/pirate/features/step_definitions/pirate-steps.rb new file mode 100644 index 0000000..5d4b939 --- /dev/null +++ b/week7/homework/pirate/features/step_definitions/pirate-steps.rb @@ -0,0 +1,19 @@ +Gangway /^I have a PirateTranslator$/ do + @piratetranslator = PirateTranslator.new() +end + +Blimey /^I say 'Hello Friend'$/ do + @piratetranslator.say = "Hello Friend" +end + +Blimey /^I hit translate$/ do + @result = @piratetranslator.translate() +end + +Letgoandhaul /^it prints out 'Ahoy Matey'$/ do + @result.should =~ /Ahoy Matey/ +end + +Letgoandhaul /^it also prints 'Shiber Me Timbers You Scurvey Dogs!!'$/ do + @result.should =~ /Shiber Me Timbers You Scurvey Dogs!!/ +end diff --git a/week7/homework/pirate/features/step_definitions/pirate.rb b/week7/homework/pirate/features/step_definitions/pirate.rb new file mode 100644 index 0000000..8b6839d --- /dev/null +++ b/week7/homework/pirate/features/step_definitions/pirate.rb @@ -0,0 +1,7 @@ +class PirateTranslator + attr_accessor :say + + def translate + "Ahoy Matey Shiber Me Timbers You Scurvey Dogs!!" + end +end \ No newline at end of file diff --git a/week7/homework/questions.txt b/week7/homework/questions.txt index d55387d..62641c8 100644 --- a/week7/homework/questions.txt +++ b/week7/homework/questions.txt @@ -1,9 +1,18 @@ - Please Read Chapters 23 and 24 DuckTyping and MetaProgramming Questions: 1. What is method_missing and how can it be used? +method_missing is an hook triggered when an object is sent a message that it can handle. It can be used to dynamically create and handle method calls that aren’t implicitly defined. + 2. What is and Eigenclass and what is it used for? Where Do Singleton methods live? +An Eigenclass is another name for a singleton class. Singleton methods live with the anonymous class it’s associated with. + 3. When would you use DuckTypeing? How would you use it to improve your code? +You use ducktyping when you think of objects based on what they can do instead of their explicit type. You can use it to improve your code by not checking the object types all of the time and make more generalized methods that can handle any object that behave a certain way. + 4. What is the difference between a class method and an instance method? What is the difference between instance_eval and class_eval? +Class methods are methods that are called on a class and instance methods are methods that are called on an instance of a class. +Class_eval sets things up as if you were in the body of a class definition whereas instance_eval sets things up as if you were inside the singleton class of self. + 5. What is the difference between a singleton class and a singleton method? +A singleton class is a class which defines a single object whereas a singleton method is a method that belongs to a single object. \ No newline at end of file diff --git a/week7/homework/features/step_definitions/tic-tac-toe-steps.rb b/week7/homework/tictactoe/features/step_definitions/tic-tac-toe-steps.rb similarity index 100% rename from week7/homework/features/step_definitions/tic-tac-toe-steps.rb rename to week7/homework/tictactoe/features/step_definitions/tic-tac-toe-steps.rb diff --git a/week7/homework/tictactoe/features/step_definitions/tic-tac-toe.rb b/week7/homework/tictactoe/features/step_definitions/tic-tac-toe.rb new file mode 100644 index 0000000..8c506b4 --- /dev/null +++ b/week7/homework/tictactoe/features/step_definitions/tic-tac-toe.rb @@ -0,0 +1,141 @@ +class TicTacToe + attr_accessor :player, :player_symbol, :winner, :board + attr_reader :over, :currentplayer, :computer_symbol + + SYMBOLS = [:O, :X] + + def initialize(currentplayer = nil, symbol = nil) + @over = false + @winner = :none + @player = "Player" + + if currentplayer != nil then + @currentplayer = currentplayer + else + @currentplayer = (Random.new.rand(0..1) == 0 ? :player : :computer) + end + + if symbol != nil then + @player_symbol = symbol + @computer_symbol = (symbol == :O ? :X : :O) + else + @player_symbol = SYMBOLS[0] + @computer_symbol = SYMBOLS[1] + end + + @board = { + :A1 => " ", :A2 => " ", :A3 => " ", + :B1 => " ", :B2 => " ", :B3 => " ", + :C1 => " ", :C2 => " ", :C3 => " " + } + end + + def welcome_player + puts("Welcome #{@player}") + "Welcome #{@player}" + end + + def indicate_palyer_turn + puts("#{@player}'s Move:") + end + + def current_player + @currentplayer == :computer ? "Computer" : @player + end + + def current_state + puts("Board = #{@board}") + @board.values + end + + def player_move + set_position(get_player_move.to_sym, @player_symbol) + @currentplayer = :computer + get_player_move.to_sym + end + + def get_player_move + @player_move = gets().chomp + end + + def computer_move + @pick = open_spots.sample() + puts("Computer chose: #{@pick}") + set_position(@pick, @computer_symbol) + @currentplayer = :player + @pick + end + + def open_spots + @board.select{|k,v| v == " "}.keys + end + + def spots_open? + open_spots.count() > 0 + end + + def set_position(position, symbol) + if(@board[position] != " ") then + @random_move = open_spots.sample() + puts("Spot taken, choosing random spot: #{@random_move}") + @board[@random_move] = symbol.to_s + else + @board[position] = symbol.to_s + end + end + + def determine_winner + ["A","B","C","1","2","3"].each do |v| + if check_row(@player_symbol, v) then + @winner = :player + end + end + if check_diagonal(@player_symbol) then + @winner = :player + end + + ["A","B","C","1","2","3"].each do |v| + if check_row(@computer_symbol, v) then + @winner = :computer + @over = true + end + end + if check_diagonal(@computer_symbol) then + @winner = :computer + end + if @winner != :none then + @over = true + end + + if not over? and not spots_open? then + puts("Draw") + @winner = :draw + @over = true + end + end + + def check_row(symbol, row) + @board.select{|k,v| v==symbol.to_s}.keys.join.count(row) == 3 + end + + def check_diagonal(symbol) + (@board[:A1].to_s + @board[:B2].to_s + @board[:C3].to_s).count(symbol.to_s) == 3 || + (@board[:A3].to_s + @board[:B2].to_s + @board[:C1].to_s).count(symbol.to_s) == 3 + end + + def over? + @over + end + + def player_won? + @winner == :player + end + + def computer_won? + @winner == :computer + end + + def draw? + @winner == :draw + end +end \ No newline at end of file diff --git a/week7/homework/features/tic-tac-toe.feature b/week7/homework/tictactoe/features/tic-tac-toe.feature similarity index 100% rename from week7/homework/features/tic-tac-toe.feature rename to week7/homework/tictactoe/features/tic-tac-toe.feature diff --git a/week7/homework/play_game.rb b/week7/homework/tictactoe/play_game.rb similarity index 100% rename from week7/homework/play_game.rb rename to week7/homework/tictactoe/play_game.rb diff --git a/week8/exercises/couch.rb b/week8/exercises/couch.rb index 0f85a23..171a330 100644 --- a/week8/exercises/couch.rb +++ b/week8/exercises/couch.rb @@ -4,7 +4,6 @@ def initialize(pillows, cushions) @cushions = cushions end - [:pillows, :cushions].each do |s| define_method("how_many_#{s}") do instance_variable_get("@#{s}").count