From d786dfbd74b2292d9f4ce2f88ee4c60e19af36cd Mon Sep 17 00:00:00 2001 From: wmuengineer Date: Sun, 20 Oct 2013 12:17:58 -0700 Subject: [PATCH 01/25] First Submittion of my homework I'm turning in late homework as I was a late add to the class. --- week1/homework/questions.txt | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/week1/homework/questions.txt b/week1/homework/questions.txt index bd581a6..f9347f5 100644 --- a/week1/homework/questions.txt +++ b/week1/homework/questions.txt @@ -2,14 +2,18 @@ Please read: Chapter 3 Classes, Objects, and Variables p.86-90 Strings (Strings section in Chapter 6 Standard Types) -1. What is an object? +1. What is an object?- Everything in Ruby is an object. -2. What is a variable? +2. What is a variable? something i can manipulate/change -3. What is the difference between an object and a class? +3. What is the difference between an object and a class? A class is an object that represents a state and methods that can change that state. -4. What is a String? +4. What is a String? text 5. What are three messages that I can send to a string object? Hint: think methods +length, new concatenate + +6. What are two ways of defining a String literal? +Single quotes : is a what you see is what you get. +double quotes : The string can use string interpolation to allow you to enter text dynamically" " -6. What are two ways of defining a String literal? Bonus: What is the difference between the two? From f305b2f196906dd7670ed65e645c05e2b798ec17 Mon Sep 17 00:00:00 2001 From: wmuengineer Date: Sun, 20 Oct 2013 22:07:32 -0700 Subject: [PATCH 02/25] Submitting Homework for week 1 --- week1/exercises/roster.txt | 4 ++-- week1/homework/string-program.rb | 12 ++++++++++++ week1/homework/strings_and_rspec_spec.rb | 11 +++++++---- 3 files changed, 21 insertions(+), 6 deletions(-) create mode 100644 week1/homework/string-program.rb diff --git a/week1/exercises/roster.txt b/week1/exercises/roster.txt index ae75ad1..af85dab 100644 --- a/week1/exercises/roster.txt +++ b/week1/exercises/roster.txt @@ -14,14 +14,14 @@ 12, David Husa, davidhusa@gmail.com, github.com/davidhusa, @davidhusa, davidhusa 13,idil, ijama26@gmail.com, idiljay, idiljay 14,Elizabeth A. Kari, eakari@live.com, catfriend, no twitter, no chat -15, +15, Karen Kirr, kkirr77@yahoo.com, kirrk, @kirrk, Karen Kirr 16, Dave, davidlane2@mac.com, davejlane, n/a, davejlane 17, Doug MacDowell, dougmac9@uw.edu, Doug-MacDowell, , doug-macdowell 18, Jordan Mayer, Jordan2210@gmail.com, jordanmayer, n/a, Jordanm 19, Martin Nash, martin.j.nash@gmail.com, MartinJNash, @MartinJNash, martin.j.nash 20, Thomas Osborn, trosborn@gmail.com, trosborn, joosushi, trosborn 21, Vy Nguyen, vnguye36@gmail.com, github.com/shrsp -22, +22, Chris Montes, chrismontes@about.me, mandelbro, @chrismontes, chrismontes 23,Alicia Quilez, alienor18@yahoo.com 24,Pat Remy, pat@remy.cc, premy52, , subs@remy.cc 25, Mohamed Saeed, mahadi@gmail.com, wahshi, n/a, mahdi diff --git a/week1/homework/string-program.rb b/week1/homework/string-program.rb new file mode 100644 index 0000000..8f07b20 --- /dev/null +++ b/week1/homework/string-program.rb @@ -0,0 +1,12 @@ + +# def initialize (my_string) + my_string = "Renée is a fun teacher. Ruby is a really cool programming language" +puts "#{my_string}" + # end + #def text_ops (text) + s_length = my_string.length + split_string = my_string.split(".") +puts "#{s_length}" +puts "#{split_string}" +# end +#end diff --git a/week1/homework/strings_and_rspec_spec.rb b/week1/homework/strings_and_rspec_spec.rb index ea79e4c..0be5d3a 100644 --- a/week1/homework/strings_and_rspec_spec.rb +++ b/week1/homework/strings_and_rspec_spec.rb @@ -12,14 +12,17 @@ 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 + @my_string.should_not be_nil + 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"))' + + @my_string.encoding.should eq (Encoding.find("UTF-8")) end end end From aab79242f45aa6a89c27278d937810e4383a5c18 Mon Sep 17 00:00:00 2001 From: wmuengineer Date: Mon, 21 Oct 2013 23:48:04 -0700 Subject: [PATCH 03/25] Week 2 Homework Week two homework turned in. --- week2/homework/questions.txt | 19 ++++++++++++++++-- week2/homework/simon_says.rb | 33 +++++++++++++++++++++++++++++++ week2/homework/simon_says_spec.rb | 2 +- 3 files changed, 51 insertions(+), 3 deletions(-) create mode 100644 week2/homework/simon_says.rb diff --git a/week2/homework/questions.txt b/week2/homework/questions.txt index 939e42d..ea8c33f 100644 --- a/week2/homework/questions.txt +++ b/week2/homework/questions.txt @@ -3,11 +3,26 @@ Containers, Blocks, and Iterators Sharing Functionality: Inheritance, Modules, and Mixins 1. What is the difference between a Hash and an Array? +An array stores data using an index as the locator. A Hash uses a key that can be any object. That object is associated with the data stored under that key. 2. When would you use an Array over a Hash and vice versa? -3. What is a module? Enumerable is a built in Ruby module, what is it? +An array seems to be a good idea when you're intaking data for processing. In the example in the book when you want to do a word counting operation, creating a large array with all of the words as entries seems like a good idea. Now when that array is read each word can serve as a key and you can count how many times that word appears. -4. Can you inherit more than one thing in Ruby? How could you get around this problem? +Another place i can see an array having short coming is if I were to design an application with custom fields. If i were to use an array i would have to "keep track" of the data to know what to report back as an answer to a query. If all of the entries are hashes I just retrieve the data based on the hash. + +3. What is a module? +A module is a collection of classes, methods and constants that can be used together. + +Enumerable is a built in Ruby module, what is it? + +Emumerable is a mixin that allows your class to use methods that aid in sorting, comparing and modifying hashes and arrays. + +4. Can you inherit more than one thing in Ruby? +No Ruby is a single inheritance language. + +How could you get around this problem? +Mixins/modules help get around this problem by providing inheritance like functionality. 5. What is the difference between a Module and a Class? +A class contains methods and constants. A module can contain classes, methods, and constants. Modules help prevent clashed between methods with the same names between different classes. diff --git a/week2/homework/simon_says.rb b/week2/homework/simon_says.rb new file mode 100644 index 0000000..2e2798f --- /dev/null +++ b/week2/homework/simon_says.rb @@ -0,0 +1,33 @@ +# what does this program need to do? repeat strings given to it. + +module SimonSays + def echo(string) + p(string) + end + + def shout(string) + p(string).upcase + end + + def repeat(string, number) + a=(string) + # one cycle + b= a + " " + c=b*number + d=c.strip + p d + end + + def start_of_word(string, number) + letters=(string).scan(/./) + returned_letters= letters.first(number).join() + p returned_letters + end + + def first_word(string) + words = (string).scan(/[\w']+/) + ret_words = words[0] + end + +end + diff --git a/week2/homework/simon_says_spec.rb b/week2/homework/simon_says_spec.rb index 7f329e5..c641f28 100644 --- a/week2/homework/simon_says_spec.rb +++ b/week2/homework/simon_says_spec.rb @@ -22,7 +22,7 @@ end it "should repeat" do - repeat("hello").should == "hello hello" + repeat("hello", 2).should == "hello hello" end it "should repeat a number of times" do From 8cd66959f240eebdac3cb75ecfbf728aadbe9aca Mon Sep 17 00:00:00 2001 From: wmuengineer Date: Sun, 20 Oct 2013 12:17:58 -0700 Subject: [PATCH 04/25] First Submittion of my homework I'm turning in late homework as I was a late add to the class. --- week1/homework/questions.txt | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/week1/homework/questions.txt b/week1/homework/questions.txt index bd581a6..f9347f5 100644 --- a/week1/homework/questions.txt +++ b/week1/homework/questions.txt @@ -2,14 +2,18 @@ Please read: Chapter 3 Classes, Objects, and Variables p.86-90 Strings (Strings section in Chapter 6 Standard Types) -1. What is an object? +1. What is an object?- Everything in Ruby is an object. -2. What is a variable? +2. What is a variable? something i can manipulate/change -3. What is the difference between an object and a class? +3. What is the difference between an object and a class? A class is an object that represents a state and methods that can change that state. -4. What is a String? +4. What is a String? text 5. What are three messages that I can send to a string object? Hint: think methods +length, new concatenate + +6. What are two ways of defining a String literal? +Single quotes : is a what you see is what you get. +double quotes : The string can use string interpolation to allow you to enter text dynamically" " -6. What are two ways of defining a String literal? Bonus: What is the difference between the two? From 9f5da2ae07c8efc49bfeecaa37bb875b75a59460 Mon Sep 17 00:00:00 2001 From: wmuengineer Date: Sun, 20 Oct 2013 22:07:32 -0700 Subject: [PATCH 05/25] Submitting Homework for week 1 --- week1/homework/string-program.rb | 12 ++++++++++++ week1/homework/strings_and_rspec_spec.rb | 11 +++++++---- 2 files changed, 19 insertions(+), 4 deletions(-) create mode 100644 week1/homework/string-program.rb diff --git a/week1/homework/string-program.rb b/week1/homework/string-program.rb new file mode 100644 index 0000000..8f07b20 --- /dev/null +++ b/week1/homework/string-program.rb @@ -0,0 +1,12 @@ + +# def initialize (my_string) + my_string = "Renée is a fun teacher. Ruby is a really cool programming language" +puts "#{my_string}" + # end + #def text_ops (text) + s_length = my_string.length + split_string = my_string.split(".") +puts "#{s_length}" +puts "#{split_string}" +# end +#end diff --git a/week1/homework/strings_and_rspec_spec.rb b/week1/homework/strings_and_rspec_spec.rb index ea79e4c..0be5d3a 100644 --- a/week1/homework/strings_and_rspec_spec.rb +++ b/week1/homework/strings_and_rspec_spec.rb @@ -12,14 +12,17 @@ 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 + @my_string.should_not be_nil + 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"))' + + @my_string.encoding.should eq (Encoding.find("UTF-8")) end end end From 673913adeb09fc227916102c39af5734a9aeef57 Mon Sep 17 00:00:00 2001 From: wmuengineer Date: Mon, 21 Oct 2013 23:48:04 -0700 Subject: [PATCH 06/25] Week 2 Homework Week two homework turned in. --- week2/homework/questions.txt | 19 ++++++++++++++++-- week2/homework/simon_says.rb | 33 +++++++++++++++++++++++++++++++ week2/homework/simon_says_spec.rb | 2 +- 3 files changed, 51 insertions(+), 3 deletions(-) create mode 100644 week2/homework/simon_says.rb diff --git a/week2/homework/questions.txt b/week2/homework/questions.txt index 939e42d..ea8c33f 100644 --- a/week2/homework/questions.txt +++ b/week2/homework/questions.txt @@ -3,11 +3,26 @@ Containers, Blocks, and Iterators Sharing Functionality: Inheritance, Modules, and Mixins 1. What is the difference between a Hash and an Array? +An array stores data using an index as the locator. A Hash uses a key that can be any object. That object is associated with the data stored under that key. 2. When would you use an Array over a Hash and vice versa? -3. What is a module? Enumerable is a built in Ruby module, what is it? +An array seems to be a good idea when you're intaking data for processing. In the example in the book when you want to do a word counting operation, creating a large array with all of the words as entries seems like a good idea. Now when that array is read each word can serve as a key and you can count how many times that word appears. -4. Can you inherit more than one thing in Ruby? How could you get around this problem? +Another place i can see an array having short coming is if I were to design an application with custom fields. If i were to use an array i would have to "keep track" of the data to know what to report back as an answer to a query. If all of the entries are hashes I just retrieve the data based on the hash. + +3. What is a module? +A module is a collection of classes, methods and constants that can be used together. + +Enumerable is a built in Ruby module, what is it? + +Emumerable is a mixin that allows your class to use methods that aid in sorting, comparing and modifying hashes and arrays. + +4. Can you inherit more than one thing in Ruby? +No Ruby is a single inheritance language. + +How could you get around this problem? +Mixins/modules help get around this problem by providing inheritance like functionality. 5. What is the difference between a Module and a Class? +A class contains methods and constants. A module can contain classes, methods, and constants. Modules help prevent clashed between methods with the same names between different classes. diff --git a/week2/homework/simon_says.rb b/week2/homework/simon_says.rb new file mode 100644 index 0000000..2e2798f --- /dev/null +++ b/week2/homework/simon_says.rb @@ -0,0 +1,33 @@ +# what does this program need to do? repeat strings given to it. + +module SimonSays + def echo(string) + p(string) + end + + def shout(string) + p(string).upcase + end + + def repeat(string, number) + a=(string) + # one cycle + b= a + " " + c=b*number + d=c.strip + p d + end + + def start_of_word(string, number) + letters=(string).scan(/./) + returned_letters= letters.first(number).join() + p returned_letters + end + + def first_word(string) + words = (string).scan(/[\w']+/) + ret_words = words[0] + end + +end + diff --git a/week2/homework/simon_says_spec.rb b/week2/homework/simon_says_spec.rb index 7f329e5..c641f28 100644 --- a/week2/homework/simon_says_spec.rb +++ b/week2/homework/simon_says_spec.rb @@ -22,7 +22,7 @@ end it "should repeat" do - repeat("hello").should == "hello hello" + repeat("hello", 2).should == "hello hello" end it "should repeat a number of times" do From a3be6b40975925f8cf39ac30de0880219f7fe7d6 Mon Sep 17 00:00:00 2001 From: wmuengineer Date: Tue, 22 Oct 2013 18:10:49 -0700 Subject: [PATCH 07/25] Doing a test to see if this works MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This does work… I think --- week3/homework/questions.txt | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/week3/homework/questions.txt b/week3/homework/questions.txt index dfb158d..8559a96 100644 --- a/week3/homework/questions.txt +++ b/week3/homework/questions.txt @@ -13,3 +13,7 @@ Please Read: 4. How do I pass a block to a method? What is the method signature? 5. Where would you use regular expressions? + + + +sdfasdfasdfasd From e79995a2f46366c69ce9f26015bfa1a9f002c86c Mon Sep 17 00:00:00 2001 From: wmuengineer Date: Thu, 24 Oct 2013 22:40:18 -0700 Subject: [PATCH 08/25] Completed calculator.rb homework All tests pass. Now i have to reafactor the factorial method before i turn in the homework. --- untitled.tmproj | 59 ++++++++++++++++++++++++++++++ week2/.DS_Store | Bin 6148 -> 6148 bytes week2/exercises/book.rb | 21 +++++++---- week2/exercises/book_spec.rb | 16 ++++++++ week2/homework/simon_says.rb | 20 +++++----- week3/homework/calculator.rb | 22 +++++++++++ week3/homework/calculator_spec.rb | 6 +-- week3/homework/questions.txt | 2 +- 8 files changed, 124 insertions(+), 22 deletions(-) create mode 100644 untitled.tmproj create mode 100644 week3/homework/calculator.rb diff --git a/untitled.tmproj b/untitled.tmproj new file mode 100644 index 0000000..1d1e532 --- /dev/null +++ b/untitled.tmproj @@ -0,0 +1,59 @@ + + + + + documents + + + filename + week2/exercises/book_spec.rb + lastUsed + 2013-10-23T03:01:38Z + + + filename + week3/exercises/monster.rb + + + filename + week3/exercises/monsters.rb + lastUsed + 2013-10-23T03:28:51Z + selected + + + + filename + week3/exercises/named_thing.rb + + + filename + week3/exercises/vampire.rb + + + filename + week2/exercises/book.rb + lastUsed + 2013-10-23T03:01:40Z + + + filename + week2/exercises/mad_libs.rb + + + filename + week2/homework/simon_says.rb + lastUsed + 2013-10-23T01:32:06Z + + + fileHierarchyDrawerWidth + 200 + metaData + + showFileHierarchyDrawer + + windowFrame + {{210, 54}, {1070, 719}} + + diff --git a/week2/.DS_Store b/week2/.DS_Store index 6f2aa37b766a3ea9609fe9a4a7c689f88a3f19fd..498d1c067f2d546aa69c4003b1aff24063d4dff0 100644 GIT binary patch delta 358 zcmZoMXfc=|#>B)qu~2NHo+2ab!~pA!7aACWj2^v-{qpr`DaFZ2`T02vK)}h6%1{Bs zMGVOdnGD51RxwPP5h%^Ukin48kP8$p2hv3h*-7Qa1xY#iNkF-^Nd-BX#U%y?*BF_Y zSyxt4;1pQOA*!Tj z;}McrRb5kC2l5RA10%@QAixWu;A8`k1@Vmtl6T;K-FR>g%Vu^Cehy%`Z9Mp$c{0C< Xt_Vmo$h-y!4K{jnh{y)!i480O&3;>C delta 72 zcmZoMXfc=|#>CJzu~2NHo+2aT!~knX#>qTPdYem_C$nsBVBW>FnVo~51E^$kA@g_U a$^0U^oQw<%3 Date: Thu, 24 Oct 2013 23:11:42 -0700 Subject: [PATCH 09/25] Calculation.rb is complete --- week2/exercises/book.rb | 25 +++++++++++++++++- week2/exercises/book_spec.rb | 51 +++++++++++++++++++++++++++++++++++- 2 files changed, 74 insertions(+), 2 deletions(-) diff --git a/week2/exercises/book.rb b/week2/exercises/book.rb index 7591974..49cca40 100644 --- a/week2/exercises/book.rb +++ b/week2/exercises/book.rb @@ -1,18 +1,41 @@ class Book + attr_accessor :title + attr_reader :page_count - attr_accessor :title, :pages + @@book_count = 0 +<<<<<<< HEAD def self.library_count @@book_count end def test @test= "hello!" +======= + def self.book_count + @@book_count + end + + def initialize title = "Not Set", page_count = 0 + @@book_count += 1 + @page_count = page_count + @title = title + end + + + def test + @test = "Hello!" +>>>>>>> fde8a74b427aa79cd14a4014401682c12225e82b end def out_put_test puts @test puts @@book_count end +<<<<<<< HEAD end +======= + +end +>>>>>>> fde8a74b427aa79cd14a4014401682c12225e82b diff --git a/week2/exercises/book_spec.rb b/week2/exercises/book_spec.rb index 9d86bc8..c8415f2 100644 --- a/week2/exercises/book_spec.rb +++ b/week2/exercises/book_spec.rb @@ -1,6 +1,7 @@ -require './book.rb' +require './book' describe Book do +<<<<<<< HEAD context "::new" do it "" @@ -28,3 +29,51 @@ end end +======= + + + context "::book_count" do + + it "should count how many books have been created" do + Book.new + Book.new + Book.new + Book.book_count.should eq 3 + end + + end + + context "::new" do + + it "should set some defaults" do + Book.new.title.should eq "Not Set" + end + + it "should allow us to set the page count" do + book = Book.new "Harry Potter", 5 + book.page_count.should eq 5 + end + + end + + context "#title" do + + before :each do + @book = Book.new + end + + it "should have a title" do + @book.should respond_to "title" + end + + it "should allow me to set the title" do + @book.title = "Snow Crash" + @book.title.should eq "Snow Crash" + end + + + + end + +end +>>>>>>> fde8a74b427aa79cd14a4014401682c12225e82b From 1382d176de2b8051f4c7bcb147533f0cb20f3cf1 Mon Sep 17 00:00:00 2001 From: wmuengineer Date: Sun, 27 Oct 2013 22:10:02 -0700 Subject: [PATCH 10/25] Merge branch 'master' of https://github.com/wmuengineer/RubyFall2013 Conflicts: week2/homework/simon_says.rb --- week3/homework/calculator.rb | 10 +++------- week3/homework/questions.txt | 12 ++++++++---- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/week3/homework/calculator.rb b/week3/homework/calculator.rb index d028ded..6613d7d 100644 --- a/week3/homework/calculator.rb +++ b/week3/homework/calculator.rb @@ -7,15 +7,11 @@ def multiply a, b=1 [a, b].flatten.reduce(:*).to_f end - def fac a - if a == 0 - a=1 - else - (1..a).inject(:*) - end + def fac a + (1..(a.zero? ? 1 : a)).inject(:*) end - def pow (a, b) + def pow a, b a**b end diff --git a/week3/homework/questions.txt b/week3/homework/questions.txt index 41fe8ba..c83d703 100644 --- a/week3/homework/questions.txt +++ b/week3/homework/questions.txt @@ -6,14 +6,18 @@ Please Read: 1. What is a symbol? +An identifier cooresponding to a string. + 2. What is the difference between a symbol and a string? +A string is just a "string". A symbol is a string serves the purpose of identifying an object. + 3. What is a block and how do I call a block? +A set of code that is inside {} or the keywords do and end. A block can be thought of as a method itself that is called a yield statement. + 4. How do I pass a block to a method? What is the method signature? +Be defining a method that used yield to invoke the code inside the block when that method is used. The method signature is the name of the method. 5. Where would you use regular expressions? - - - - +To sort through various strings searching for patterns. \ No newline at end of file From 485cf44636832e679f2903b84992d9835f6754bc Mon Sep 17 00:00:00 2001 From: wmuengineer Date: Tue, 29 Oct 2013 18:42:07 -0700 Subject: [PATCH 11/25] in class comments --- week3/homework/calculator.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/week3/homework/calculator.rb b/week3/homework/calculator.rb index 6613d7d..12a8e6c 100644 --- a/week3/homework/calculator.rb +++ b/week3/homework/calculator.rb @@ -1,6 +1,6 @@ class Calculator def sum input - input.reduce(:+).to_f + input.reduce(:+)#.to_f # Class notes ( result = 0, input.each do |i| result +=1 end def multiply a, b=1 From 27789a68eb3a0bf164daae520d7e6f4be3c82922 Mon Sep 17 00:00:00 2001 From: wmuengineer Date: Mon, 4 Nov 2013 21:35:18 -0800 Subject: [PATCH 12/25] Submiting Homework 4 Includes questions and the programming work. --- week3/homework/calculator.rb | 8 ++++---- week4/code_timer.rb | 9 +++++++++ week4/filemaker.rb | 2 ++ week4/homework/my_output.txt | 0 week4/homework/questions.txt | 6 ++++++ week4/homework/work.rb | 7 +++++++ week4/homework/worker_spec.rb | 36 +++++++++++++++++++++++++++++++++++ week4/my_output.txt | 0 week4/nameed_thing.rb | 10 ++++++++++ week4/output.txt | 2 ++ week4/person.rb | 10 ++++++++++ week4/timer_spec.rb | 24 +++++++++++++++++++++++ week4/untitled.rb | 5 +++++ week4/vampire.rb | 10 ++++++++++ 14 files changed, 125 insertions(+), 4 deletions(-) create mode 100644 week4/code_timer.rb create mode 100644 week4/filemaker.rb create mode 100644 week4/homework/my_output.txt create mode 100644 week4/homework/work.rb create mode 100644 week4/homework/worker_spec.rb create mode 100644 week4/my_output.txt create mode 100644 week4/nameed_thing.rb create mode 100644 week4/output.txt create mode 100644 week4/person.rb create mode 100644 week4/timer_spec.rb create mode 100644 week4/untitled.rb create mode 100644 week4/vampire.rb diff --git a/week3/homework/calculator.rb b/week3/homework/calculator.rb index 12a8e6c..1eff83f 100644 --- a/week3/homework/calculator.rb +++ b/week3/homework/calculator.rb @@ -1,14 +1,14 @@ class Calculator def sum input - input.reduce(:+)#.to_f # Class notes ( result = 0, input.each do |i| result +=1 - end + input.reduce(:+).to_f # Class notes ( result = 0, input.each do |i| result +=1 + end - def multiply a, b=1 + def multiply a, b=1 # *(args or input) [a, b].flatten.reduce(:*).to_f end def fac a - (1..(a.zero? ? 1 : a)).inject(:*) + (1..(a.zero? ? 1 : a)).inject(:*) # 1.upto() end def pow a, b diff --git a/week4/code_timer.rb b/week4/code_timer.rb new file mode 100644 index 0000000..0285d19 --- /dev/null +++ b/week4/code_timer.rb @@ -0,0 +1,9 @@ +class CodeTimer + def time_code + start_time = Time.now + yield + Time.now - start_time + + end + +end diff --git a/week4/filemaker.rb b/week4/filemaker.rb new file mode 100644 index 0000000..4ca60a7 --- /dev/null +++ b/week4/filemaker.rb @@ -0,0 +1,2 @@ +File.open("output.txt", "w") { |file| file.puts "Hello" +file.puts "1 + 2 = #{1+2}" } diff --git a/week4/homework/my_output.txt b/week4/homework/my_output.txt new file mode 100644 index 0000000..e69de29 diff --git a/week4/homework/questions.txt b/week4/homework/questions.txt index bc1ab7c..00093fd 100644 --- a/week4/homework/questions.txt +++ b/week4/homework/questions.txt @@ -3,7 +3,13 @@ Chapter 10 Basic Input and Output The Rake Gem: http://rake.rubyforge.org/ 1. How does Ruby read files? +Ruby uses the IO base class to handle all input and output. A sub class called file provides the tools for reading files. the method gets can be used to read lines of a file. 2. How would you output "Hello World!" to a file called my_output.txt? +File.open("my_output.txt", "w") { |file| file.puts "Hello world } 3. What is the Directory class and what is it used for? +The directory class contains methods used to list diretories in the file system. 4. What is an IO object? +A bidirectional channel between ruby and the "outside world". + 5. What is rake and what is it used for? What is a rake task? +Rake must be a package written in ruby that helps run tests on the code that we are developing. You can run a task called unit test or default. I'm also thinking you can run rake db:migrate. So rake must be the task executer for ruby. diff --git a/week4/homework/work.rb b/week4/homework/work.rb new file mode 100644 index 0000000..f51fdc6 --- /dev/null +++ b/week4/homework/work.rb @@ -0,0 +1,7 @@ +class Worker + def self.work a=0 + (a.zero? ? yield : (a-1).times {yield } ) + #(a-1).times {|input| yield} + yield + end +end \ No newline at end of file diff --git a/week4/homework/worker_spec.rb b/week4/homework/worker_spec.rb new file mode 100644 index 0000000..a8bcfc1 --- /dev/null +++ b/week4/homework/worker_spec.rb @@ -0,0 +1,36 @@ +require './work.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 diff --git a/week4/my_output.txt b/week4/my_output.txt new file mode 100644 index 0000000..e69de29 diff --git a/week4/nameed_thing.rb b/week4/nameed_thing.rb new file mode 100644 index 0000000..b8a3799 --- /dev/null +++ b/week4/nameed_thing.rb @@ -0,0 +1,10 @@ +module NameThing + + def initialize name + @name = name + end + + def shout + @name.upcase + end +end diff --git a/week4/output.txt b/week4/output.txt new file mode 100644 index 0000000..273b43f --- /dev/null +++ b/week4/output.txt @@ -0,0 +1,2 @@ +Hello +1 + 2 = 3 diff --git a/week4/person.rb b/week4/person.rb new file mode 100644 index 0000000..5995269 --- /dev/null +++ b/week4/person.rb @@ -0,0 +1,10 @@ +require './named_thing' + +class Person + include NamedThing + + def shout_name + "My NAME IS #{@name.upcase}" + end + +end diff --git a/week4/timer_spec.rb b/week4/timer_spec.rb new file mode 100644 index 0000000..ab33a8c --- /dev/null +++ b/week4/timer_spec.rb @@ -0,0 +1,24 @@ +require './code_timer.rb' + +describe CodeTimer do + + it " should run out code " do + flag = false + + CodeTimer.time_code do + flag = true + end + flag.should eq true + end + + it "should timer our code" do + CodeTimer.time_code do + sleep(3) + puts "hi" + end + + run_time.should be_within(0.1).of(3.0) + + + end +end diff --git a/week4/untitled.rb b/week4/untitled.rb new file mode 100644 index 0000000..f55eb78 --- /dev/null +++ b/week4/untitled.rb @@ -0,0 +1,5 @@ +class vampire + attr_accessor :name + +end + diff --git a/week4/vampire.rb b/week4/vampire.rb new file mode 100644 index 0000000..8aab7e6 --- /dev/null +++ b/week4/vampire.rb @@ -0,0 +1,10 @@ +require './named_thing' + +class Vampire + include NamedThing + + + + +end + From f5096e52cf7bb03836d5f1ab5026e141eff556c8 Mon Sep 17 00:00:00 2001 From: wmuengineer Date: Tue, 19 Nov 2013 16:28:29 -0800 Subject: [PATCH 13/25] Commiting homework --- midterm/untitled.rb | 0 week4/Rakefile.rb | 10 ++++++++++ week4/class_materials/class_power.rb | 19 +++++++++++++++++++ week4/class_materials/untitled.rb | 5 +++++ week4/homework/work.rb | 10 +++++++++- week5/exercises/Rakefile.rb | 7 +++++++ week5/exercises/{names => names.txt} | 0 ...{do_your_mid_term => do_your_mid_term.txt} | 0 8 files changed, 50 insertions(+), 1 deletion(-) create mode 100644 midterm/untitled.rb create mode 100644 week4/Rakefile.rb create mode 100644 week4/class_materials/class_power.rb create mode 100644 week4/class_materials/untitled.rb create mode 100644 week5/exercises/Rakefile.rb rename week5/exercises/{names => names.txt} (100%) rename week5/homework/{do_your_mid_term => do_your_mid_term.txt} (100%) diff --git a/midterm/untitled.rb b/midterm/untitled.rb new file mode 100644 index 0000000..e69de29 diff --git a/week4/Rakefile.rb b/week4/Rakefile.rb new file mode 100644 index 0000000..63c0a92 --- /dev/null +++ b/week4/Rakefile.rb @@ -0,0 +1,10 @@ +task :default => [:hello_world] + +desc "this outputs hello world!" +task :hello_world do + puts "hello world!" +end + +def test + "tequila" +end \ No newline at end of file diff --git a/week4/class_materials/class_power.rb b/week4/class_materials/class_power.rb new file mode 100644 index 0000000..278fd3f --- /dev/null +++ b/week4/class_materials/class_power.rb @@ -0,0 +1,19 @@ +class PowerOfTwo + attr_reader :value + + def initialize(value) + @value = value + end + + def <=>(other) + @value <=> other.value + end + + def succ + PowerOfTwo.new(@value + @value) + end + + def to_s + @value.to_s + end +end \ No newline at end of file diff --git a/week4/class_materials/untitled.rb b/week4/class_materials/untitled.rb new file mode 100644 index 0000000..3f2ff2d --- /dev/null +++ b/week4/class_materials/untitled.rb @@ -0,0 +1,5 @@ + + + + + diff --git a/week4/homework/work.rb b/week4/homework/work.rb index f51fdc6..d21fada 100644 --- a/week4/homework/work.rb +++ b/week4/homework/work.rb @@ -1,7 +1,15 @@ class Worker def self.work a=0 - (a.zero? ? yield : (a-1).times {yield } ) + (a.zero? ? yield : (a-1).times {yield } ) # inject can replace this. #(a-1).times {|input| yield} yield + + #class solution + n.times.inject(nil){|results, i| yield} + n.times.inject(nil){ yield } + + # inject needs a starting variable [0].inject{|results, i| puts i } + # this outputs zero as the inject module doesn't run the block next to it if it don't have a initial value. + end end \ No newline at end of file diff --git a/week5/exercises/Rakefile.rb b/week5/exercises/Rakefile.rb new file mode 100644 index 0000000..cc316bd --- /dev/null +++ b/week5/exercises/Rakefile.rb @@ -0,0 +1,7 @@ +#task :default => [:hello_world] + +desc "spits out the contents of the file" +task :filereader do + a = File.open("names.txt", "r") + a.readlines +end \ No newline at end of file diff --git a/week5/exercises/names b/week5/exercises/names.txt similarity index 100% rename from week5/exercises/names rename to week5/exercises/names.txt diff --git a/week5/homework/do_your_mid_term b/week5/homework/do_your_mid_term.txt similarity index 100% rename from week5/homework/do_your_mid_term rename to week5/homework/do_your_mid_term.txt From d370a44bf5d787791663a1365914b29d5169a38c Mon Sep 17 00:00:00 2001 From: wmuengineer Date: Sun, 24 Nov 2013 17:53:44 -0800 Subject: [PATCH 14/25] Commit to make sure things are updating properly. --- README.md | 8 ++++++-- week7/homework/play_game.rb | 2 ++ 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index f777da9..4d1f3c3 100644 --- a/README.md +++ b/README.md @@ -34,15 +34,14 @@ Week 5 * Rake Week 6 -* Mid-term due! * Projects intro * Gems * CI Week 7 +* Mid-term due! * Cucumber * Testing frameworks -* Refactoring Week 8 * Metaprogramming @@ -51,9 +50,14 @@ Week 8 Week 9 * Exceptions +* Refactoring * Review Week 10 * Final due! * Project due! * Interesting Stuff + + +[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/UWE-Ruby/rubyfall2013/trend.png)](https://bitdeli.com/free "Bitdeli Badge") + diff --git a/week7/homework/play_game.rb b/week7/homework/play_game.rb index cf7847f..0535830 100644 --- a/week7/homework/play_game.rb +++ b/week7/homework/play_game.rb @@ -1,6 +1,8 @@ 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? From 98de6bcf53a1b8514314a96668fbca8412e1994a Mon Sep 17 00:00:00 2001 From: wmuengineer Date: Tue, 26 Nov 2013 15:41:17 -0800 Subject: [PATCH 15/25] Only the questions this week. Please disregard the pirate homework files. I will complete late. Thanks, Adam --- week7/homework/Gemfile | 3 +++ week7/homework/TicTacToe_game.rb | 17 +++++++++++++++ .../step_definitions/tic-tac-toe-steps.rb | 18 +++++++++++++--- week7/homework/features/support/env.rb | 2 ++ week7/homework/pirate_translator.rb | 11 ++++++++++ week7/homework/play_game.rb | 4 +++- week7/homework/questions.txt | 21 ++++++++++++++++--- 7 files changed, 69 insertions(+), 7 deletions(-) create mode 100644 week7/homework/Gemfile create mode 100644 week7/homework/TicTacToe_game.rb create mode 100644 week7/homework/features/support/env.rb create mode 100644 week7/homework/pirate_translator.rb diff --git a/week7/homework/Gemfile b/week7/homework/Gemfile new file mode 100644 index 0000000..5dc0645 --- /dev/null +++ b/week7/homework/Gemfile @@ -0,0 +1,3 @@ +source 'https://rubygems.org' + +gem Cucumber diff --git a/week7/homework/TicTacToe_game.rb b/week7/homework/TicTacToe_game.rb new file mode 100644 index 0000000..951a3ed --- /dev/null +++ b/week7/homework/TicTacToe_game.rb @@ -0,0 +1,17 @@ +class TicTacToe + + attr_accessor :player + attr_reader :welcome_player + + def initialize + @player = gets.chomp + + end + + def welcome_player + puts "Welcome #{@player}" + end + + + +end 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..05b0141 100644 --- a/week7/homework/features/step_definitions/tic-tac-toe-steps.rb +++ b/week7/homework/features/step_definitions/tic-tac-toe-steps.rb @@ -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,11 +77,11 @@ @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 +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 @@ -122,3 +122,15 @@ @game.should_receive(:get_player_move).twice.and_return(@taken_spot, arg1) @game.player_move.should eq arg1.to_sym end + +Given(/^it is the computer's turn$/) do + pending # express the regexp above with the code you wish you had +end + +Then(/^it is now the computer's turn$/) do + pending # express the regexp above with the code you wish you had +end + +When(/^there are three X's in a row$/) do + pending # express the regexp above with the code you wish you had +end diff --git a/week7/homework/features/support/env.rb b/week7/homework/features/support/env.rb new file mode 100644 index 0000000..69f0efe --- /dev/null +++ b/week7/homework/features/support/env.rb @@ -0,0 +1,2 @@ +require 'rspec/expectations' +World(RSpec::Matchers) diff --git a/week7/homework/pirate_translator.rb b/week7/homework/pirate_translator.rb new file mode 100644 index 0000000..5945ae0 --- /dev/null +++ b/week7/homework/pirate_translator.rb @@ -0,0 +1,11 @@ +require './features/step_definitions/pirate_steps.rb' + +class Kernel + + def send + + + end + + +end diff --git a/week7/homework/play_game.rb b/week7/homework/play_game.rb index 0535830..7437824 100644 --- a/week7/homework/play_game.rb +++ b/week7/homework/play_game.rb @@ -1,4 +1,6 @@ -require './features/step_definitions/tic-tac-toe.rb' +require './features/step_definitions/tic-tac-toe-steps.rb' +require './TicTacToe_game.rb' + @game = TicTacToe.new puts "What is your name?" diff --git a/week7/homework/questions.txt b/week7/homework/questions.txt index d55387d..60cb009 100644 --- a/week7/homework/questions.txt +++ b/week7/homework/questions.txt @@ -3,7 +3,22 @@ Please Read Chapters 23 and 24 DuckTyping and MetaProgramming Questions: 1. What is method_missing and how can it be used? -2. What is and Eigenclass and what is it used for? Where Do Singleton methods live? -3. When would you use DuckTypeing? How would you use it to improve your code? -4. What is the difference between a class method and an instance method? What is the difference between instance_eval and class_eval? +When an object it cannot handel the method_missing method can be used to implement proxies, delegators, and forwarders (of which i have no idea what they are) + +2. What is and Eigenclass and what is it used for? +-An Eigenclass is an anonymous that is created when you create a singleton method. It defines the singleton methods we create. +Where Do Singleton methods live? +-In the singleton class or Eigenclass + +3. When would you use DuckTypeing? +-When the inputs my code will work on can be in a variety of types and the operations that i the code will perform is independant of the format of the input. +How would you use it to improve your code? +-I would use the Ducktyping Style of coding to make my code more flexible. It can focus more on the contents of the inputs instead of the format of the input. + +4. What is the difference between a class method and an instance method? +-A class method can modify can work on the class level. It can manage all of the items for various instances of the class. An instance method only works at the instance level. It cannot operate on another instance of the same class. +What is the difference between instance_eval and class_eval? +-Class_eval defined instance methods and instance_eval defines class methods. + 5. What is the difference between a singleton class and a singleton method? +-A singleton class is a anonymous class that defines the singleton methods we create. Singleton methods are created by to be methods that are specific to an object. From 02db76a70601946696b96fb7fb8d7d9c8a6e977a Mon Sep 17 00:00:00 2001 From: wmuengineer Date: Sun, 8 Dec 2013 00:49:56 -0800 Subject: [PATCH 16/25] Added initial player functionality, board, and game locations. --- week7/homework/TicTacToe_game.rb | 76 ++++++++++++------- .../step_definitions/tic-tac-toe-steps.rb | 2 +- 2 files changed, 51 insertions(+), 27 deletions(-) diff --git a/week7/homework/TicTacToe_game.rb b/week7/homework/TicTacToe_game.rb index 2e88589..76d229a 100644 --- a/week7/homework/TicTacToe_game.rb +++ b/week7/homework/TicTacToe_game.rb @@ -1,13 +1,22 @@ class TicTacToe - SYMBOLS = ["X","O"] + SYMBOLS = [:X,:O] attr_accessor :player attr_reader :welcome_player attr_reader :player_symbol attr_reader :computer_symbol - def initialize (name = " ", ) + def initialize (name = " " ) @player = name + game_locations = { + + :A1 => " ", :A1 => " ", :A1 => " ", + :B1 => " ", :B2 => " ", :A1 => " ", + :A1 => " ", :A1 => " ", :A1 => " ", + + } + move_counter = 0 + end def player @@ -15,51 +24,66 @@ def player end def welcome_player - @welcome_player = "Welcome #{@player}" end + def initial_player + + + current_move= rand() > 0.5 ? computer_move : @player + + end + def current_player - # If random number is less than .5 then compy if grater than @player - t = rand(2) - if t > 1 - computer_move - else - @player - end + + case move_counter = 0 + + when move_counter = 0 + player_on_deck = rand() > 0.5 ? "Computer" : @player + move_counter +=1 + else + player_on_deck == "Computer" ? @player : "Computer" + + end + return player_on_deck + end + def get_move + move = @player ? get_player_move : get_computer_move end def player_symbol - t = rand(4) - if t > 2 - player_symbol= "X" - else - player_symbol= "O" - end - player_symbol + player_symbol= rand() > 0.5 ? :X : :O + end def computer_symbol - t = rand(6) - if t > 3 - computer_symbol= "X" - else - computer_symbol= "O" - end - computer_symbol + + computer_symbol= rand() > 0.5 ? :X : :O end def get_player_move - + location = gets.chomp.to_sym + game_locations[location] = player_symbol + move = computer_move + return move end def board - + puts "----------------------------" + puts "#{@player}" + + puts " 1| 2 | 3\n" + puts " A| #{board_locations[:A1]} | #{board_locations[:A2]} | #{board_locations[:A3]} " + puts " ----------------------------" + puts " B| #{board_locations[:B1]} | #{board_locations[:B2]} | #{board_locations[:B3]} " + puts " ----------------------------" + puts " C| #{board_locations[:C1]} | #{board_locations[:C2]} | #{board_locations[:C3]} " + puts " ----------------------------" end 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 a0573c3..494a23e 100644 --- a/week7/homework/features/step_definitions/tic-tac-toe-steps.rb +++ b/week7/homework/features/step_definitions/tic-tac-toe-steps.rb @@ -124,7 +124,7 @@ @game.player_move.should eq arg1.to_sym end -Given(/^it is the computer's turn$/) do +Given /^ it is the computers turn $/ do pending # express the regexp above with the code you wish you had end From acd67e74694552a1fbf3524bff0d0c4d7860890a Mon Sep 17 00:00:00 2001 From: wmuengineer Date: Mon, 9 Dec 2013 00:57:48 -0800 Subject: [PATCH 17/25] Making load of progress, Working on getting the player inputs to work properly. --- week7/homework/.idea/.name | 1 + week7/homework/.idea/encodings.xml | 5 + week7/homework/.idea/homework.iml | 10 + week7/homework/.idea/misc.xml | 22 ++ week7/homework/.idea/modules.xml | 9 + .../homework/.idea/scopes/scope_settings.xml | 5 + week7/homework/.idea/vcs.xml | 7 + week7/homework/.idea/workspace.xml | 219 ++++++++++++++++++ week7/homework/TicTacToe_game.rb | 125 +++++++--- .../step_definitions/tic-tac-toe-steps.rb | 12 +- 10 files changed, 375 insertions(+), 40 deletions(-) create mode 100644 week7/homework/.idea/.name create mode 100644 week7/homework/.idea/encodings.xml create mode 100644 week7/homework/.idea/homework.iml create mode 100644 week7/homework/.idea/misc.xml create mode 100644 week7/homework/.idea/modules.xml create mode 100644 week7/homework/.idea/scopes/scope_settings.xml create mode 100644 week7/homework/.idea/vcs.xml create mode 100644 week7/homework/.idea/workspace.xml diff --git a/week7/homework/.idea/.name b/week7/homework/.idea/.name new file mode 100644 index 0000000..158b7eb --- /dev/null +++ b/week7/homework/.idea/.name @@ -0,0 +1 @@ +homework \ No newline at end of file diff --git a/week7/homework/.idea/encodings.xml b/week7/homework/.idea/encodings.xml new file mode 100644 index 0000000..e206d70 --- /dev/null +++ b/week7/homework/.idea/encodings.xml @@ -0,0 +1,5 @@ + + + + + diff --git a/week7/homework/.idea/homework.iml b/week7/homework/.idea/homework.iml new file mode 100644 index 0000000..e91821c --- /dev/null +++ b/week7/homework/.idea/homework.iml @@ -0,0 +1,10 @@ + + + + + + + + + + diff --git a/week7/homework/.idea/misc.xml b/week7/homework/.idea/misc.xml new file mode 100644 index 0000000..aab4c35 --- /dev/null +++ b/week7/homework/.idea/misc.xml @@ -0,0 +1,22 @@ + + + + + + + diff --git a/week7/homework/.idea/modules.xml b/week7/homework/.idea/modules.xml new file mode 100644 index 0000000..f766474 --- /dev/null +++ b/week7/homework/.idea/modules.xml @@ -0,0 +1,9 @@ + + + + + + + + + diff --git a/week7/homework/.idea/scopes/scope_settings.xml b/week7/homework/.idea/scopes/scope_settings.xml new file mode 100644 index 0000000..922003b --- /dev/null +++ b/week7/homework/.idea/scopes/scope_settings.xml @@ -0,0 +1,5 @@ + + + + \ No newline at end of file diff --git a/week7/homework/.idea/vcs.xml b/week7/homework/.idea/vcs.xml new file mode 100644 index 0000000..def6a6a --- /dev/null +++ b/week7/homework/.idea/vcs.xml @@ -0,0 +1,7 @@ + + + + + + + diff --git a/week7/homework/.idea/workspace.xml b/week7/homework/.idea/workspace.xml new file mode 100644 index 0000000..86c5f03 --- /dev/null +++ b/week7/homework/.idea/workspace.xml @@ -0,0 +1,219 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 1386573063093 + 1386573063093 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/week7/homework/TicTacToe_game.rb b/week7/homework/TicTacToe_game.rb index 76d229a..5b16115 100644 --- a/week7/homework/TicTacToe_game.rb +++ b/week7/homework/TicTacToe_game.rb @@ -2,21 +2,42 @@ class TicTacToe SYMBOLS = [:X,:O] attr_accessor :player + attr_accessor :player_on_deck + attr_accessor :current_player attr_reader :welcome_player attr_reader :player_symbol attr_reader :computer_symbol + attr_accessor :board + attr_reader :first + attr_accessor :board_locations - def initialize (name = " " ) - @player = name - game_locations = { + def initialize (name = " ",symbol = :X ) - :A1 => " ", :A1 => " ", :A1 => " ", - :B1 => " ", :B2 => " ", :A1 => " ", - :A1 => " ", :A1 => " ", :A1 => " ", - } - move_counter = 0 - + @winning_patterns = [ + + [:A1,:A2,:A3], + [:B1,:B2,:B3], + [:A1,:C2,:C3], + [:A1,:B1,:C1], + [:A2,:B2,:C2], + [:A3,:B3,:C3], + [:A1,:B2,:C3], + [:A3,:B2,:C1], + + ] + + @move_counter = 0 + + + @player_symbol = symbol + + @player_symbol == :X ? @computer_symbol =:O : @computer_symbol =:X + + + + + end def player @@ -27,25 +48,29 @@ def welcome_player @welcome_player = "Welcome #{@player}" end - def initial_player - - - current_move= rand() > 0.5 ? computer_move : @player + def indicate_player_turn + puts "#{@player}'s Move:" end def current_player - - case move_counter = 0 - - when move_counter = 0 - player_on_deck = rand() > 0.5 ? "Computer" : @player - move_counter +=1 - else - player_on_deck == "Computer" ? @player : "Computer" - end - return player_on_deck + #if @move_counter == 0 + # player_on_deck = rand() > 0.5 ? "Computer" : @player + #else + # player_on_deck == "Computer" ? @player : "Computer" + #end + + case @move_counter + when @move_counter == 0 + @player_on_deck = rand() > 0.5 ? "Computer" : @player + else + @player_on_deck = @player + end + + (player_on_deck == @player) ? symbol = @player_symbol : symbol = @computer_symbol + + return @player_on_deck ; symbol end def get_move @@ -54,44 +79,72 @@ def get_move def player_symbol - player_symbol= rand() > 0.5 ? :X : :O + @player_symbol end def computer_symbol - computer_symbol= rand() > 0.5 ? :X : :O + @computer_symbol end def get_player_move location = gets.chomp.to_sym - game_locations[location] = player_symbol - move = computer_move - return move + #board_locations[location] = player_symbol + #computer_move + end - def board + def board (*args) + board_locations = {:A1 => " ", :A2 => " ", :A3 => " ", + :B1 => " ", :B2 => " ", :B3 => " ", + :C1 => " ", :C2 => " ", :C3 => " ", + } - puts "----------------------------" - puts "#{@player}" + board_locations[get_player_move] = player_symbol + + + #puts "-------------------" + #puts "#{@player}" - puts " 1| 2 | 3\n" + #puts " 1| 2 | 3\n" puts " A| #{board_locations[:A1]} | #{board_locations[:A2]} | #{board_locations[:A3]} " - puts " ----------------------------" + #puts " ------------------" puts " B| #{board_locations[:B1]} | #{board_locations[:B2]} | #{board_locations[:B3]} " - puts " ----------------------------" + #puts " ----------------------------" puts " C| #{board_locations[:C1]} | #{board_locations[:C2]} | #{board_locations[:C3]} " - puts " ----------------------------" + #puts " ------------------" end - def indicate_player_turn + def open_spots end def computer_move end + def player_move + end + + def current_state + end + + def player_won? + + end + + def over? + + end + + def draw? + + end + + def determine_winner + + end end 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 494a23e..3083777 100644 --- a/week7/homework/features/step_definitions/tic-tac-toe-steps.rb +++ b/week7/homework/features/step_definitions/tic-tac-toe-steps.rb @@ -1,6 +1,8 @@ require 'rspec/mocks/standalone' require 'rspec/expectations' require '/Users/adam/Documents/Git_Repos/RubyFall2013-1/week7/homework/TicTacToe_game.rb' + +# Scenario: Begin Game Given /^I start a new Tic\-Tac\-Toe game$/ do @game = TicTacToe.new end @@ -21,9 +23,11 @@ TicTacToe::SYMBOLS.should include @game.player_symbol, @game.computer_symbol # end +# Scenario: My Turn + Given /^I have a started Tic\-Tac\-Toe game$/ do # @game = TicTacToe.new(:player) - @game.player = "Renee" + @game.player = "Renee" # end Given /^it is my turn$/ do # @@ -50,7 +54,7 @@ end Then /^the computer randomly chooses an open position for its move$/ do # - open_spots = @game.open_spots + open_spots = @game.open_spots # @com_move = @game.computer_move open_spots.should include(@com_move) end @@ -71,7 +75,7 @@ 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 + @game.player_move.should eq arg1.to_sym # end When /^"(.*?)" is not taken$/ do |arg1| @@ -124,7 +128,7 @@ @game.player_move.should eq arg1.to_sym end -Given /^ it is the computers turn $/ do +Given(/^it is the computer's turn$/) do pending # express the regexp above with the code you wish you had end From 4ac1ce0535ab8e711ddf896c1a2eafb34c40970f Mon Sep 17 00:00:00 2001 From: wmuengineer Date: Mon, 9 Dec 2013 20:20:07 -0800 Subject: [PATCH 18/25] Added current_state method. --- week7/homework/.idea/workspace.xml | 130 ++++++++++++++++++++++++++--- week7/homework/TicTacToe_game.rb | 87 +++++++++---------- 2 files changed, 163 insertions(+), 54 deletions(-) diff --git a/week7/homework/.idea/workspace.xml b/week7/homework/.idea/workspace.xml index 86c5f03..a2867f4 100644 --- a/week7/homework/.idea/workspace.xml +++ b/week7/homework/.idea/workspace.xml @@ -4,7 +4,8 @@ - + +