From 9010db3fcc16abb3eb9b90fa6e8f462c37a48b3f Mon Sep 17 00:00:00 2001 From: Price Hardman Date: Wed, 10 Oct 2012 15:43:13 -0700 Subject: [PATCH 01/12] Price added himself to roster.txt --- week1/exercises/roster.txt | 3 +++ 1 file changed, 3 insertions(+) diff --git a/week1/exercises/roster.txt b/week1/exercises/roster.txt index a97ec74..1506f16 100644 --- a/week1/exercises/roster.txt +++ b/week1/exercises/roster.txt @@ -1,4 +1,7 @@ name, email, github, twitter, hipchat + Renee D, renee@nird.us, reneedv, @gigglegirl4e, renee.devoursney@gmail.com + Nell Shamrell, nellshamrell@gmail.com, nellshamrell, @nellshamrell +Price Hardman, PriceHardman@gmail.com, PriceHardman, @PLHardman From c4ecd535a016fe305dbe7448a2b3ec7a42ff726a Mon Sep 17 00:00:00 2001 From: Price Hardman Date: Thu, 11 Oct 2012 14:51:48 -0700 Subject: [PATCH 02/12] Changed the sample rspec file --- week1/exercises/rspec_spec.rb | 13 ++++++++++--- week1/homework/questions.txt | 13 +++++++++++++ 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/week1/exercises/rspec_spec.rb b/week1/exercises/rspec_spec.rb index 31800c8..90d06de 100644 --- a/week1/exercises/rspec_spec.rb +++ b/week1/exercises/rspec_spec.rb @@ -79,9 +79,16 @@ # Parentheses, Exponents, Multiplication, Division, Addition, Subtraction (1+2-5*6/2).should eq -10 end - it "should count the charaters in your name" - it "should check basic math" - it "should check basic spelling" + it "should count the charaters in your name" do + "Price".should have(5).characters + end + it "should check basic math" do + (5+7).should eq 12 + end + it "should check basic spelling" do + "team".should_not include("I") + end + end end diff --git a/week1/homework/questions.txt b/week1/homework/questions.txt index 1f41026..edd00a3 100644 --- a/week1/homework/questions.txt +++ b/week1/homework/questions.txt @@ -1,6 +1,19 @@ 1. What is an object? +=> An object is an abstact data structure combined with functionality that allows the data structure to interact and be interacted with. + 2. What is a variable? +=> A variable is a named location in memory whose value can be changed, but can always be referenced using its name. + 3. What is the difference between an object and a class? +=> Classes are essentially blueprints for objects. All of the attributes and properties of an object are determined by the class of which it is an instance. The difference between classes and objects is that a class defines the form and functionality of an object while an object (or class instance) is an entity that actually posseses that form and functionality described in the class. In short, the class defines how an object looks and behaves. + 4. What is a String? +=> A string is a sequence of characters, usually in the form of readable text. In Ruby, string objects are instances of the String class. + 5. What are three messages that I can send to a string object? Hint: think methods +=> a) .length ## returns the length of the string + b) .empty? ## returns true if the string is of length 0, else false. + c) .index("") # if the character is in the string, returns the index, else nil. + 6. What are two ways of defining a String literal? Bonus: What is the difference between the two? +=> The two ways of creating string literals are enclosing the desired text in either double or single quotes. The difference between the two is the amount of processing Ruby does on the string itself. With double quotes, more processing is done, such as expression interpolation (e.g. #{name}) and interpreting control characters (e.g. "Hello, \nWorld!"). String literals created with single quotes are treated essential as-is. For instance, a newline character placed in a string in single quotes would literally be treated as a backslash and an n, rather than forcing a line break. From d11895e6459d6bee81e4bfd4d8b6af172e577949 Mon Sep 17 00:00:00 2001 From: Price Hardman Date: Fri, 12 Oct 2012 09:13:26 -0700 Subject: [PATCH 03/12] Answered the questions --- week1/homework/questions.txt | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/week1/homework/questions.txt b/week1/homework/questions.txt index edd00a3..3a2ac61 100644 --- a/week1/homework/questions.txt +++ b/week1/homework/questions.txt @@ -1,11 +1,11 @@ 1. What is an object? -=> An object is an abstact data structure combined with functionality that allows the data structure to interact and be interacted with. +=> An object is an abstract data structure combined with functionality (in the form of methods) that allows the data structure to interact and be interacted with. 2. What is a variable? -=> A variable is a named location in memory whose value can be changed, but can always be referenced using its name. +=> A variable is a named location in memory whose value can be changed. 3. What is the difference between an object and a class? -=> Classes are essentially blueprints for objects. All of the attributes and properties of an object are determined by the class of which it is an instance. The difference between classes and objects is that a class defines the form and functionality of an object while an object (or class instance) is an entity that actually posseses that form and functionality described in the class. In short, the class defines how an object looks and behaves. +=> Classes are essentially templates for objects. All of the attributes and properties of an object are determined by the class of which it is an instance. The difference between classes and objects is that a class defines the form and functionality of an object while an object (or class instance) is an entity that actually posseses the form and functionality described in the class. 4. What is a String? => A string is a sequence of characters, usually in the form of readable text. In Ruby, string objects are instances of the String class. @@ -13,7 +13,7 @@ 5. What are three messages that I can send to a string object? Hint: think methods => a) .length ## returns the length of the string b) .empty? ## returns true if the string is of length 0, else false. - c) .index("") # if the character is in the string, returns the index, else nil. + c) .index("") ## if the character is in the string, returns the index, else nil. 6. What are two ways of defining a String literal? Bonus: What is the difference between the two? -=> The two ways of creating string literals are enclosing the desired text in either double or single quotes. The difference between the two is the amount of processing Ruby does on the string itself. With double quotes, more processing is done, such as expression interpolation (e.g. #{name}) and interpreting control characters (e.g. "Hello, \nWorld!"). String literals created with single quotes are treated essential as-is. For instance, a newline character placed in a string in single quotes would literally be treated as a backslash and an n, rather than forcing a line break. +=> The two ways of creating string literals are enclosing the desired text in either double or single quotes. The difference between the two is the amount of processing Ruby does on the string. With double quotes, more processing is done, such as expression interpolation (e.g. #{name}) and interpreting control characters (e.g. In, "Hello, \nWorld!" \n forces a line break). String literals created with single quotes are treated essentially as-is. For instance, a newline character placed in a string in single quotes would literally be treated as a backslash and an n, rather than forcing a line break. From 30081a40441030c539f8996af17a2ae7eb86db3b Mon Sep 17 00:00:00 2001 From: Price Hardman Date: Sun, 14 Oct 2012 13:41:48 -0700 Subject: [PATCH 04/12] Answered the questions and got the spec file working --- week1/homework/questions.txt | 2 +- week1/homework/strings_and_rspec_spec.rb | 12 +++++++----- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/week1/homework/questions.txt b/week1/homework/questions.txt index 3a2ac61..2ae78fa 100644 --- a/week1/homework/questions.txt +++ b/week1/homework/questions.txt @@ -2,7 +2,7 @@ => An object is an abstract data structure combined with functionality (in the form of methods) that allows the data structure to interact and be interacted with. 2. What is a variable? -=> A variable is a named location in memory whose value can be changed. +=> A variable is a named location in memory whose value can be changed. In Ruby, a variable is named reference to an object. 3. What is the difference between an object and a class? => Classes are essentially templates for objects. All of the attributes and properties of an object are determined by the class of which it is an instance. The difference between classes and objects is that a class defines the form and functionality of an object while an object (or class instance) is an entity that actually posseses the form and functionality described in the class. diff --git a/week1/homework/strings_and_rspec_spec.rb b/week1/homework/strings_and_rspec_spec.rb index ec1662f..138704d 100644 --- a/week1/homework/strings_and_rspec_spec.rb +++ b/week1/homework/strings_and_rspec_spec.rb @@ -4,15 +4,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 + result = @my_string.length + result.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 - #should eq (Encoding.find("UTF-8")) + result = @my_string.encoding + result.should eq (Encoding.find("UTF-8")) end end end From 6c6aeaf9d74065caa6cab5be6bed6d8b89ff19ef Mon Sep 17 00:00:00 2001 From: Price Hardman Date: Mon, 22 Oct 2012 17:28:58 -0700 Subject: [PATCH 05/12] updating --- .gitignore | 1 + week1/exercises/rspec_spec.rb | 3 ++- week2/exercises/book.rb | 7 +++++++ week2/exercises/mad_libs.rb | 13 ++++++++----- 4 files changed, 18 insertions(+), 6 deletions(-) diff --git a/.gitignore b/.gitignore index 18d7cf1..db62a8a 100644 --- a/.gitignore +++ b/.gitignore @@ -13,6 +13,7 @@ test/version_tmp tmp *.*~ *~ +notes # YARD artifacts .yardoc diff --git a/week1/exercises/rspec_spec.rb b/week1/exercises/rspec_spec.rb index 708d6ef..95a7a7d 100644 --- a/week1/exercises/rspec_spec.rb +++ b/week1/exercises/rspec_spec.rb @@ -100,6 +100,7 @@ it "should check basic spelling" do "team".should_not include("I") end - end + +end end diff --git a/week2/exercises/book.rb b/week2/exercises/book.rb index e05e468..6cced3a 100644 --- a/week2/exercises/book.rb +++ b/week2/exercises/book.rb @@ -1,2 +1,9 @@ class Book + + attr_reader :title, :pageCount + + def initialize(title, pageCount = "Not Given") + @title = title + @pageCount = pageCount + end end diff --git a/week2/exercises/mad_libs.rb b/week2/exercises/mad_libs.rb index 128ee92..13584c5 100644 --- a/week2/exercises/mad_libs.rb +++ b/week2/exercises/mad_libs.rb @@ -1,8 +1,11 @@ -puts "Please enter a noun" -noun = gets.chomp puts "Please enter an adjective" adjective = gets.chomp -puts "Please enter a past tense action verb" -verb_past_tense = gets.chomp -story = "The #{adjective} #{noun} #{verb_past_tense} past the graveyard" +puts "Please enter a plural noun" +plural_noun = gets.chomp +puts "Please enter a present-tense action verb" +verb = gets.chomp +puts "Please enter another present-tense action verb" +another_verb = gets.chomp +story = "Twas #{adjective}, and the slythy #{plural_noun} +Did #{verb} and #{another_verb} in the wabe:" puts story From 66857d2ce6f50893b13251824d03b76b72e9017d Mon Sep 17 00:00:00 2001 From: Price Hardman Date: Tue, 23 Oct 2012 15:38:25 -0700 Subject: [PATCH 06/12] answered homework questions --- week2/homework/questions.txt | 10 +++++----- week2/homework/simon_says.rb | 7 ++++--- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/week2/homework/questions.txt b/week2/homework/questions.txt index 4cfc0a3..60c41d5 100644 --- a/week2/homework/questions.txt +++ b/week2/homework/questions.txt @@ -3,16 +3,16 @@ Containers, Blocks, and Iterators Sharing Functionality: Inheritance, Modules, and Mixins 1. What is the difference between a Hash and an Array? -An array is an ordered list of items that are referenced by their index (order), a hash is a collection of items that can be referenced by a key and have no order. +A hash is a non-ordered collection of objects, indexed via a key value that need not (and usually is not) an integer. An array on the other hand is a numerally-indexed collection. 2. When would you use an Array over a Hash and vice versa? -When the items have an inherent order I would use an array, when I want to reference the items in my collection by a name or key and their order does not matter I would use a hash. +Collections requiring some sort of order are ideal for being placed into an array, whereas a hash is best for collections whose members are referenced by a category (e.g. name: Price Hardman). 3. What is a module? Enumerable is a built in Ruby module, what is it? -A module is a way to group code that you can use across multiple classes. Enumerable is a Ruby module that provides collection functionality; iteration, searching, and sorting. It requires an implementation of the each method. +A module is code intended to be used by many classes. Modules have many advantages including namespaces, which can keep large programs from getting messy/buggy with regard to variable and method names. Enumerable is a module that provides the ability to traverse and manipulate collections. 4. Can you inherit more than one thing in Ruby? How could you get around this problem? -No, multiple inheritance is not allowed in Ruby. You can include multiple modules if you wanted to mix-in different functionality into your code. Code that is related with a hierarchical nature should be subclassed (inherited). A class can only have 1 direct parent, but can have lots of ancestors. +Ruby only supports single inheritance. The workaround to this is using modules (of which you can include multiple in a class). 5. What is the difference between a Module and a Class? -A class can be instantiated into an object, a module cannot. A module is code that can be used across many classes. +A module defines a namespace in which classes, methods, and constants can be defined. Whereas classes define functionality that can be inherited from parent classes and passed down to child classes, modules define functionality that can included across classes. diff --git a/week2/homework/simon_says.rb b/week2/homework/simon_says.rb index 971a674..a6fddef 100644 --- a/week2/homework/simon_says.rb +++ b/week2/homework/simon_says.rb @@ -8,14 +8,15 @@ def shout(st) end def first_word(st) - st.split.first + st.split(' ')[0] end def start_of_word(st,i) - st[0...i] + st.slice!(0..i-1) end def repeat(st, t=2) - ([st]*t).join(' ') + (st+" ")*(t-1)+st # or could use Array join method. + end end From 650189006cf0dfe8e89b3dae1ee9484f658325a4 Mon Sep 17 00:00:00 2001 From: Price Hardman Date: Mon, 29 Oct 2012 12:21:29 -0700 Subject: [PATCH 07/12] Completed Week 3 Homework --- week3/homework/calculator.rb | 15 ++++++++++++++ week3/homework/calculator_spec.rb | 34 ++++++++++++++++++++++--------- week3/homework/questions.txt | 16 +++++++++++++++ 3 files changed, 55 insertions(+), 10 deletions(-) diff --git a/week3/homework/calculator.rb b/week3/homework/calculator.rb index ede464b..0a2a2f9 100644 --- a/week3/homework/calculator.rb +++ b/week3/homework/calculator.rb @@ -1,2 +1,17 @@ class Calculator + def sum(input) + input.empty? ? 0 : input.inject(:+) #if input is empty array, return 0, else sum the elements + end + + def product(*input) #takes multiple arguments, as an array + input.length > 1 ? input.inject(:*) : input[0].inject(:*) + end + + def power(base, power) + base**power + end + + def factorial(n) + n == 0 ? 1 : (n==1 ? 1 : n*factorial(n-1) ) #recursive definition, takes into account zero case using nested ternary + end end \ No newline at end of file diff --git a/week3/homework/calculator_spec.rb b/week3/homework/calculator_spec.rb index 08b81cb..14d7319 100644 --- a/week3/homework/calculator_spec.rb +++ b/week3/homework/calculator_spec.rb @@ -27,19 +27,33 @@ # Once the above tests pass, # write tests and code for the following: - it "multiplies two numbers" - - it "multiplies an array of numbers" - - it "raises one number to the power of another number" + it "multiplies two numbers" do + @calculator.product(3,2).should == 6 + end + it "multiplies an array of numbers" do + @calculator.product([3,2,1]).should == 6 + end + it "raises one number to the power of another number" do + @calculator.power(3,2).should == 9 + end # http://en.wikipedia.org/wiki/Factorial describe "#factorial" do - it "computes the factorial of 0" - it "computes the factorial of 1" - it "computes the factorial of 2" - it "computes the factorial of 5" - it "computes the factorial of 10" + it "computes the factorial of 0" do + @calculator.factorial(0).should == 1 + end + it "computes the factorial of 1"do + @calculator.factorial(1).should == 1 + end + it "computes the factorial of 2"do + @calculator.factorial(2).should == 2 + end + it "computes the factorial of 5"do + @calculator.factorial(5).should == 120 + end + it "computes the factorial of 10"do + @calculator.factorial(10).should == 3628800 + end end end diff --git a/week3/homework/questions.txt b/week3/homework/questions.txt index e9a9079..1be4924 100644 --- a/week3/homework/questions.txt +++ b/week3/homework/questions.txt @@ -5,8 +5,24 @@ Please Read: - Chapter 22 The Ruby Language: basic types (symbols), variables and constants 1. What is a symbol? +=>A symbol is a constant defined by a name following a colon, e.g. :north. The advantage of doing this as opposed to defining a variable is that the value of a symbol is assigned and handled internally by Ruby, leading to greater simplicity and performance. All the user has to do is declare a symbol with a memorable name. + 2. What is the difference between a symbol and a string? +=>A symbol is a unique numerical value (handled by Ruby under the surface) that is referenced with a string name. A symbol is not an instance of the string class, and thereby has no access to all the methods of String. A symbol can be turned into a string with to_s though. + 3. What is a block and how do I call a block? +=> A block is a snippet of code -- enclosed either in braces or do..end -- that is called immediately after the invocation of a method and is used to generalize the functionality of that method. For instance, +array1 = [] +def double(input) + yield(input*2) +end +double(3) {|x| puts "The method output is #{x}"} #prints "The method output is 6". +double(three) {|x| array1 << x} #places output threethree into array1. +Thus, while the functionality of the method double is the same (it simply multiplies its input by 2, according to the defintion of the * operator for the input's class), the block allows that output to be implemented in a wide variety of ways. Blocks are also very convenient for iterating over collections. + 4. How do I pass a block to a method? What is the method signature? +=> A block is passed to a method either by using "yield" in the method definition (see previous answer), or as a parameter itself with a leading ampersand (e.g. def method(input, &block) ). + 5. Where would you use regular expressions? +=> Regular expressions are used for parsing strings in powerful and more robust ways than simpler string manipulation techniques allow. One of the many instances in which one could use regular expressions is in processing data read from a file, which would get read by IO as a string. From 7d3fd1d9c265c0ad3d42fcc5163e5ed498127525 Mon Sep 17 00:00:00 2001 From: Price Hardman Date: Tue, 6 Nov 2012 15:57:56 -0800 Subject: [PATCH 08/12] Completed week 4 questions --- week4/homework/questions.txt | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/week4/homework/questions.txt b/week4/homework/questions.txt index bc1ab7c..df7eb13 100644 --- a/week4/homework/questions.txt +++ b/week4/homework/questions.txt @@ -3,7 +3,18 @@ Chapter 10 Basic Input and Output The Rake Gem: http://rake.rubyforge.org/ 1. How does Ruby read files? +=> Ruby has an IO object that handles input and output, of which File is a child object. A file can be read by instantiating a File object and passing the filename as a parameter, then using a variety of methods and iterators to process the data as desired. + 2. How would you output "Hello World!" to a file called my_output.txt? +=> File.open('this\is\the\path\to\my_output.txt',"w") do |file| + file.print "Hello, world!" +end + 3. What is the Directory class and what is it used for? +=> The Directory -- or Dir -- class gives a powerful way to navigate and manipulate directories and the contents thereof. It can be used in any situation where it would be useful to change or write to specific directories. + 4. What is an IO object? +=> An IO object is a Ruby object that provides a convenient and safe interface for doing input and output in a variety of contexts, most commonly standard IO and files. File is a child class of IO. + 5. What is rake and what is it used for? What is a rake task? +=> Rake is a gem that provides an environment for managing file dependencies and buidling multi-file projects. It features the functionality of make, but uses Ruby syntax. The operations and functions a user can perform with rake is contained in rake tasks, which are essentially scripts. From cdf500445431a6699f30fcb49a962be4f627f249 Mon Sep 17 00:00:00 2001 From: Price Hardman Date: Tue, 27 Nov 2012 10:58:20 -0800 Subject: [PATCH 09/12] merge conflict --- mid_term/questions.txt | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/mid_term/questions.txt b/mid_term/questions.txt index b0e27a8..ea96bae 100644 --- a/mid_term/questions.txt +++ b/mid_term/questions.txt @@ -1,4 +1,5 @@ Instructions for Mid-Term submission and Git Review (10pts): + - Create a git repository for your answers - Add and Commit as you work through the questions and programming problems - Your git log should reflect your work, don't just commit after you have finished working @@ -8,8 +9,7 @@ Instructions for Mid-Term submission and Git Review (10pts): - Add a remote to your git repository: git@nird.us:RubyFall2012/YOURREPOSITORYNAME.git - Push your changes to the remote - After 6pm Tuesday November 13th you will not be able to push to your remote repository (or clone). - - Questions (20pts): +Questions (20pts): - What are the three uses of the curly brackets {} in Ruby? - 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? @@ -20,18 +20,18 @@ Instructions for Mid-Term submission and Git Review (10pts): - Why would I use a Hash instead of an Array? - What is your favorite thing about Ruby so far? - What is your least favorite thing about Ruby so far? - Programming Problems (10pts each): - - Write a passing rspec file called even_number_spec.rb that tests a class called EvenNumber. - - The EvenNumber class should: +- Write a passing rspec file called even_number_spec.rb that tests a class called EvenNumber. +- The EvenNumber class should: - Only allow even numbers - - Get the next even number + -Get the next even number - Compare even numbers - Generate a range of even numbers + - Make the rspec tests in wish_list_spec.rb pass by writing a WishList class - - The WishList class should: - - Mixin Enumerable - - Define each so it returns wishes as strings with their index as part of the string +- The WishList class should: +- Mixin Enumerable +- Define each so it returns wishes as strings with their index as part of the string Mid-Term Spec (50pts): - Make the tests pass. From cfd5a95abfc9b96531547fe6744a07f875ab2f41 Mon Sep 17 00:00:00 2001 From: Price Hardman Date: Tue, 27 Nov 2012 15:41:52 -0800 Subject: [PATCH 10/12] Completed pirate part of week7 homework --- .../features/step_definitions/pirate.rb | 20 +++++++++++++++++++ .../features/step_definitions/pirate_steps.rb | 19 ++++++++++++++++++ 2 files changed, 39 insertions(+) create mode 100644 week7/homework/features/step_definitions/pirate.rb create mode 100644 week7/homework/features/step_definitions/pirate_steps.rb diff --git a/week7/homework/features/step_definitions/pirate.rb b/week7/homework/features/step_definitions/pirate.rb new file mode 100644 index 0000000..c3670d1 --- /dev/null +++ b/week7/homework/features/step_definitions/pirate.rb @@ -0,0 +1,20 @@ +class PirateTranslator + + def initialize + @translation_buffer = "" + end + + def get_input(input) + @translation_buffer = input + end + + def translate + case @translation + when 'Hello Friend' + output = 'Ahoy Matey' + else + output = 'Avast!' + end + output+'Shiber Me Timbers You Scurvey Dogs!!' + end +end \ No newline at end of file diff --git a/week7/homework/features/step_definitions/pirate_steps.rb b/week7/homework/features/step_definitions/pirate_steps.rb new file mode 100644 index 0000000..12503a4 --- /dev/null +++ b/week7/homework/features/step_definitions/pirate_steps.rb @@ -0,0 +1,19 @@ +Gangway /^I have a PirateTranslator$/ do + @translator = PirateTranslator.new +end + +Blimey /^I say 'Hello Friend'$/ do + @translator.get_input('Hello Friend') +end + +Blimey /^I hit translate$/ do + @translator.translate +end + +Letgoandhaul /^it prints out 'Ahoy Matey'$/ do + @translator.translate.include?('Ahoy Matey') +end + +Letgoandhaul /^it also prints 'Shiber Me Timbers You Scurvey Dogs!!'$/ do + @translator.translate.include?('Shiber Me Timbers You Scurvey Dogs!!') +end \ No newline at end of file From 256fa5eefb27f243d03e1b83700d2c717ea18aa9 Mon Sep 17 00:00:00 2001 From: Price Hardman Date: Mon, 10 Dec 2012 17:41:33 -0800 Subject: [PATCH 11/12] Finished TicTacToe --- .../features/step_definitions/tic-tac-toe.rb | 196 ++++++++++++++++++ 1 file changed, 196 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..505fb53 --- /dev/null +++ b/week7/homework/features/step_definitions/tic-tac-toe.rb @@ -0,0 +1,196 @@ +class TicTacToe + attr_accessor :board + attr_reader :player, :current_player, :other_player, :player_symbol, :computer_symbol, :SYMBOLS, :current_state, :open_spots + SYMBOLS = [:X,:O] + + def initialize(who_goes_first = nil,player_symbol=nil) #optional arguments: who goes first, and which symbol player has. + + process_optional_arguments(who_goes_first,player_symbol) #private method that decides who goes first and has which + #symbol, depending on optional arguments + + @board = { #initialize the board as empty + :A1 => " ", :A2 => " ", :A3 => " ", + :B1 => " ", :B2 => " ", :B3 => " ", + :C1 => " ", :C2 => " ", :C3 => " " + } + + @open_spots = [:A1,:A2,:A3,:B1,:B2,:B3,:C1,:C2,:C3] + + @draw = false + @over = false + @player_won = false + @computer_won = false + end + + + def welcome_player() #shows the board and welcomes the player on game start + return current_state,"Welcome #{@player}" + end + + def player=(player_name) + @player = player_name + @current_player = @player if @current_player!="Computer" #If human was current_player, updates after name entered. + end + + def decide_who_goes_first(first_player=nil) + if first_player&&first_player.downcase == :player + @current_player = @player #assign first move to player if specified + elsif first_player&&first_player.downcase == :computer + @current_player = "Computer" #assign first move to computer if specified + else + @current_player = (rand()>0.5) ? @player : "Computer" + end + end + + def randomly_assign_symbols(player_symbol=nil) + if player_symbol + @player_symbol = player_symbol + @computer_symbol = SYMBOLS[SYMBOLS.index(player_symbol)-1] + else + @player_symbol = SYMBOLS[rand.round] #rand.round randomly returns 0 or 1, assigns symbol based on index + @computer_symbol = SYMBOLS[SYMBOLS.index(@player_symbol)-1] #returns the other symbol + end + end + + def indicate_player_turn + print "#{@current_player}'s Move:" + end + + def get_player_move + gets.chomp + end + + def player_move + update_open_spots + move = get_player_move.upcase.to_sym #upcases it (in case entered as lower case) and makes into symbol + validate_move(move) + end + + def validate_move(move) #makes sure a move is valid, and if its not, asks for another one. + if @open_spots.include?(move) + @board[move] = @player_symbol + move + else + print "Error, invalid move. Make another: " + second_try = get_player_move.upcase.to_sym + validate_move(second_try) + end + end + + def computer_move + update_open_spots + #chooses an element from open_spots at random + move = @open_spots.sample + @board[move] = @computer_symbol + puts "Computer's move was #{move.to_s}" + move + end + + def spots_open? + !@open_spots.empty? + end + + def draw? + @draw + end + + def over? + @over + end + + def current_state #makes a heredoc, which when called with puts, prints out the current board configuration + <<-board + =============== + 1 || #{@board[:A1].to_s} | #{@board[:B1].to_s} | #{@board[:C1].to_s} || + 2 || #{@board[:A2].to_s} | #{@board[:B2].to_s} | #{@board[:C2].to_s} || + 3 || #{@board[:A3].to_s} | #{@board[:B3].to_s} | #{@board[:C3].to_s} || + =============== + A B C +board + end + + def determine_winner + update_open_spots + #there are 8 different winning configurations. For both players, check all of them + ["Player","Computer"].each do |player| + symbol = instance_variable_get("@"+player.downcase+"_symbol") #gets the player/computer's symbol + winning_moves = [[@board[:A1],@board[:A2],@board[:A3]], #vertical winning moves + [@board[:B1],@board[:B2],@board[:B3]], + [@board[:C1],@board[:C2],@board[:C3]], + [@board[:A1],@board[:B1],@board[:C1]], #horizontal winning moves + [@board[:A2],@board[:B2],@board[:C2]], + [@board[:A3],@board[:B3],@board[:C3]], + [@board[:A1],@board[:B2],@board[:C3]], #diagonal winning moves + [@board[:A3],@board[:B2],@board[:C1]]] + + winning_moves.each do |move| #for each triplet of winning moves + if move.all?{|spot| spot == symbol} #if each of the spots contains the given player's symbol + @over = true #then the game is over + instance_variable_set("@"+player.downcase+"_won",true) #and that player is the winner + end + end + end + + #if there are no open spots (spots_open? is false), then the game is over and a draw is declared + if spots_open? == false + @over = true + @draw = true + end + + next_turn #makes it the next player's turn + end + + def player_won? + @player_won + end + + def computer_won? + @computer_won + end + + private + + def process_optional_arguments(who_goes_first= nil,player_symbol=nil) #deals with optional arguments passed to intialize + + #as far as I can tell, optional arguments to initialize are for testing purposes to get the game into a desired + #configuration. In order to avoid conflicts with certain + + if who_goes_first #if a first player is specified + + if who_goes_first.downcase == :computer #if :computer is going to go first + @player = "Human" + decide_who_goes_first(:computer) #set first player to computer + elsif who_goes_first.downcase == :player #if :player is going to go first + @player = "Human" #gets overridden when player is set to Renee + decide_who_goes_first(:player) + end + #if symbol was passed and its valid + if ( player_symbol && ((player_symbol.upcase ==:X)||(player_symbol.upcase ==:O)) ) + randomly_assign_symbols(player_symbol.upcase) + else + #otherwise, + randomly_assign_symbols + end + + else #if no optional parameters are passed, then randomly assign first mover and symbols. + @player = "Human" + print "Please enter your name: " + self.player = gets.chomp + decide_who_goes_first + randomly_assign_symbols + end + end + + def update_open_spots + made_moves = @board.keys.select{|key| @board[key] != " "} + @open_spots = @open_spots-made_moves + end + + def next_turn + players = [@player,"Computer"] + @current_player = players[players.index(@current_player)-1] + @current_player + end + + +end \ No newline at end of file From 7058c0262e73320f8ca232b5e05a73d852414059 Mon Sep 17 00:00:00 2001 From: Price Hardman Date: Mon, 10 Dec 2012 17:50:43 -0800 Subject: [PATCH 12/12] Fix typo in TicTacToe.rb --- week7/homework/features/step_definitions/tic-tac-toe.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/week7/homework/features/step_definitions/tic-tac-toe.rb b/week7/homework/features/step_definitions/tic-tac-toe.rb index 505fb53..39f5a1e 100644 --- a/week7/homework/features/step_definitions/tic-tac-toe.rb +++ b/week7/homework/features/step_definitions/tic-tac-toe.rb @@ -131,8 +131,8 @@ def determine_winner end end - #if there are no open spots (spots_open? is false), then the game is over and a draw is declared - if spots_open? == false + #if there are no open spots (spots_open? is false) and that game isn't over, then declare the game over and a draw + if spots_open? == false && @over==false @over = true @draw = true end