From 21470e62c0a492050dcd50b2c4e47377a0b3e822 Mon Sep 17 00:00:00 2001 From: Chris Tsongas Date: Tue, 27 Nov 2012 13:46:08 -0800 Subject: [PATCH 1/5] answered questions --- week7/homework/questions.txt | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/week7/homework/questions.txt b/week7/homework/questions.txt index d55387d..9b77715 100644 --- a/week7/homework/questions.txt +++ b/week7/homework/questions.txt @@ -3,7 +3,16 @@ Please Read Chapters 23 and 24 DuckTyping and MetaProgramming Questions: 1. What is method_missing and how can it be used? +It is a method predefined in BasicObject to throw an error if a call to another method isn't found. It can be used to do cool things like creating dynamically named methods i.e. a method for a person object that responds to person.say_whatever by outputting "whatever" + 2. What is and Eigenclass and what is it used for? Where Do Singleton methods live? +An eigenclass is generated internally by Ruby to be the intermediary between one object that inherits from another, so the inheriting object can inherit unique items from the eigenclass while still inheriting from the other object. Singleton methods live there. + 3. When would you use DuckTypeing? How would you use it to improve your code? +I would use duck typing in RSpec tests to simulate a row in a database by using a struct instead. I would use it in an actual application to make my code more flexible, for example by creating a method that will do the same thing for an array as for a string. + 4. What is the difference between a class method and an instance method? What is the difference between instance_eval and class_eval? +In a class method self is the class object, while in an instance method self is the instance object. Oddly, instance_eval defines class methods and class_eval defines instance methods. + 5. What is the difference between a singleton class and a singleton method? +A singleton method is a method of a singleton class. Singleton classes don't necessarily contain singleton methods, for example they can reference a module's method instead. \ No newline at end of file From 912c879d29da921188fa012dedaa83e4f5b7f98f Mon Sep 17 00:00:00 2001 From: Chris Tsongas Date: Tue, 4 Dec 2012 20:25:59 -0800 Subject: [PATCH 2/5] pirate feature --- .../features/step_definitions/pirate-steps.rb | 19 +++++++++++++++++++ .../features/step_definitions/pirate.rb | 12 ++++++++++++ .../step_definitions/tic-tac-toe-steps.rb | 2 +- 3 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 week7/homework/features/step_definitions/pirate-steps.rb create mode 100644 week7/homework/features/step_definitions/pirate.rb 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..cafd503 --- /dev/null +++ b/week7/homework/features/step_definitions/pirate-steps.rb @@ -0,0 +1,19 @@ +Gangway /^I have a PirateTranslator$/ do + @translator = PirateTranslator.new() +end + +Blimey /^I say 'Hello Friend'$/ do + @translator.blimey +end + +Blimey /^I hit translate$/ do + @translator.letgoandhaul +end + +Letgoandhaul /^it prints out 'Ahoy Matey'$/ do + @translator.letgoandhaul +end + +Letgoandhaul /^it also prints 'Shiber Me Timbers You Scurvey Dogs!!'$/ do + @translator.letgoandhaul +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 new file mode 100644 index 0000000..fe12e88 --- /dev/null +++ b/week7/homework/features/step_definitions/pirate.rb @@ -0,0 +1,12 @@ +class PirateTranslator + + def blimey + puts "Hello Friend" + end + + def letgoandhaul + puts "Ahoy Matey" + puts "Shiber Me Timbers You Scurvey Dogs" + end + +end \ No newline at end of file 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..d00e957 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| From 9bfe4ee7a5f987a28336e0cfb64f43583052fdc0 Mon Sep 17 00:00:00 2001 From: Chris Tsongas Date: Wed, 5 Dec 2012 10:00:05 -0800 Subject: [PATCH 3/5] adding tic-tac-toe.rb --- week7/homework/features/step_definitions/tic-tac-toe.rb | 0 1 file changed, 0 insertions(+), 0 deletions(-) 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..e69de29 From cef5843304f5fee61370a28e4b071f8ad24ed009 Mon Sep 17 00:00:00 2001 From: Chris Tsongas Date: Tue, 11 Dec 2012 11:37:29 -0800 Subject: [PATCH 4/5] writing initialize function, listing out other necessary functions --- .../features/step_definitions/tic-tac-toe.rb | 160 ++++++++++++++++++ week7/homework/play_game.rb | 2 +- 2 files changed, 161 insertions(+), 1 deletion(-) diff --git a/week7/homework/features/step_definitions/tic-tac-toe.rb b/week7/homework/features/step_definitions/tic-tac-toe.rb index e69de29..1963481 100644 --- a/week7/homework/features/step_definitions/tic-tac-toe.rb +++ b/week7/homework/features/step_definitions/tic-tac-toe.rb @@ -0,0 +1,160 @@ +class TicTacToe + + attr_reader :player, :welcome_player, :current_player, :player_symbol, :computer_symbol, :board, :player_won, :computer_won + +#@game = TicTacToe.new +#puts @game.welcome_player + +#until @game.over? +# case @game.current_player +# when "Computer" +# @game.computer_move +# when @game.player +# @game.indicate_player_turn +# @game.player_move +# end +# puts @game.current_state +# @game.determine_winner +#end + +#puts "You Won!" if @game.player_won? +#puts "I Won!" if @game.computer_won? +#puts "DRAW!" if @game.draw? + + def initialize(*args) + + # Get name of player and set welcome message + puts "Please enter your name:" + @player = gets + @welcome_player = "Welcome #{player}" + + # Initialize optional argument variables for who goes first, their symbol + first = nil + symbol = nil + if args.length == 1 + first = args[1] + end + if args.length == 2 + symbol = args[2] + end + + # Determine whether computer or player goes first + if first == :computer + # Computer goes first + @current_player = "Computer" + elsif first == :player + # Player goes first + @current_player = "Player" + else + # Randomly choose who goes first + @current_player = [@player, "Computer"].sample + end + + # Set who is X and who is 0 + if symbol == :X + # Set whoever was first to X + if first == :computer + @computer_symbol = :X + @player_symbol = :O + else + @computer_symbol = :O + @player_symbol = :X + end + elsif symbol == :Y + # Set whoever was first to O + if first == :computer + @computer_symbol = :O + @player_symbol = :X + else + @computer_symbol = :X + @player_symbol = :O + end + else + # No symbol specified so assign defaults + @player_symbol = :X + @computer_symbol = :O + end + + # Set initial state + @board = { + :A1 => "", :A2 => "", :A3 => "", + :B1 => "", :B2 => "", :B3 => "", + :C1 => "", :C2 => "", :C3 => "" + } + @player_won = false + @computer_won = false + + end + + + def current_state + puts @board.to_s + end + + + def spots_open? + + end + + + def over? + if @player_won || @computer_won || @draw + true + else + false + end + end + + + def draw? + + end + + + def open_spots + + end + + + def computer_move + + end + + + def player_move + + end + + + def get_player_move + + end + + + def indicate_player_turn + + end + + + def determine_winner + + 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 + +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 006724393a8a11f459dbb178535020860b6432eb Mon Sep 17 00:00:00 2001 From: Chris Tsongas Date: Tue, 11 Dec 2012 16:52:25 -0800 Subject: [PATCH 5/5] posting what I've finished so far --- .../features/step_definitions/tic-tac-toe.rb | 108 ++++++++---------- 1 file changed, 49 insertions(+), 59 deletions(-) diff --git a/week7/homework/features/step_definitions/tic-tac-toe.rb b/week7/homework/features/step_definitions/tic-tac-toe.rb index 1963481..bfadef0 100644 --- a/week7/homework/features/step_definitions/tic-tac-toe.rb +++ b/week7/homework/features/step_definitions/tic-tac-toe.rb @@ -1,25 +1,8 @@ class TicTacToe - attr_reader :player, :welcome_player, :current_player, :player_symbol, :computer_symbol, :board, :player_won, :computer_won - -#@game = TicTacToe.new -#puts @game.welcome_player - -#until @game.over? -# case @game.current_player -# when "Computer" -# @game.computer_move -# when @game.player -# @game.indicate_player_turn -# @game.player_move -# end -# puts @game.current_state -# @game.determine_winner -#end - -#puts "You Won!" if @game.player_won? -#puts "I Won!" if @game.computer_won? -#puts "DRAW!" if @game.draw? + attr_accessor :player, :board + + attr_reader :welcome_player, :current_player, :player_symbol, :computer_symbol def initialize(*args) @@ -75,30 +58,19 @@ def initialize(*args) @computer_symbol = :O end - # Set initial state + # Create empty board @board = { - :A1 => "", :A2 => "", :A3 => "", - :B1 => "", :B2 => "", :B3 => "", - :C1 => "", :C2 => "", :C3 => "" + :A1 => "_", :A2 => "_", :A3 => "_", + :B1 => "_", :B2 => "_", :B3 => "_", + :C1 => "_", :C2 => "_", :C3 => "_" } - @player_won = false - @computer_won = false - - end - - - def current_state - puts @board.to_s - end - - def spots_open? - end def over? - if @player_won || @computer_won || @draw + # The game is over if someone has won or it is a draw + if self.player_won? || self.computer_won? || self.draw? true else false @@ -106,33 +78,53 @@ def over? end - def draw? - + def current_state + # Show board + "A1#{@board[:A1]} A2#{@board[:A2]} A3#{@board[:A3]}\nB1#{@board[:B1]} B2#{@board[:B2]} B3#{@board[:B3]}\nC1#{@board[:C1]} C2#{@board[:C2]} C3#{@board[:C3]}" + end + + + def spots_open? + # If the board has any underscores, there are spots open + @board.value?("_") end def open_spots + # Return an array containing symbols of open spots + @board.select {|key, value| value == "_"}.keys + end + + def indicate_player_turn + # Alert user and show board + puts "It is your turn. Here is the board:" + puts self.current_state end - def computer_move - + def get_player_move + # Prompt user for move + puts "Please enter your move:" + gets end def player_move - - end + # Get player move + move = self.get_player_move + # Validate player move + # Update board - def get_player_move - end - def indicate_player_turn - + def computer_move + # Choose random open spot (the computer isn't very good) + move = self.open_spots.sample + # Update board + end @@ -141,20 +133,18 @@ def determine_winner end -#When /^I enter my name (\w+)$/ do |name| -# @game.player = name -#end + def player_won? + + 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 + def computer_won? + + end -#Then /^who is X and who is O$/ do -# TicTacToe::SYMBOLS.should include @game.player_symbol, @game.computer_symbol -#end + + def draw? + + end end \ No newline at end of file