diff --git a/Final/features/step_definitions/tic-tac-toe-steps.rb b/Final/features/step_definitions/tic-tac-toe-steps.rb new file mode 100644 index 0000000..060b5f5 --- /dev/null +++ b/Final/features/step_definitions/tic-tac-toe-steps.rb @@ -0,0 +1,131 @@ +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 + [:human, :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.assign_symbols(0) + @game.current_player.should eq :human + +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_player_turn +end + +Then /^waits for my input of "(.*?)"$/ do |arg1| + @game.should_receive(:gets).and_return(arg1) + @game.player_move +end + +Given /^it is the computer's turn$/ do + @game = TicTacToe.new(:computer) + @game.assign_symbols(1) + @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(@game.board.key(:X)) +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.board.values.should include :X +end + +Given /^I am playing X$/ do + @game = TicTacToe.new(:human) + @game.assign_symbols(0) + @game.player_symbol.should eq :X +end + +When /^I enter a position "(.*?)" on the board$/ do |arg1| + @old_pos = @game.board[arg1.downcase.to_sym] + @game.should_receive(:get_player_move).and_return(arg1) + @game.player_move.should eq arg1 +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(:human) + @game.assign_symbols(0) + @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 + @game.draw?.should be_false + @game.computer_won?.should be_false +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.open_spots.empty?.should be_true +end + +Then /^the game is declared a draw$/ do + @game.draw?.should be_true +end + +When /^"(.*?)" is taken$/ do |arg1| + @game.board[arg1] = :O + @taken_spot = arg1 +end + +Then /^computer should ask me for another position "(.*?)"$/ do |arg1| + @game.board[arg1] = ' ' + @game.should_receive(:get_player_move).twice + +end diff --git a/Final/features/step_definitions/tic-tac-toe.rb b/Final/features/step_definitions/tic-tac-toe.rb new file mode 100644 index 0000000..9a34015 --- /dev/null +++ b/Final/features/step_definitions/tic-tac-toe.rb @@ -0,0 +1,163 @@ + + +class TicTacToe + + attr_accessor :player, :board + attr_reader :wins + + + SYMBOLS = [:X, :O] + + def initialize(player = "PUNY HUMAN") + @board = { + :a1 => " ", :a2 => " ", :a3 => " ", + :b1 => " ", :b2 => " ", :b3 => " ", + :c1 => " ", :c2 => " ", :c3 => " " + } + + @wins = [ + [:a1, :b1, :c1], + [:a2, :b2, :c2], + [:a3, :b3, :c3], + [:a1, :a2, :a3], + [:b1, :b2, :b3], + [:c1, :c2, :c3], + [:a1, :b2, :c3], + [:a3, :b2, :c1] + ] + + @roll = rand(2) + @player = player + assign_symbols + open_spots + current_state + end + + + def welcome_player + "Welcome #{@player}" + end + + def assign_symbols(roll = @roll) + if roll == 0 + @competitors = {:human => :X, :computer => :O} + else + @competitors = {:human => :O, :computer => :X} + end + end + + + def current_player + if open_spots.count.even? == false + return @competitors.key(:X) + else + return @competitors.key(:O) + end + end + + def player_symbol + @competitors[:human] + end + + + def computer_symbol + @competitors[:computer] + end + + def open_spots + r = @board.find_all {|i| i[1] == " "} + r.flatten.delete_if{|i| i == " "} + end + + def players_board + p = @board.find_all {|i| i[1] == player_symbol} + p.flatten.delete_if{|i| i == player_symbol} + end + + def computers_board + c = @board.find_all {|i| i[1] == computer_symbol} + c.flatten.delete_if{|i| i == computer_symbol} + end + + + def computer_move + compmove = open_spots.sample + @board[compmove] = computer_symbol + end + + def check_move(playmove) + if open_spots.include?(playmove) == true + true + else + false + end + end + + def get_player_move + move = gets.chomp.downcase.to_sym + end + + def player_move + playmove = get_player_move + if check_move(playmove) == true + @board[playmove] = player_symbol + return playmove + else + puts "invalid move" + return playmove + player_move + end + end + + def current_state + "#{@board[:a1]}|#{@board[:a2]}|#{@board[:a3]}\n----- + \n#{@board[:b1]}|#{@board[:b2]}|#{@board[:b3]}\n----- + \n#{@board[:c1]}|#{@board[:c2]}|#{@board[:c3]}\n#{'#'*10}" + end + + def indicate_player_turn + "Your move, #{@player}: " + end + + + + + def player_won? + won = @wins.map{|i| i&players_board} + won.any? {|x| x.length == 3} + end + + def computer_won? + won = @wins.map{|i| i&computers_board} + won.any? {|x| x.length == 3} + end + + def draw? + if open_spots.length == 0 + true + else + false + end + end + + def over? + if determine_winner == true + true + else + false + end + end + + def determine_winner + if player_won? == true + true + elsif computer_won? == true + true + elsif draw? == true + true + else + false + end + end + +end \ No newline at end of file diff --git a/Final/features/tic-tac-toe.feature b/Final/features/tic-tac-toe.feature new file mode 100644 index 0000000..a8a46b9 --- /dev/null +++ b/Final/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 "Your move, Renee: " + 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/Final/play_game.rb b/Final/play_game.rb new file mode 100644 index 0000000..cb800f9 --- /dev/null +++ b/Final/play_game.rb @@ -0,0 +1,22 @@ +require './features/step_definitions/tic-tac-toe.rb' + +@game = TicTacToe.new +puts "What is your name?" +@game.player = gets.chomp +puts @game.welcome_player + +until @game.over? + case @game.current_player + when :computer + @game.computer_move + when :human + @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? diff --git a/Programming Ruby 1.9 & 2.0, 4th edition.pdf b/Programming Ruby 1.9 & 2.0, 4th edition.pdf new file mode 100644 index 0000000..c4c6258 Binary files /dev/null and b/Programming Ruby 1.9 & 2.0, 4th edition.pdf differ diff --git a/exceptions.rb b/exceptions.rb new file mode 100644 index 0000000..935a1c0 --- /dev/null +++ b/exceptions.rb @@ -0,0 +1,6 @@ +module hiya + + def hello + puts yield + end +end diff --git a/exceptions2.rb b/exceptions2.rb new file mode 100644 index 0000000..a59b750 --- /dev/null +++ b/exceptions2.rb @@ -0,0 +1,6 @@ +require_relative "exceptons" + +include hiya + +hello + diff --git a/friendly_error b/friendly_error new file mode 160000 index 0000000..7ea5f1d --- /dev/null +++ b/friendly_error @@ -0,0 +1 @@ +Subproject commit 7ea5f1d83885f3caeadc3354fde77d3b38ba9ba9 diff --git a/midterm/instructions_and_questions.txt b/midterm/instructions_and_questions.txt index e38c84d..5eedc8c 100644 --- a/midterm/instructions_and_questions.txt +++ b/midterm/instructions_and_questions.txt @@ -11,6 +11,9 @@ Instructions for Mid-Term submission and Git Review (10pts): Questions (20pts): - What are the three uses of the curly brackets {} in Ruby? + 1. To contain a block + 2. to create a hash + 3. - What is a regular expression and what is a common use for them? - What is the difference between how a String, a symbol, a FixNum, and a Float are stored in Ruby? - Are these two statements equivalent? Why or Why Not? diff --git a/ptions b/ptions new file mode 100644 index 0000000..e69de29 diff --git a/test_hello_gem b/test_hello_gem new file mode 160000 index 0000000..219adab --- /dev/null +++ b/test_hello_gem @@ -0,0 +1 @@ +Subproject commit 219adab73fe26fbc9c05f59b242029ad927d1ca7 diff --git a/testex.rb b/testex.rb new file mode 100644 index 0000000..dadef81 --- /dev/null +++ b/testex.rb @@ -0,0 +1,3 @@ +class Greetings + puts "hello world" +end \ No newline at end of file diff --git a/week1/homework/.DS_Store b/week1/homework/.DS_Store new file mode 100644 index 0000000..b430373 Binary files /dev/null and b/week1/homework/.DS_Store differ diff --git a/week1/homework/questions.txt b/week1/homework/questions.txt index 2257bb9..5467aaa 100644 --- a/week1/homework/questions.txt +++ b/week1/homework/questions.txt @@ -4,12 +4,32 @@ p.86-90 Strings (Strings section in Chapter 6 Standard Types) 1. What is an object? +A group of values which, when taken together, represent a specific thing. + 2. What is a variable? +A previously defined word or letter which stands in for another value. + 3. What is the difference between an object and a class? +An object is a specific instance of a class. A class will have a one or more categories that objects of that class are expected to have, and may also have definitions of how the information in those categories is to be presented. + 4. What is a String? +A sequence of characters. + 5. What are three messages that I can send to a string object? Hint: think methods -6. What are two ways of defining a String literal? Bonus: What is the difference between them? +split, which will divide the string at a specific character +encoding, which will tell you which method of encoding is used by the string and it’s source file +chomp, which will remove extra spaces around the characters + +6. What are two ways of defining a String literal? + +“…” can be used to define a string + +%q/… / can be used to define a string + +Bonus: What is the difference between them? + +The escape sequences supported within each of them, as well as which character you’d need to use an escape sequence before in order for it to print in the string. diff --git a/week1/homework/strings_and_rspec_spec.rb b/week1/homework/strings_and_rspec_spec.rb index ea79e4c..4abbc4d 100644 --- a/week1/homework/strings_and_rspec_spec.rb +++ b/week1/homework/strings_and_rspec_spec.rb @@ -12,14 +12,20 @@ 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 count the charaters" do + result = @my_string.length + result.should eq 66 + puts result + end it "should be able to split on the . charater" do - pending - result = #do something with @my_string here + result = @my_string.split('.') result.should have(2).items + puts result end it "should be able to give the encoding of the string" do - pending 'helpful hint: should eq (Encoding.find("UTF-8"))' + result = @my_string.encoding + puts "Encoding is #{result}" + end end end diff --git a/week2/exercises/book_spec.rb b/week2/exercises/book_spec.rb index c3b1292..3f78f94 100644 --- a/week2/exercises/book_spec.rb +++ b/week2/exercises/book_spec.rb @@ -1,4 +1,4 @@ -require './book' +require 'RubyWinter2014/week2/excercises/book.rb' describe Book do diff --git a/week2/exercises/mad_libs.rb b/week2/exercises/mad_libs.rb index 3af5583..3aa9db1 100644 --- a/week2/exercises/mad_libs.rb +++ b/week2/exercises/mad_libs.rb @@ -6,5 +6,5 @@ verb_past_tense = gets.chomp puts "What does the #{noun} say?" says = gets.chomp -story = "The #{adjective} #{noun} #{verb_past_tense} past the graveyard and says #{says}" +story = "The #{adjective} #{noun} #{verb_past_tense} past the graveyard and says \"#{says}\"" puts story diff --git a/week2/homework/questions.txt b/week2/homework/questions.txt index 939e42d..e9401d7 100644 --- a/week2/homework/questions.txt +++ b/week2/homework/questions.txt @@ -4,10 +4,21 @@ Sharing Functionality: Inheritance, Modules, and Mixins 1. What is the difference between a Hash and an Array? +A hash requires a keyword for each entry that is used to catagorized the entry and can be used to refer back to that entry (as well as any other entries which share the keyword), while an array assigns each entry an integer reference number based on where it falls in the array. + 2. When would you use an Array over a Hash and vice versa? +I would use and array when I had a list of things to call back in a specific order, or needed to manipulate the order of -- like a line, or ordered ranking. I would use a hash when I had data to sort into catagories. + 3. What is a module? Enumerable is a built in Ruby module, what is it? +A module is a group of methods, classes and constants. The Enumerable is an object which iterates, which can be useful when you need to manipulate an object which counts or to merge two arrays into a hash. + 4. Can you inherit more than one thing in Ruby? How could you get around this problem? +No. You can have a "chain of sucession", where each object inherits from a prior object. Or you can use "include" and specify a module, which will include all the module's methods within the class. + 5. What is the difference between a Module and a Class? + +Classes define objects, while Modules group together classes, methods and objects which are designed to work together. + diff --git a/week2/homework/simon_says.rb b/week2/homework/simon_says.rb new file mode 100644 index 0000000..c97caaf --- /dev/null +++ b/week2/homework/simon_says.rb @@ -0,0 +1,17 @@ +module SimonSays + def echo(input) + @input = input + end + def shout(input) + @input = input.upcase + end + def repeat(input, num=2) + @input = ((input + " ") * num).strip + end + def start_of_word(input, num=1) + @input = input.split(//).first(num).join + end + def first_word(input) + @input = input.split(/\s+/).first + end +end diff --git a/week2/homework/simon_says_spec.rb b/week2/homework/simon_says_spec.rb index 7f329e5..0aee041 100644 --- a/week2/homework/simon_says_spec.rb +++ b/week2/homework/simon_says_spec.rb @@ -1,5 +1,5 @@ # Hint: require needs to be able to find this file to load it -require "./simon_says.rb" +require_relative "simon_says" describe SimonSays do include SimonSays # Hint: Inclusion is different than SimonSays.new (read about modules) diff --git a/week3/exercises/human.rb b/week3/exercises/human.rb new file mode 100644 index 0000000..01fd058 --- /dev/null +++ b/week3/exercises/human.rb @@ -0,0 +1,8 @@ +require './named_thing' + +class Human + include NamedThing +end + + + diff --git a/week3/homework/calculator.rb b/week3/homework/calculator.rb new file mode 100644 index 0000000..93322f3 --- /dev/null +++ b/week3/homework/calculator.rb @@ -0,0 +1,19 @@ +class Calculator + + def sum num + num.inject(0) {|sum, element| sum+element} + end + + def multiply *num + num.flatten.inject(1) {|multiply, element| multiply*element} + end + + def pow num, exp + num**exp + end + + def fac num + (1..num).to_a.inject(1) {|fac, element| fac*element} + end +end + diff --git a/week3/homework/code_timer.rb b/week3/homework/code_timer.rb new file mode 100644 index 0000000..d45f575 --- /dev/null +++ b/week3/homework/code_timer.rb @@ -0,0 +1,8 @@ +class CodeTimer + def self.time_code n = 1 + start = Time.now + n.times{ yield } + (Time.now - start)/n + end + +end \ No newline at end of file diff --git a/week3/homework/code_timer_spec.rb b/week3/homework/code_timer_spec.rb new file mode 100644 index 0000000..42ed407 --- /dev/null +++ b/week3/homework/code_timer_spec.rb @@ -0,0 +1,44 @@ +require_relative 'code_timer.rb' + +describe CodeTimer do + + it "should run our code" do + flag = false + CodeTimer.time_code do + flag = true + end + + flag.should be_true + + end + + it "should time our code" do + Time.stub(:now).and_return(0,3) + time = CodeTimer.time_code do + end + time.should be_within(0.1).of(3.0) + + end + + it "should run our code multiple times" do + count = 0 + CodeTimer.time_code 100 do + count +=1 + end + count.should eq 100 + + end + + it "should retun the average time our code runs" do + Time.stub(:now).and_return(0,300) + time = CodeTimer.time_code 100 do + end + time.should be_within(0.1).of(3.0) + end + + + + + + +end \ No newline at end of file diff --git a/week3/homework/questions.txt b/week3/homework/questions.txt index dfb158d..2c67593 100644 --- a/week3/homework/questions.txt +++ b/week3/homework/questions.txt @@ -6,10 +6,20 @@ Please Read: 1. What is a symbol? +A symbol is an immutable string + 2. What is the difference between a symbol and a string? +Strings are mutable, symbols are immutable. + 3. What is a block and how do I call a block? +A block is code contained within do and end, or in curly {} braces. It is called by defining a variable within || for the block to scan for within a given array, hash or other ream of data. + 4. How do I pass a block to a method? What is the method signature? +The yeild statement within the block can then pass the output of a block into a method when the block is called. + 5. Where would you use regular expressions? + +Where you needed to search a string for a pattern or word. \ No newline at end of file diff --git a/week4/homework/questions.txt b/week4/homework/questions.txt index 187b3d3..274b65b 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? +There is a build-in Kernal module which reads and writes to files. + 2. How would you output "Hello World!" to a file called my_output.txt? +File.open("my_output.txt", "w") do |file| + puts "Hello World" +end + 3. What is the Directory class and what is it used for? +They are generally lists of directories and their contents, useful for finding what is in something and what commands it will respond to + 4. What is an IO object? +An instance of a class which handles input and output in ruby + 5. What is rake and what is it used for? What is a rake task? +It is used to organize and automate ruby tasks. A rake task is a group of files which are to be run in a specific order + diff --git a/week4/homework/worker.rb b/week4/homework/worker.rb new file mode 100644 index 0000000..16facc8 --- /dev/null +++ b/week4/homework/worker.rb @@ -0,0 +1,12 @@ +require "../../bearror/lib/bearror.rb" +include Bearror +include Kernal + +class Worker + + def self.work r= 1 + result = nil + r.times.map { |i| result = yield(i) } + result + end + diff --git a/week4/homework/worker_spec.rb b/week4/homework/worker_spec.rb index dfc6f02..e0e8cff 100644 --- a/week4/homework/worker_spec.rb +++ b/week4/homework/worker_spec.rb @@ -1,5 +1,12 @@ +require "../../bearror/lib/bearror.rb" +include Bearror +include Kernal + require "#{File.dirname(__FILE__)}/worker" + + + describe Worker do it "executes a block and returns a string" do diff --git a/week5/exercises/Rakefile.rb b/week5/exercises/Rakefile.rb new file mode 100644 index 0000000..39a0fb6 --- /dev/null +++ b/week5/exercises/Rakefile.rb @@ -0,0 +1,47 @@ + + +desc "Outputs all names" +task :getnames do + file_helper("names") do |line| + puts line + end +end + +desc "Creates a Directory called Class" +task :create_class_dir do + Dir.mkdir "class" unless Dir.exists? "class" +end + +desc "create a student dir in the class dir for each student" +task :create_student_dirs => [:create_class_dir] do + Dir.chdir("class") + file_helper("names") do |line| + Dir.mkdir line unless Dir.exists? line + end + Dir.chdir("..") +end + + +desc "cleans up all the dirs we created" +task :remove_all_dirs =>[:create_student_dirs] do + Dir.chdir("names") + file_helper("../names") do |line| + Dir.rmdir line if Dir.exists? line + end + Dir.chdir("..") + Dir.rmdir ("class") +end + + + + + +def file_helper file_name + File.open(file_name) do |f| + f.each do |line| + yield line.chomp + end + end +end + + diff --git a/week7/class_materials/play_game.rb b/week7/class_materials/play_game.rb new file mode 100644 index 0000000..0535830 --- /dev/null +++ b/week7/class_materials/play_game.rb @@ -0,0 +1,22 @@ +require './features/step_definitions/tic-tac-toe.rb' + +@game = TicTacToe.new +puts "What is your name?" +@game.player = gets.chomp +puts @game.welcome_player + +until @game.over? + case @game.current_player + when "Computer" + @game.computer_move + when @game.player + @game.indicate_palyer_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? diff --git a/week7/class_materials/tic-tac-toe-steps.rb b/week7/class_materials/tic-tac-toe-steps.rb new file mode 100644 index 0000000..a3287c1 --- /dev/null +++ b/week7/class_materials/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/class_materials/tic-tac-toe.feature b/week7/class_materials/tic-tac-toe.feature new file mode 100644 index 0000000..6f3134d --- /dev/null +++ b/week7/class_materials/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/exercises/features/converter.feature b/week7/exercises/features/converter.feature index 5e262a8..372bc41 100644 --- a/week7/exercises/features/converter.feature +++ b/week7/exercises/features/converter.feature @@ -5,13 +5,13 @@ Feature: Converting metric Scenario: Given I have entered 32 into the converter - And I set the type to Fahrenheit + And I set the type to "Fahrenheit" When I press convert Then the result returned should be 0.0 Scenario: Given I have entered 75 into the converter - And I set the type to Fahrenheit + And I set the type to "Fahrenheit" When I press convert Then the result returned should be 23.9 diff --git a/week7/exercises/features/converter.rb b/week7/exercises/features/converter.rb new file mode 100644 index 0000000..33d7507 --- /dev/null +++ b/week7/exercises/features/converter.rb @@ -0,0 +1,23 @@ +class Converter + + attr_accessor :num, :type + + def initialize num + @num = num.to_f + end + + + def convert + self.send("#{@type}_conversion") + end + + def Fahrenheit_conversion + (@num - 32) * 5 / 9 + end + + + + + + +end diff --git a/week7/exercises/features/converter_steps.rb b/week7/exercises/features/converter_steps.rb new file mode 100644 index 0000000..41b660e --- /dev/null +++ b/week7/exercises/features/converter_steps.rb @@ -0,0 +1,16 @@ + +Given(/^I have entered (\d+) into the converter$/) do |arg1| + @converter = Converter.new(arg1) +end + +Given(/^I set the type to "(.*?)"$/) do |type| + @converter.type = "Fahrenheit" +end + +When(/^I press convert$/) do + @result = @converter.convert +end + +Then(/^the result returned should be (\d+)\.(\d+)$/) do |arg1, arg2| + @result.should eq "#{arg1}. #{arg2}".to_f +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..52e7712 --- /dev/null +++ b/week7/homework/features/step_definitions/pirate.rb @@ -0,0 +1,28 @@ +class PirateTranslator + + + + def say string + @string = string + @@pirate_farewell + end + + + def translate + pirate_string = @string.downcase.gsub(/\w+/) do |w| + if @@pirate_dictionary.has_key?(w) + @@pirate_dictionary[w].capitalize + else + w.capitalize + end + end + puts pirate_string + pirate_string + + end + + @@pirate_farewell = "\n Shiber Me Timbers You Scurvey Dogs!!" + @@pirate_dictionary = {"hello" => "ahoy", "friend" => "matey", "but" => "avast", "yes" => "aye"} + + +end + diff --git a/week7/homework/features/step_definitions/pirate_steps.rb b/week7/homework/features/step_definitions/pirate_steps.rb index faf1a7f..38c9c96 100644 --- a/week7/homework/features/step_definitions/pirate_steps.rb +++ b/week7/homework/features/step_definitions/pirate_steps.rb @@ -1,3 +1,5 @@ + + Gangway /^I have a (\w+)$/ do |arg| @translator = Kernel.const_get(arg).new end diff --git a/week7/homework/questions.txt b/week7/homework/questions.txt index d55387d..eb097ab 100644 --- a/week7/homework/questions.txt +++ b/week7/homework/questions.txt @@ -2,8 +2,26 @@ Please Read Chapters 23 and 24 DuckTyping and MetaProgramming Questions: + 1. What is method_missing and how can it be used? + +Its a way to catch no method errors and pring out a custom error code for them. + + 2. What is and Eigenclass and what is it used for? Where Do Singleton methods live? + +An Eigenclass is a singleton class. Singleton methods live in the instance of the class they were created in. + + 3. When would you use DuckTypeing? How would you use it to improve your code? + +When you had an object that could respond to the methods of multiple classes you can use it to imitate an instance of that class. Essentially, the class of an object in ruby matters less than the methods that can be called upon it. It can be used, for example, to store multiple strings in an array (a single object in memory) as opposed to creating individual objects in memory for each of them. + 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 runs from the class, but and instance method requires an instance of the class to have been created. class_eval and instance_eval both set up things for method defintions. One defines class methods, the other instance methods + + 5. What is the difference between a singleton class and a singleton method? + +A singleton class is the anonymous class ruby defines and assigns to self in order. A singleton method is a method only defined and able to be used by a single class. It will often create a singleton class to be run. diff --git a/week8/class_materials/couch.rb b/week8/class_materials/couch.rb index 660da0d..a86d6e8 100644 --- a/week8/class_materials/couch.rb +++ b/week8/class_materials/couch.rb @@ -5,23 +5,12 @@ def initialize(pillows, cushions, dogs) @dogs = dogs end - # [:pillows, :cushions, :dogs].each do |s| - # define_method("how_many_#{s}?") do - # instance_variable_get("@#{s}").count - # end - # end - - def how_many_pillows? - @pillows.count - end + [:pillows, :cushions, :dogs].each do |s| + define_method("how_many_#{s}?") do + instance_variable_get("@#{s}").count + end + end - def how_many_cushions? - @cushions.count - end - - def how_many_dogs? - @dogs.count - end # def respond_to?(method_name) # true diff --git a/week8/exercises/couch.rb b/week8/exercises/couch.rb index b32ea96..3774929 100644 --- a/week8/exercises/couch.rb +++ b/week8/exercises/couch.rb @@ -9,18 +9,22 @@ def initialize pillows, cushions, dogs define_method("how_many_#{s}?") do instance_variable_get("@#{s}").count end - end - - def pillow_colors - @pillows.map &:to_s + define_method "get_strings_for_#{s}" do + instance_variable_get("@#{s}").map &:to_s + end end - def cushions_colors - @cushions.map &:to_s + + [:pillows, :cushions].each do |s| + define_method "#{s.to_s.chop}_colors" do + send("get_strings_for_#{s}") + end + end + def dog_names - @dogs.map &:to_s + get_strings_for_dogs end end \ No newline at end of file diff --git a/week8/exercises/couch_spec.rb b/week8/exercises/couch_spec.rb index 8af3ee0..71b78ac 100644 --- a/week8/exercises/couch_spec.rb +++ b/week8/exercises/couch_spec.rb @@ -9,7 +9,7 @@ end it "should tell me the cushions colors" do - @couch.cushions_colors.should eq ["grey", "grey"] + @couch.cushion_colors.should eq ["grey", "grey"] end it "should tell me the dogs names" do