Skip to content
This repository was archived by the owner on Dec 1, 2021. It is now read-only.

Eddie Lee - Week 7 Homework #157

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions week7/homework/features/step_definitions/pirate-steps.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Gangway /^I have a PirateTranslator$/ do
PirateTranslator.is_a?(Class).should eq true
end

Blimey /^I say '(.*?)'$/ do |say|
@piratetranslator = PirateTranslator.new(say)
end

Blimey /^I hit translate$/ do
@pirate = @piratetranslator.translate
end

Letgoandhaul /^it prints out 'Ahoy Matey'$/ do
@pirate[0].should eq "Ahoy Matey"
end

Letgoandhaul /^it also prints 'Shiber Me Timbers You Scurvey Dogs!!'$/ do
@pirate[1].should eq 'Shiber Me Timbers You Scurvey Dogs!!'
end
13 changes: 13 additions & 0 deletions week7/homework/features/step_definitions/pirate.rb
Original file line number Diff line number Diff line change
@@ -0,0 +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
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
end

Given /^it is the computer's turn$/ do
@game = TicTacToe.new(:computer, :O)
@game = TicTacToe.new(:computer, :X)
@game.current_player.should eq "Computer"
end

Expand Down
73 changes: 73 additions & 0 deletions week7/homework/features/step_definitions/tic-tac-toe.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
class TicTacToe
attr_accessor :player, :current_player

SYMBOLS = [:X, :O]

def initialize(player=nil, symbol=nil)
@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(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
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
@board.select {|k,v| v == " "}
end
def computer_move
open_spots.sample
end
def current_state
end
def player_move
end
def determine_winner
end
def board
end
def spots_open?
end

end
12 changes: 11 additions & 1 deletion week7/homework/questions.txt
Original file line number Diff line number Diff line change
@@ -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.