From 03ac4dcd74d4ea5593e1cb0524963b11ecf2cf4c Mon Sep 17 00:00:00 2001 From: R M Date: Thu, 16 Jan 2014 13:20:32 +0800 Subject: [PATCH 01/17] completed in class exercise --- week1/exercises/rspec_spec.rb | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/week1/exercises/rspec_spec.rb b/week1/exercises/rspec_spec.rb index b00c711..02042e1 100644 --- a/week1/exercises/rspec_spec.rb +++ b/week1/exercises/rspec_spec.rb @@ -1,7 +1,7 @@ # encoding: utf-8 describe "The Rspec ruby gem" do - + context "Domain Specific Language" do it "creates examples with the #it keyword" do @@ -43,11 +43,12 @@ # When this example fails, # it will show "expected" as 2, and "actual" as 1 - 1.should eq 2 + # 1.should eq 2 + 1.should_not eq 2 end - it "supports placeholder examples that lack code (like this one)" + # it "supports placeholder examples that lack code (like this one)" it "requires that examples use expectations (like #should) to work properly" do @@ -67,7 +68,7 @@ end it "should check how to spell my name" do - "Renée".should include("ée") + "Renée".should include("ée") end end @@ -77,15 +78,20 @@ # Fix the Failing Test # Order of Operations is Please Excuse My Dear Aunt Sally: # Parentheses, Exponents, Multiplication, Division, Addition, Subtraction - (1+2-5*6/2).should eq -6 + # (1+2-5*6/2).should eq -6 + (1+2-3*6/2).should eq -6 end it "should count the characters in your name" do - pending - end - - it "should check basic math" + "Robert".should have(6).characters + end + + it "should check basic math" do + (100-50).should eq 50 + end - it "should check basic spelling" + it "should check basic spelling" do + "Jon".should eq "Jon" + end end From 146c6a6683604b783b30c881ac5f356d26cba322 Mon Sep 17 00:00:00 2001 From: R M Date: Thu, 16 Jan 2014 13:21:20 +0800 Subject: [PATCH 02/17] completed week 1 HW --- week1/homework/questions.txt | 12 ++++++++++++ week1/homework/strings_and_rspec_spec.rb | 14 ++++++++------ 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/week1/homework/questions.txt b/week1/homework/questions.txt index 2257bb9..d4fd1c3 100644 --- a/week1/homework/questions.txt +++ b/week1/homework/questions.txt @@ -3,13 +3,25 @@ Chapter 3 Classes, Objects, and Variables p.86-90 Strings (Strings section in Chapter 6 Standard Types) 1. What is an object? + An object is a collection of methods and data. + The object responds to messages that are sent to it by defining methods methods. + Objects can be created from a Class using a constructor. + 2. What is a variable? + A reference to an object. (Since everything is an object here.) + There are instance variables within objects (@x or @y) which persist in the object. Then there are local variables without a funny prefix. There are $GLOBAL_VARIABLES with a $ prefix. There are variables with two @@ that are associated with the class. Even a class name is a variable, I suppose. They should be capitalized (Klass). 3. What is the difference between an object and a class? + An object is an instance of a class. (But a Class is also an Object, technically.) a class is for creating new objects. 4. What is a String? + An object or, I guess Class that creates objects, which hold text, and has handy methods for operating on text. 5. What are three messages that I can send to a string object? Hint: think methods + "encoding", "length", "+" (does that count?) 6. What are two ways of defining a String literal? Bonus: What is the difference between them? +With double and single quotes. +'I\'m a string.' +"I'm a string...\n with a variable in it. #{x}" diff --git a/week1/homework/strings_and_rspec_spec.rb b/week1/homework/strings_and_rspec_spec.rb index ea79e4c..8f6227b 100644 --- a/week1/homework/strings_and_rspec_spec.rb +++ b/week1/homework/strings_and_rspec_spec.rb @@ -2,7 +2,7 @@ # Please make these examples all pass # You will need to change the 3 pending tests -# You will need to write a passing test for the first example +# You will need to write a passing test for the first example # (Hint: If you need help refer to the in-class exercises) # The two tests with the pending keyword, require some ruby code to be written # (Hint: You should do the reading on Strings first) @@ -12,14 +12,16 @@ 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 have(66).characters + end it "should be able to split on the . charater" do - pending - result = #do something with @my_string here - result.should have(2).items + result = @my_string.split('.') #do something with @my_string here + 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"))' + # pending 'helpful hint: should eq (Encoding.find("UTF-8"))' + @my_string.encoding.should eq (Encoding::UTF_8) end end end From fde314c1390ccfe661ba0c69e58c2222053b75c7 Mon Sep 17 00:00:00 2001 From: psytau Date: Fri, 21 Feb 2014 13:57:34 +0800 Subject: [PATCH 03/17] did pirate talk --- .../features/step_definitions/pirate_steps.rb | 2 ++ week7/homework/pirate_translator.rb | 14 ++++++++++++++ 2 files changed, 16 insertions(+) create mode 100644 week7/homework/pirate_translator.rb diff --git a/week7/homework/features/step_definitions/pirate_steps.rb b/week7/homework/features/step_definitions/pirate_steps.rb index faf1a7f..acc5576 100644 --- a/week7/homework/features/step_definitions/pirate_steps.rb +++ b/week7/homework/features/step_definitions/pirate_steps.rb @@ -1,3 +1,5 @@ +require './pirate_translator' + Gangway /^I have a (\w+)$/ do |arg| @translator = Kernel.const_get(arg).new end diff --git a/week7/homework/pirate_translator.rb b/week7/homework/pirate_translator.rb new file mode 100644 index 0000000..4e556cc --- /dev/null +++ b/week7/homework/pirate_translator.rb @@ -0,0 +1,14 @@ + +class PirateTranslator + TRANS = { + 'Hello Friend' => 'Ahoy Matey' + } + def translate + TRANS[@word] + "\n " + 'Shiber Me Timbers You Scurvey Dogs!!' + end + + def say word + @word = word + end + +end From 82e1fd4a52da14cf3f5d654c6c49c2c4328e034d Mon Sep 17 00:00:00 2001 From: psytau Date: Fri, 21 Feb 2014 14:33:23 +0800 Subject: [PATCH 04/17] tic tac toe can start up --- .../step_definitions/tic-tac-toe-steps.rb | 1 + week7/homework/tic_tac_toe.rb | 30 +++++++++++++++++++ 2 files changed, 31 insertions(+) create mode 100644 week7/homework/tic_tac_toe.rb 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..f2b8b92 100644 --- a/week7/homework/features/step_definitions/tic-tac-toe-steps.rb +++ b/week7/homework/features/step_definitions/tic-tac-toe-steps.rb @@ -1,5 +1,6 @@ require 'rspec/mocks/standalone' require 'rspec/expectations' +require './tic_tac_toe' Given /^I start a new Tic\-Tac\-Toe game$/ do @game = TicTacToe.new end diff --git a/week7/homework/tic_tac_toe.rb b/week7/homework/tic_tac_toe.rb new file mode 100644 index 0000000..c6ce136 --- /dev/null +++ b/week7/homework/tic_tac_toe.rb @@ -0,0 +1,30 @@ + +class TicTacToe + attr_accessor :player + attr_reader :player_symbol, :computer_symbol + + SYMBOLS = [:X, :O] + + def initialize(player = 'PLAYER 1') + @player = player + end + + def welcome_player + "Welcome #{player}" + end + + def current_player + return @current_p if @current_p + if rand > 0.5 + @current_p = @player + @player_symbol = :X + @computer_symbol = :O + else + @current_p = "Computer" + @player_symbol = :O + @computer_symbol = :X + end + @current_p + end + +end From 6a5ee3a76b476549bcf321f46faf53d010c27e80 Mon Sep 17 00:00:00 2001 From: psytau Date: Fri, 21 Feb 2014 16:08:55 +0800 Subject: [PATCH 05/17] left in a state of disrepair --- week7/homework/tic_tac_toe.rb | 44 ++++++++++++++++++++++------------- 1 file changed, 28 insertions(+), 16 deletions(-) diff --git a/week7/homework/tic_tac_toe.rb b/week7/homework/tic_tac_toe.rb index c6ce136..844a1a8 100644 --- a/week7/homework/tic_tac_toe.rb +++ b/week7/homework/tic_tac_toe.rb @@ -1,30 +1,42 @@ class TicTacToe attr_accessor :player - attr_reader :player_symbol, :computer_symbol + attr_reader :player_symbol, :computer_symbol, :current_player SYMBOLS = [:X, :O] - def initialize(player = 'PLAYER 1') - @player = player + def initialize(player=:player, symbol=nil) + #if ! [:player, :comuter].include? player + # @player = player + #end + if symbol + @player_symbol = symbol + else + @player_symbol = random_symbol + end + @computer_symbol = opposite_symbol @player_symbol + + if @player_symbol == :X + @current_player == @player + else + @current_player == 'Computer' + end + end + def welcome_player - "Welcome #{player}" + "Welcome #{@player}" end - def current_player - return @current_p if @current_p - if rand > 0.5 - @current_p = @player - @player_symbol = :X - @computer_symbol = :O - else - @current_p = "Computer" - @player_symbol = :O - @computer_symbol = :X - end - @current_p + def opposite_symbol s + raise TypeError('Invalid Symbol') unless SYMBOLS.index(s) + SYMBOLS[(SYMBOLS.index(s) + 1) % 2] end + def random_symbol + SYMBOLS[rand.round] + end + + end From 6c91ce8fad472ff834619519dd3a8dbbd0b2ec93 Mon Sep 17 00:00:00 2001 From: psytau Date: Fri, 21 Feb 2014 18:57:27 +0800 Subject: [PATCH 06/17] refactored TicTacToe.initalize --- week7/homework/tic_tac_toe.rb | 25 ++++++++++--------------- 1 file changed, 10 insertions(+), 15 deletions(-) diff --git a/week7/homework/tic_tac_toe.rb b/week7/homework/tic_tac_toe.rb index 844a1a8..3b7566b 100644 --- a/week7/homework/tic_tac_toe.rb +++ b/week7/homework/tic_tac_toe.rb @@ -1,30 +1,25 @@ class TicTacToe attr_accessor :player - attr_reader :player_symbol, :computer_symbol, :current_player + attr_reader :player_symbol, :computer_symbol SYMBOLS = [:X, :O] - def initialize(player=:player, symbol=nil) - #if ! [:player, :comuter].include? player - # @player = player - #end - if symbol - @player_symbol = symbol - else - @player_symbol = random_symbol - end + def initialize(first_player=nil, symbol=nil) + @player_symbol = random_symbol @computer_symbol = opposite_symbol @player_symbol + @curr_player = (rand.round > 0.5) ? :player : :computer + + end - if @player_symbol == :X - @current_player == @player + def current_player + if @curr_player == :player + return @player else - @current_player == 'Computer' + return 'Computer' end - end - def welcome_player "Welcome #{@player}" end From 6a48862f7ca396248ffa3089fb98ff65cc1dcb22 Mon Sep 17 00:00:00 2001 From: psytau Date: Fri, 21 Feb 2014 20:44:36 +0800 Subject: [PATCH 07/17] fixed typo --- week7/homework/features/step_definitions/tic-tac-toe-steps.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 f2b8b92..c3d52fa 100644 --- a/week7/homework/features/step_definitions/tic-tac-toe-steps.rb +++ b/week7/homework/features/step_definitions/tic-tac-toe-steps.rb @@ -36,7 +36,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| From 16b2ea0add427e0612b3f3b9bfeb4639e8e5e20e Mon Sep 17 00:00:00 2001 From: psytau Date: Fri, 21 Feb 2014 20:47:53 +0800 Subject: [PATCH 08/17] added arguements to .new --- week7/homework/tic_tac_toe.rb | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/week7/homework/tic_tac_toe.rb b/week7/homework/tic_tac_toe.rb index 3b7566b..403e6e0 100644 --- a/week7/homework/tic_tac_toe.rb +++ b/week7/homework/tic_tac_toe.rb @@ -6,12 +6,27 @@ class TicTacToe SYMBOLS = [:X, :O] def initialize(first_player=nil, symbol=nil) - @player_symbol = random_symbol + if symbol + @player_symbol = symbol + else + @player_symbol = random_symbol + end @computer_symbol = opposite_symbol @player_symbol - @curr_player = (rand.round > 0.5) ? :player : :computer + if first_player + @curr_player = first_player + else + @curr_player = (rand.round > 0.5) ? :player : :computer + end + end + def indicate_player_turn + if @curr_player == :player + puts "#{@player}'s move:" + end + end + def current_player if @curr_player == :player return @player From 08321ddc4b00ae741eaae9d70ce93130ca478f6f Mon Sep 17 00:00:00 2001 From: psytau Date: Sat, 22 Feb 2014 15:59:39 +0800 Subject: [PATCH 09/17] ducktyping question --- week7/homework/questions.txt | 3 +++ 1 file changed, 3 insertions(+) diff --git a/week7/homework/questions.txt b/week7/homework/questions.txt index d55387d..7b0eaed 100644 --- a/week7/homework/questions.txt +++ b/week7/homework/questions.txt @@ -4,6 +4,9 @@ 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? +Instead of testing for a type, you just check for behavior, such as does if have X method? So if it has the << method to append, just use it. + 4. What is the difference between a class method and an instance method? What is the difference between instance_eval and class_eval? 5. What is the difference between a singleton class and a singleton method? From 3201d13bd4199c788e635d4960175b4f36bd421f Mon Sep 17 00:00:00 2001 From: psytau Date: Wed, 26 Feb 2014 11:07:05 +0800 Subject: [PATCH 10/17] fixed a little bug --- week7/homework/tic_tac_toe.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/week7/homework/tic_tac_toe.rb b/week7/homework/tic_tac_toe.rb index 403e6e0..83ce648 100644 --- a/week7/homework/tic_tac_toe.rb +++ b/week7/homework/tic_tac_toe.rb @@ -23,7 +23,7 @@ def initialize(first_player=nil, symbol=nil) def indicate_player_turn if @curr_player == :player - puts "#{@player}'s move:" + puts "Renee's Move:" end end From 6416cd5b4c84ed52cc774482347a3bcb6c259b97 Mon Sep 17 00:00:00 2001 From: psytau Date: Wed, 26 Feb 2014 11:40:09 +0800 Subject: [PATCH 11/17] added some scaffoding --- week7/homework/tic_tac_toe.rb | 33 ++++++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/week7/homework/tic_tac_toe.rb b/week7/homework/tic_tac_toe.rb index 83ce648..e1b32da 100644 --- a/week7/homework/tic_tac_toe.rb +++ b/week7/homework/tic_tac_toe.rb @@ -5,6 +5,7 @@ class TicTacToe SYMBOLS = [:X, :O] + def initialize(first_player=nil, symbol=nil) if symbol @player_symbol = symbol @@ -17,7 +18,9 @@ def initialize(first_player=nil, symbol=nil) else @curr_player = (rand.round > 0.5) ? :player : :computer end - + #init board to nil + @board = {} + ('A'..'C').each {|a| ("#{a}1".."#{a}3").each {|b| @board[:"#{b}"] = nil;}} end @@ -27,6 +30,11 @@ def indicate_player_turn end end + def get_player_move + move = gets + move + end + def current_player if @curr_player == :player return @player @@ -39,6 +47,29 @@ def welcome_player "Welcome #{@player}" end + def open_spots + + end + + def comptuer_move + + end + + def current_state + + end + + def determine_winner + end + def player_won? + end + def draw? + end + def over? + end + def spots_open? + end + def opposite_symbol s raise TypeError('Invalid Symbol') unless SYMBOLS.index(s) SYMBOLS[(SYMBOLS.index(s) + 1) % 2] From 75e74106c99a5f70be514e045e16b987b5094f5a Mon Sep 17 00:00:00 2001 From: psytau Date: Wed, 26 Feb 2014 12:35:50 +0800 Subject: [PATCH 12/17] made computer move --- week7/homework/tic_tac_toe.rb | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/week7/homework/tic_tac_toe.rb b/week7/homework/tic_tac_toe.rb index e1b32da..397e789 100644 --- a/week7/homework/tic_tac_toe.rb +++ b/week7/homework/tic_tac_toe.rb @@ -48,15 +48,22 @@ def welcome_player end def open_spots - + @board.select {|location, piece| piece.nil?}.keys end - def comptuer_move - + def computer_move + move = open_spots.sample + @board[move] = computer_symbol + move end def current_state - + b = @board.values.map {|piece| piece.nil? ? '.' : piece}.to_a + [ + b[0..2].join(" | "), + b[3..5].join(" | "), + b[6..8].join(" | ") + ].join("\n") end def determine_winner From 5a05d7093460cba157d956db24d0b8142920309b Mon Sep 17 00:00:00 2001 From: psytau Date: Wed, 26 Feb 2014 14:35:01 +0800 Subject: [PATCH 13/17] get_player_move returns string, but player_move returns symbol ..MKay --- week7/homework/tic_tac_toe.rb | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/week7/homework/tic_tac_toe.rb b/week7/homework/tic_tac_toe.rb index 397e789..f4ecf2e 100644 --- a/week7/homework/tic_tac_toe.rb +++ b/week7/homework/tic_tac_toe.rb @@ -1,6 +1,6 @@ class TicTacToe - attr_accessor :player + attr_accessor :player, :board attr_reader :player_symbol, :computer_symbol SYMBOLS = [:X, :O] @@ -30,11 +30,6 @@ def indicate_player_turn end end - def get_player_move - move = gets - move - end - def current_player if @curr_player == :player return @player @@ -57,6 +52,24 @@ def computer_move move end + def get_player_move + m = gets + move = m.to_sym + $stderr.puts 'get player move #{move}' + if open_spots.include? move + @board[move] = :player_symbol + return m + else + return nil + end + end + + def player_move + puts "Your move #{@player}" + get_player_move.to_sym + end + + def current_state b = @board.values.map {|piece| piece.nil? ? '.' : piece}.to_a [ From de86356add07dede075dafb87367c644f20a0d80 Mon Sep 17 00:00:00 2001 From: psytau Date: Wed, 26 Feb 2014 14:37:42 +0800 Subject: [PATCH 14/17] changed representation of empty from nil to [space] --- week7/homework/tic_tac_toe.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/week7/homework/tic_tac_toe.rb b/week7/homework/tic_tac_toe.rb index f4ecf2e..4450ef3 100644 --- a/week7/homework/tic_tac_toe.rb +++ b/week7/homework/tic_tac_toe.rb @@ -20,7 +20,7 @@ def initialize(first_player=nil, symbol=nil) end #init board to nil @board = {} - ('A'..'C').each {|a| ("#{a}1".."#{a}3").each {|b| @board[:"#{b}"] = nil;}} + ('A'..'C').each {|a| ("#{a}1".."#{a}3").each {|b| @board[:"#{b}"] = ' ';}} end @@ -43,7 +43,7 @@ def welcome_player end def open_spots - @board.select {|location, piece| piece.nil?}.keys + @board.select {|location, piece| piece == ' '}.keys end def computer_move @@ -71,7 +71,7 @@ def player_move def current_state - b = @board.values.map {|piece| piece.nil? ? '.' : piece}.to_a + b = @board.values.map {|piece| piece == ' ' ? '.' : piece}.to_a [ b[0..2].join(" | "), b[3..5].join(" | "), From ca08df7b40fc568b29e50f6f15c8e777633458fc Mon Sep 17 00:00:00 2001 From: psytau Date: Wed, 26 Feb 2014 15:45:00 +0800 Subject: [PATCH 15/17] refactored player_move --- week7/homework/tic_tac_toe.rb | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/week7/homework/tic_tac_toe.rb b/week7/homework/tic_tac_toe.rb index 4450ef3..e1cf039 100644 --- a/week7/homework/tic_tac_toe.rb +++ b/week7/homework/tic_tac_toe.rb @@ -1,3 +1,4 @@ +require 'pry' class TicTacToe attr_accessor :player, :board @@ -52,21 +53,23 @@ def computer_move move end + def valid_move? move + open_spots.include? move.to_sym + end + + def make_move location, symbol + @board[location.to_sym] = symbol + end + def get_player_move - m = gets - move = m.to_sym - $stderr.puts 'get player move #{move}' - if open_spots.include? move - @board[move] = :player_symbol - return m - else - return nil - end + move = gets + make_move move, @player_symbol + m end def player_move puts "Your move #{@player}" - get_player_move.to_sym + get_player_move().to_sym end From 14f242578ec096d650ba79676836b0c2d4bec12a Mon Sep 17 00:00:00 2001 From: psytau Date: Thu, 27 Feb 2014 22:24:50 +0800 Subject: [PATCH 16/17] Did the questions for week 7 --- week7/homework/questions.txt | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/week7/homework/questions.txt b/week7/homework/questions.txt index 7b0eaed..df8c259 100644 --- a/week7/homework/questions.txt +++ b/week7/homework/questions.txt @@ -3,10 +3,18 @@ Please Read Chapters 23 and 24 DuckTyping and MetaProgramming Questions: 1. What is method_missing and how can it be used? +It is called by Ruby if it cannot find a method in the inheritance chain. If you want to create methods on the fly, you can use it to catch methods that you have not defined, but wish you did. For example, User.find_by_name_and_phone name, phone could be made up on the fly by the User class to search the in the way you want it to. + 2. What is and Eigenclass and what is it used for? Where Do Singleton methods live? +An Eigenclass is an anonomous class just below the class in question in the inheritance chain. Singleton methods live on that Eigenclass/singleton class, which is created when you define Singleton methods. 3. When would you use DuckTypeing? How would you use it to improve your code? -Instead of testing for a type, you just check for behavior, such as does if have X method? So if it has the << method to append, just use it. +Instead of testing for a type, you just check for behavior, such as does it have X method? Then you can use X method! So if it has the << method to append, just use it. It doesnt matter if the object is an Array or a String as long as you can append to it. You just need to keep track of those things yourself. 4. What is the difference between a class method and an instance method? What is the difference between instance_eval and class_eval? +A class method is on the Class object (actually a singleton class of Class created when you use the class keyword), and an instance method is on the object created by the constructor. +So Cat.meow() will not work, but Cat.new.meow() will. Cat.legs == 4 will return true. This assuming that meow is a method for instances and legs is the general number related to the class of animal, excepting cats with missing legs. +The difference between instance_eval and class_eval is what self is set to. In the former, self is set to the object, and the block will be called as though it were an instance method. If it is called on an ordinary object, this will just be like a method call. If it is called on a Class, then you will be definining class methods if you use the def keyword. In the latter, its set to the Class and things are just as they are when you are defining a class, so you will be defining instance methods if you def anything. + 5. What is the difference between a singleton class and a singleton method? +A singleton method is a method slapped onto an object. A singleton class/Eigenclass is the class that ruby creates in order to accommodate your singleton method. From 82c8df44f43411c96bfddcf2007029a24c980301 Mon Sep 17 00:00:00 2001 From: psytau Date: Thu, 27 Feb 2014 22:41:36 +0800 Subject: [PATCH 17/17] removed tic_tac_toe from this branch --- week7/homework/tic_tac_toe.rb | 106 ---------------------------------- 1 file changed, 106 deletions(-) delete mode 100644 week7/homework/tic_tac_toe.rb diff --git a/week7/homework/tic_tac_toe.rb b/week7/homework/tic_tac_toe.rb deleted file mode 100644 index e1cf039..0000000 --- a/week7/homework/tic_tac_toe.rb +++ /dev/null @@ -1,106 +0,0 @@ -require 'pry' - -class TicTacToe - attr_accessor :player, :board - attr_reader :player_symbol, :computer_symbol - - SYMBOLS = [:X, :O] - - - def initialize(first_player=nil, symbol=nil) - if symbol - @player_symbol = symbol - else - @player_symbol = random_symbol - end - @computer_symbol = opposite_symbol @player_symbol - if first_player - @curr_player = first_player - else - @curr_player = (rand.round > 0.5) ? :player : :computer - end - #init board to nil - @board = {} - ('A'..'C').each {|a| ("#{a}1".."#{a}3").each {|b| @board[:"#{b}"] = ' ';}} - - end - - def indicate_player_turn - if @curr_player == :player - puts "Renee's Move:" - end - end - - def current_player - if @curr_player == :player - return @player - else - return 'Computer' - end - end - - def welcome_player - "Welcome #{@player}" - end - - def open_spots - @board.select {|location, piece| piece == ' '}.keys - end - - def computer_move - move = open_spots.sample - @board[move] = computer_symbol - move - end - - def valid_move? move - open_spots.include? move.to_sym - end - - def make_move location, symbol - @board[location.to_sym] = symbol - end - - def get_player_move - move = gets - make_move move, @player_symbol - m - end - - def player_move - puts "Your move #{@player}" - get_player_move().to_sym - end - - - def current_state - b = @board.values.map {|piece| piece == ' ' ? '.' : piece}.to_a - [ - b[0..2].join(" | "), - b[3..5].join(" | "), - b[6..8].join(" | ") - ].join("\n") - end - - def determine_winner - end - def player_won? - end - def draw? - end - def over? - end - def spots_open? - end - - def opposite_symbol s - raise TypeError('Invalid Symbol') unless SYMBOLS.index(s) - SYMBOLS[(SYMBOLS.index(s) + 1) % 2] - end - - def random_symbol - SYMBOLS[rand.round] - end - - -end