From cbf50af2df22744c855918c064947e94322f54b6 Mon Sep 17 00:00:00 2001 From: brandon Date: Thu, 16 Jan 2014 12:07:24 -0800 Subject: [PATCH 01/16] Homework: Week 1 --- week1/homework/questions.txt | 9 +++++++++ week1/homework/strings_and_rspec_spec.rb | 11 ++++++----- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/week1/homework/questions.txt b/week1/homework/questions.txt index 2257bb9..95092d4 100644 --- a/week1/homework/questions.txt +++ b/week1/homework/questions.txt @@ -3,13 +3,22 @@ Chapter 3 Classes, Objects, and Variables p.86-90 Strings (Strings section in Chapter 6 Standard Types) 1. What is an object? +Objects are specific things that are known. For example, the title of a book. Classes are then created to categorize those known things into groups which share similar characteristics. 2. What is a variable? +A variable is a reference to an object, but it is not an object itself. 3. What is the difference between an object and a class? +All classes function as objects in Ruby, but not all objects are classes. Classes classify groups of things based on similar characertistics. In the assigned reading's bookstore example, certain genres (such as thrillers, romance, literary fiction, etc.) could represent classes that contain books (things/objects). A specific book within one of those classes would be an instance of that class. For example, the book "The Hunger Games" is categorized as Young Adult at bookstores, so this particular book (an object) would be an instance of the class YoungAdult. 4. What is a String? +A string is an object of class String that can hold sequences of printable characters or binary data. + 5. What are three messages that I can send to a string object? Hint: think methods +Squeeze, chomp, and split. Squeeze trims runs of repeated characters, which makes it good for removing extra spaces in a string. Chomp removes white space that occurs before or after a specified string. Split separates a string into two or more pieces. + 6. What are two ways of defining a String literal? Bonus: What is the difference between them? + +Single quotes (' ') and double quotes (" ") can both be used to define String literals. Single quote strings support two escape sequences: double backslash (\\), which prints a backslash within a string (\), and a backslash followed by a single quote (\'), which prints a single quote/apostrophe within the string ('I\'m going to the store today' => I'm going to the store today). Double quotes support even more escape sequences, including newline character (\n), which can be used to signify the end of a line. \ No newline at end of file diff --git a/week1/homework/strings_and_rspec_spec.rb b/week1/homework/strings_and_rspec_spec.rb index ea79e4c..69dcac8 100644 --- a/week1/homework/strings_and_rspec_spec.rb +++ b/week1/homework/strings_and_rspec_spec.rb @@ -12,14 +12,15 @@ 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" - it "should be able to split on the . charater" do - pending - result = #do something with @my_string here + it "should be able to count the characters" do + @my_string.should have(@my_string.length).characters + end + it "should be able to split on the . character" do + result = @my_string.chomp.split(".") result.should have(2).items end it "should be able to give the encoding of the string" do - pending 'helpful hint: should eq (Encoding.find("UTF-8"))' + @my_string.encoding.should eq (Encoding.find("UTF-8")) end end end From 90880b892ef7825d9571ddf2532006851a37b037 Mon Sep 17 00:00:00 2001 From: brandon Date: Thu, 16 Jan 2014 12:54:35 -0800 Subject: [PATCH 02/16] delete --- week1/exercises/roster.txt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/week1/exercises/roster.txt b/week1/exercises/roster.txt index 0642ee5..eaa23be 100644 --- a/week1/exercises/roster.txt +++ b/week1/exercises/roster.txt @@ -1,8 +1,8 @@ #, name, email, github, twitter, hip chat -0, Brandon, lafaveb@gmail.com, none, @BrandonLaFave +0, 1, 2, -3, +3, Brandon, lafaveb@gmail.com, none, @BrandonLaFave 4, 5, 6, From 466495f7858a68f638cf66b9d256e285db630a83 Mon Sep 17 00:00:00 2001 From: brandon Date: Thu, 23 Jan 2014 17:31:58 -0800 Subject: [PATCH 03/16] Homework: Week 2 --- week2/homework/questions.txt | 10 ++++++++++ week2/homework/simon_says.rb | 21 +++++++++++++++++++++ 2 files changed, 31 insertions(+) create mode 100644 week2/homework/simon_says.rb diff --git a/week2/homework/questions.txt b/week2/homework/questions.txt index 939e42d..5702353 100644 --- a/week2/homework/questions.txt +++ b/week2/homework/questions.txt @@ -4,10 +4,20 @@ Sharing Functionality: Inheritance, Modules, and Mixins 1. What is the difference between a Hash and an Array? +An array is an ordered list of objects that's indexed using integers. A hash is collection of key/value pairs. Hashes are indexed with objects of any type (symbols, strings, regular expressions, etc.). + 2. When would you use an Array over a Hash and vice versa? +Use hashes when indexing requires objects other than integers (i.e. strings, symbols, expressions, etc.). Use arrays when indexing with integers. + 3. What is a module? Enumerable is a built in Ruby module, what is it? +Modules are a way of grouping together methods, classes, and constants. They provide a namespace and prevent names clashes and support the mixin facility. Enumerable enables the "each" iterator and allows collections within classes to be sorted, traversed, etc. + 4. Can you inherit more than one thing in Ruby? How could you get around this problem? +Ruby doesn't support multiple inheritance. You could get around this problem by using mixins. + 5. What is the difference between a Module and a Class? + +Classes can inherit behavior and can be the base for inheritance. They can also be instantiated. Modules have no inheritance and cannot be instantiated. diff --git a/week2/homework/simon_says.rb b/week2/homework/simon_says.rb new file mode 100644 index 0000000..25e66a5 --- /dev/null +++ b/week2/homework/simon_says.rb @@ -0,0 +1,21 @@ +module SimonSays + def echo(string) + string + end + + def shout(string) + string.upcase + end + + def first_word(string) + string.split.first + end + + def start_of_word(string,i) + string[0...i] + end + + def repeat(string, x=2) + ([string]*x).join(' ') + end +end \ No newline at end of file From 4c9cc9585e976525faed3f6e57aa259fdca8bcf5 Mon Sep 17 00:00:00 2001 From: brandon Date: Wed, 12 Feb 2014 15:17:54 -0800 Subject: [PATCH 04/16] Homework: Week 3 --- week3/homework/calculator.rb | 22 +++++++++++++++++++ week3/homework/calculator.rb~ | 24 +++++++++++++++++++++ week3/homework/questions.txt | 25 ++++++++++++++++++++++ week3/homework/questions.txt~ | 40 +++++++++++++++++++++++++++++++++++ 4 files changed, 111 insertions(+) create mode 100644 week3/homework/calculator.rb create mode 100644 week3/homework/calculator.rb~ create mode 100644 week3/homework/questions.txt~ diff --git a/week3/homework/calculator.rb b/week3/homework/calculator.rb new file mode 100644 index 0000000..59ff7d6 --- /dev/null +++ b/week3/homework/calculator.rb @@ -0,0 +1,22 @@ +class Calculator + def sum(array) + array.inject(0){|sum, x| sum +x} + end + + def multiply(*numbers) + puts numbers.inspect + numbers.flatten.inject(:*) + end + + def pow(base, p) + pow_fac(base, p) + end + + def fac(n) + pow_fac(n) + end +private + def pow_fac(base=nil, p) + (1..p).to_a.inject(1){|f,v| f *= base || v} + end +end diff --git a/week3/homework/calculator.rb~ b/week3/homework/calculator.rb~ new file mode 100644 index 0000000..f169295 --- /dev/null +++ b/week3/homework/calculator.rb~ @@ -0,0 +1,24 @@ +class Calculator + def sum(array) + array.inject(0){|sum, x| sum +x} + end + + def multiply(*numbers) + puts numbers.inspect + numbers.flatten.inject(:*) + end + + def pow(base, p) + #(1...p).to_a.inject(base){|r,v| r *= base} + pow_fac(base, p) + 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 +end diff --git a/week3/homework/questions.txt b/week3/homework/questions.txt index dfb158d..4652cc6 100644 --- a/week3/homework/questions.txt +++ b/week3/homework/questions.txt @@ -6,10 +6,35 @@ Please Read: 1. What is a symbol? +A Ruby symbol is an identifier corresponding to a string of characters, often a name. + 2. What is the difference between a symbol and a string? +Symbols are immutable. Strings are mutable. + 3. What is a block and how do I call a block? +A block is simply a chunk of code enclosed between either braces or the keywords do and +end. You can call a block within a method using yield. + 4. How do I pass a block to a method? What is the method signature? +There are two mains ways to pass a block to a method. You can define the block after the method call with either the curly bracket enclosure {} or the do/end syntax. Or you can add a block to a method by putting an ampersand before the variable name. + +An example of defining the block after the method call: + +string.split {|s| puts s} + +An example of adding a block to a method with an ampersand: + +def your_method(&your_block) +your_block.call +end + 5. Where would you use regular expressions? + +Regular expressions are used for pattern matching and replacing/changing strings. Below are three examples of ways you can use regular expressions: + + You can use them to test a string to see whether it matches a pattern. + You can use them to extract from a string the sections that match all or part of a pattern. + You can use them to change the string, replacing parts that match a pattern. diff --git a/week3/homework/questions.txt~ b/week3/homework/questions.txt~ new file mode 100644 index 0000000..4652cc6 --- /dev/null +++ b/week3/homework/questions.txt~ @@ -0,0 +1,40 @@ +Please Read: + - Chapter 6 Standard Types + - Review Blocks + - Chapter 7 Regular Expressions + - Chapter 22 The Ruby Language: basic types (symbols), variables and constants + +1. What is a symbol? + +A Ruby symbol is an identifier corresponding to a string of characters, often a name. + +2. What is the difference between a symbol and a string? + +Symbols are immutable. Strings are mutable. + +3. What is a block and how do I call a block? + +A block is simply a chunk of code enclosed between either braces or the keywords do and +end. You can call a block within a method using yield. + +4. How do I pass a block to a method? What is the method signature? + +There are two mains ways to pass a block to a method. You can define the block after the method call with either the curly bracket enclosure {} or the do/end syntax. Or you can add a block to a method by putting an ampersand before the variable name. + +An example of defining the block after the method call: + +string.split {|s| puts s} + +An example of adding a block to a method with an ampersand: + +def your_method(&your_block) +your_block.call +end + +5. Where would you use regular expressions? + +Regular expressions are used for pattern matching and replacing/changing strings. Below are three examples of ways you can use regular expressions: + + You can use them to test a string to see whether it matches a pattern. + You can use them to extract from a string the sections that match all or part of a pattern. + You can use them to change the string, replacing parts that match a pattern. From 4578becd2c82124896b9c4066353e4995dd9b187 Mon Sep 17 00:00:00 2001 From: brandon Date: Wed, 12 Feb 2014 15:19:35 -0800 Subject: [PATCH 05/16] Homework: Week 4 --- week4/homework/questions.txt | 12 ++++++++++++ week4/homework/questions.txt~ | 26 ++++++++++++++++++++++++++ week4/homework/worker.rb | 7 +++++++ week4/homework/worker.rb~ | 7 +++++++ 4 files changed, 52 insertions(+) create mode 100644 week4/homework/questions.txt~ create mode 100644 week4/homework/worker.rb create mode 100644 week4/homework/worker.rb~ diff --git a/week4/homework/questions.txt b/week4/homework/questions.txt index 187b3d3..42d2c09 100644 --- a/week4/homework/questions.txt +++ b/week4/homework/questions.txt @@ -4,11 +4,23 @@ The Rake Gem: http://rake.rubyforge.org/ 1. How does Ruby read files? +Ruby reads files by using the File class (a subclass of the IO class) and its associated methods (File.open, File.gets, etc.). + 2. How would you output "Hello World!" to a file called my_output.txt? + File.open(​"my_output.txt"​, ​"w"​) ​do​ |file| + file.puts ​"Hello World"​ +​ end​ + 3. What is the Directory class and what is it used for? +The directory class represents directories in the underlying file system. They provide a variety of ways to list directories and their contents. + 4. What is an IO object? +An IO object is a bidirectional channel between a Ruby program and some external source. + 5. What is rake and what is it used for? What is a rake task? +Rake (also known as Ruby Make) is a build language written in Ruby that's used to define a set of tasks and the dependencies between them in a file. Rake tasks are the main unit of work in a Rakefile and have a name (usually a symbol or string), a list of prerequisites (more symbols or strings), and a list of actions (given as a block). + diff --git a/week4/homework/questions.txt~ b/week4/homework/questions.txt~ new file mode 100644 index 0000000..42d2c09 --- /dev/null +++ b/week4/homework/questions.txt~ @@ -0,0 +1,26 @@ +Please Read: +Chapter 10 Basic Input and Output +The Rake Gem: http://rake.rubyforge.org/ + +1. How does Ruby read files? + +Ruby reads files by using the File class (a subclass of the IO class) and its associated methods (File.open, File.gets, etc.). + +2. How would you output "Hello World!" to a file called my_output.txt? + + File.open(​"my_output.txt"​, ​"w"​) ​do​ |file| + file.puts ​"Hello World"​ +​ end​ + +3. What is the Directory class and what is it used for? + +The directory class represents directories in the underlying file system. They provide a variety of ways to list directories and their contents. + +4. What is an IO object? + +An IO object is a bidirectional channel between a Ruby program and some external source. + +5. What is rake and what is it used for? What is a rake task? + +Rake (also known as Ruby Make) is a build language written in Ruby that's used to define a set of tasks and the dependencies between them in a file. Rake tasks are the main unit of work in a Rakefile and have a name (usually a symbol or string), a list of prerequisites (more symbols or strings), and a list of actions (given as a block). + diff --git a/week4/homework/worker.rb b/week4/homework/worker.rb new file mode 100644 index 0000000..0b7a708 --- /dev/null +++ b/week4/homework/worker.rb @@ -0,0 +1,7 @@ +module Worker + def self.work t = 1 + r = nil + t.times { r = yield } + r + end +end diff --git a/week4/homework/worker.rb~ b/week4/homework/worker.rb~ new file mode 100644 index 0000000..0b7a708 --- /dev/null +++ b/week4/homework/worker.rb~ @@ -0,0 +1,7 @@ +module Worker + def self.work t = 1 + r = nil + t.times { r = yield } + r + end +end From 1a51d88cf995c7a9a1929a3039098c074db9f8f8 Mon Sep 17 00:00:00 2001 From: brandonlafave Date: Wed, 12 Feb 2014 15:23:22 -0800 Subject: [PATCH 06/16] Delete questions.txt~ --- week4/homework/questions.txt~ | 26 -------------------------- 1 file changed, 26 deletions(-) delete mode 100644 week4/homework/questions.txt~ diff --git a/week4/homework/questions.txt~ b/week4/homework/questions.txt~ deleted file mode 100644 index 42d2c09..0000000 --- a/week4/homework/questions.txt~ +++ /dev/null @@ -1,26 +0,0 @@ -Please Read: -Chapter 10 Basic Input and Output -The Rake Gem: http://rake.rubyforge.org/ - -1. How does Ruby read files? - -Ruby reads files by using the File class (a subclass of the IO class) and its associated methods (File.open, File.gets, etc.). - -2. How would you output "Hello World!" to a file called my_output.txt? - - File.open(​"my_output.txt"​, ​"w"​) ​do​ |file| - file.puts ​"Hello World"​ -​ end​ - -3. What is the Directory class and what is it used for? - -The directory class represents directories in the underlying file system. They provide a variety of ways to list directories and their contents. - -4. What is an IO object? - -An IO object is a bidirectional channel between a Ruby program and some external source. - -5. What is rake and what is it used for? What is a rake task? - -Rake (also known as Ruby Make) is a build language written in Ruby that's used to define a set of tasks and the dependencies between them in a file. Rake tasks are the main unit of work in a Rakefile and have a name (usually a symbol or string), a list of prerequisites (more symbols or strings), and a list of actions (given as a block). - From 083cc0e414c738b84cfd5639c1f3821daca31a2f Mon Sep 17 00:00:00 2001 From: brandonlafave Date: Wed, 12 Feb 2014 15:23:32 -0800 Subject: [PATCH 07/16] Delete worker.rb~ --- week4/homework/worker.rb~ | 7 ------- 1 file changed, 7 deletions(-) delete mode 100644 week4/homework/worker.rb~ diff --git a/week4/homework/worker.rb~ b/week4/homework/worker.rb~ deleted file mode 100644 index 0b7a708..0000000 --- a/week4/homework/worker.rb~ +++ /dev/null @@ -1,7 +0,0 @@ -module Worker - def self.work t = 1 - r = nil - t.times { r = yield } - r - end -end From e8dab3046925069d74dea3019ec41a5c49c7c6f5 Mon Sep 17 00:00:00 2001 From: brandonlafave Date: Wed, 12 Feb 2014 15:28:15 -0800 Subject: [PATCH 08/16] Delete calculator.rb~ --- week3/homework/calculator.rb~ | 24 ------------------------ 1 file changed, 24 deletions(-) delete mode 100644 week3/homework/calculator.rb~ diff --git a/week3/homework/calculator.rb~ b/week3/homework/calculator.rb~ deleted file mode 100644 index f169295..0000000 --- a/week3/homework/calculator.rb~ +++ /dev/null @@ -1,24 +0,0 @@ -class Calculator - def sum(array) - array.inject(0){|sum, x| sum +x} - end - - def multiply(*numbers) - puts numbers.inspect - numbers.flatten.inject(:*) - end - - def pow(base, p) - #(1...p).to_a.inject(base){|r,v| r *= base} - pow_fac(base, p) - 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 -end From aa63da02aca192422c8cd93af3204021a3943845 Mon Sep 17 00:00:00 2001 From: brandonlafave Date: Wed, 12 Feb 2014 15:28:24 -0800 Subject: [PATCH 09/16] Delete questions.txt~ --- week3/homework/questions.txt~ | 40 ----------------------------------- 1 file changed, 40 deletions(-) delete mode 100644 week3/homework/questions.txt~ diff --git a/week3/homework/questions.txt~ b/week3/homework/questions.txt~ deleted file mode 100644 index 4652cc6..0000000 --- a/week3/homework/questions.txt~ +++ /dev/null @@ -1,40 +0,0 @@ -Please Read: - - Chapter 6 Standard Types - - Review Blocks - - Chapter 7 Regular Expressions - - Chapter 22 The Ruby Language: basic types (symbols), variables and constants - -1. What is a symbol? - -A Ruby symbol is an identifier corresponding to a string of characters, often a name. - -2. What is the difference between a symbol and a string? - -Symbols are immutable. Strings are mutable. - -3. What is a block and how do I call a block? - -A block is simply a chunk of code enclosed between either braces or the keywords do and -end. You can call a block within a method using yield. - -4. How do I pass a block to a method? What is the method signature? - -There are two mains ways to pass a block to a method. You can define the block after the method call with either the curly bracket enclosure {} or the do/end syntax. Or you can add a block to a method by putting an ampersand before the variable name. - -An example of defining the block after the method call: - -string.split {|s| puts s} - -An example of adding a block to a method with an ampersand: - -def your_method(&your_block) -your_block.call -end - -5. Where would you use regular expressions? - -Regular expressions are used for pattern matching and replacing/changing strings. Below are three examples of ways you can use regular expressions: - - You can use them to test a string to see whether it matches a pattern. - You can use them to extract from a string the sections that match all or part of a pattern. - You can use them to change the string, replacing parts that match a pattern. From 9eb45a76080f9ee23f28cb994d51d2a449c85482 Mon Sep 17 00:00:00 2001 From: brandon Date: Thu, 13 Mar 2014 00:45:16 -0700 Subject: [PATCH 10/16] Week 7: Homework and Tic-Tac-Toe Final --- week7/homework/features/pirate.feature~ | 11 ++ .../features/step_definitions/pirate.rb | 17 +++ .../features/step_definitions/pirate.rb~ | 17 +++ .../step_definitions/tic-tac-toe-steps.rb~ | 124 +++++++++++++++++ .../features/step_definitions/tic-tac-toe.rb | 129 ++++++++++++++++++ .../features/step_definitions/tic-tac-toe.rb~ | 129 ++++++++++++++++++ week7/homework/features/tic-tac-toe.feature~ | 57 ++++++++ week7/homework/questions.txt | 19 ++- week7/homework/questions.txt~ | 24 ++++ 9 files changed, 525 insertions(+), 2 deletions(-) create mode 100644 week7/homework/features/pirate.feature~ create mode 100644 week7/homework/features/step_definitions/pirate.rb create mode 100644 week7/homework/features/step_definitions/pirate.rb~ create mode 100644 week7/homework/features/step_definitions/tic-tac-toe-steps.rb~ create mode 100644 week7/homework/features/step_definitions/tic-tac-toe.rb create mode 100644 week7/homework/features/step_definitions/tic-tac-toe.rb~ create mode 100644 week7/homework/features/tic-tac-toe.feature~ create mode 100644 week7/homework/questions.txt~ diff --git a/week7/homework/features/pirate.feature~ b/week7/homework/features/pirate.feature~ new file mode 100644 index 0000000..7de1a0b --- /dev/null +++ b/week7/homework/features/pirate.feature~ @@ -0,0 +1,11 @@ +# language: en-pirate + +Ahoy matey!: Pirate Speak + I would like help to talk like a pirate + +Heave to: The mighty speaking pirate + Gangway! I have a PirateTranslator + Blimey! I say 'Hello Friend' + Aye I hit translate + Let go and haul it prints out 'Ahoy Matey' + Avast! it also prints 'Shiber Me Timbers You Scurvey Dogs!!' diff --git a/week7/homework/features/step_definitions/pirate.rb b/week7/homework/features/step_definitions/pirate.rb new file mode 100644 index 0000000..cd4a163 --- /dev/null +++ b/week7/homework/features/step_definitions/pirate.rb @@ -0,0 +1,17 @@ +class PirateTranslator + Pirate_words = { + "Hello Friend" => "Ahoy Matey" + } + def say(str) + @said = lookup_pirate(str).to_s + end + + def translate + @said + "\n Shiber Me Timbers You Scurvey Dogs!!" + end + +private + def lookup_pirate(str) + Pirate_words[str] + end +end diff --git a/week7/homework/features/step_definitions/pirate.rb~ b/week7/homework/features/step_definitions/pirate.rb~ new file mode 100644 index 0000000..cd4a163 --- /dev/null +++ b/week7/homework/features/step_definitions/pirate.rb~ @@ -0,0 +1,17 @@ +class PirateTranslator + Pirate_words = { + "Hello Friend" => "Ahoy Matey" + } + def say(str) + @said = lookup_pirate(str).to_s + end + + def translate + @said + "\n Shiber Me Timbers You Scurvey Dogs!!" + end + +private + def lookup_pirate(str) + Pirate_words[str] + end +end diff --git a/week7/homework/features/step_definitions/tic-tac-toe-steps.rb~ b/week7/homework/features/step_definitions/tic-tac-toe-steps.rb~ new file mode 100644 index 0000000..a3287c1 --- /dev/null +++ b/week7/homework/features/step_definitions/tic-tac-toe-steps.rb~ @@ -0,0 +1,124 @@ +require 'rspec/mocks/standalone' +require 'rspec/expectations' +Given /^I start a new Tic\-Tac\-Toe game$/ do + @game = TicTacToe.new +end + +When /^I enter my name (\w+)$/ do |name| + @game.player = name +end + +Then /^the computer welcomes me to the game with "(.*?)"$/ do |arg1| + @game.welcome_player.should eq arg1 +end + +Then /^randomly chooses who goes first$/ do + [@game.player, "Computer"].should include @game.current_player +end + +Then /^who is X and who is O$/ do + TicTacToe::SYMBOLS.should include @game.player_symbol, @game.computer_symbol +end + +Given /^I have a started Tic\-Tac\-Toe game$/ do + @game = TicTacToe.new(:player) + @game.player = "Renee" +end + +Given /^it is my turn$/ do + @game.current_player.should eq "Renee" +end + +Given /^the computer knows my name is Renee$/ do + @game.player.should eq "Renee" +end + +Then /^the computer prints "(.*?)"$/ do |arg1| + @game.should_receive(:puts).with(arg1) + @game.indicate_palyer_turn +end + +Then /^waits for my input of "(.*?)"$/ do |arg1| + @game.should_receive(:gets).and_return(arg1) + @game.get_player_move +end + +Given /^it is the computer's turn$/ do + @game = TicTacToe.new(:computer, :O) + @game.current_player.should eq "Computer" +end + +Then /^the computer randomly chooses an open position for its move$/ do + open_spots = @game.open_spots + @com_move = @game.computer_move + open_spots.should include(@com_move) +end + +Given /^the computer is playing X$/ do + @game.computer_symbol.should eq :X +end + +Then /^the board should have an X on it$/ do + @game.current_state.should include 'X' +end + +Given /^I am playing X$/ do + @game = TicTacToe.new(:computer, :X) + @game.player_symbol.should eq :X +end + +When /^I enter a position "(.*?)" on the board$/ do |arg1| + @old_pos = @game.board[arg1.to_sym] + @game.should_receive(:get_player_move).and_return(arg1) + @game.player_move.should eq arg1.to_sym +end + +When /^"(.*?)" is not taken$/ do |arg1| + @old_pos.should eq " " +end + +Then /^it is now the computer's turn$/ do + @game.current_player.should eq "Computer" +end + +When /^there are three X's in a row$/ do + @game = TicTacToe.new(:computer, :X) + @game.board[:C1] = @game.board[:B2] = @game.board[:A3] = :X +end + +Then /^I am declared the winner$/ do + @game.determine_winner + @game.player_won?.should be_true +end + +Then /^the game ends$/ do + @game.over?.should be_true +end + +Given /^there are not three symbols in a row$/ do + @game.board = { + :A1 => :X, :A2 => :O, :A3 => :X, + :B1 => :X, :B2 => :O, :B3 => :X, + :C1 => :O, :C2 => :X, :C3 => :O + } + @game.determine_winner +end + +When /^there are no open spaces left on the board$/ do + @game.spots_open?.should be_false +end + +Then /^the game is declared a draw$/ do + @game.draw?.should be_true +end + +When /^"(.*?)" is taken$/ do |arg1| + @game.board[arg1.to_sym] = :O + @taken_spot = arg1.to_sym +end + +Then /^computer should ask me for another position "(.*?)"$/ do |arg1| + @game.board[arg1.to_sym] = ' ' + @game.should_receive(:get_player_move).twice.and_return(@taken_spot, arg1) + @game.player_move.should eq arg1.to_sym +end diff --git a/week7/homework/features/step_definitions/tic-tac-toe.rb b/week7/homework/features/step_definitions/tic-tac-toe.rb new file mode 100644 index 0000000..9213581 --- /dev/null +++ b/week7/homework/features/step_definitions/tic-tac-toe.rb @@ -0,0 +1,129 @@ +class TicTacToe + SYMBOLS = [:X,:O] + attr_accessor :player, :board + + def initialize(current_player=nil,player_sym=nil) + setup_board + @current_player = current_player || [:computer, :player].sample + choose_player_symbol(player_sym) + end + + def computer_symbol + @player_symbol[:computer] + end + + def player_symbol + @player_symbol[:player] + end + + def current_player + {:computer => "Computer", + :player => @player}[@current_player] + end + + def welcome_player + "Welcome #{@player}" + end + + def indicate_palyer_turn + puts "#{@player}'s Move:" + end + + def get_player_move + gets.chomp + end + + def player_move + move = get_player_move.to_sym + until open_spots.include?(move) + move = get_player_move.to_sym + end + @board[move] = player_symbol + @current_player = :computer + move + end + + def computer_move + move = get_computer_move + @board[move] = computer_symbol + @current_player = :player + move + end + + def get_computer_move + @board.select{|k,v| v.to_s.strip.empty?}.map{|k,v| k}.sample + end + + def current_state + row1 = "#{@board[:A1]}|#{@board[:A2]}|#{@board[:A3]}\n" + row2 = "#{@board[:B1]}|#{@board[:B2]}|#{@board[:B3]}\n" + row3 = "#{@board[:C1]}|#{@board[:C2]}|#{@board[:C3]}\n" + row1 + "-"*row1.size+"\n"+ + row2 + "-"*row2.size+"\n"+ + row3 + "-"*row3.size+"\n"+ + "******" + end + + def determine_winner + p_spots = @board.select{|k,v| v==player_symbol} + c_spots = @board.select{|k,v| v==computer_symbol} + + player_spots = p_spots.map{|k,v| {k[0].to_sym => k[1].to_i} } + computer_spots = c_spots.map{|k,v| {k[0].to_sym => k[1].to_i} } + @player_win = false + @computer_win = false + [:A, :B, :C].each do |l| + return if @player_win = player_spots.map{|i| i[l]}.reject{|f| f.nil?}.sort == [1,2,3] + return if @computer_win = computer_spots.map{|i| i[l]}.reject{|f| f.nil?}.sort == [1,2,3] + end + + [1,2,3].each do |l| + return if @player_win = player_spots.map{|i| i.invert[l]}.reject{|f| f.nil?}.sort == [:A,:B,:C] + return if @computer_win = computer_spots.map{|i| i.invert[l]}.reject{|f| f.nil?}.sort == [:A,:B,:C] + end + + return if @player_win = p_spots.keys.sort.reject{|r| ![:A1,:B2,:C3].include? r} == [:A1,:B2,:C3] + return if @player_win = p_spots.keys.sort.reject{|r| ![:A3,:B2,:C1].include? r} == [:A3,:B2,:C1] + return if @computer_win = c_spots.keys.sort.reject{|r| ![:A1,:B2,:C3].include? r} == [:A1,:B2,:C3] + return if @computer_win = c_spots.keys.sort.reject{|r| ![:A3,:B2,:C1].include? r} == [:A3,:B2,:C1] + end + + def player_won? + !!@player_win + end + + def computer_won? + !!@computer_win + end + + def draw? + !player_won? && !computer_won? + end + + def over? + player_won? || computer_won? || !spots_open? + end + + def spots_open? + !open_spots.empty? + end + + def open_spots + @board.select{|k,v| v.to_s.strip.empty?}.map{|k,v| k} + end + +private + def setup_board + @board = {:A1 => ' ', :A2 => ' ', :A3 => ' ', + :B1 => ' ', :B2 => ' ', :B3 => ' ', + :C1 => ' ', :C2 => ' ', :C3 => ' '} + end + + def choose_player_symbol(player_sym=nil) + player_sym ||= SYMBOLS.sample + @player_symbol = { + :computer => SYMBOLS.reject{|s| s==player_sym}.first, + :player => player_sym + } + end +end diff --git a/week7/homework/features/step_definitions/tic-tac-toe.rb~ b/week7/homework/features/step_definitions/tic-tac-toe.rb~ new file mode 100644 index 0000000..9213581 --- /dev/null +++ b/week7/homework/features/step_definitions/tic-tac-toe.rb~ @@ -0,0 +1,129 @@ +class TicTacToe + SYMBOLS = [:X,:O] + attr_accessor :player, :board + + def initialize(current_player=nil,player_sym=nil) + setup_board + @current_player = current_player || [:computer, :player].sample + choose_player_symbol(player_sym) + end + + def computer_symbol + @player_symbol[:computer] + end + + def player_symbol + @player_symbol[:player] + end + + def current_player + {:computer => "Computer", + :player => @player}[@current_player] + end + + def welcome_player + "Welcome #{@player}" + end + + def indicate_palyer_turn + puts "#{@player}'s Move:" + end + + def get_player_move + gets.chomp + end + + def player_move + move = get_player_move.to_sym + until open_spots.include?(move) + move = get_player_move.to_sym + end + @board[move] = player_symbol + @current_player = :computer + move + end + + def computer_move + move = get_computer_move + @board[move] = computer_symbol + @current_player = :player + move + end + + def get_computer_move + @board.select{|k,v| v.to_s.strip.empty?}.map{|k,v| k}.sample + end + + def current_state + row1 = "#{@board[:A1]}|#{@board[:A2]}|#{@board[:A3]}\n" + row2 = "#{@board[:B1]}|#{@board[:B2]}|#{@board[:B3]}\n" + row3 = "#{@board[:C1]}|#{@board[:C2]}|#{@board[:C3]}\n" + row1 + "-"*row1.size+"\n"+ + row2 + "-"*row2.size+"\n"+ + row3 + "-"*row3.size+"\n"+ + "******" + end + + def determine_winner + p_spots = @board.select{|k,v| v==player_symbol} + c_spots = @board.select{|k,v| v==computer_symbol} + + player_spots = p_spots.map{|k,v| {k[0].to_sym => k[1].to_i} } + computer_spots = c_spots.map{|k,v| {k[0].to_sym => k[1].to_i} } + @player_win = false + @computer_win = false + [:A, :B, :C].each do |l| + return if @player_win = player_spots.map{|i| i[l]}.reject{|f| f.nil?}.sort == [1,2,3] + return if @computer_win = computer_spots.map{|i| i[l]}.reject{|f| f.nil?}.sort == [1,2,3] + end + + [1,2,3].each do |l| + return if @player_win = player_spots.map{|i| i.invert[l]}.reject{|f| f.nil?}.sort == [:A,:B,:C] + return if @computer_win = computer_spots.map{|i| i.invert[l]}.reject{|f| f.nil?}.sort == [:A,:B,:C] + end + + return if @player_win = p_spots.keys.sort.reject{|r| ![:A1,:B2,:C3].include? r} == [:A1,:B2,:C3] + return if @player_win = p_spots.keys.sort.reject{|r| ![:A3,:B2,:C1].include? r} == [:A3,:B2,:C1] + return if @computer_win = c_spots.keys.sort.reject{|r| ![:A1,:B2,:C3].include? r} == [:A1,:B2,:C3] + return if @computer_win = c_spots.keys.sort.reject{|r| ![:A3,:B2,:C1].include? r} == [:A3,:B2,:C1] + end + + def player_won? + !!@player_win + end + + def computer_won? + !!@computer_win + end + + def draw? + !player_won? && !computer_won? + end + + def over? + player_won? || computer_won? || !spots_open? + end + + def spots_open? + !open_spots.empty? + end + + def open_spots + @board.select{|k,v| v.to_s.strip.empty?}.map{|k,v| k} + end + +private + def setup_board + @board = {:A1 => ' ', :A2 => ' ', :A3 => ' ', + :B1 => ' ', :B2 => ' ', :B3 => ' ', + :C1 => ' ', :C2 => ' ', :C3 => ' '} + end + + def choose_player_symbol(player_sym=nil) + player_sym ||= SYMBOLS.sample + @player_symbol = { + :computer => SYMBOLS.reject{|s| s==player_sym}.first, + :player => player_sym + } + end +end diff --git a/week7/homework/features/tic-tac-toe.feature~ b/week7/homework/features/tic-tac-toe.feature~ new file mode 100644 index 0000000..6f3134d --- /dev/null +++ b/week7/homework/features/tic-tac-toe.feature~ @@ -0,0 +1,57 @@ +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 + +Scenario: Begin Game + Given I start a new Tic-Tac-Toe game + When I enter my name Renee + Then the computer welcomes me to the game with "Welcome Renee" + And randomly chooses who goes first + And who is X and who is O + +Scenario: My Turn + Given I have a started Tic-Tac-Toe game + And it is my turn + And the computer knows my name is Renee + Then the computer prints "Renee's Move:" + And waits for my input of "B2" + +Scenario: Computer's Turn + Given I have a started Tic-Tac-Toe game + And it is the computer's turn + And the computer is playing X + Then the computer randomly chooses an open position for its move + And the board should have an X on it + +Scenario: Making Moves + Given I have a started Tic-Tac-Toe game + And it is my turn + And I am playing X + When I enter a position "A1" on the board + And "A1" is not taken + Then the board should have an X on it + And it is now the computer's turn + +Scenario: Making Bad Moves + Given I have a started Tic-Tac-Toe game + And it is my turn + And I am playing X + When I enter a position "A1" on the board + And "A1" is taken + Then computer should ask me for another position "B2" + And it is now the computer's turn + +Scenario: Winning the Game + Given I have a started Tic-Tac-Toe game + And I am playing X + When there are three X's in a row + Then I am declared the winner + And the game ends + +Scenario: Game is a draw + Given I have a started Tic-Tac-Toe game + And there are not three symbols in a row + When there are no open spaces left on the board + Then the game is declared a draw + And the game ends diff --git a/week7/homework/questions.txt b/week7/homework/questions.txt index d55387d..1c4009c 100644 --- a/week7/homework/questions.txt +++ b/week7/homework/questions.txt @@ -3,7 +3,22 @@ Please Read Chapters 23 and 24 DuckTyping and MetaProgramming Questions: 1. What is method_missing and how can it be used? -2. What is and Eigenclass and what is it used for? Where Do Singleton methods live? -3. When would you use DuckTypeing? How would you use it to improve your code? + +When Ruby can't find a called method in the object's class, superclass, etc., it calls method_missing, a method that raises an exception (either a NoMethodError or a NameError depending on the circumstances). Method_missing can be used to intercept every use of an undefined method and handle it. It can also intercept all calls, but it handles only some of them. + +2. What is a Eigenclass and what is it used for? Where Do Singleton methods live? + +A eigenclass is an anonymous class (also known as a singleton class) located between an object and the object's original class that defines methods for a specific object. Singleton methods live within the eigenclass/singleton class. + +3. When would you use DuckTyping? How would you use it to improve your code? + +With DuckTyping, an object's type is defined by its behavior (i.e. what it can do) and not its class. In other words, if quacks like a duck, walks like a duck, and looks like a duck, it's a duck regardless of what its class really is. I'd use DuckTyping for when I wanted to avoid type-related errors, improve garbage collection, and increase my productivity by writing less code. Using DuckTyping would allow me to avoid testing a class in order to determine its type. Without the check, the method I'm working with will be more felxible, and I could pass it to an array, a string, a file, or any other object that appends using <<, and it would just work. + 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. Instance methods, on the other hand, are methods that are called on an instance of a class. Class_eval and instance_eval differ in the way they set up the environment for method definition. Class_eval sets things up as if you were in the body of a class definition, so method definitions will define instance methods. Instance_eval, on the other hand, acts as if you were working inside the singleton class of self. Any methods you define will become class methods. + 5. What is the difference between a singleton class and a singleton method? + +A singleton method is a method that belongs to a single object. A singleton class is a class which defines a single object. + diff --git a/week7/homework/questions.txt~ b/week7/homework/questions.txt~ new file mode 100644 index 0000000..1c4009c --- /dev/null +++ b/week7/homework/questions.txt~ @@ -0,0 +1,24 @@ + +Please Read Chapters 23 and 24 DuckTyping and MetaProgramming + +Questions: +1. What is method_missing and how can it be used? + +When Ruby can't find a called method in the object's class, superclass, etc., it calls method_missing, a method that raises an exception (either a NoMethodError or a NameError depending on the circumstances). Method_missing can be used to intercept every use of an undefined method and handle it. It can also intercept all calls, but it handles only some of them. + +2. What is a Eigenclass and what is it used for? Where Do Singleton methods live? + +A eigenclass is an anonymous class (also known as a singleton class) located between an object and the object's original class that defines methods for a specific object. Singleton methods live within the eigenclass/singleton class. + +3. When would you use DuckTyping? How would you use it to improve your code? + +With DuckTyping, an object's type is defined by its behavior (i.e. what it can do) and not its class. In other words, if quacks like a duck, walks like a duck, and looks like a duck, it's a duck regardless of what its class really is. I'd use DuckTyping for when I wanted to avoid type-related errors, improve garbage collection, and increase my productivity by writing less code. Using DuckTyping would allow me to avoid testing a class in order to determine its type. Without the check, the method I'm working with will be more felxible, and I could pass it to an array, a string, a file, or any other object that appends using <<, and it would just work. + +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. Instance methods, on the other hand, are methods that are called on an instance of a class. Class_eval and instance_eval differ in the way they set up the environment for method definition. Class_eval sets things up as if you were in the body of a class definition, so method definitions will define instance methods. Instance_eval, on the other hand, acts as if you were working inside the singleton class of self. Any methods you define will become class methods. + +5. What is the difference between a singleton class and a singleton method? + +A singleton method is a method that belongs to a single object. A singleton class is a class which defines a single object. + From 802b4597c8bcd5c9c068735e68a0180bf192cca3 Mon Sep 17 00:00:00 2001 From: brandonlafave Date: Thu, 13 Mar 2014 00:49:56 -0700 Subject: [PATCH 11/16] Delete tic-tac-toe.rb~ --- .../features/step_definitions/tic-tac-toe.rb~ | 129 ------------------ 1 file changed, 129 deletions(-) delete mode 100644 week7/homework/features/step_definitions/tic-tac-toe.rb~ diff --git a/week7/homework/features/step_definitions/tic-tac-toe.rb~ b/week7/homework/features/step_definitions/tic-tac-toe.rb~ deleted file mode 100644 index 9213581..0000000 --- a/week7/homework/features/step_definitions/tic-tac-toe.rb~ +++ /dev/null @@ -1,129 +0,0 @@ -class TicTacToe - SYMBOLS = [:X,:O] - attr_accessor :player, :board - - def initialize(current_player=nil,player_sym=nil) - setup_board - @current_player = current_player || [:computer, :player].sample - choose_player_symbol(player_sym) - end - - def computer_symbol - @player_symbol[:computer] - end - - def player_symbol - @player_symbol[:player] - end - - def current_player - {:computer => "Computer", - :player => @player}[@current_player] - end - - def welcome_player - "Welcome #{@player}" - end - - def indicate_palyer_turn - puts "#{@player}'s Move:" - end - - def get_player_move - gets.chomp - end - - def player_move - move = get_player_move.to_sym - until open_spots.include?(move) - move = get_player_move.to_sym - end - @board[move] = player_symbol - @current_player = :computer - move - end - - def computer_move - move = get_computer_move - @board[move] = computer_symbol - @current_player = :player - move - end - - def get_computer_move - @board.select{|k,v| v.to_s.strip.empty?}.map{|k,v| k}.sample - end - - def current_state - row1 = "#{@board[:A1]}|#{@board[:A2]}|#{@board[:A3]}\n" - row2 = "#{@board[:B1]}|#{@board[:B2]}|#{@board[:B3]}\n" - row3 = "#{@board[:C1]}|#{@board[:C2]}|#{@board[:C3]}\n" - row1 + "-"*row1.size+"\n"+ - row2 + "-"*row2.size+"\n"+ - row3 + "-"*row3.size+"\n"+ - "******" - end - - def determine_winner - p_spots = @board.select{|k,v| v==player_symbol} - c_spots = @board.select{|k,v| v==computer_symbol} - - player_spots = p_spots.map{|k,v| {k[0].to_sym => k[1].to_i} } - computer_spots = c_spots.map{|k,v| {k[0].to_sym => k[1].to_i} } - @player_win = false - @computer_win = false - [:A, :B, :C].each do |l| - return if @player_win = player_spots.map{|i| i[l]}.reject{|f| f.nil?}.sort == [1,2,3] - return if @computer_win = computer_spots.map{|i| i[l]}.reject{|f| f.nil?}.sort == [1,2,3] - end - - [1,2,3].each do |l| - return if @player_win = player_spots.map{|i| i.invert[l]}.reject{|f| f.nil?}.sort == [:A,:B,:C] - return if @computer_win = computer_spots.map{|i| i.invert[l]}.reject{|f| f.nil?}.sort == [:A,:B,:C] - end - - return if @player_win = p_spots.keys.sort.reject{|r| ![:A1,:B2,:C3].include? r} == [:A1,:B2,:C3] - return if @player_win = p_spots.keys.sort.reject{|r| ![:A3,:B2,:C1].include? r} == [:A3,:B2,:C1] - return if @computer_win = c_spots.keys.sort.reject{|r| ![:A1,:B2,:C3].include? r} == [:A1,:B2,:C3] - return if @computer_win = c_spots.keys.sort.reject{|r| ![:A3,:B2,:C1].include? r} == [:A3,:B2,:C1] - end - - def player_won? - !!@player_win - end - - def computer_won? - !!@computer_win - end - - def draw? - !player_won? && !computer_won? - end - - def over? - player_won? || computer_won? || !spots_open? - end - - def spots_open? - !open_spots.empty? - end - - def open_spots - @board.select{|k,v| v.to_s.strip.empty?}.map{|k,v| k} - end - -private - def setup_board - @board = {:A1 => ' ', :A2 => ' ', :A3 => ' ', - :B1 => ' ', :B2 => ' ', :B3 => ' ', - :C1 => ' ', :C2 => ' ', :C3 => ' '} - end - - def choose_player_symbol(player_sym=nil) - player_sym ||= SYMBOLS.sample - @player_symbol = { - :computer => SYMBOLS.reject{|s| s==player_sym}.first, - :player => player_sym - } - end -end From 1ee8331df4dae1851c8ad53181c13802a599e9cd Mon Sep 17 00:00:00 2001 From: brandonlafave Date: Thu, 13 Mar 2014 00:50:09 -0700 Subject: [PATCH 12/16] Delete pirate.rb~ --- .../features/step_definitions/pirate.rb~ | 17 ----------------- 1 file changed, 17 deletions(-) delete mode 100644 week7/homework/features/step_definitions/pirate.rb~ diff --git a/week7/homework/features/step_definitions/pirate.rb~ b/week7/homework/features/step_definitions/pirate.rb~ deleted file mode 100644 index cd4a163..0000000 --- a/week7/homework/features/step_definitions/pirate.rb~ +++ /dev/null @@ -1,17 +0,0 @@ -class PirateTranslator - Pirate_words = { - "Hello Friend" => "Ahoy Matey" - } - def say(str) - @said = lookup_pirate(str).to_s - end - - def translate - @said + "\n Shiber Me Timbers You Scurvey Dogs!!" - end - -private - def lookup_pirate(str) - Pirate_words[str] - end -end From 7422b64e0b5a2e71119f3fda1ee020a2d9543c78 Mon Sep 17 00:00:00 2001 From: brandonlafave Date: Thu, 13 Mar 2014 00:50:18 -0700 Subject: [PATCH 13/16] Delete tic-tac-toe-steps.rb~ --- .../step_definitions/tic-tac-toe-steps.rb~ | 124 ------------------ 1 file changed, 124 deletions(-) delete mode 100644 week7/homework/features/step_definitions/tic-tac-toe-steps.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~ deleted file mode 100644 index a3287c1..0000000 --- a/week7/homework/features/step_definitions/tic-tac-toe-steps.rb~ +++ /dev/null @@ -1,124 +0,0 @@ -require 'rspec/mocks/standalone' -require 'rspec/expectations' -Given /^I start a new Tic\-Tac\-Toe game$/ do - @game = TicTacToe.new -end - -When /^I enter my name (\w+)$/ do |name| - @game.player = name -end - -Then /^the computer welcomes me to the game with "(.*?)"$/ do |arg1| - @game.welcome_player.should eq arg1 -end - -Then /^randomly chooses who goes first$/ do - [@game.player, "Computer"].should include @game.current_player -end - -Then /^who is X and who is O$/ do - TicTacToe::SYMBOLS.should include @game.player_symbol, @game.computer_symbol -end - -Given /^I have a started Tic\-Tac\-Toe game$/ do - @game = TicTacToe.new(:player) - @game.player = "Renee" -end - -Given /^it is my turn$/ do - @game.current_player.should eq "Renee" -end - -Given /^the computer knows my name is Renee$/ do - @game.player.should eq "Renee" -end - -Then /^the computer prints "(.*?)"$/ do |arg1| - @game.should_receive(:puts).with(arg1) - @game.indicate_palyer_turn -end - -Then /^waits for my input of "(.*?)"$/ do |arg1| - @game.should_receive(:gets).and_return(arg1) - @game.get_player_move -end - -Given /^it is the computer's turn$/ do - @game = TicTacToe.new(:computer, :O) - @game.current_player.should eq "Computer" -end - -Then /^the computer randomly chooses an open position for its move$/ do - open_spots = @game.open_spots - @com_move = @game.computer_move - open_spots.should include(@com_move) -end - -Given /^the computer is playing X$/ do - @game.computer_symbol.should eq :X -end - -Then /^the board should have an X on it$/ do - @game.current_state.should include 'X' -end - -Given /^I am playing X$/ do - @game = TicTacToe.new(:computer, :X) - @game.player_symbol.should eq :X -end - -When /^I enter a position "(.*?)" on the board$/ do |arg1| - @old_pos = @game.board[arg1.to_sym] - @game.should_receive(:get_player_move).and_return(arg1) - @game.player_move.should eq arg1.to_sym -end - -When /^"(.*?)" is not taken$/ do |arg1| - @old_pos.should eq " " -end - -Then /^it is now the computer's turn$/ do - @game.current_player.should eq "Computer" -end - -When /^there are three X's in a row$/ do - @game = TicTacToe.new(:computer, :X) - @game.board[:C1] = @game.board[:B2] = @game.board[:A3] = :X -end - -Then /^I am declared the winner$/ do - @game.determine_winner - @game.player_won?.should be_true -end - -Then /^the game ends$/ do - @game.over?.should be_true -end - -Given /^there are not three symbols in a row$/ do - @game.board = { - :A1 => :X, :A2 => :O, :A3 => :X, - :B1 => :X, :B2 => :O, :B3 => :X, - :C1 => :O, :C2 => :X, :C3 => :O - } - @game.determine_winner -end - -When /^there are no open spaces left on the board$/ do - @game.spots_open?.should be_false -end - -Then /^the game is declared a draw$/ do - @game.draw?.should be_true -end - -When /^"(.*?)" is taken$/ do |arg1| - @game.board[arg1.to_sym] = :O - @taken_spot = arg1.to_sym -end - -Then /^computer should ask me for another position "(.*?)"$/ do |arg1| - @game.board[arg1.to_sym] = ' ' - @game.should_receive(:get_player_move).twice.and_return(@taken_spot, arg1) - @game.player_move.should eq arg1.to_sym -end From 5199d586205acc2d777272dc5b213b522d2c602c Mon Sep 17 00:00:00 2001 From: brandonlafave Date: Thu, 13 Mar 2014 00:51:13 -0700 Subject: [PATCH 14/16] Delete pirate.feature~ --- week7/homework/features/pirate.feature~ | 11 ----------- 1 file changed, 11 deletions(-) delete mode 100644 week7/homework/features/pirate.feature~ diff --git a/week7/homework/features/pirate.feature~ b/week7/homework/features/pirate.feature~ deleted file mode 100644 index 7de1a0b..0000000 --- a/week7/homework/features/pirate.feature~ +++ /dev/null @@ -1,11 +0,0 @@ -# language: en-pirate - -Ahoy matey!: Pirate Speak - I would like help to talk like a pirate - -Heave to: The mighty speaking pirate - Gangway! I have a PirateTranslator - Blimey! I say 'Hello Friend' - Aye I hit translate - Let go and haul it prints out 'Ahoy Matey' - Avast! it also prints 'Shiber Me Timbers You Scurvey Dogs!!' From 2f9e68dbbc457a29a0f961dc446b734ce9b7852e Mon Sep 17 00:00:00 2001 From: brandonlafave Date: Thu, 13 Mar 2014 00:51:20 -0700 Subject: [PATCH 15/16] Delete tic-tac-toe.feature~ --- week7/homework/features/tic-tac-toe.feature~ | 57 -------------------- 1 file changed, 57 deletions(-) delete mode 100644 week7/homework/features/tic-tac-toe.feature~ diff --git a/week7/homework/features/tic-tac-toe.feature~ b/week7/homework/features/tic-tac-toe.feature~ deleted file mode 100644 index 6f3134d..0000000 --- a/week7/homework/features/tic-tac-toe.feature~ +++ /dev/null @@ -1,57 +0,0 @@ -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 - -Scenario: Begin Game - Given I start a new Tic-Tac-Toe game - When I enter my name Renee - Then the computer welcomes me to the game with "Welcome Renee" - And randomly chooses who goes first - And who is X and who is O - -Scenario: My Turn - Given I have a started Tic-Tac-Toe game - And it is my turn - And the computer knows my name is Renee - Then the computer prints "Renee's Move:" - And waits for my input of "B2" - -Scenario: Computer's Turn - Given I have a started Tic-Tac-Toe game - And it is the computer's turn - And the computer is playing X - Then the computer randomly chooses an open position for its move - And the board should have an X on it - -Scenario: Making Moves - Given I have a started Tic-Tac-Toe game - And it is my turn - And I am playing X - When I enter a position "A1" on the board - And "A1" is not taken - Then the board should have an X on it - And it is now the computer's turn - -Scenario: Making Bad Moves - Given I have a started Tic-Tac-Toe game - And it is my turn - And I am playing X - When I enter a position "A1" on the board - And "A1" is taken - Then computer should ask me for another position "B2" - And it is now the computer's turn - -Scenario: Winning the Game - Given I have a started Tic-Tac-Toe game - And I am playing X - When there are three X's in a row - Then I am declared the winner - And the game ends - -Scenario: Game is a draw - Given I have a started Tic-Tac-Toe game - And there are not three symbols in a row - When there are no open spaces left on the board - Then the game is declared a draw - And the game ends From f3e2184c8935cedb2e84b5557f0bce0f8d1fe1f7 Mon Sep 17 00:00:00 2001 From: brandonlafave Date: Thu, 13 Mar 2014 00:51:53 -0700 Subject: [PATCH 16/16] Delete questions.txt~ --- week7/homework/questions.txt~ | 24 ------------------------ 1 file changed, 24 deletions(-) delete mode 100644 week7/homework/questions.txt~ diff --git a/week7/homework/questions.txt~ b/week7/homework/questions.txt~ deleted file mode 100644 index 1c4009c..0000000 --- a/week7/homework/questions.txt~ +++ /dev/null @@ -1,24 +0,0 @@ - -Please Read Chapters 23 and 24 DuckTyping and MetaProgramming - -Questions: -1. What is method_missing and how can it be used? - -When Ruby can't find a called method in the object's class, superclass, etc., it calls method_missing, a method that raises an exception (either a NoMethodError or a NameError depending on the circumstances). Method_missing can be used to intercept every use of an undefined method and handle it. It can also intercept all calls, but it handles only some of them. - -2. What is a Eigenclass and what is it used for? Where Do Singleton methods live? - -A eigenclass is an anonymous class (also known as a singleton class) located between an object and the object's original class that defines methods for a specific object. Singleton methods live within the eigenclass/singleton class. - -3. When would you use DuckTyping? How would you use it to improve your code? - -With DuckTyping, an object's type is defined by its behavior (i.e. what it can do) and not its class. In other words, if quacks like a duck, walks like a duck, and looks like a duck, it's a duck regardless of what its class really is. I'd use DuckTyping for when I wanted to avoid type-related errors, improve garbage collection, and increase my productivity by writing less code. Using DuckTyping would allow me to avoid testing a class in order to determine its type. Without the check, the method I'm working with will be more felxible, and I could pass it to an array, a string, a file, or any other object that appends using <<, and it would just work. - -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. Instance methods, on the other hand, are methods that are called on an instance of a class. Class_eval and instance_eval differ in the way they set up the environment for method definition. Class_eval sets things up as if you were in the body of a class definition, so method definitions will define instance methods. Instance_eval, on the other hand, acts as if you were working inside the singleton class of self. Any methods you define will become class methods. - -5. What is the difference between a singleton class and a singleton method? - -A singleton method is a method that belongs to a single object. A singleton class is a class which defines a single object. -