From 857cee5ab72a570767209d0e39e692b1ebcf58f5 Mon Sep 17 00:00:00 2001 From: Eddie Lee Date: Sun, 25 Nov 2012 01:59:12 -0800 Subject: [PATCH 1/8] homework07 files --- week7/homework/features/pirate.rb | 2 ++ .../features/step_definitions/pirate-steps.rb | 19 +++++++++++++++++++ 2 files changed, 21 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..774169d --- /dev/null +++ b/week7/homework/features/pirate.rb @@ -0,0 +1,2 @@ +class PirateTranslator +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..0e4f9cb --- /dev/null +++ b/week7/homework/features/step_definitions/pirate-steps.rb @@ -0,0 +1,19 @@ +Gangway /^I have a PirateTranslator$/ do + pending # express the regexp above with the code you wish you had +end + +Blimey /^I say 'Hello Friend'$/ do + pending # express the regexp above with the code you wish you had +end + +Blimey /^I hit translate$/ do + pending # express the regexp above with the code you wish you had +end + +Letgoandhaul /^it prints out 'Ahoy Matey'$/ do + pending # express the regexp above with the code you wish you had +end + +Letgoandhaul /^it also prints 'Shiber Me Timbers You Scurvey Dogs!!'$/ do + pending # express the regexp above with the code you wish you had +end \ No newline at end of file From cf3ab8d52d616e2c65be04ede2b2851ffa68208a Mon Sep 17 00:00:00 2001 From: Eddie Lee Date: Sun, 25 Nov 2012 02:07:06 -0800 Subject: [PATCH 2/8] moved pirate.rb --- week7/homework/features/{ => step_definitions}/pirate.rb | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename week7/homework/features/{ => step_definitions}/pirate.rb (100%) diff --git a/week7/homework/features/pirate.rb b/week7/homework/features/step_definitions/pirate.rb similarity index 100% rename from week7/homework/features/pirate.rb rename to week7/homework/features/step_definitions/pirate.rb From af38340d40aa6d689e5e9e12e764a3666ce88ad4 Mon Sep 17 00:00:00 2001 From: Eddie Lee Date: Mon, 26 Nov 2012 11:09:44 -0800 Subject: [PATCH 3/8] pirate test update --- .../features/step_definitions/pirate-steps.rb | 12 ++++++------ week7/homework/features/step_definitions/pirate.rb | 11 +++++++++++ 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/week7/homework/features/step_definitions/pirate-steps.rb b/week7/homework/features/step_definitions/pirate-steps.rb index 0e4f9cb..f624831 100644 --- a/week7/homework/features/step_definitions/pirate-steps.rb +++ b/week7/homework/features/step_definitions/pirate-steps.rb @@ -1,19 +1,19 @@ -Gangway /^I have a PirateTranslator$/ do - pending # express the regexp above with the code you wish you had +Gangway /^I have a PirateTranslator$/ do + PirateTranslator.is_a?(Class).should eq true end Blimey /^I say 'Hello Friend'$/ do - pending # express the regexp above with the code you wish you had + @piratetranslator = PirateTranslator.new("Hello Friend") end Blimey /^I hit translate$/ do - pending # express the regexp above with the code you wish you had + @pirate = @piratetranslator.translate end Letgoandhaul /^it prints out 'Ahoy Matey'$/ do - pending # express the regexp above with the code you wish you had + @pirate[0].should eq "Ahoy Matey" end Letgoandhaul /^it also prints 'Shiber Me Timbers You Scurvey Dogs!!'$/ do - pending # express the regexp above with the code you wish you had + @pirate[1].should eq 'Shiber Me Timbers You Scurvey Dogs!!' end \ No newline at end of file diff --git a/week7/homework/features/step_definitions/pirate.rb b/week7/homework/features/step_definitions/pirate.rb index 774169d..d04207d 100644 --- a/week7/homework/features/step_definitions/pirate.rb +++ b/week7/homework/features/step_definitions/pirate.rb @@ -1,2 +1,13 @@ class PirateTranslator + def initialize (some_words) + @some_words = some_words + end + def translate + self.send("some_words") + end + def some_words + if @some_words == "Hello Friend" + ["Ahoy Matey", 'Shiber Me Timbers You Scurvey Dogs!!'] + end + end end \ No newline at end of file From 776d10037aeba62c83c0f40ae22c40a2619c3725 Mon Sep 17 00:00:00 2001 From: Eddie Lee Date: Mon, 26 Nov 2012 11:25:48 -0800 Subject: [PATCH 4/8] pirate steps regex update --- week7/homework/features/step_definitions/pirate-steps.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/week7/homework/features/step_definitions/pirate-steps.rb b/week7/homework/features/step_definitions/pirate-steps.rb index f624831..dbda22f 100644 --- a/week7/homework/features/step_definitions/pirate-steps.rb +++ b/week7/homework/features/step_definitions/pirate-steps.rb @@ -2,8 +2,8 @@ PirateTranslator.is_a?(Class).should eq true end -Blimey /^I say 'Hello Friend'$/ do - @piratetranslator = PirateTranslator.new("Hello Friend") +Blimey /^I say '(.*?)'$/ do |say| + @piratetranslator = PirateTranslator.new(say) end Blimey /^I hit translate$/ do From a344c5083db12bda9700fa7f85b3a6bd7a88b140 Mon Sep 17 00:00:00 2001 From: Eddie Lee Date: Tue, 27 Nov 2012 17:54:12 -0800 Subject: [PATCH 5/8] tic-tac-toe first commit --- .../homework/features/step_definitions/tic-tac-toe.rb | 11 +++++++++++ 1 file changed, 11 insertions(+) create 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 new file mode 100644 index 0000000..60ade60 --- /dev/null +++ b/week7/homework/features/step_definitions/tic-tac-toe.rb @@ -0,0 +1,11 @@ +class TicTacToe + attr_accessor :player + attr_reader :current_player + def welcome_player + "Welcome #@player" + end + def current_player + @current_player = [self.player, "Computer"].sample + @current_player + end +end \ No newline at end of file From 6060590f310c8ae36eb8ed94643986272be454c1 Mon Sep 17 00:00:00 2001 From: Eddie Lee Date: Sat, 1 Dec 2012 16:35:05 -0800 Subject: [PATCH 6/8] tic-tac-toe update --- .../step_definitions/tic-tac-toe-steps.rb | 2 +- .../features/step_definitions/tic-tac-toe.rb | 28 ++++++++++++++++++- 2 files changed, 28 insertions(+), 2 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 b353120..4217c2b 100644 --- a/week7/homework/features/step_definitions/tic-tac-toe-steps.rb +++ b/week7/homework/features/step_definitions/tic-tac-toe-steps.rb @@ -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 diff --git a/week7/homework/features/step_definitions/tic-tac-toe.rb b/week7/homework/features/step_definitions/tic-tac-toe.rb index 60ade60..9aa8117 100644 --- a/week7/homework/features/step_definitions/tic-tac-toe.rb +++ b/week7/homework/features/step_definitions/tic-tac-toe.rb @@ -1,11 +1,37 @@ class TicTacToe attr_accessor :player attr_reader :current_player + + SYMBOLS = [:X, :O] + + def initialize(player="player", symbols=:X) + if player == :computer + @current_player = "Computer" + @player = "player" + else + @player = player + end + end def welcome_player "Welcome #@player" end def current_player @current_player = [self.player, "Computer"].sample - @current_player + end + def player_symbol + @player_symbol = SYMBOLS.sample + end + def computer_symbol + @player_symbol == :X ? @computer_symbol = :O : @computer_symbol = :X + end + def indicate_palyer_turn + puts "#@current_player's Move:" + end + def get_player_move + @move = gets + end + def open_spots + end + def computer_move end end \ No newline at end of file From 1cc278e489e64be68aa4473d120619cc7100d688 Mon Sep 17 00:00:00 2001 From: Eddie Lee Date: Tue, 4 Dec 2012 17:57:32 -0800 Subject: [PATCH 7/8] updates --- .../step_definitions/tic-tac-toe-steps.rb | 4 +- .../features/step_definitions/tic-tac-toe.rb | 42 +++++++++++++++---- 2 files changed, 36 insertions(+), 10 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 4217c2b..d895247 100644 --- a/week7/homework/features/step_definitions/tic-tac-toe-steps.rb +++ b/week7/homework/features/step_definitions/tic-tac-toe-steps.rb @@ -43,8 +43,8 @@ @game.get_player_move end -Given /^it is the computers turn$/ do - @game = TicTacToe.new(:computer, :O) +Given /^it is the computer's turn$/ do + @game = TicTacToe.new(:computer, :X) @game.current_player.should eq "Computer" end diff --git a/week7/homework/features/step_definitions/tic-tac-toe.rb b/week7/homework/features/step_definitions/tic-tac-toe.rb index 9aa8117..b5284e1 100644 --- a/week7/homework/features/step_definitions/tic-tac-toe.rb +++ b/week7/homework/features/step_definitions/tic-tac-toe.rb @@ -1,22 +1,32 @@ class TicTacToe - attr_accessor :player - attr_reader :current_player + attr_accessor :player, :current_player SYMBOLS = [:X, :O] - def initialize(player="player", symbols=:X) - if player == :computer - @current_player = "Computer" - @player = "player" + def initialize(player=nil, symbol=nil) + # if player == :computer + # @current_player = "Computer" + # else + # @player = player + # end + + @current_player = player + if @current_player == :computer + @computer_symbol = symbol + @computer_symbol == :X ? @player_symbol = :O : @player_symbol = :X else - @player = player + @current_player = player + @player_symbol == symbol + self.computer_symbol end end def welcome_player "Welcome #@player" end def current_player - @current_player = [self.player, "Computer"].sample + if @current_player == nil + @current_player = [self.player, "Computer"].sample + end end def player_symbol @player_symbol = SYMBOLS.sample @@ -34,4 +44,20 @@ def open_spots end def computer_move end + def current_state + end + def player_move + end + def determine_winner + end + def board + { + :A1 => :X, :A2 => :O, :A3 => :X, + :B1 => :X, :B2 => :O, :B3 => :X, + :C1 => :O, :C2 => :X, :C3 => :O + } + end + def spots_open? + end + end \ No newline at end of file From 06a6f7c0be391244a0ce2e3cf9b19ccbb46c4126 Mon Sep 17 00:00:00 2001 From: Eddie Lee Date: Tue, 11 Dec 2012 17:11:05 -0800 Subject: [PATCH 8/8] week7 questions and tictactoe --- .../features/step_definitions/tic-tac-toe.rb | 58 +++++++++++-------- week7/homework/questions.txt | 12 +++- 2 files changed, 45 insertions(+), 25 deletions(-) diff --git a/week7/homework/features/step_definitions/tic-tac-toe.rb b/week7/homework/features/step_definitions/tic-tac-toe.rb index b5284e1..0a6ae4c 100644 --- a/week7/homework/features/step_definitions/tic-tac-toe.rb +++ b/week7/homework/features/step_definitions/tic-tac-toe.rb @@ -4,29 +4,42 @@ class TicTacToe SYMBOLS = [:X, :O] def initialize(player=nil, symbol=nil) - # if player == :computer - # @current_player = "Computer" - # else - # @player = player - # end - - @current_player = player - if @current_player == :computer - @computer_symbol = symbol - @computer_symbol == :X ? @player_symbol = :O : @player_symbol = :X - else - @current_player = player - @player_symbol == symbol - self.computer_symbol - end + @board = { + :A1 => " ", :A2 => " ", :A3 => " ", + :B1 => " ", :B2 => " ", :B3 => " ", + :C1 => " ", :C2 => " ", :C3 => " " + } + # if symbol == nil + # @player_symbol, @computer_symbol = :X, :O + # if player == :computer && symbol == :O + # @computer_symbol = :O + # @player_symbol = :X + # elsif player == :computer && symbol == :X + # @computer_symbol = :X + # @player_symbol = :O + # elsif player != :computer && symbol == :O + # @computer_symbol = :X + # @player_symbol = :O + # else player != :computer && symbol == :X + # @computer_symbol = :O + # @player_symbol = :X + # end + current_player(player) end def welcome_player "Welcome #@player" end - def current_player - if @current_player == nil - @current_player = [self.player, "Computer"].sample - end + def current_player(player=nil) + # if @current_player == nil + # @current_player = [@player, "Computer"].sample + # end + if player == :computer + @current_player = "Computer" + elsif player == nil + @current_player = [@player, "Computer"].sample + else + @current_player = player + end end def player_symbol @player_symbol = SYMBOLS.sample @@ -41,8 +54,10 @@ def get_player_move @move = gets end def open_spots + @board.select {|k,v| v == " "} end def computer_move + open_spots.sample end def current_state end @@ -51,11 +66,6 @@ def player_move def determine_winner end def board - { - :A1 => :X, :A2 => :O, :A3 => :X, - :B1 => :X, :B2 => :O, :B3 => :X, - :C1 => :O, :C2 => :X, :C3 => :O - } end def spots_open? end diff --git a/week7/homework/questions.txt b/week7/homework/questions.txt index d55387d..a4dd1d4 100644 --- a/week7/homework/questions.txt +++ b/week7/homework/questions.txt @@ -1,9 +1,19 @@ - Please Read Chapters 23 and 24 DuckTyping and MetaProgramming Questions: 1. What is method_missing and how can it be used? +If the object we’re calling the method on doesn’t have the method (and doesn’t inherit the method from another class or module), Ruby will give us one more chance to do something useful: + 2. What is and Eigenclass and what is it used for? Where Do Singleton methods live? +A hidden class associated with each specific instance of another class. In the method call chain. + 3. When would you use DuckTypeing? How would you use it to improve your code? + + 4. What is the difference between a class method and an instance method? What is the difference between instance_eval and class_eval? +A class method can be executed without having created an instance of the class, while an instance method requires an instance that is created using the new method. + + 5. What is the difference between a singleton class and a singleton method? +Singleton method: A method which belongs to a single object rather than to an entire class and other objects. +Singleton class: A singleton class is a class which defines a single object. \ No newline at end of file