diff --git a/week1/homework/questions_Yan_answers.txt b/week1/homework/questions_Yan_answers.txt new file mode 100644 index 0000000..c31eb09 --- /dev/null +++ b/week1/homework/questions_Yan_answers.txt @@ -0,0 +1,21 @@ +Please read: +Chapter 3 Classes, Objects, and Variables +p.90-94 Strings + +1. What is an object? +Everything we manipulate in Ruby is an object. + +2. What is a variable? +Variables are used to keep track of objects; each variable holds a referece to an object. + +3. What is the difference between an object and a class? +Every object in Ruby was generated either directly or indirectly from a class. + +4. What is a String? +Ruby strings are simply sequences of characters. Strings are objects of class String. + +5. What are three messages that I can send to a string object? Hint: think methods +length, size, count + +6. What are two ways of defining a String literal? Bonus: What is the difference between the two? +The type of string delimiter determines the degree of substitution performed. Single-quoted strings and double-quoted strings. \ No newline at end of file diff --git a/week1/homework/strings_and_rspec_spec.rb b/week1/homework/strings_and_rspec_spec.rb index 2f188f6..116e390 100644 --- a/week1/homework/strings_and_rspec_spec.rb +++ b/week1/homework/strings_and_rspec_spec.rb @@ -1,5 +1,4 @@ # encoding: utf-8 - # 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 @@ -12,15 +11,22 @@ before(:all) do @my_string = "Renée is a fun teacher. Ruby is a really cool programming language" end + it "should be able to count the charaters" do - @my_string.should have(@my_string.size).characters + @my_string.should respond_to(:length) + #I should be able to do @my_string.length end + it "should be able to split on the . charater" do - result = @my_string.split('.') + result = @my_string.split(/\./) + # '\' 'escapes' any character so that it just means that character result.should have(2).items end + it "should be able to give the encoding of the string" do @my_string.encoding.should eq (Encoding.find("UTF-8")) end end end + + 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 b353120..eeee201 100644 --- a/week7/homework/features/step_definitions/tic-tac-toe-steps.rb +++ b/week7/homework/features/step_definitions/tic-tac-toe-steps.rb @@ -35,7 +35,7 @@ Then /^the computer prints "(.*?)"$/ do |arg1| @game.should_receive(:puts).with(arg1) - @game.indicate_palyer_turn + @game.indicate_player_turn end Then /^waits for my input of "(.*?)"$/ do |arg1| @@ -43,7 +43,7 @@ @game.get_player_move end -Given /^it is the computer's turn$/ do +Given /^it is the computers turn$/ do @game = TicTacToe.new(:computer, :O) @game.current_player.should eq "Computer" end @@ -70,6 +70,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 end 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..7402de0 --- /dev/null +++ b/week7/homework/features/step_definitions/tic-tac-toe.rb @@ -0,0 +1,106 @@ +class TicTacToe + attr_accessor :player, :current_turn, :board, :player_symbol, + :computer_symbol + SYMBOLS = [:X, :O] + + def initialize(player = nil, player_symbol = nil) + if player + @current_turn = player + + if player_symbol + @player_symbol = player_symbol + @computer_symbol = SYMBOLS[SYMBOLS.index(player_symbol) - 1] + end + + else + @current_turn = randomly_choose + end + + randomly_assign_symbol unless (@player_symbol && @computer_symbol) + initialize_board + end + + def welcome_player + return "Welcome #{@player}" + end + + def randomly_choose + return [:player, :computer].sample + end + + def current_player + return {:player => @player || "Player0", :computer => "Computer"}[@current_turn] + end + + def indicate_player_turn + puts "#{current_player}'s Move:" + end + + def player_move + move = get_player_move.to_sym + + if @board[move] == " " + @board[move] = @player_symbol + else + return :B2 #need to change + end + + return move + end + + def get_player_move + gets.chomp + end + + def initialize_board + @board = {:A1 => " ", :A2 => " ", :A3 => " ", + :B1 => " ", :B2 => " ", :B3 => " ", + :C1 => " ", :C2 => " ", :C3 => " "} + end + + def winning_cases + @cases = [ + ['A1','B2','C3'], + ['B1','B2','B3'], + ['C1','C2','C3'], + ['A1','B1','C1'], + ['A2','B2','C2'], + ['A3','B3','C3'], + ['A1','B2','C3'], + ['C1','B2','A3'] + ] + end + + def randomly_assign_symbol + @computer_symbol = SYMBOLS.sample + @player_symbol = @computer_symbol == :X ? :O : :X + end + + #@cpu = rand() > 0.5 ? 'X' : 'O' + #@user = @cpu == 'X' ? 'O' : 'X' + + #@computer_symbol = SYMBOLS.sample + #@player_symbol = SYMBOLS[SYMBOLS.index(@computer_symbol) - 1] + + def current_state + @player_symbol.to_s + end + + def determine_winner + return "#{player_won?}" + end + + def player_won? + return @board.values + end + + def draw? + end + + def spots_open? + end + + def over? + end + +end \ No newline at end of file diff --git a/week7/homework/play_game.rb b/week7/homework/play_game.rb index cf7847f..f64b949 100644 --- a/week7/homework/play_game.rb +++ b/week7/homework/play_game.rb @@ -8,7 +8,7 @@ when "Computer" @game.computer_move when @game.player - @game.indicate_palyer_turn + @game.indicate_player_turn @game.player_move end puts @game.current_state