diff --git a/HW1.rtf b/HW1.rtf new file mode 100644 index 0000000..719eae7 Binary files /dev/null and b/HW1.rtf differ diff --git a/HW2.rtf b/HW2.rtf new file mode 100644 index 0000000..7ca5c55 Binary files /dev/null and b/HW2.rtf differ diff --git a/HW3.rtf b/HW3.rtf new file mode 100644 index 0000000..3b520ec Binary files /dev/null and b/HW3.rtf differ diff --git a/HW4.rtf b/HW4.rtf new file mode 100644 index 0000000..bc6b009 Binary files /dev/null and b/HW4.rtf differ diff --git a/HW7.rtf b/HW7.rtf new file mode 100644 index 0000000..83ceb97 Binary files /dev/null and b/HW7.rtf differ diff --git a/HW7_.rtf b/HW7_.rtf new file mode 100644 index 0000000..83ceb97 Binary files /dev/null and b/HW7_.rtf differ diff --git a/calculator.rb b/calculator.rb new file mode 100644 index 0000000..4775e73 --- /dev/null +++ b/calculator.rb @@ -0,0 +1,26 @@ +class Calculator + def sum(arr) + answer = 0 + arr.each{|i| answer += i} + answer + end + + def multiply(x, y = 0) + answer = x * y + answer = x.inject(:*) if x.is_a?Array + answer + end + + def pow(x, y) + answer = 1 + y.times{answer *= x} + answer + end + + def fac(x) + return 1 if x == 0 + (1..x).inject(:*) + end +end + + diff --git a/pirate/features/pirate.feature b/pirate/features/pirate.feature new file mode 100644 index 0000000..7c66eb0 --- /dev/null +++ b/pirate/features/pirate.feature @@ -0,0 +1,12 @@ +# language: en-pirate + +Ahoy matey!: Pirate Speak + I would like help to talk like a pirate + +Heave to: The mighty speaking pirate + Gangway! I have a PirateTranslator + Blimey! I say 'Hello Friend' + Aye I hit translate + Let go and haul it prints out 'Ahoy Matey' + Avast! it also prints 'Shiber Me Timbers You Scurvey Dogs!!' + diff --git a/pirate/features/step_defintions/pirate.rb b/pirate/features/step_defintions/pirate.rb new file mode 100644 index 0000000..403f888 --- /dev/null +++ b/pirate/features/step_defintions/pirate.rb @@ -0,0 +1,12 @@ +class PirateTranslator + DICTIONARY = {"Hello Friend" => "Ahoy Matey"} + + def say(str) + @phrase = str + DICTIONARY[@phrase] + end + + def translate + say(@phrase) + "\n Shiber Me Timbers You Scurvey Dogs!!" + end +end diff --git a/pirate/features/step_defintions/pirate_steps.rb b/pirate/features/step_defintions/pirate_steps.rb new file mode 100644 index 0000000..1eb2ef7 --- /dev/null +++ b/pirate/features/step_defintions/pirate_steps.rb @@ -0,0 +1,19 @@ +Gangway /^I have a (\w+)$/ do |arg| + @translator = Kernel.const_get(arg).new +end + +Blimey /^I (\w+) '(.+)'$/ do |method, arg| + @translator.send(method, arg) +end + +Blimey /^I hit (\w+)$/ do |arg| + @result = @translator.send(arg) +end + +Letgoandhaul /^it prints out '(.+)'$/ do |arg| + @result.split("\n ").first.should == arg +end + +Letgoandhaul /^it also prints '(.+)'$/ do |arg| + @result.split("\n ").last.should == arg +end diff --git a/pirate_/features/pirate.feature b/pirate_/features/pirate.feature new file mode 100644 index 0000000..7c66eb0 --- /dev/null +++ b/pirate_/features/pirate.feature @@ -0,0 +1,12 @@ +# language: en-pirate + +Ahoy matey!: Pirate Speak + I would like help to talk like a pirate + +Heave to: The mighty speaking pirate + Gangway! I have a PirateTranslator + Blimey! I say 'Hello Friend' + Aye I hit translate + Let go and haul it prints out 'Ahoy Matey' + Avast! it also prints 'Shiber Me Timbers You Scurvey Dogs!!' + diff --git a/pirate_/features/step_defintions/pirate.rb b/pirate_/features/step_defintions/pirate.rb new file mode 100644 index 0000000..403f888 --- /dev/null +++ b/pirate_/features/step_defintions/pirate.rb @@ -0,0 +1,12 @@ +class PirateTranslator + DICTIONARY = {"Hello Friend" => "Ahoy Matey"} + + def say(str) + @phrase = str + DICTIONARY[@phrase] + end + + def translate + say(@phrase) + "\n Shiber Me Timbers You Scurvey Dogs!!" + end +end diff --git a/pirate_/features/step_defintions/pirate_steps.rb b/pirate_/features/step_defintions/pirate_steps.rb new file mode 100644 index 0000000..1eb2ef7 --- /dev/null +++ b/pirate_/features/step_defintions/pirate_steps.rb @@ -0,0 +1,19 @@ +Gangway /^I have a (\w+)$/ do |arg| + @translator = Kernel.const_get(arg).new +end + +Blimey /^I (\w+) '(.+)'$/ do |method, arg| + @translator.send(method, arg) +end + +Blimey /^I hit (\w+)$/ do |arg| + @result = @translator.send(arg) +end + +Letgoandhaul /^it prints out '(.+)'$/ do |arg| + @result.split("\n ").first.should == arg +end + +Letgoandhaul /^it also prints '(.+)'$/ do |arg| + @result.split("\n ").last.should == arg +end diff --git a/simon_says.rb b/simon_says.rb new file mode 100644 index 0000000..b24e57d --- /dev/null +++ b/simon_says.rb @@ -0,0 +1,25 @@ +module SimonSays + def echo(str) + str + end + + def shout(str) + str.upcased + end + + def repeat(str, n = 2) + arr = [] + n.times {arr << str} + arr.join(' ') + end + + def start_of_word(str, n) + str[0..n-1] + end + + def first_word(str) + arr = str.split(' ') + arr[0] + end +end + diff --git a/simon_says_spec.rb b/simon_says_spec.rb new file mode 100644 index 0000000..1f84258 --- /dev/null +++ b/simon_says_spec.rb @@ -0,0 +1,48 @@ +# Hint: require needs to be able to find this file to load it +require_relative "simon_says.rb" +#require_relative '../../spec_helper' + +describe SimonSays do + include SimonSays # Hint: Inclusion is different than SimonSays.new (read about modules) + + # Hint: We are just calling methods, we are not passing a message to a SimonSays object. + it "should echo hello" do + echo("hello").should == "hello" + end + + it "should echo bye" do + echo("bye").should == "bye" + end + + it "should shout hello" do + shout("hello").should == "HELLO" + end + + it "should shout multiple words" do + shout("hello world").should == "HELLO WORLD" + end + + it "should repeat" do + repeat("hello").should == "hello hello" + end + + it "should repeat a number of times" do + repeat("hello", 3).should == "hello hello hello" + end + + it "should return the first letter" do + start_of_word("hello", 1).should == "h" + end + + it "should return the first two letters" do + start_of_word("Bob", 2).should == "Bo" + end + + it "should tell us the first word of 'Hello World' is 'Hello'" do + first_word("Hello World").should == "Hello" + end + + it "should tell us the first word of 'oh dear' is 'oh'" do + first_word("oh dear").should == "oh" + end +end diff --git a/strings_and_rspec_spec.rb b/strings_and_rspec_spec.rb new file mode 100644 index 0000000..3a54c38 --- /dev/null +++ b/strings_and_rspec_spec.rb @@ -0,0 +1,33 @@ +# encoding: utf-8 +require 'rspec/collection_matchers' +# require_relative '../../spec_helper' + +# Please make these examples all pass +# You will need to change the 3 pending tests +# You will need to write a passing test for the first example +# (Hint: If you need help refer to the in-class exercises) +# The two tests with the pending keyword, require some ruby code to be written +# (Hint: You should do the reading on Strings first) + +describe String do + context "When a string is defined" do + 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" do + @my_string.length.should == 66 + # expect(@my_string.length).to eq 66 + end + + it "should be able to split on the . character" do + result = @my_string.split('.') + result.should have(2).items + end + + it "should be able to give the encoding of the string" do + @my_string.encoding.should eq (Encoding.find("UTF-8")) + end + end +end + diff --git a/tic-tac-toe/features/step_defintions/tic-tac-toe.rb b/tic-tac-toe/features/step_defintions/tic-tac-toe.rb new file mode 100644 index 0000000..94a5083 --- /dev/null +++ b/tic-tac-toe/features/step_defintions/tic-tac-toe.rb @@ -0,0 +1,124 @@ +class TicTacToe + SYMBOLS = [:X, :O] + attr_accessor :player, :playerXO, :board, :random, :comp_turn, :winning_symbol + + def initialize(player_turn = nil, playerXO = nil) + @player = player_turn.to_s.capitalize unless player_turn.nil? + @random = true if player_turn == nil + @playerXO = playerXO + @board = {:A1 => " ", :A2 => " ", :A3 => " ", + :B1 => " ", :B2 => " ", :B3 => " ", + :C1 => " ", :C2 => " ", :C3 => " "} + end + + def welcome_player + "Welcome #{@player}" + end + + def current_player + if @random == true + @random = false + [@player, "Computer"].sample + elsif @comp_turn == true + @comp_turn == false + "Computer" + else + @player + end + end + + def player_symbol + @playerXO = SYMBOLS.sample + end + + def computer_symbol + sym = SYMBOLS.clone + sym.delete(@playerXO) + sym[0] + end + + def indicate_player_turn + puts "#{@player}'s Move:" + end + + def get_player_move + gets.chomp + end + + def open_spots + open_arr = [] + @board.select{|k, v| open_arr << k if v == " "} + open_arr + end + + def computer_move + move_arr = [] + @board.select{|k, v| move_arr << k if v == " "} + move = move_arr.sample + @board[move] = "#{computer_symbol}" + @comp_turn = false + move + end + + def player_move + move = get_player_move.to_sym until open_spots.include?move + @board[move] = "#{player_symbol}" + @comp_turn = true + move + end + + def current_state + result = "" + result << "#{@board[:A1]}|#{@board[:A2]}|#{@board[:A3]}\n" + result << "-" "|" "-" "|" "-\n" + result << "#{@board[:B1]}|#{@board[:B2]}|#{@board[:B3]}\n" + result << "-" "|" "-" "|" "-\n" + result << "#{@board[:C1]}|#{@board[:C2]}|#{@board[:C3]}" + end + + def determine_winner + x_arr = [] + o_arr = [] + @board.each{|k, v| x_arr << k if v == "X"} + @board.each{|k, v| o_arr << k if v == "O"} + + x_l = [] + x_n = [] + o_l = [] + o_n = [] + x_arr.each{|i| x_l << i[0]} + x_arr.each{|i| x_n << i[1]} + o_arr.each{|i| o_l << i[0]} + o_arr.each{|i| o_n << i[1]} + + la = ["A","A","A"] + lb = ["B","B","B"] + lc = ["C","C","C"] + n1 = ["1","1","1"] + n2 = ["2","2","2"] + n3 = ["3","3","3"] + @winning_symbol = :X if ((((x_l==(la||lb||lc))&&(x_n==(n1||n2||n3)))&&(@board[:B2]==X))||((x_l.uniq.sort==["A","B","C"])||(x_n.uniq.sort==["1","2","3"]))) + @winning_symbol = :O if ((((o_l==(la||lb||lc))&&(o_n==(n1||n2||n3)))&&(@board[:B2]==X))||((o_l.uniq.sort==["A","B","C"])||(o_n.uniq.sort==["1","2","3"]))) + end + + def over? + spots_open? || player_won? || computer_won? + end + + def spots_open? + @board.each{|k, v| v.to_s.include?(" ")} + end + + def player_won? + @winning_symbol == player_symbol + end + + def computer_won? + @winning_symbol == computer_symbol + end + + def draw? + over? && !player_won? && !computer_won? + end +end + diff --git a/tic-tac-toe/features/step_defintions/tic-tac-toe_steps.rb b/tic-tac-toe/features/step_defintions/tic-tac-toe_steps.rb new file mode 100644 index 0000000..d1f5265 --- /dev/null +++ b/tic-tac-toe/features/step_defintions/tic-tac-toe_steps.rb @@ -0,0 +1,109 @@ +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_player_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 computers turn$/ do +# @game = TicTacToe.new(:computer, :O) + @game = TicTacToe.new(:computer, :X) + @game.current_player.should eq "Computer" +end +Given /^the computer is playing X$/ do + @game.computer_symbol.should eq :X +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 +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 computers turn$/ do + @game.current_player.should eq "Computer" +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 + +When /^there are three Xs 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_truthy +end +Then /^the game ends$/ do + @game.over?.should be_truthy +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_truthy +end +Then /^the game is declared a draw$/ do + @game.draw?.should be_truthy +end + + diff --git a/tic-tac-toe/features/tic-tac-toe.feature b/tic-tac-toe/features/tic-tac-toe.feature new file mode 100644 index 0000000..aaae033 --- /dev/null +++ b/tic-tac-toe/features/tic-tac-toe.feature @@ -0,0 +1,58 @@ +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 computers 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 computers 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 computers turn + +Scenario: Winning the Game + Given I have a started Tic-Tac-Toe game + And I am playing X + When there are three Xs 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/worker.rb b/worker.rb new file mode 100644 index 0000000..20266b5 --- /dev/null +++ b/worker.rb @@ -0,0 +1,10 @@ +module Worker + def self.work(default = 1) + answer = "" + default.times{answer = yield} + answer + + # default.times.inject(nil){yield} + end +end + diff --git a/worker_spec.rb b/worker_spec.rb new file mode 100644 index 0000000..75de3dc --- /dev/null +++ b/worker_spec.rb @@ -0,0 +1,37 @@ +# require_relative '../../spec_helper.rb' +require "c:/sites/worker.rb" + +describe Worker do + + it "executes a block and returns a string" do + result = Worker.work do + "hello" + end + result.should == "hello" + end + + it "executes a block and returns a number" do + result = Worker.work do + 3 + 4 + end + result.should == 7 + end + + it "executes a block in the context of the calling method" do + n = 1 + result = Worker.work do + n + 4 + end + result.should == 5 + end + + + it "executes a block 3 times and returns the result" do + n = 5 + result = Worker.work(3) do + n += 1 + end + result.should == 8 + end + +end