From ece178de4896546eb588ab93251b738af1d4ceef Mon Sep 17 00:00:00 2001 From: Ktrops Date: Thu, 9 Oct 2014 14:40:43 -0700 Subject: [PATCH 01/16] Update strings_and_rspec_spec.rb --- week1/homework/strings_and_rspec_spec.rb | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/week1/homework/strings_and_rspec_spec.rb b/week1/homework/strings_and_rspec_spec.rb index 496e61d..d17634a 100644 --- a/week1/homework/strings_and_rspec_spec.rb +++ b/week1/homework/strings_and_rspec_spec.rb @@ -16,18 +16,18 @@ @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 + @my_string.length.should eq 66 + 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 end it "should be able to give the encoding of the string" do - pending 'helpful hint: should eq (Encoding.find("UTF-8"))' - encodeing #do something with @my_string here - #use helpful hint here + encode_result = @my_string.encoding + encode_result.should eq Encoding.find("UTF-8") end end end From 143ea754e5a6ef3c2d3539e4e8796b033c3baad7 Mon Sep 17 00:00:00 2001 From: Ktrops Date: Thu, 9 Oct 2014 17:31:03 -0700 Subject: [PATCH 02/16] Update questions.txt --- week1/homework/questions.txt | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/week1/homework/questions.txt b/week1/homework/questions.txt index 2257bb9..948d6a4 100644 --- a/week1/homework/questions.txt +++ b/week1/homework/questions.txt @@ -4,12 +4,28 @@ p.86-90 Strings (Strings section in Chapter 6 Standard Types) 1. What is an object? +An object is a data structure that interacts with its own code, in other words, anything we manipulate in Ruby. + 2. What is a variable? +A variable labels and holds data, like an integer or a string. + 3. What is the difference between an object and a class? +An object is a class instance and a class contains class instances. +Classes contain objects that interact with methods and each other. + 4. What is a String? +A string is a sequence of characters that are sometimes printable. In Ruby, there are many built-in methods +that can be used on strings. + 5. What are three messages that I can send to a string object? Hint: think methods +String#split, String#scan, String#chomp. + 6. What are two ways of defining a String literal? Bonus: What is the difference between them? + +To define a String literal you can use single quotes (‘ ’), double quotes (“ “), %q, %Q, or here documents. +%q and %Q are the same as single quotes and double quotes. +Here documents are treated as a section of source code and they can be used to create multiline strings. From 25cb2cd7871342707feeddeb9f49685fdd5b0a88 Mon Sep 17 00:00:00 2001 From: Ktrops Date: Thu, 16 Oct 2014 16:29:13 -0700 Subject: [PATCH 03/16] Update questions.txt --- week2/homework/questions.txt | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/week2/homework/questions.txt b/week2/homework/questions.txt index 939e42d..4e196d7 100644 --- a/week2/homework/questions.txt +++ b/week2/homework/questions.txt @@ -3,11 +3,18 @@ Containers, Blocks, and Iterators Sharing Functionality: Inheritance, Modules, and Mixins 1. What is the difference between a Hash and an Array? +A Hash has key-value pairs that can be any object and an Array uses only integers as an index. 2. When would you use an Array over a Hash and vice versa? +An Array is used when you have an ordered list and it's ok to have an integer for the index. Also, it's easier to use an Array for iteration. +A Hash is used when you want to store information using a key that is not an integer. + 3. What is a module? Enumerable is a built in Ruby module, what is it? +A module is a collection of constants and methods. Enumerable has methods used for searching and sorting collections (i.e. Hash and Array). 4. Can you inherit more than one thing in Ruby? How could you get around this problem? +A class can only inherit from one parent class, but by using Mixin Modules you can simulate multiple inheritance. 5. What is the difference between a Module and a Class? +Modules you can use across multiple classes. Modules are about functions and constants and classes are about objects and instances. From 500e6f9d77bbce46d757bf3757289e7e5b1b69ae Mon Sep 17 00:00:00 2001 From: Ktrops Date: Thu, 16 Oct 2014 16:37:42 -0700 Subject: [PATCH 04/16] Homework for week 2; Katie I created a new file called 'simon_says.rb' with the methods to make these tests pass. and I didn't make any changes to 'simon_says_spec.rb' --- week2/homework/simon_says.rb | 28 ++++++++++++++++++ week2/homework/simon_says_spec.rb | 48 ------------------------------- 2 files changed, 28 insertions(+), 48 deletions(-) create mode 100644 week2/homework/simon_says.rb delete mode 100644 week2/homework/simon_says_spec.rb diff --git a/week2/homework/simon_says.rb b/week2/homework/simon_says.rb new file mode 100644 index 0000000..5be35d2 --- /dev/null +++ b/week2/homework/simon_says.rb @@ -0,0 +1,28 @@ +class SimonSays + def echo answer + puts answer + end + def shout phrase + puts phrase.upcase + end + def repeat (text, num) + if num == nil + num = 1 + num.times do + puts text + end + else + num.times do + puts text + end + end + end + def start_of_word(text, num) + index = num - 1 + puts text[0..index] + + end + def first_word(text) + puts text.split(' ').first + end +end diff --git a/week2/homework/simon_says_spec.rb b/week2/homework/simon_says_spec.rb deleted file mode 100644 index 4e6f5bc..0000000 --- a/week2/homework/simon_says_spec.rb +++ /dev/null @@ -1,48 +0,0 @@ -# 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 From e08997b6794734ccc3757d4cc3c06380e1862757 Mon Sep 17 00:00:00 2001 From: Katie Atrops Date: Thu, 23 Oct 2014 17:43:35 -0700 Subject: [PATCH 05/16] homework for week 3 --- week3/homework/calculator.rb | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 week3/homework/calculator.rb diff --git a/week3/homework/calculator.rb b/week3/homework/calculator.rb new file mode 100644 index 0000000..e3b4ca8 --- /dev/null +++ b/week3/homework/calculator.rb @@ -0,0 +1,35 @@ +class Calculator + attr_accessor :collection, :num + + #def initialize + # @calculator = calculator + #end + def sum (collection) + if collection == [] + return 0 + else + return collection.inject(:+) + end + def multiply (collection, *arg) + if collection.class == String + return collection.to_a.inject(:*) + else + return collection.inject(:*) + + end + + def pow (collection, num) + b = 1 + num.times{b *= collection} + end + def fac (num) + if num == 0 + return 1 + else + array = (1..num).to_a + return array.inject(:*) + end + end + end + end + end From 2b6079fac1b2ef1aceeaaef71c50abb685e9e8a8 Mon Sep 17 00:00:00 2001 From: Katie Atrops Date: Thu, 23 Oct 2014 17:48:59 -0700 Subject: [PATCH 06/16] week2 hw --- week2/homework/questions.txt | 7 +++++++ week2/homework/simon_says_spec.rb | 2 +- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/week2/homework/questions.txt b/week2/homework/questions.txt index 939e42d..350cfbc 100644 --- a/week2/homework/questions.txt +++ b/week2/homework/questions.txt @@ -3,11 +3,18 @@ Containers, Blocks, and Iterators Sharing Functionality: Inheritance, Modules, and Mixins 1. What is the difference between a Hash and an Array? +A Hash has key-value pairs that can be any object and an Array uses only integers as an index. 2. When would you use an Array over a Hash and vice versa? +An Array is used when you have an ordered list and it's ok to have an integer for the index. Also, it's easier to use an Array for iteration. +A Hash is used when you want to store information using a key that is not an integer. + 3. What is a module? Enumerable is a built in Ruby module, what is it? +A module is a collection of constants and methods. Enumerable has methods used for searching and sorting collections (i.e. Hash and Array). 4. Can you inherit more than one thing in Ruby? How could you get around this problem? +A class can only inherit from one parent class, but by using Mixin Modules you can simulate multiple inheritance. 5. What is the difference between a Module and a Class? +Modules you can use across multiple classes. Modules are about functions and constants and classes are about objects and instances. diff --git a/week2/homework/simon_says_spec.rb b/week2/homework/simon_says_spec.rb index 4e6f5bc..0861cfb 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_relative "simon_says.rb" +require_relative 'simon_says.rb' require_relative '../../spec_helper' describe SimonSays do From b8624b4ace3b3ec6204eab7e7a37471ac33f09b8 Mon Sep 17 00:00:00 2001 From: Ktrops Date: Thu, 23 Oct 2014 18:36:44 -0700 Subject: [PATCH 07/16] Update questions.txt --- week3/homework/questions.txt | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/week3/homework/questions.txt b/week3/homework/questions.txt index dfb158d..51e4e32 100644 --- a/week3/homework/questions.txt +++ b/week3/homework/questions.txt @@ -1,3 +1,4 @@ + Please Read: - Chapter 6 Standard Types - Review Blocks @@ -6,10 +7,20 @@ Please Read: 1. What is a symbol? +A symbol is a constant name that is guaranteed to be unique and contain the same value through out the entire program. + 2. What is the difference between a symbol and a string? +Symbols are immutable, where as strings are mutable. Symbols are often used as hash keys because they are immutable. + 3. What is a block and how do I call a block? +A block is code between do and end or between brackets. An iterator method can call a block. The format for a block is first you have a collection for the method to iterator over then you have the keyword “do” or a open curly bracket then the value to be passed in, then the code, and finally a closing curly bracket or and “end” keyword. + 4. How do I pass a block to a method? What is the method signature? +By using the keyword yield you can pass a block to a method. You can also pass parameters to a block and a method can receive values from a block using method. The method signature is the method name with the parameter list. + 5. Where would you use regular expressions? + +You can use a regular expression to test a string for a matching pattern, to extract a matching pattern from a string, and substitution. From fbdb37d62bfd89ad8328fe90edf6957bc997e776 Mon Sep 17 00:00:00 2001 From: Ktrops Date: Thu, 23 Oct 2014 18:38:59 -0700 Subject: [PATCH 08/16] Update calculator.rb --- week3/homework/calculator.rb | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/week3/homework/calculator.rb b/week3/homework/calculator.rb index e3b4ca8..a932905 100644 --- a/week3/homework/calculator.rb +++ b/week3/homework/calculator.rb @@ -1,9 +1,7 @@ class Calculator attr_accessor :collection, :num - #def initialize - # @calculator = calculator - #end + def sum (collection) if collection == [] return 0 From 29178255131a6cd88bd8f1e536da2d91f191ad03 Mon Sep 17 00:00:00 2001 From: Katie Atrops Date: Wed, 29 Oct 2014 14:05:00 -0700 Subject: [PATCH 09/16] To commit changes to calculator.rb --- week3/homework/calculator.rb | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/week3/homework/calculator.rb b/week3/homework/calculator.rb index e3b4ca8..1987578 100644 --- a/week3/homework/calculator.rb +++ b/week3/homework/calculator.rb @@ -1,20 +1,18 @@ class Calculator - attr_accessor :collection, :num - - #def initialize - # @calculator = calculator - #end - def sum (collection) - if collection == [] + + def sum input + if input == [] return 0 else - return collection.inject(:+) + total = input.inject(:+) end - def multiply (collection, *arg) - if collection.class == String - return collection.to_a.inject(:*) + total +end + def multiply *input + if input.class == String + return input.to_a.inject(:*) else - return collection.inject(:*) + return input.inject(:*) end From f305230bdca33fb079e8a21daaa5061ad0993148 Mon Sep 17 00:00:00 2001 From: Katie Atrops Date: Thu, 30 Oct 2014 17:53:06 -0700 Subject: [PATCH 10/16] to add worker.rb --- week4/homework/questions.txt | 15 +++++++++++++++ week4/homework/worker.rb | 19 +++++++++++++++++++ 2 files changed, 34 insertions(+) create mode 100644 week4/homework/worker.rb diff --git a/week4/homework/questions.txt b/week4/homework/questions.txt index ffaf215..ec33793 100644 --- a/week4/homework/questions.txt +++ b/week4/homework/questions.txt @@ -4,10 +4,25 @@ The Rake Gem: http://rake.rubyforge.org/ 1. How does Ruby read files? +Ruby reads a line from a standard input or files specified when gets or file.gets is used. +There are also iterators you can use to read files. IO#each_byte iterates over 8-bit bytes from the IO object. IO#each_line calls the block with each line from the file. IO#foreach opens the file to read, calls the block on one line from the file then closes the file. IO#read gets an entire file into a string or into an array of lines. + 2. How would you output "Hello World!" to a file called my_output.txt? +File.open(“my_output.txt”, “w”) do |file| + file.puts “Hello World!” +end + +puts File.read(“my_output.txt”) + 3. What is the Directory class and what is it used for? +the Directory class is a way to list files and directories in Ruby. You can make instances of the Dir class and you can iterate over an object of the Dir class. + 4. What is an IO object? +An IO object is a way for a ruby program to write to or read from an external resource. + 5. What is rake and what is it used for? What is a rake task? + +Rake is an internal Domain Specific Language based in Ruby. Rake has libraries that make it easy to do tasks that are common during the build and deploy process. You can define a rake task to customize it or if you don’t define a rake task then it defaults to test. diff --git a/week4/homework/worker.rb b/week4/homework/worker.rb new file mode 100644 index 0000000..a1439fd --- /dev/null +++ b/week4/homework/worker.rb @@ -0,0 +1,19 @@ +class Worker +def self.work (*t) + if t == [] + yield + else + n = t[0] + num = 0 + while num < n + num += 1 + total = yield + end + total + + + +end + +end +end From 9e8331bab4f128909f301521da7adb8cd59a3e3d Mon Sep 17 00:00:00 2001 From: Katie Atrops Date: Thu, 13 Nov 2014 20:00:44 -0800 Subject: [PATCH 11/16] to add files --- midterm/instructions_and_questions.txt | 5 +++++ midterm/mid_term_spec.rb | 10 +++++----- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/midterm/instructions_and_questions.txt b/midterm/instructions_and_questions.txt index a038c9d..a7f4d80 100644 --- a/midterm/instructions_and_questions.txt +++ b/midterm/instructions_and_questions.txt @@ -11,8 +11,13 @@ Instructions for Mid-Term submission and Git Review (10pts): Questions (20pts): - What are the three uses of the curly brackets {} in Ruby? + +T - 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? +Floats are saved by the same object id, saves them as a singleton above 32 bits it doesn’t freeze it anymore +symbols are fixed strings, its a singleton, it is always the same symbol when you reference it.saves on memorygit +in contrast when you use strings a a differnt instance when referenced by the same characters - Are these two statements equivalent? Why or Why Not? 1. x, y = "hello", "hello" 2. x = y = "hello" diff --git a/midterm/mid_term_spec.rb b/midterm/mid_term_spec.rb index 87e5c74..73db16d 100644 --- a/midterm/mid_term_spec.rb +++ b/midterm/mid_term_spec.rb @@ -1,4 +1,4 @@ -require_relative '../../spec_helper.rb' +require_relative '../spec_helper.rb' require "#{File.dirname(__FILE__)}/turkey" describe Turkey do @@ -12,7 +12,7 @@ end it "should be a kind of animal" do - @turkey.kind_of?(Animal).should be_true + @turkey.kind_of?(Animal).should be_truthy end it "should gobble speak" do @@ -31,7 +31,7 @@ end it "should be a kind of dinner" do - @t_dinner.kind_of?(Dinner).should be_true + @t_dinner.kind_of?(Dinner).should be_truthy end # Use inject here @@ -40,7 +40,7 @@ end it "should provide a menu" do - @t_dinner.respond_to?(:menu).should be_true + @t_dinner.respond_to?(:menu).should be_truthy end context "#menu" do @@ -54,7 +54,7 @@ end it "should have vegetables" do - @t_dinner.menu[:veggies].should eq [:ginger_carrots , :potatoes, :yams] + @t_dinner.menu[:veggies].should eq [:ginger_carrots, :potatoes, :yams] end # Dinners don't always have dessert, but ThanksgivingDinners always do! From 063a7ef325b42d7097be9cdd45b4bbcbc5c53f07 Mon Sep 17 00:00:00 2001 From: Katie Atrops Date: Thu, 20 Nov 2014 13:41:44 -0800 Subject: [PATCH 12/16] to add homework for week7 --- week7/homework/features/pirate.feature | 4 ++-- .../homework/features/step_definitions/pirate.rb | 11 +++++++++++ week7/homework/questions.txt | 16 ++++++++++++++++ 3 files changed, 29 insertions(+), 2 deletions(-) create mode 100644 week7/homework/features/step_definitions/pirate.rb diff --git a/week7/homework/features/pirate.feature b/week7/homework/features/pirate.feature index 7de1a0b..b030c86 100644 --- a/week7/homework/features/pirate.feature +++ b/week7/homework/features/pirate.feature @@ -6,6 +6,6 @@ Ahoy matey!: Pirate Speak Heave to: The mighty speaking pirate Gangway! I have a PirateTranslator Blimey! I say 'Hello Friend' - Aye I hit translate + Aye I hit translate Let go and haul it prints out 'Ahoy Matey' - Avast! it also prints 'Shiber Me Timbers You Scurvey Dogs!!' + Avast! it also prints 'Shiber Me Timbers You Scurvey Dogs!!' diff --git a/week7/homework/features/step_definitions/pirate.rb b/week7/homework/features/step_definitions/pirate.rb new file mode 100644 index 0000000..8044f66 --- /dev/null +++ b/week7/homework/features/step_definitions/pirate.rb @@ -0,0 +1,11 @@ +class PirateTranslator + + def say(arg) + @hi = "Ahoy Matey" + return @hi + end + + def translate + return "#{@hi}\n Shiber Me Timbers You Scurvey Dogs!!" + end +end \ No newline at end of file diff --git a/week7/homework/questions.txt b/week7/homework/questions.txt index d55387d..8e5ba1c 100644 --- a/week7/homework/questions.txt +++ b/week7/homework/questions.txt @@ -3,7 +3,23 @@ Please Read Chapters 23 and 24 DuckTyping and MetaProgramming Questions: 1. What is method_missing and how can it be used? + +If Ruby can't find the name of the method in the object's class ancestry then it looks for method_missing. Method_missing can be overridden and used in your own classes, so you can make your own error message in an application specific way. Method_missing can also be used to simulate accessors by putting the object in its singleton class. + 2. What is and Eigenclass and what is it used for? Where Do Singleton methods live? + +An Eigenclass is a singleton class. When you define a Singleton method for an object Ruby creates an anonymous class for the Singleton method called an Eigenclass. + 3. When would you use DuckTypeing? How would you use it to improve your code? + +You can use DuckTypeing if you want more flexible code. For example, if you want to append something to a file, you can pass in a string, an array, or a file and #append will work the same. + 4. What is the difference between a class method and an instance method? What is the difference between instance_eval and class_eval? + +With class method, you can call the method on the class itself, where as with an instance method you have to create an instance of the class before you can call the method. +With Class_eval things are set in the body of the class and will be define an instance method. Where as instance_eval sets things up like you were in a singleton method of class self and thus any methods defined will be class methods. + + 5. What is the difference between a singleton class and a singleton method? + +A singleton method is unique to a single object a singleton class contains singleton methods. From 754f6de4b8d40b6eacce9c916459291013aeea40 Mon Sep 17 00:00:00 2001 From: Katie Atrops Date: Wed, 10 Dec 2014 16:25:12 -0800 Subject: [PATCH 13/16] for final --- .../features/step_definitions/play_game.rb | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 week7/homework/features/step_definitions/play_game.rb diff --git a/week7/homework/features/step_definitions/play_game.rb b/week7/homework/features/step_definitions/play_game.rb new file mode 100644 index 0000000..a8948fa --- /dev/null +++ b/week7/homework/features/step_definitions/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_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? From 78226cdd12c7dd1805aea522dfb754f2ac92aca3 Mon Sep 17 00:00:00 2001 From: Katie Atrops Date: Wed, 10 Dec 2014 16:26:03 -0800 Subject: [PATCH 14/16] my code for the final --- .../features/step_definitions/tic-tac-toe.rb | 156 ++++++++++++++++++ 1 file changed, 156 insertions(+) create mode 100644 week7/homework/features/step_definitions/tic-tac-toe.rb 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..2f1893d --- /dev/null +++ b/week7/homework/features/step_definitions/tic-tac-toe.rb @@ -0,0 +1,156 @@ +class TicTacToe + attr_accessor :player + attr_accessor :board + attr_accessor :player_symbol + attr_accessor :computer_symbol + + SYMBOLS = [:O, :X] + + def initialize(first_player = nil, player_symbol = nil) + if player_symbol == nil + @player_symbol = :O + @computer_symbol = :X + else + @player_symbol = player_symbol + if @player_symbol == :X + @computer_symbol = :O + else + @computer_symbol = :X + end + end + + if first_player == nil + @c_player = [:player, :computer].sample + else + @c_player = first_player + end + @board = {:A1 => " ", :A2 => " ", :A3 => " ", + :B1 => " ", :B2 => " ", :B3 => " ", + :C1 => " ", :C2 => " ", :C3 => " " + } + end + + def player=(p) + @player = p + + end + + + def welcome_player + return "Welcome #{@player}" + + + end + + def current_player + if @c_player == :player + return @player + else + return "Computer" + end + end + + def indicate_player_turn + puts "#{@player}'s Move:" + end + + def get_player_move + player_move = gets.chomp + return player_move + end + + def player_move + move = get_player_move.to_sym + while @board[move] != " " + move = get_player_move.to_sym + end + @board[move] = @player_symbol + @c_player = :computer + return move + end + + def computer_move + c_move = open_spots.sample + @board[c_move] = @computer_symbol + @c_player = :player + return c_move + end + + def spots_open? + if open_spots.empty? + false + else + true + end + end + + + def open_spots + open_array = [] + @board.each do |k, v| + if v == " " + open_array.push(k) + end + end + open_array + end + + def determine_winner + in_a_row = [[:A1, :A2, :A3], + [:B1, :B2, :B3], + [:C1, :C2, :C3], + [:A1, :B1, :C1], + [:A3, :B3, :C3], + [:A1, :B2, :C3], + [:A3, :B2, :C1]] + if @c_player == :player + symbol = @player_symbol + else + symbol = @computer_symbol + end + in_a_row.each do |a| + first = a[0] + second = a[1] + third = a[2] + if @board[first] == symbol && @board[second] == symbol && @board[third] == symbol + puts "you won, #{symbol}" + if symbol == @player_symbol + return :player + else + return :computer + end + end + end + end + + def player_won? + determine_winner == :player + end + + def computer_won? + determine_winner == :computer + end + + def current_state + @board.to_s + end + + def draw? + if player_won? || computer_won? + return false + end + @board.each do |k, v| + if v == " " + return false + end + end + true + end + + def over? + true if determine_winner || draw? + end + + + +end From 5ad3a2c3d1c9e91001cb09aee92efa66a18828fb Mon Sep 17 00:00:00 2001 From: Katie Atrops Date: Wed, 10 Dec 2014 16:26:50 -0800 Subject: [PATCH 15/16] for final --- .../step_definitions/tic-tac-toe-steps.rb | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) 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 a3287c1..fed8b28 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| @@ -43,7 +43,7 @@ @game.get_player_move end -Given /^it is the computer's turn$/ do +Given /^it is the computers turn$/ do @game = TicTacToe.new(:computer, :O) @game.current_player.should eq "Computer" end @@ -77,22 +77,22 @@ @old_pos.should eq " " end -Then /^it is now the computer's turn$/ do +Then /^it is now the computers turn$/ do @game.current_player.should eq "Computer" end -When /^there are three X's in a row$/ do - @game = TicTacToe.new(:computer, :X) +When /^there are three Xs in a row$/ do + @game = TicTacToe.new(:player, :X) #computer @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.player_won?.should be_truthy end Then /^the game ends$/ do - @game.over?.should be_true + @game.over?.should be_truthy end Given /^there are not three symbols in a row$/ do @@ -105,11 +105,11 @@ end When /^there are no open spaces left on the board$/ do - @game.spots_open?.should be_false + @game.spots_open?.should be_falsey end Then /^the game is declared a draw$/ do - @game.draw?.should be_true + @game.draw?.should be_truthy end When /^"(.*?)" is taken$/ do |arg1| From 62aea11dca2153fea090ae5ac780f0251984d5e4 Mon Sep 17 00:00:00 2001 From: Katie Atrops Date: Wed, 10 Dec 2014 16:27:54 -0800 Subject: [PATCH 16/16] for final --- week7/homework/features/tic-tac-toe.feature | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/week7/homework/features/tic-tac-toe.feature b/week7/homework/features/tic-tac-toe.feature index 6f3134d..a63d919 100644 --- a/week7/homework/features/tic-tac-toe.feature +++ b/week7/homework/features/tic-tac-toe.feature @@ -1,7 +1,7 @@ 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 + I would like to play against the computer Scenario: Begin Game Given I start a new Tic-Tac-Toe game @@ -19,7 +19,7 @@ Scenario: My Turn Scenario: Computer's Turn Given I have a started Tic-Tac-Toe game - And it is the computer's turn + 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 @@ -31,7 +31,7 @@ Scenario: Making Moves 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 + And it is now the computers turn Scenario: Making Bad Moves Given I have a started Tic-Tac-Toe game @@ -40,12 +40,12 @@ Scenario: Making Bad Moves 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 + 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 X's in a row + When there are three Xs in a row Then I am declared the winner And the game ends