From 017cef84a7c476a459d2f8088e9045bbfb91bfcd Mon Sep 17 00:00:00 2001 From: "Kelli A. Mohr" Date: Fri, 12 Oct 2012 18:51:30 -0700 Subject: [PATCH 01/26] Changes to class exercises --- week1/exercises/rspec_spec.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/week1/exercises/rspec_spec.rb b/week1/exercises/rspec_spec.rb index 31800c8..5987349 100644 --- a/week1/exercises/rspec_spec.rb +++ b/week1/exercises/rspec_spec.rb @@ -43,7 +43,7 @@ # When this example fails, # it will show "expected" as 2, and "actual" as 1 - 1.should eq 2 + 1.should_not eq 2 end @@ -77,7 +77,7 @@ # Fix the Failing Test # Order of Operations is Please Excuse My Dear Aunt Sally: # Parentheses, Exponents, Multiplication, Division, Addition, Subtraction - (1+2-5*6/2).should eq -10 + (1+2-5*6/2).should eq -12 end it "should count the charaters in your name" it "should check basic math" From 5552f604ee5190624cbcc4e53043b4065e7e4034 Mon Sep 17 00:00:00 2001 From: "Kelli A. Mohr" Date: Mon, 15 Oct 2012 17:54:17 -0700 Subject: [PATCH 02/26] Homework changes --- week1/homework/strings_and_rspec_spec.rb | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/week1/homework/strings_and_rspec_spec.rb b/week1/homework/strings_and_rspec_spec.rb index ec1662f..10a5ddb 100644 --- a/week1/homework/strings_and_rspec_spec.rb +++ b/week1/homework/strings_and_rspec_spec.rb @@ -1,18 +1,20 @@ -# encoding: utf-8 +encoding: utf-8 describe String do context "When a string is defined" do 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" + @my_string.should have(5).characters + it "should be able to split on the . charater" do pending result = #do something with @my_string here result.should have(2).items end it "should be able to give the encoding of the string" do - pending - #should eq (Encoding.find("UTF-8")) + + should eq (Encoding.find("UTF-8")) end end end From ef4eed844265020a36ab37994a2d1cad3aa3b69a Mon Sep 17 00:00:00 2001 From: "Kelli A. Mohr" Date: Tue, 16 Oct 2012 11:19:05 -0700 Subject: [PATCH 03/26] Homework Answers --- week1/homework/questions.txt | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/week1/homework/questions.txt b/week1/homework/questions.txt index acade2a..dc5b501 100644 --- a/week1/homework/questions.txt +++ b/week1/homework/questions.txt @@ -1,11 +1,23 @@ 1. What is an object? + + Object is the root of Ruby's class hierarchy. Its methods are available to all classes unless explicitly overridden. 2. What is a variable? + A variable is a reference to an object. + 3. What is the difference between an object and a class? + All objects are instances of a class. + 4. What is a String? + A string is a sequence of characters. Strings are objects of class String. + 5. What are three messages that I can send to a string object? Hint: think methods + downcase, upcase, strip + 6. What are two ways of defining a String literal? Bonus: What is the difference between the two? + + Two ways to define a String literal are single quoted string and double quoted string. A double quoted string can undergo additional subsitutions such as Tab, Space, Return, Escape or Backspace. \ No newline at end of file From 1e951470367603ba3da83389689441e64009c56e Mon Sep 17 00:00:00 2001 From: "Kelli A. Mohr" Date: Tue, 16 Oct 2012 13:00:00 -0700 Subject: [PATCH 04/26] Completed homework for rspec tests --- week1/homework/strings_and_rspec_spec.rb | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/week1/homework/strings_and_rspec_spec.rb b/week1/homework/strings_and_rspec_spec.rb index 8aa1ced..049f051 100644 --- a/week1/homework/strings_and_rspec_spec.rb +++ b/week1/homework/strings_and_rspec_spec.rb @@ -1,7 +1,4 @@ -<<<<<<< HEAD -encoding: utf-8 -======= -# encoding: utf-8 +#encoding: utf-8 # Please make these examples all pass # You will need to change the 3 pending tests @@ -10,23 +7,20 @@ # The two tests with the pending keyword, require some ruby code to be written # (Hint: You should do the reading on Strings first) ->>>>>>> a00f5073fc6c191d7113ad9100ce994cb2154297 describe String do context "When a string is defined" do 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" - @my_string.should have(5).characters - + it "should be able to count the charaters" do + @my_string.should have(66).characters + end it "should be able to split on the . charater" do - pending - result = #do something with @my_string here + result = @my_string.split(/\./) result.should have(2).items end it "should be able to give the encoding of the string" do - - should eq (Encoding.find("UTF-8")) + #{@my_string.encoding} should eq (Encoding.find("UTF-8")) end end end From d991c000b44cdbfcbc8cf10dc5584781347ff53f Mon Sep 17 00:00:00 2001 From: "Kelli A. Mohr" Date: Sun, 21 Oct 2012 10:01:31 -0700 Subject: [PATCH 05/26] Exercise Updates --- week2/exercises/book.rb | 11 +++++++++++ week2/exercises/book_spec.rb | 2 ++ week2/exercises/lor | 0 week2/exercises/mad_libs.rb | 4 +++- week2/exercises/spec_helper.rb | 3 +++ 5 files changed, 19 insertions(+), 1 deletion(-) create mode 100644 week2/exercises/lor create mode 100644 week2/exercises/spec_helper.rb diff --git a/week2/exercises/book.rb b/week2/exercises/book.rb index e05e468..f103e16 100644 --- a/week2/exercises/book.rb +++ b/week2/exercises/book.rb @@ -1,2 +1,13 @@ class Book + + attr_accessor :title, :page_count + + def initialize(title, page_count) + @title = title + @page_count = page_count + end + + def page_count + return "Page count is #{@page_count}" + end end diff --git a/week2/exercises/book_spec.rb b/week2/exercises/book_spec.rb index f01bb91..acbbc20 100644 --- a/week2/exercises/book_spec.rb +++ b/week2/exercises/book_spec.rb @@ -1,4 +1,6 @@ require './book.rb' +require './spec_helper.rb' + describe Book do before :each do @book = Book.new("Harry Potter", 200) diff --git a/week2/exercises/lor b/week2/exercises/lor new file mode 100644 index 0000000..e69de29 diff --git a/week2/exercises/mad_libs.rb b/week2/exercises/mad_libs.rb index 128ee92..b0a156a 100644 --- a/week2/exercises/mad_libs.rb +++ b/week2/exercises/mad_libs.rb @@ -4,5 +4,7 @@ adjective = gets.chomp puts "Please enter a past tense action verb" verb_past_tense = gets.chomp -story = "The #{adjective} #{noun} #{verb_past_tense} past the graveyard" +puts "Enter another noun" +noun1 = gets.chomp +story = "The #{adjective} #{noun} #{verb_past_tense} past the graveyard and ate the #{noun1}" puts story diff --git a/week2/exercises/spec_helper.rb b/week2/exercises/spec_helper.rb new file mode 100644 index 0000000..53828f6 --- /dev/null +++ b/week2/exercises/spec_helper.rb @@ -0,0 +1,3 @@ +RSpec.configure do |config| + config.color_enabled = true +end \ No newline at end of file From dca2603a26f4375f75f882b936c8788e8fa758e2 Mon Sep 17 00:00:00 2001 From: "Kelli A. Mohr" Date: Sun, 21 Oct 2012 14:56:36 -0700 Subject: [PATCH 06/26] Finished Homework for week 2 --- week2/homework/questions.txt | 14 ++++++++++++++ week2/homework/simon_says.rb | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+) create mode 100644 week2/homework/simon_says.rb diff --git a/week2/homework/questions.txt b/week2/homework/questions.txt index 247547d..51f6916 100644 --- a/week2/homework/questions.txt +++ b/week2/homework/questions.txt @@ -3,7 +3,21 @@ Containers, Blocks, and Iterators Sharing Functionality: Inheritance, Modules, and Mixins 1. What is the difference between a Hash and an Array? + + The difference between a Hash and an Array is a Hash can be indexed with an object of any type, such as a symbol or strig whereas an array is indexed with a non-negative integer. + 2. When would you use an Array over a Hash and vice versa? + + An array is useful for storing a list of items where an index isn't necessary. A hash would be used to store a collection of items with a descriptive index (key) for easier look-up later. + 3. What is a module? Enumerable is a built in Ruby module, what is it? + + A module groups together methods, classes and constants. Enumerable is a collecton of classes with traversal and searching methods with the ability to sort. + 4. Can you inherit more than one thing in Ruby? How could you get around this problem? + + Ruby is a single-inheritance language. Although you can use "mixins" and include a module within a class definition to access to the module's instance methods. + 5. What is the difference between a Module and a Class? + + The difference between a Module and a Class is that a module cannot be instantiated. A class may inherit from another class. A module cannot inherit from anything. diff --git a/week2/homework/simon_says.rb b/week2/homework/simon_says.rb new file mode 100644 index 0000000..44dd095 --- /dev/null +++ b/week2/homework/simon_says.rb @@ -0,0 +1,35 @@ +module SimonSays + + def echo(word) + @word = word + return "#{@word}" + end + + def shout(word) + @word = word.upcase + return "#{@word}" + end + + def repeat(word, times=2) + + @word = "#{word}" + @final_word = @word + + for i in 1...times + @final_word = @final_word + " " + "#{word}" + end + + return @final_word + end + + def start_of_word(word, end_pnt) + @word = word + return @word[0...end_pnt] + end + + def first_word(word) + @word = word.scan(/\w+/) + return @word[0] + + end +end \ No newline at end of file From d1518c94e1a8a687003cde3f9bafe38b1ea2d558 Mon Sep 17 00:00:00 2001 From: "Kelli A. Mohr" Date: Sun, 21 Oct 2012 14:57:53 -0700 Subject: [PATCH 07/26] merge address changes --- week1/exercises/roster.txt | 4 ---- 1 file changed, 4 deletions(-) diff --git a/week1/exercises/roster.txt b/week1/exercises/roster.txt index 479f1e5..8d13804 100644 --- a/week1/exercises/roster.txt +++ b/week1/exercises/roster.txt @@ -31,7 +31,3 @@ Price Hardman, PriceHardman@gmail.com, PriceHardman Will Sugg, sugg.will@gmail.com, wsugg, , sugg.will@gmail.com Nicole Lewis, lewis.nicole@gmail.com, nelewis, nil, lewis.nicole@gmail.com Sol Wagner, capnsol@gmail.com, Soladin, n/a, capnsol@gmail.com -Price Hardman, PriceHardman@gmail.com, PriceHardman -Will Sugg, sugg.will@gmail.com, wsugg, , sugg.will@gmail.com -Nicole Lewis, lewis.nicole@gmail.com, nelewis, nil, lewis.nicole@gmail.com -Sol Wagner, capnsol@gmail.com, Soladin, n/a, capnsol@gmail.com From 7b7f643f513969ef160d8d682bb24dcfc28223be Mon Sep 17 00:00:00 2001 From: "Kelli A. Mohr" Date: Tue, 23 Oct 2012 18:52:27 -0700 Subject: [PATCH 08/26] Finished programming homework for week 3 --- week3/homework/calculator.rb | 16 ++++++++++++ week3/homework/calculator_spec.rb | 43 +++++++++++++++++++++++-------- 2 files changed, 48 insertions(+), 11 deletions(-) diff --git a/week3/homework/calculator.rb b/week3/homework/calculator.rb index ede464b..58a21a1 100644 --- a/week3/homework/calculator.rb +++ b/week3/homework/calculator.rb @@ -1,2 +1,18 @@ class Calculator + + def sum(num) + num.inject(0,:+) + end + + def multiply(num) + num.inject(:*) + end + + def power(num) + num.inject(:**) + end + + def factorial(num) + (1..num).inject(0,:*) + end end \ No newline at end of file diff --git a/week3/homework/calculator_spec.rb b/week3/homework/calculator_spec.rb index 08b81cb..5ffe20d 100644 --- a/week3/homework/calculator_spec.rb +++ b/week3/homework/calculator_spec.rb @@ -26,20 +26,41 @@ # Once the above tests pass, # write tests and code for the following: - - it "multiplies two numbers" +describe "#multiply" do + it "multiplies two numbers" do + @calculator.multiply([5,6]).should == 30 + end + + it "multiplies an array of numbers" do + @calculator.multiply([2,4,2,4]).should == 64 + end + end + describe "#power" do + it "raises one number to the power of another number" do + @calculator.power([2,4]) == 16 + end +end - it "multiplies an array of numbers" - - it "raises one number to the power of another number" - # http://en.wikipedia.org/wiki/Factorial describe "#factorial" do - it "computes the factorial of 0" - it "computes the factorial of 1" - it "computes the factorial of 2" - it "computes the factorial of 5" - it "computes the factorial of 10" + it "computes the factorial of 0" do + @calculator.factorial(0) == 0 + end + + it "computes the factorial of 1" do + @calculator.factorial(1) == 1 + end + + it "computes the factorial of 2" do + @calculator.factorial(2) == 2 + end + + it "computes the factorial of 5" do + @calculator.factorial(5) == 120 + end + it "computes the factorial of 10" do + @calculator.factorial(10) == 3628800 + end end end From cb6353efcba93391b2d614e23a581432726cde65 Mon Sep 17 00:00:00 2001 From: "Kelli A. Mohr" Date: Mon, 29 Oct 2012 22:15:04 -0700 Subject: [PATCH 09/26] Finished homework questions. --- week3/homework/calculator_spec.rb | 1 + week3/homework/questions.txt | 15 +++++++++++++++ week3/homework/spec_helper.rb | 10 ++++++++++ 3 files changed, 26 insertions(+) create mode 100644 week3/homework/spec_helper.rb diff --git a/week3/homework/calculator_spec.rb b/week3/homework/calculator_spec.rb index 5ffe20d..a8aa35b 100644 --- a/week3/homework/calculator_spec.rb +++ b/week3/homework/calculator_spec.rb @@ -1,3 +1,4 @@ +require "./spec_helper.rb" require "#{File.dirname(__FILE__)}/calculator" describe Calculator do diff --git a/week3/homework/questions.txt b/week3/homework/questions.txt index e9a9079..b57bd89 100644 --- a/week3/homework/questions.txt +++ b/week3/homework/questions.txt @@ -5,8 +5,23 @@ Please Read: - Chapter 22 The Ruby Language: basic types (symbols), variables and constants 1. What is a symbol? + + A symbol is a string that uses the same space in memory. A symbol is also a constant name that does not have to be predeclared and are guaranteed to be unique. + 2. What is the difference between a symbol and a string? + + A symbol cannot change, but a string can change. + 3. What is a block and how do I call a block? + + A block is a piece of code that can be associated to method invocation. The block is the code between two curly braces or a do and an end. + 4. How do I pass a block to a method? What is the method signature? + + A block can be passed to a method by putting the block at the end of the source line containing the method call. Methods with parameters appear before the block. + + The method signature is a combination of the method name and its parameters. + 5. Where would you use regular expressions? + Regular expressions can be used for matching strings with patterns, changing strings with patterns, negative matching, repetition of chaacters or patterns, grouping, subsitution, and replacement to name a few. \ No newline at end of file diff --git a/week3/homework/spec_helper.rb b/week3/homework/spec_helper.rb new file mode 100644 index 0000000..45878fd --- /dev/null +++ b/week3/homework/spec_helper.rb @@ -0,0 +1,10 @@ +RSpec.configure do |config| + # Use color in STDOUT + config.color_enabled = true + + # Use color not only in STDOUT but also in pagers and files + config.tty = true + + # Use the specified formatter + config.formatter = :documentation # :progress, :html, :textmate +end \ No newline at end of file From 8b56cabf44f4b5854cb8845c38eeb22a5d5f2a09 Mon Sep 17 00:00:00 2001 From: "Kelli A. Mohr" Date: Fri, 2 Nov 2012 08:52:45 -0700 Subject: [PATCH 10/26] In class exercises --- week4/class_materials/odd_number.rb | 21 +++++++++++++++++++++ week4/class_materials/timer.rb | 12 ++++++++++++ week4/class_materials/timer_spec.rb | 11 +++++++++++ week4/exercises/worker.rb | 13 +++++++++++++ week4/homework/questions.txt | 12 ++++++++++++ 5 files changed, 69 insertions(+) create mode 100644 week4/class_materials/odd_number.rb create mode 100644 week4/class_materials/timer.rb create mode 100644 week4/exercises/worker.rb diff --git a/week4/class_materials/odd_number.rb b/week4/class_materials/odd_number.rb new file mode 100644 index 0000000..db8b9ad --- /dev/null +++ b/week4/class_materials/odd_number.rb @@ -0,0 +1,21 @@ +class OddNumber + attr_accessor :value + + def initialize (value) + @value = value + end + + def succ + new_val = nil + if @value.even? + new_val =OddNumber.new(@value + 1) + else + new_val =OddNumber.new(@value + 2) + end + new_val + end + + def <=> (other) + end + +end \ No newline at end of file diff --git a/week4/class_materials/timer.rb b/week4/class_materials/timer.rb new file mode 100644 index 0000000..69a2ad4 --- /dev/null +++ b/week4/class_materials/timer.rb @@ -0,0 +1,12 @@ +class Timer + + def self.time_code(n=1) + start_time = Time.now + n.times{yield} + end_time = Time.now + + end_time - start_time + end + + +end diff --git a/week4/class_materials/timer_spec.rb b/week4/class_materials/timer_spec.rb index a6092f1..5383d38 100644 --- a/week4/class_materials/timer_spec.rb +++ b/week4/class_materials/timer_spec.rb @@ -17,4 +17,15 @@ flag.should be_true end + it "should run out 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,1) + result = Timer.time_code(1) {} + result.should be_within(0.1).of(1.0) + end end diff --git a/week4/exercises/worker.rb b/week4/exercises/worker.rb new file mode 100644 index 0000000..7a07278 --- /dev/null +++ b/week4/exercises/worker.rb @@ -0,0 +1,13 @@ +class Worker + + def self.work(n=1) + result = nil + + n.times do + result = yield + end + + result + + end +end diff --git a/week4/homework/questions.txt b/week4/homework/questions.txt index bc1ab7c..0128064 100644 --- a/week4/homework/questions.txt +++ b/week4/homework/questions.txt @@ -3,7 +3,19 @@ Chapter 10 Basic Input and Output The Rake Gem: http://rake.rubyforge.org/ 1. How does Ruby read files? + + + 2. How would you output "Hello World!" to a file called my_output.txt? + + + 3. What is the Directory class and what is it used for? + + + 4. What is an IO object? + + + 5. What is rake and what is it used for? What is a rake task? From 865c51f23fd49e9344abbde49e7f306bb5af89c7 Mon Sep 17 00:00:00 2001 From: "Kelli A. Mohr" Date: Tue, 13 Nov 2012 18:02:48 -0800 Subject: [PATCH 11/26] rake exercises --- week5/exercises/Rakefile | 21 +++++++++++++++++++++ week5/exercises/names | 35 ----------------------------------- 2 files changed, 21 insertions(+), 35 deletions(-) diff --git a/week5/exercises/Rakefile b/week5/exercises/Rakefile index 68a8f60..3b48aa1 100644 --- a/week5/exercises/Rakefile +++ b/week5/exercises/Rakefile @@ -1,3 +1,24 @@ task :test do puts "Hello World!" end + +desc "Read names files" +task :create_names do +store_names = [] + File.open("names", "w") do |names| + open_file_use_lines("names") do |l| + name = l + store_names << name + puts store_names + end + end +end + +def open_file_use_lines(file="names") + File.open(file) do |f| + f.each do |l| + yield "#{l.chomp}.git" + end + end +end + diff --git a/week5/exercises/names b/week5/exercises/names index 9eec0ec..e69de29 100644 --- a/week5/exercises/names +++ b/week5/exercises/names @@ -1,35 +0,0 @@ -Ben -BrianT -BrianWard -BryanWilliams -Cheri -Chris B -ChristopherF -ChristopherT -Danny -Dimitry -Eddie -Emily -Gregory -Josh -Karen -Kat -Kelli -Kris -Mallory -Nell -NicholasP -Nicole -Nikky -Peter -Price -Renée -Ryan -Santy -Serene -Sol -Steven -Strand -Timothy -Will -Yan From a1ab4e628d1b497b0c81afcede625999d47cf5ec Mon Sep 17 00:00:00 2001 From: kellimohr Date: Tue, 20 Nov 2012 19:34:05 -0800 Subject: [PATCH 12/26] Passed all cucmber test --- .../features/step_definitions/converter.rb | 19 ++++++++++ .../step_definitions/converter_steps.rb | 15 ++++++++ .../features/step_definitions/puppy.rb | 16 +++++++++ .../features/step_definitions/puppy_steps.rb | 35 +++++++++++++++++++ 4 files changed, 85 insertions(+) create mode 100644 week7/exercises/features/step_definitions/converter.rb create mode 100644 week7/exercises/features/step_definitions/converter_steps.rb create mode 100644 week7/exercises/features/step_definitions/puppy.rb create mode 100644 week7/exercises/features/step_definitions/puppy_steps.rb diff --git a/week7/exercises/features/step_definitions/converter.rb b/week7/exercises/features/step_definitions/converter.rb new file mode 100644 index 0000000..4d25511 --- /dev/null +++ b/week7/exercises/features/step_definitions/converter.rb @@ -0,0 +1,19 @@ +class Converter + + def initialize (value) + @value = value.to_f + end + + def type=(type) + @type = type + end + + def convert + self.send("#{@type}_convertion") + end + + def Fahrenheit_convertion + ((@value - 32) / 1.8).round(1) + end + +end diff --git a/week7/exercises/features/step_definitions/converter_steps.rb b/week7/exercises/features/step_definitions/converter_steps.rb new file mode 100644 index 0000000..42a6d70 --- /dev/null +++ b/week7/exercises/features/step_definitions/converter_steps.rb @@ -0,0 +1,15 @@ +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 \ No newline at end of file diff --git a/week7/exercises/features/step_definitions/puppy.rb b/week7/exercises/features/step_definitions/puppy.rb new file mode 100644 index 0000000..a5e8a51 --- /dev/null +++ b/week7/exercises/features/step_definitions/puppy.rb @@ -0,0 +1,16 @@ +class Puppy + + attr_accessor :name + + def pet + "The puppy wags its tail!" + end + + def ring_bell + "it wags its tail!" + end + + def go_potty + "it will not pee on the floor!" + end +end diff --git a/week7/exercises/features/step_definitions/puppy_steps.rb b/week7/exercises/features/step_definitions/puppy_steps.rb new file mode 100644 index 0000000..b17ad80 --- /dev/null +++ b/week7/exercises/features/step_definitions/puppy_steps.rb @@ -0,0 +1,35 @@ +Given /^we have a puppy$/ do + @puppy = Puppy.new() +end + +Given /^its name is Fred$/ do + @puppy.name = "Fred" +end + +When /^we pet the puppy$/ do + @pet = @puppy.pet +end + +Then /^the puppy wags its tail$/ do + @pet.should eq "The puppy wags its tail!" +end + +Given /^its name is Bella$/ do + @puppy.name = "Bella" +end + +When /^we ring the bell$/ do + @ring = @puppy.ring_bell +end + +When /^it wags its tail$/ do + @ring.should eq "it wags its tail!" +end + +Then /^we must take it out$/ do + @out = @puppy.go_potty +end + +Then /^then it will not pee on the floor$/ do + @out.should eq "it will not pee on the floor!" +end \ No newline at end of file From 07551c4f61cd5dcefa1fc9379539d1248f358a60 Mon Sep 17 00:00:00 2001 From: kellimohr Date: Mon, 26 Nov 2012 07:15:06 -0800 Subject: [PATCH 13/26] Class Exercises --- week7/class_materials/minitest/book.rb | 3 +++ 1 file changed, 3 insertions(+) diff --git a/week7/class_materials/minitest/book.rb b/week7/class_materials/minitest/book.rb index 6bdc6c3..294444b 100644 --- a/week7/class_materials/minitest/book.rb +++ b/week7/class_materials/minitest/book.rb @@ -1,3 +1,6 @@ +class Book +end + require 'minitest/autorun' # Unit tests From 5dc1247243c2e9ca772e102bc657b01bff81c0ba Mon Sep 17 00:00:00 2001 From: kellimohr Date: Mon, 26 Nov 2012 16:34:30 -0800 Subject: [PATCH 14/26] Answered questions --- week7/homework/features/pirate.rb | 11 ++++++++++ .../features/step_definitions/pirate-steps.rb | 20 +++++++++++++++++++ 2 files changed, 31 insertions(+) create mode 100644 week7/homework/features/pirate.rb create mode 100644 week7/homework/features/step_definitions/pirate-steps.rb diff --git a/week7/homework/features/pirate.rb b/week7/homework/features/pirate.rb new file mode 100644 index 0000000..1cb3e1f --- /dev/null +++ b/week7/homework/features/pirate.rb @@ -0,0 +1,11 @@ +class Pirate + + def translate(value) + if value = "Hello Friend" + @value = "Ahoy Matey" + else + @value = "Shiber Me Timbers You Scurvey Dogs!!" + end + @value + end +end \ No newline at end of file diff --git a/week7/homework/features/step_definitions/pirate-steps.rb b/week7/homework/features/step_definitions/pirate-steps.rb new file mode 100644 index 0000000..d3e9768 --- /dev/null +++ b/week7/homework/features/step_definitions/pirate-steps.rb @@ -0,0 +1,20 @@ +Gangway /^I have a PirateTranslator$/ do + @pirateTranslator = Pirate.new() +end + +Blimey /^I say 'Hello Friend'$/ do + "Hello Friend" +end + +Blimey /^I hit translate$/ do + @translate = @pirateTranslator.translate("Hello Friend") +end + +Letgoandhaul /^it prints out 'Ahoy Matey'$/ do + @translate.should eq "Ahoy Matey" +end + +Letgoandhaul /^it also prints 'Shiber Me Timbers You Scurvey Dogs!!'$/ do + @translate = @pirateTranslator.translate("") + @translate.should eq "Shiber Me Timbers You Scurvey Dogs!!" +end From 9086291fcfa76e20a1242c4f224313d0f28df953 Mon Sep 17 00:00:00 2001 From: kellimohr Date: Mon, 26 Nov 2012 17:37:50 -0800 Subject: [PATCH 15/26] Homework --- week7/homework/questions.txt | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/week7/homework/questions.txt b/week7/homework/questions.txt index d55387d..2bb2e3e 100644 --- a/week7/homework/questions.txt +++ b/week7/homework/questions.txt @@ -3,7 +3,21 @@ Please Read Chapters 23 and 24 DuckTyping and MetaProgramming Questions: 1. What is method_missing and how can it be used? + + The method_missing method in Object with a default that throws and error and terminates the program. If a method is non-existent in self then ruby will look for the method in the superclass chain. If the method is not found it will call method_missing. The method_missing can be used to simulate the existence of methods in the receiver. + 2. What is and Eigenclass and what is it used for? Where Do Singleton methods live? + + The Eigenclass is an object's own class. The eiganclass becomes the class of the object and is pushed up in the lookup chain as the superclass. Singleton methods are defined on the object itself and not the class. + 3. When would you use DuckTypeing? How would you use it to improve your code? + + DuckTyping could be used for testing as well as applications. DuckTyping help create flexible code. With DuckTyping, you don't need to test the type of arguments, which makes the code more flexible. + 4. What is the difference between a class method and an instance method? What is the difference between instance_eval and class_eval? + + The difference between a class method and and instance method is class methods are called on a class whereas instance methods are called on an instance of a class. The difference between instance_eval and class_eval is instance_eval evaluates code against a single object instance (within singleton class) whereas class_eval acts as if you are in the body of a class definition, method definitions will define instance methods. + 5. What is the difference between a singleton class and a singleton method? + + The difference between a singleton class and a singleton method is a singleton method is an instance method associated with one specific object whereas a singleton class is an anonymous class created by subclassing the class associated with a particular object. \ No newline at end of file From dd40024c83e9075856d64b336ffeb035dff0e888 Mon Sep 17 00:00:00 2001 From: kellimohr Date: Mon, 26 Nov 2012 18:01:45 -0800 Subject: [PATCH 16/26] Finished pirate cucumber specs --- week7/homework/features/pirate.rb | 2 +- week7/homework/features/step_definitions/pirate-steps.rb | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/week7/homework/features/pirate.rb b/week7/homework/features/pirate.rb index 1cb3e1f..af32b07 100644 --- a/week7/homework/features/pirate.rb +++ b/week7/homework/features/pirate.rb @@ -1,7 +1,7 @@ class Pirate def translate(value) - if value = "Hello Friend" + if value == "Hello Friend" @value = "Ahoy Matey" else @value = "Shiber Me Timbers You Scurvey Dogs!!" diff --git a/week7/homework/features/step_definitions/pirate-steps.rb b/week7/homework/features/step_definitions/pirate-steps.rb index d3e9768..4b68cb4 100644 --- a/week7/homework/features/step_definitions/pirate-steps.rb +++ b/week7/homework/features/step_definitions/pirate-steps.rb @@ -8,6 +8,7 @@ Blimey /^I hit translate$/ do @translate = @pirateTranslator.translate("Hello Friend") + @translate1 = @pirateTranslator.translate("") end Letgoandhaul /^it prints out 'Ahoy Matey'$/ do @@ -15,6 +16,5 @@ end Letgoandhaul /^it also prints 'Shiber Me Timbers You Scurvey Dogs!!'$/ do - @translate = @pirateTranslator.translate("") - @translate.should eq "Shiber Me Timbers You Scurvey Dogs!!" + @translate1.should eq "Shiber Me Timbers You Scurvey Dogs!!" end From b27f5fc6e43b79fba931aa173ab360d90a65865b Mon Sep 17 00:00:00 2001 From: kellimohr Date: Tue, 27 Nov 2012 19:28:55 -0800 Subject: [PATCH 17/26] metaprogramming example --- week8/exercises/couch.rb | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/week8/exercises/couch.rb b/week8/exercises/couch.rb index a52eb35..baa7327 100644 --- a/week8/exercises/couch.rb +++ b/week8/exercises/couch.rb @@ -4,17 +4,15 @@ def initialize(pillows, cushions) @cushions = cushions end - def pillow_colors - @pillows.join(", ") - end - - def cushion_colors - @cushions.join(", ") - end - [:pillows, :cushions].each do |s| define_method("how_many_#{s}") do instance_variable_get("@#{s}").count end end + + [:pillows, :cushions].each do |s| + define_method("colors_#{s}") do + instance_variable_get("@#{s}").join(",") + end + end end From 4bfcf04bc4ea2e2bc6ef6c9c747b49f509fc4f68 Mon Sep 17 00:00:00 2001 From: kellimohr Date: Mon, 3 Dec 2012 20:12:31 -0800 Subject: [PATCH 18/26] start of tic tac toe --- .../step_definitions/tic-tac-toe-steps.rb | 20 ++++- week7/homework/features/tic-tac-toe.feature | 2 +- week7/homework/features/tic-tac-toe.rb | 86 +++++++++++++++++++ week7/homework/play_game.rb | 2 +- 4 files changed, 104 insertions(+), 6 deletions(-) create mode 100644 week7/homework/features/tic-tac-toe.rb diff --git a/week7/homework/features/step_definitions/tic-tac-toe-steps.rb b/week7/homework/features/step_definitions/tic-tac-toe-steps.rb index b353120..1811a90 100644 --- a/week7/homework/features/step_definitions/tic-tac-toe-steps.rb +++ b/week7/homework/features/step_definitions/tic-tac-toe-steps.rb @@ -35,7 +35,7 @@ Then /^the computer prints "(.*?)"$/ do |arg1| @game.should_receive(:puts).with(arg1) - @game.indicate_palyer_turn + @game.indicate_player_turn end Then /^waits for my input of "(.*?)"$/ do |arg1| @@ -43,7 +43,7 @@ @game.get_player_move end -Given /^it is the computer's turn$/ do +Given /^it is the computers turn$/ do @game = TicTacToe.new(:computer, :O) @game.current_player.should eq "Computer" end @@ -77,11 +77,11 @@ @old_pos.should eq " " end -Then /^it is now the computer's turn$/ do +Then /^it is now the computers turn$/ do @game.current_player.should eq "Computer" end -When /^there are three X's in a row$/ do +When /^there are three Xs in a row$/ do @game = TicTacToe.new(:computer, :X) @game.board[:C1] = @game.board[:B2] = @game.board[:A3] = :X end @@ -122,3 +122,15 @@ @game.should_receive(:get_player_move).twice.and_return(@taken_spot, arg1) @game.player_move.should eq arg1.to_sym end + +Given /^it is the computers turn$/ do + pending # express the regexp above with the code you wish you had +end + +Then /^it is now the computers turn$/ do + pending # express the regexp above with the code you wish you had +end + +When /^there are three Xs in a row$/ do + pending # express the regexp above with the code you wish you had +end diff --git a/week7/homework/features/tic-tac-toe.feature b/week7/homework/features/tic-tac-toe.feature index 6f3134d..71a5970 100644 --- a/week7/homework/features/tic-tac-toe.feature +++ b/week7/homework/features/tic-tac-toe.feature @@ -1,7 +1,7 @@ Feature: Tic-Tac-Toe Game As a game player I like tic-tac-toe In order to up my skills - I would like to play agaist the computer + I would like to play against the computer Scenario: Begin Game Given I start a new Tic-Tac-Toe game diff --git a/week7/homework/features/tic-tac-toe.rb b/week7/homework/features/tic-tac-toe.rb new file mode 100644 index 0000000..48a03e3 --- /dev/null +++ b/week7/homework/features/tic-tac-toe.rb @@ -0,0 +1,86 @@ +class TicTacToe + + attr_accessor :player, :computer, :player_symbol, :computer_symbol, :get_player_move, :X, :O + TicTacToe = [:X,:O] + + def initialize(starter=nil, symbol=nil) + @starter = starter + @symbol = symbol + + if @starter == "Computer" + @computer_symbol = symbol + else + @player_symbol = symbol + end + + @board = { + :A1 => '', :A2 => '', :A3 => '', + :B1 => '', :B2 => '', :B3 => '', + :C1 => '', :C2 => '', :C3 => '' + } + end + + def welcome_player + "Welcome #{@player}" + end + + def current_player + if @symbol.nil? + @player_symbol = :X + @computer_symbol = :O + @current_player = [@player, @computer].sample + else + @current_player = @starter + end + @current_player + end + + def indicate_player_turn + puts "#{@current_player}'s Move:" + @indicate_player_turn = @current_player + end + + def indicate_player_move + @indicate_player_move + end + + def should_receive(move) + @move = move + end + + def computer_move + end + + def player_move + end + + def current_state + end + + def board(get_player_move) + @board = board[get_player_move] + end + + def determine_winner + end + + def open_spots + end + + def draw + end + + def with(with) + @with = with + end + + def player_won + end + + def computer_won + end + + def over + end + +end \ No newline at end of file diff --git a/week7/homework/play_game.rb b/week7/homework/play_game.rb index cf7847f..f64b949 100644 --- a/week7/homework/play_game.rb +++ b/week7/homework/play_game.rb @@ -8,7 +8,7 @@ when "Computer" @game.computer_move when @game.player - @game.indicate_palyer_turn + @game.indicate_player_turn @game.player_move end puts @game.current_state From 83c8476c1b0d70c2a3cfb32126e05de658d05780 Mon Sep 17 00:00:00 2001 From: kellimohr Date: Tue, 4 Dec 2012 11:37:46 -0800 Subject: [PATCH 19/26] More TicTacToe --- .../step_definitions/tic-tac-toe-steps.rb | 5 +- week7/homework/features/tic-tac-toe.rb | 51 ++++++++++++------- 2 files changed, 37 insertions(+), 19 deletions(-) diff --git a/week7/homework/features/step_definitions/tic-tac-toe-steps.rb b/week7/homework/features/step_definitions/tic-tac-toe-steps.rb index 1811a90..2417433 100644 --- a/week7/homework/features/step_definitions/tic-tac-toe-steps.rb +++ b/week7/homework/features/step_definitions/tic-tac-toe-steps.rb @@ -124,7 +124,8 @@ end Given /^it is the computers turn$/ do - pending # express the regexp above with the code you wish you had + @game = TicTacToe.new(:computer, :O) + @game.current_player.should eq "Computer" end Then /^it is now the computers turn$/ do @@ -132,5 +133,5 @@ end When /^there are three Xs in a row$/ do - pending # express the regexp above with the code you wish you had + @game.board[:C1] = @game.board[:B2] = @game.board[:A3] = :X end diff --git a/week7/homework/features/tic-tac-toe.rb b/week7/homework/features/tic-tac-toe.rb index 48a03e3..111ba00 100644 --- a/week7/homework/features/tic-tac-toe.rb +++ b/week7/homework/features/tic-tac-toe.rb @@ -8,16 +8,16 @@ def initialize(starter=nil, symbol=nil) @symbol = symbol if @starter == "Computer" - @computer_symbol = symbol + @computer_symbol = @symbol else - @player_symbol = symbol + @player_symbol = @symbol end @board = { - :A1 => '', :A2 => '', :A3 => '', - :B1 => '', :B2 => '', :B3 => '', - :C1 => '', :C2 => '', :C3 => '' - } + :A1 => " ", :A2 => " ", :A3 => " ", + :B1 => " ", :B2 => " ", :B3 => " ", + :C1 => " ", :C2 => " ", :C3 => " " + } end def welcome_player @@ -31,10 +31,16 @@ def current_player @current_player = [@player, @computer].sample else @current_player = @starter + if @player_symbol == :X then @computer_symbol = :Y else @computer_symbol = :X end + if @computer_symbol == :X then @player_symbol = :Y else @player_symbol = :X end end @current_player end + def change_turn(current_player) + @current_player = (@current_player == TicTacToe::X) ? TicTacToe::O : TicTacToe::X + end + def indicate_player_turn puts "#{@current_player}'s Move:" @indicate_player_turn = @current_player @@ -44,36 +50,48 @@ def indicate_player_move @indicate_player_move end - def should_receive(move) - @move = move - end - def computer_move + @computer_move end def player_move + @player_move end def current_state + @current_state = @board.values end - def board(get_player_move) - @board = board[get_player_move] + def board(player_move) + @player_move = player_move + + @board = [player_move] end def determine_winner + @winning_combos = [ + [:A1,:A2,:A3], + [:B1,:B2,:B3], + [:C1,:C2,:C3], + [:A1,:B1,:C1], + [:A2,:B2,:C2], + [:A3,:B3,:C3], + [:A1,:B2,:C3], + [:C1,:B2,:A3] + ] + + + + end def open_spots + if @board.inject.include(" ") then true else false end end def draw end - def with(with) - @with = with - end - def player_won end @@ -82,5 +100,4 @@ def computer_won def over end - end \ No newline at end of file From 65f5cbf1ed82a993ed59bfc855e560f23dccb605 Mon Sep 17 00:00:00 2001 From: kellimohr Date: Sun, 9 Dec 2012 14:08:40 -0800 Subject: [PATCH 20/26] Test changes --- week7/homework/features/tic-tac-toe.rb | 27 +++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/week7/homework/features/tic-tac-toe.rb b/week7/homework/features/tic-tac-toe.rb index 111ba00..120744c 100644 --- a/week7/homework/features/tic-tac-toe.rb +++ b/week7/homework/features/tic-tac-toe.rb @@ -1,18 +1,24 @@ class TicTacToe - attr_accessor :player, :computer, :player_symbol, :computer_symbol, :get_player_move, :X, :O - TicTacToe = [:X,:O] + attr_accessor :player, :computer, :player_symbol, :computer_symbol, :X, :O + #TicTacToe = [:X,:O] def initialize(starter=nil, symbol=nil) @starter = starter @symbol = symbol - if @starter == "Computer" - @computer_symbol = @symbol - else - @player_symbol = @symbol + unless @symbol.nil? + if @starter == "Computer" + @computer_symbol = @symbol + @computer = "Computer" + else + @player_symbol = @symbol + @player = @starter + end end + current_player + @board = { :A1 => " ", :A2 => " ", :A3 => " ", :B1 => " ", :B2 => " ", :B3 => " ", @@ -28,7 +34,7 @@ def current_player if @symbol.nil? @player_symbol = :X @computer_symbol = :O - @current_player = [@player, @computer].sample + @current_player = [@player, "Computer"].sample else @current_player = @starter if @player_symbol == :X then @computer_symbol = :Y else @computer_symbol = :X end @@ -37,8 +43,10 @@ def current_player @current_player end - def change_turn(current_player) - @current_player = (@current_player == TicTacToe::X) ? TicTacToe::O : TicTacToe::X + def get_player_move() + + #@current_player = (@current_player == TicTacToe::X) ? TicTacToe::O : TicTacToe::X + @get_player_move = gets.chomp end def indicate_player_turn @@ -86,6 +94,7 @@ def determine_winner end def open_spots + @open_spots = board.delete_if{|k,v| {} == v.delete_if{|a,b| b==""}} if @board.inject.include(" ") then true else false end end From e8d36eb37ffc201639e2c2b5c37a355b346585d7 Mon Sep 17 00:00:00 2001 From: kellimohr Date: Mon, 10 Dec 2012 20:18:58 -0800 Subject: [PATCH 21/26] passed first test --- week7/homework/features/tic-tac-toe.rb | 41 +++++++++++++++++++------- 1 file changed, 30 insertions(+), 11 deletions(-) diff --git a/week7/homework/features/tic-tac-toe.rb b/week7/homework/features/tic-tac-toe.rb index 120744c..f015131 100644 --- a/week7/homework/features/tic-tac-toe.rb +++ b/week7/homework/features/tic-tac-toe.rb @@ -1,23 +1,25 @@ class TicTacToe attr_accessor :player, :computer, :player_symbol, :computer_symbol, :X, :O - #TicTacToe = [:X,:O] + SYMBOLS = [:X,:O] def initialize(starter=nil, symbol=nil) @starter = starter @symbol = symbol - unless @symbol.nil? + unless @starter.nil? if @starter == "Computer" - @computer_symbol = @symbol + @computer_symbol = :X + @player_symbol = :O @computer = "Computer" else - @player_symbol = @symbol + @player_symbol = :X + @computer_symbol = :O @player = @starter end end - current_player + #current_player @board = { :A1 => " ", :A2 => " ", :A3 => " ", @@ -43,7 +45,7 @@ def current_player @current_player end - def get_player_move() + def get_player_move #@current_player = (@current_player == TicTacToe::X) ? TicTacToe::O : TicTacToe::X @get_player_move = gets.chomp @@ -52,6 +54,7 @@ def get_player_move() def indicate_player_turn puts "#{@current_player}'s Move:" @indicate_player_turn = @current_player + @current_player = "Computer" end def indicate_player_move @@ -59,7 +62,7 @@ def indicate_player_move end def computer_move - @computer_move + @computer_move =@open_spots.sample end def player_move @@ -70,10 +73,8 @@ def current_state @current_state = @board.values end - def board(player_move) - @player_move = player_move - - @board = [player_move] + def board(board) + @board = [board] end def determine_winner @@ -88,6 +89,7 @@ def determine_winner [:C1,:B2,:A3] ] + #if winning_combos[:] @@ -99,14 +101,31 @@ def open_spots end def draw + + @draw = false + + unless @open_spots == false + @draw = true + end end def player_won + @player_won == true end def computer_won + @computer_won == true end def over + if @computer_won == true + @over = true + elsif @player_won == true + @over = true + elsif @draw == true + @over = true + else + @over = false + end end end \ No newline at end of file From c7e716dae6271727e48d40d1b31c74a3b2bf90bf Mon Sep 17 00:00:00 2001 From: kellimohr Date: Mon, 10 Dec 2012 21:03:15 -0800 Subject: [PATCH 22/26] passed another test --- week7/homework/features/tic-tac-toe.rb | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/week7/homework/features/tic-tac-toe.rb b/week7/homework/features/tic-tac-toe.rb index f015131..9d2dc9a 100644 --- a/week7/homework/features/tic-tac-toe.rb +++ b/week7/homework/features/tic-tac-toe.rb @@ -1,19 +1,19 @@ class TicTacToe - attr_accessor :player, :computer, :player_symbol, :computer_symbol, :X, :O + attr_accessor :player, :computer, :player_symbol, :computer_symbol, :board SYMBOLS = [:X,:O] def initialize(starter=nil, symbol=nil) @starter = starter @symbol = symbol - unless @starter.nil? + unless @symbol.nil? if @starter == "Computer" - @computer_symbol = :X + @computer_symbol = @symbol @player_symbol = :O @computer = "Computer" else - @player_symbol = :X + @player_symbol = @symbol @computer_symbol = :O @player = @starter end @@ -49,6 +49,7 @@ def get_player_move #@current_player = (@current_player == TicTacToe::X) ? TicTacToe::O : TicTacToe::X @get_player_move = gets.chomp + #@get_player_move.to_sym end def indicate_player_turn @@ -66,16 +67,14 @@ def computer_move end def player_move - @player_move + board[get_player_move.to_sym] = @player_symbol + @player_move = get_player_move.to_sym + end def current_state @current_state = @board.values end - - def board(board) - @board = [board] - end def determine_winner @winning_combos = [ @@ -95,8 +94,8 @@ def determine_winner end - def open_spots - @open_spots = board.delete_if{|k,v| {} == v.delete_if{|a,b| b==""}} + def spots_open + @spots_open = board.delete_if{|k,v| {} == v.delete_if{|a,b| b==""}} if @board.inject.include(" ") then true else false end end @@ -104,7 +103,7 @@ def draw @draw = false - unless @open_spots == false + unless @spots_open == false @draw = true end end From 03906d9ff5b2024111be5968f28b8f6a0dec8710 Mon Sep 17 00:00:00 2001 From: kellimohr Date: Mon, 10 Dec 2012 21:35:20 -0800 Subject: [PATCH 23/26] passed test 120 --- week7/homework/features/tic-tac-toe.rb | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/week7/homework/features/tic-tac-toe.rb b/week7/homework/features/tic-tac-toe.rb index 9d2dc9a..e46e9fd 100644 --- a/week7/homework/features/tic-tac-toe.rb +++ b/week7/homework/features/tic-tac-toe.rb @@ -64,16 +64,17 @@ def indicate_player_move def computer_move @computer_move =@open_spots.sample + end def player_move - board[get_player_move.to_sym] = @player_symbol + board[get_player_move] = @player_symbol @player_move = get_player_move.to_sym end def current_state - @current_state = @board.values + @current_state = @board.values.to_s end def determine_winner @@ -94,12 +95,15 @@ def determine_winner end - def spots_open - @spots_open = board.delete_if{|k,v| {} == v.delete_if{|a,b| b==""}} - if @board.inject.include(" ") then true else false end + def open_spots + @open_spots = board.delete_if{|k,v| {} == v.delete_if{|a,b| b==""}} + end + + def spots_open? + if @board.values.include?(" ") then true else false end end - def draw + def draw? @draw = false @@ -108,15 +112,15 @@ def draw end end - def player_won + def player_won? @player_won == true end - def computer_won + def computer_won? @computer_won == true end - def over + def over? if @computer_won == true @over = true elsif @player_won == true From e4494c377f5313f26158894b5e9ae28482aeab94 Mon Sep 17 00:00:00 2001 From: kellimohr Date: Mon, 10 Dec 2012 22:22:31 -0800 Subject: [PATCH 24/26] 30 tests passed --- .../features/step_definitions/tic-tac-toe-steps.rb | 3 +-- week7/homework/features/tic-tac-toe.rb | 10 +--------- 2 files changed, 2 insertions(+), 11 deletions(-) diff --git a/week7/homework/features/step_definitions/tic-tac-toe-steps.rb b/week7/homework/features/step_definitions/tic-tac-toe-steps.rb index 2417433..9e6e2b9 100644 --- a/week7/homework/features/step_definitions/tic-tac-toe-steps.rb +++ b/week7/homework/features/step_definitions/tic-tac-toe-steps.rb @@ -125,11 +125,10 @@ Given /^it is the computers turn$/ do @game = TicTacToe.new(:computer, :O) - @game.current_player.should eq "Computer" end Then /^it is now the computers turn$/ do - pending # express the regexp above with the code you wish you had + @game.current_player.should eq "Computer" end When /^there are three Xs in a row$/ do diff --git a/week7/homework/features/tic-tac-toe.rb b/week7/homework/features/tic-tac-toe.rb index e46e9fd..4df46e4 100644 --- a/week7/homework/features/tic-tac-toe.rb +++ b/week7/homework/features/tic-tac-toe.rb @@ -47,15 +47,13 @@ def current_player def get_player_move - #@current_player = (@current_player == TicTacToe::X) ? TicTacToe::O : TicTacToe::X + #@current_player = (@current_player == SYMBOL::X) ? SYMBOL::O : SYMBOL::X @get_player_move = gets.chomp - #@get_player_move.to_sym end def indicate_player_turn puts "#{@current_player}'s Move:" @indicate_player_turn = @current_player - @current_player = "Computer" end def indicate_player_move @@ -70,7 +68,6 @@ def computer_move def player_move board[get_player_move] = @player_symbol @player_move = get_player_move.to_sym - end def current_state @@ -88,11 +85,6 @@ def determine_winner [:A1,:B2,:C3], [:C1,:B2,:A3] ] - - #if winning_combos[:] - - - end def open_spots From 64625a69d9d59785c7fb7ebff052f8dfddce4426 Mon Sep 17 00:00:00 2001 From: kellimohr Date: Tue, 11 Dec 2012 13:53:08 -0800 Subject: [PATCH 25/26] need to refactor smelly code --- week7/homework/features/tic-tac-toe.rb | 28 +++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/week7/homework/features/tic-tac-toe.rb b/week7/homework/features/tic-tac-toe.rb index 4df46e4..70b0640 100644 --- a/week7/homework/features/tic-tac-toe.rb +++ b/week7/homework/features/tic-tac-toe.rb @@ -46,7 +46,6 @@ def current_player end def get_player_move - #@current_player = (@current_player == SYMBOL::X) ? SYMBOL::O : SYMBOL::X @get_player_move = gets.chomp end @@ -75,6 +74,8 @@ def current_state end def determine_winner + i = 0 + winning_combos = [] @winning_combos = [ [:A1,:A2,:A3], [:B1,:B2,:B3], @@ -85,6 +86,31 @@ def determine_winner [:A1,:B2,:C3], [:C1,:B2,:A3] ] + + while i < 8 do + unless winning_combos[i].unique.length != 1 + if winning_combos[i].includes "X" && player_symbol = "X" + player_won? + elsif winning_combos[i].includes "Y" && player_symbol = "Y" + player_won? + elsif winning_combos[i].includes "X" && computer_symbol = "X" + computer_won? + elsif winning_combos[i].includes "Y" && computer_symbol = "Y" + computer_won? + else + end + end + + if spots_open? == false + draw? + else + if @current_player == "Computer" + @current_player = @player + else + current_player = "Computer" + end + end + end end def open_spots From eb092459e31a32079e66000e4b34491637b7bdc5 Mon Sep 17 00:00:00 2001 From: kellimohr Date: Tue, 11 Dec 2012 14:39:52 -0800 Subject: [PATCH 26/26] change to cucmber test --- .../features/step_definitions/tic-tac-toe-steps.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/week7/homework/features/step_definitions/tic-tac-toe-steps.rb b/week7/homework/features/step_definitions/tic-tac-toe-steps.rb index 9e6e2b9..b36fc9b 100644 --- a/week7/homework/features/step_definitions/tic-tac-toe-steps.rb +++ b/week7/homework/features/step_definitions/tic-tac-toe-steps.rb @@ -127,10 +127,10 @@ @game = TicTacToe.new(:computer, :O) end -Then /^it is now the computers turn$/ do - @game.current_player.should eq "Computer" -end - When /^there are three Xs in a row$/ do @game.board[:C1] = @game.board[:B2] = @game.board[:A3] = :X end + +Then /^it is now the computers turn$/ do + @game.current_player.should eq "Computer" +end \ No newline at end of file