Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pull #59

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open

pull #59

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
Binary file added HW1.rtf
Binary file not shown.
Binary file added HW2.rtf
Binary file not shown.
Binary file added HW3.rtf
Binary file not shown.
Binary file added HW4.rtf
Binary file not shown.
Binary file added HW7.rtf
Binary file not shown.
Binary file added HW7_.rtf
Binary file not shown.
26 changes: 26 additions & 0 deletions calculator.rb
Original file line number Diff line number Diff line change
@@ -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


12 changes: 12 additions & 0 deletions pirate/features/pirate.feature
Original file line number Diff line number Diff line change
@@ -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!!'

12 changes: 12 additions & 0 deletions pirate/features/step_defintions/pirate.rb
Original file line number Diff line number Diff line change
@@ -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
19 changes: 19 additions & 0 deletions pirate/features/step_defintions/pirate_steps.rb
Original file line number Diff line number Diff line change
@@ -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
12 changes: 12 additions & 0 deletions pirate_/features/pirate.feature
Original file line number Diff line number Diff line change
@@ -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!!'

12 changes: 12 additions & 0 deletions pirate_/features/step_defintions/pirate.rb
Original file line number Diff line number Diff line change
@@ -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
19 changes: 19 additions & 0 deletions pirate_/features/step_defintions/pirate_steps.rb
Original file line number Diff line number Diff line change
@@ -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
25 changes: 25 additions & 0 deletions simon_says.rb
Original file line number Diff line number Diff line change
@@ -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

48 changes: 48 additions & 0 deletions simon_says_spec.rb
Original file line number Diff line number Diff line change
@@ -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
33 changes: 33 additions & 0 deletions strings_and_rspec_spec.rb
Original file line number Diff line number Diff line change
@@ -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

124 changes: 124 additions & 0 deletions tic-tac-toe/features/step_defintions/tic-tac-toe.rb
Original file line number Diff line number Diff line change
@@ -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

Loading