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| 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..bfadef0 --- /dev/null +++ b/week7/homework/features/step_definitions/tic-tac-toe.rb @@ -0,0 +1,150 @@ +class TicTacToe + + attr_accessor :player, :board + + attr_reader :welcome_player, :current_player, :player_symbol, :computer_symbol + + 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 + + # Create empty board + @board = { + :A1 => "_", :A2 => "_", :A3 => "_", + :B1 => "_", :B2 => "_", :B3 => "_", + :C1 => "_", :C2 => "_", :C3 => "_" + } + + end + + + def over? + # 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 + end + end + + + 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 get_player_move + # Prompt user for move + puts "Please enter your move:" + gets + end + + + def player_move + # Get player move + move = self.get_player_move + # Validate player move + + # Update board + + end + + + def computer_move + # Choose random open spot (the computer isn't very good) + move = self.open_spots.sample + # Update board + + end + + + def determine_winner + + end + + + def player_won? + + end + + + def computer_won? + + end + + + def draw? + + 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 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