From 1a109d3c4d0e7b8bfcab2b1310bdff3b2709bfaf Mon Sep 17 00:00:00 2001 From: wsugg Date: Wed, 10 Oct 2012 13:11:07 -0700 Subject: [PATCH 01/14] Added my contact info --- week1/exercises/roster.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/week1/exercises/roster.txt b/week1/exercises/roster.txt index a97ec74..548c410 100644 --- a/week1/exercises/roster.txt +++ b/week1/exercises/roster.txt @@ -2,3 +2,4 @@ name, email, github, twitter, hipchat Renee D, renee@nird.us, reneedv, @gigglegirl4e, renee.devoursney@gmail.com Nell Shamrell, nellshamrell@gmail.com, nellshamrell, @nellshamrell +Will Sugg, sugg.will@gmail.com, wsugg, , From 66e03dea6bac165ea355db3d243099e407ea421a Mon Sep 17 00:00:00 2001 From: wsugg Date: Tue, 16 Oct 2012 12:09:57 -0700 Subject: [PATCH 02/14] adding my contact info --- week1/exercises/roster.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/week1/exercises/roster.txt b/week1/exercises/roster.txt index fcf5b9d..4d79ab3 100644 --- a/week1/exercises/roster.txt +++ b/week1/exercises/roster.txt @@ -27,3 +27,4 @@ Karen Keasler, keaslk@gmail.com, github.com/irissilvermoon, irissilvermoon, keas Bryan Williams, b_d_williams@yahoo.com, tallbryan, no twitter, b_d_williams@yahoo.com Serene C., ruby@serenecareaga.com, serened, @grrlcoder, serened@gmail.com Peter E. Granger, peter@granger.net, evac156, N/A, N/A +Will Sugg, sugg.will@gmail.com, wsugg, , sugg.will@gmail.com From df49f8729bfaae3e379d94fafc4b1a322d523f10 Mon Sep 17 00:00:00 2001 From: wsugg Date: Tue, 16 Oct 2012 18:23:03 -0700 Subject: [PATCH 03/14] homewrk week1 --- week1/exercises/rspec_spec.rb | 18 ++++++++-------- week1/homework/questions.txt | 26 ++++++++++++++++++++++-- week1/homework/strings_and_rspec_spec.rb | 21 +++++++++++++------ 3 files changed, 49 insertions(+), 16 deletions(-) diff --git a/week1/exercises/rspec_spec.rb b/week1/exercises/rspec_spec.rb index f4c2f0b..4d8899e 100644 --- a/week1/exercises/rspec_spec.rb +++ b/week1/exercises/rspec_spec.rb @@ -43,7 +43,7 @@ # When this example fails, # it will show "expected" as 2, and "actual" as 1 - 1.should eq 2 + (1+1).should eq 2 end @@ -71,24 +71,26 @@ end end - + context "Examples for in-class test exploration" do it "should know order of operations" do # 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 -12 + + (((1+2-5)*6)/2).should eq -10 end + it "should count the charaters in your name" do - "Nell".should have(4).characters - end + "Will".should have(4).characters + end it "should check basic math" do - (1+2+3+4+5).should eq 15 + (5+2).should eq 7 end - + it "should check basic spelling" do - "Field".should include('ie') + "spelling".should include("ing") end end diff --git a/week1/homework/questions.txt b/week1/homework/questions.txt index cd5ff26..43b619a 100644 --- a/week1/homework/questions.txt +++ b/week1/homework/questions.txt @@ -3,8 +3,30 @@ Chapter 3 Classes, Objects, and Variables p.90-94 Strings 1. What is an object? +A is something created from a class constructor code. myObj = object.new + 2. What is a variable? +A var is a container for a value ie, var = 1. + 3. What is the difference between an object and a class? -4. What is a String? +Objects are created from a class and inherit the class properties. + +4. What is a String? +A set of chars in sequence or can also be a binary sequence. + 5. What are three messages that I can send to a string object? Hint: think methods -6. What are two ways of defining a String literal? Bonus: What is the difference between the two? +a.puts +b.spilt +c.concat +d.include + +6. What are two ways of defining a String literal? +a. using a delimiter like % . +b. using a multiline delimiter such as +string = < Date: Tue, 6 Nov 2012 11:38:34 -0800 Subject: [PATCH 04/14] Update .gitignore --- .gitignore | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/.gitignore b/.gitignore index 18d7cf1..556820a 100644 --- a/.gitignore +++ b/.gitignore @@ -13,6 +13,10 @@ test/version_tmp tmp *.*~ *~ +./week1 +./week3 +./week2 +*/Fall2012RubyMidTerm/* # YARD artifacts .yardoc From 386389eea2f972cf5741cb2b9eced3f2eaa347ee Mon Sep 17 00:00:00 2001 From: wsugg Date: Tue, 6 Nov 2012 12:01:40 -0800 Subject: [PATCH 05/14] trying to filter older weeks dirs out for next pull --- .gitignore | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/.gitignore b/.gitignore index 18d7cf1..da71224 100644 --- a/.gitignore +++ b/.gitignore @@ -13,6 +13,11 @@ test/version_tmp tmp *.*~ *~ +./week1 +./week2 +./week3 +./midterm +./week4 # YARD artifacts .yardoc From fb2d9ca2058fc37070ac76f9912549d4772e392f Mon Sep 17 00:00:00 2001 From: wsugg Date: Tue, 13 Nov 2012 19:43:45 -0800 Subject: [PATCH 06/14] adding class exersicses --- week6/class_materials/test_gem/.rspec | 0 week6/class_materials/test_gem/Rakefile | 5 +++++ week6/class_materials/test_gem/bin/test_gem.rb | 5 +++++ week6/class_materials/test_gem/lib/test_gem.rb | 2 +- week6/class_materials/test_gem/spec/test_gem_spec.rb | 5 +++++ week6/class_materials/test_gem/test_gem.gemspec | 1 + 6 files changed, 17 insertions(+), 1 deletion(-) create mode 100644 week6/class_materials/test_gem/.rspec create mode 100644 week6/class_materials/test_gem/Rakefile create mode 100644 week6/class_materials/test_gem/bin/test_gem.rb create mode 100644 week6/class_materials/test_gem/spec/test_gem_spec.rb diff --git a/week6/class_materials/test_gem/.rspec b/week6/class_materials/test_gem/.rspec new file mode 100644 index 0000000..e69de29 diff --git a/week6/class_materials/test_gem/Rakefile b/week6/class_materials/test_gem/Rakefile new file mode 100644 index 0000000..119bcdb --- /dev/null +++ b/week6/class_materials/test_gem/Rakefile @@ -0,0 +1,5 @@ +require 'rspec/core/rake_task' + +Rspec::Core::RakeTask.new('spec') + +task :default => :spec \ No newline at end of file diff --git a/week6/class_materials/test_gem/bin/test_gem.rb b/week6/class_materials/test_gem/bin/test_gem.rb new file mode 100644 index 0000000..7c8f688 --- /dev/null +++ b/week6/class_materials/test_gem/bin/test_gem.rb @@ -0,0 +1,5 @@ +#!usr/bin/env ruby + +puts ""Hello Word again" + +puts ARGV[0] \ No newline at end of file diff --git a/week6/class_materials/test_gem/lib/test_gem.rb b/week6/class_materials/test_gem/lib/test_gem.rb index 7f09b2f..f9eb2bb 100644 --- a/week6/class_materials/test_gem/lib/test_gem.rb +++ b/week6/class_materials/test_gem/lib/test_gem.rb @@ -1 +1 @@ -puts "Hello World!" \ No newline at end of file +puts "Hello World from test_gem in lib dir!" \ No newline at end of file diff --git a/week6/class_materials/test_gem/spec/test_gem_spec.rb b/week6/class_materials/test_gem/spec/test_gem_spec.rb new file mode 100644 index 0000000..57eb31e --- /dev/null +++ b/week6/class_materials/test_gem/spec/test_gem_spec.rb @@ -0,0 +1,5 @@ +require 'test_gem' + +describ "test_gem" do + +end \ No newline at end of file diff --git a/week6/class_materials/test_gem/test_gem.gemspec b/week6/class_materials/test_gem/test_gem.gemspec index 55a34a4..978cf3a 100644 --- a/week6/class_materials/test_gem/test_gem.gemspec +++ b/week6/class_materials/test_gem/test_gem.gemspec @@ -8,4 +8,5 @@ Gem::Specification.new do |s| s.email = 'renee@nird.us' s.homepage = 'http://rubygems.org/gems/test_gem' s.files = ["lib/test_gem.rb"] + s.executables << "test_gem.rb" end \ No newline at end of file From c1fedefbb2a4dd94c77d691f85789e3f133db0c5 Mon Sep 17 00:00:00 2001 From: wsugg Date: Tue, 4 Dec 2012 18:14:43 -0800 Subject: [PATCH 07/14] week8 ex --- week8/exercises/couch.rb | 5 ++- week8/exercises/experiment.rb | 82 +++++++++++++++++++++++++++++++++++ 2 files changed, 85 insertions(+), 2 deletions(-) create mode 100644 week8/exercises/experiment.rb diff --git a/week8/exercises/couch.rb b/week8/exercises/couch.rb index a52eb35..0157b2c 100644 --- a/week8/exercises/couch.rb +++ b/week8/exercises/couch.rb @@ -1,7 +1,8 @@ class Couch - def initialize(pillows, cushions) + def initialize(pillows, cushions, cats) @pillows = pillows @cushions = cushions + @cats = cats end def pillow_colors @@ -12,7 +13,7 @@ def cushion_colors @cushions.join(", ") end - [:pillows, :cushions].each do |s| + [:pillows, :cushions, :cats].each do |s| define_method("how_many_#{s}") do instance_variable_get("@#{s}").count end diff --git a/week8/exercises/experiment.rb b/week8/exercises/experiment.rb new file mode 100644 index 0000000..c73f71f --- /dev/null +++ b/week8/exercises/experiment.rb @@ -0,0 +1,82 @@ +#!usr/bin/env ruby +require '/home/wsugg/.rvm/gems/ruby-1.9.3-p194/gems/ruby-audio-1.6.1/lib/ruby-audio.rb' + +class Object + def call_chain + "#{self}" + end +end + +class Animal + def call_chain + "Animal.#{super}" + end +end + +module NamedThing + def call_chain + "NamedThing.#{super}" + end +end + +module Speaker + def call_chain + "Speaker.#{super}" + end +end + +class Person < Animal + include NamedThing + include Speaker + def call_chain + "Person.#{super}" + end +end + +class Renee < Person +#include Beep + def call_chain + "Renee.#{super}" + super + end + def rbeep + #result = Beep::Sound.generate('beep -f 100') + puts result + end + +end + + +info = RubyAudio::SoundInfo.new :channels => 1, :samplerate => 48000, :format => RubyAudio::FORMAT_WAV|RubyAudio::FORMAT_PCM_16 +puts info + + +buf = RubyAudio::Buffer.float(1000) +snd.read(buf) #=> 1000 +snd.read(buf, 50) #=> 50 +=begin +snd = RubyAudio::Sound.open("snd.wav") +puts snd.info.channels #=> 2 +puts snd.info.samplerate #=> 48000 +snd.close=end + + +=begin +Samplerate = 8000 + +def beep(frequency, amplitude, duration) + + f = File.open("/dev/dsp", "w") + + wave = "" + + 0.step(duration, 1.0/Samplerate) do |t| + y = Math.sin(t * frequency) * 50 + 127; + wave << y.to_i.chr + end + + f.write(wave) +end + +beep(2000, 100, 1) +=end From 770ff3a949a2f7a0acbfd6ab09aa95365418c514 Mon Sep 17 00:00:00 2001 From: wsugg Date: Tue, 11 Dec 2012 18:21:49 -0800 Subject: [PATCH 08/14] adding week7 HW --- .../step_definitions/tic-tac-toe-steps.rb | 8 +- .../features/step_definitions/tic-tac-toe.rb | 116 ++++++++++++++++++ week7/homework/play_game.rb | 2 +- week8/exercises/couch.rb | 2 + 4 files changed, 123 insertions(+), 5 deletions(-) create mode 100644 week7/homework/features/step_definitions/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 b353120..db06d6d 100644 --- a/week7/homework/features/step_definitions/tic-tac-toe-steps.rb +++ b/week7/homework/features/step_definitions/tic-tac-toe-steps.rb @@ -35,7 +35,7 @@ Then /^the computer prints "(.*?)"$/ do |arg1| @game.should_receive(:puts).with(arg1) - @game.indicate_palyer_turn + @game.indicate_player_turn end Then /^waits for my input of "(.*?)"$/ do |arg1| @@ -43,7 +43,7 @@ @game.get_player_move end -Given /^it is the computer's turn$/ do +Given /^it is the computers turn$/ do @game = TicTacToe.new(:computer, :O) @game.current_player.should eq "Computer" end @@ -77,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 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..fde1610 --- /dev/null +++ b/week7/homework/features/step_definitions/tic-tac-toe.rb @@ -0,0 +1,116 @@ +module Symbols + SYMBOLS = [:X , :O] +end + +class TicTacToe + include Symbols + + attr_accessor = :player + + def initialize(*args_in) + @board = { + :A1 => :X, :A2 => :X, :A3 => "", + :B1 => "", :B2 => "", :B3 => :X, + :C1 => "", :C2 => "", :C3 => "" + } + @player = {:computer =>"Computer",:player => ""} + + puts "What is your name" + @player[:player] = gets.chop + welcome_player + + #puts " #{args_in}" + + if args_in[0] == :player + @current_player = @player[:player] + elsif args_in[0] == :computer + @current_player = @player[:computer] + end + + end + + def welcome_player + puts "Welcome #{@player[:player]}" + end + + + def current_player + puts "current is: #{@current_player}" + @current_player + end + + def player=(name) + @player[:player] = name + @current_player = @player[:player] + end + + def player + rand = Random.new + num = rand(max=2) + puts "num: #{num}" + + if num == 1 + @current_player = @player[:player] + else + @current_player = @player[:computer] + end + end + + def player_symbol + SYMBOLS[1] + end + + def computer_symbol + SYMBOLS[0] + end + + def indicate_player_turn + "#{@current_player}'s' Move" + end + + def get_player_move + players_move = gets + #puts "move: #{players_move} current: #{@current_player}" + if @current_player != @player[:computer] + @current_player = @player[:computer] + end + player_move + end + + def player_move + end + + def determine_winner + xs = [] + @board.select {|k,v| if v == :X; puts "k= #{k} v= #{v}" end} + xs = @board.sort + puts "xs: #{xs}" + #player_won = FALSE + end + + def open_spots + opens = {} + opens = @board.select{|k,v| if v == ""; opens.store(k,v) end} + puts "open spots are: #{opens}" + computer_move(opens) + + end + + def computer_move(opens) + + rand = Random.new + num = rand(max=3) + if num == 1 + letter = "A" + elsif num == 2 + letter = "B" + else num = 3 + letter = "C" + end + + opens.fetch(":#{letter}#{num}", "#{letter}#{num} sorry, not open") + + 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 diff --git a/week8/exercises/couch.rb b/week8/exercises/couch.rb index 0157b2c..120f312 100644 --- a/week8/exercises/couch.rb +++ b/week8/exercises/couch.rb @@ -1,3 +1,5 @@ + + class Couch def initialize(pillows, cushions, cats) @pillows = pillows From f68e59ef725277a30f8213849e60cb536011980d Mon Sep 17 00:00:00 2001 From: wsugg Date: Tue, 11 Dec 2012 18:31:58 -0800 Subject: [PATCH 09/14] commiting my tic tac toe file as orig --- .../step_definitions/tic-tac-toe-orig.rb | 118 ++++++++++++++++++ 1 file changed, 118 insertions(+) create mode 100644 week7/homework/features/step_definitions/tic-tac-toe-orig.rb diff --git a/week7/homework/features/step_definitions/tic-tac-toe-orig.rb b/week7/homework/features/step_definitions/tic-tac-toe-orig.rb new file mode 100644 index 0000000..8ddfcc6 --- /dev/null +++ b/week7/homework/features/step_definitions/tic-tac-toe-orig.rb @@ -0,0 +1,118 @@ +<<<<<<< HEAD +module Symbols + SYMBOLS = [:X , :O] +end + +class TicTacToe + include Symbols + + attr_accessor = :player + + def initialize(*args_in) + @board = { + :A1 => :X, :A2 => :X, :A3 => "", + :B1 => "", :B2 => "", :B3 => :X, + :C1 => "", :C2 => "", :C3 => "" + } + @player = {:computer =>"Computer",:player => ""} + + puts "What is your name" + @player[:player] = gets.chop + welcome_player + + #puts " #{args_in}" + + if args_in[0] == :player + @current_player = @player[:player] + elsif args_in[0] == :computer + @current_player = @player[:computer] + end + + end + + def welcome_player + puts "Welcome #{@player[:player]}" + end + + + def current_player + puts "current is: #{@current_player}" + @current_player + end + + def player=(name) + @player[:player] = name + @current_player = @player[:player] + end + + def player + rand = Random.new + num = rand(max=2) + puts "num: #{num}" + + if num == 1 + @current_player = @player[:player] + else + @current_player = @player[:computer] + end + end + + def player_symbol + SYMBOLS[1] + end + + def computer_symbol + SYMBOLS[0] + end + + def indicate_player_turn + "#{@current_player}'s' Move" + end + + def get_player_move + players_move = gets + #puts "move: #{players_move} current: #{@current_player}" + if @current_player != @player[:computer] + @current_player = @player[:computer] + end + player_move + end + + def player_move + end + + def determine_winner + xs = [] + @board.select {|k,v| if v == :X; puts "k= #{k} v= #{v}" end} + xs = @board.sort + puts "xs: #{xs}" + #player_won = FALSE + end + + def open_spots + opens = {} + opens = @board.select{|k,v| if v == ""; opens.store(k,v) end} + puts "open spots are: #{opens}" + computer_move(opens) + + end + + def computer_move(opens) + + rand = Random.new + num = rand(max=3) + if num == 1 + letter = "A" + elsif num == 2 + letter = "B" + else num = 3 + letter = "C" + end + + opens.fetch(":#{letter}#{num}", "#{letter}#{num} sorry, not open") + + end + + +end +======= From 0b015ff356c9f81e99e3dd99e3599427ec1c5a96 Mon Sep 17 00:00:00 2001 From: wsugg Date: Tue, 11 Dec 2012 18:45:34 -0800 Subject: [PATCH 10/14] renamed files --- .../step_definitions/tic-tac-toe-renee.rb | 130 ++++++++++ .../features/step_definitions/tic-tac-toe.rb | 244 +++++++++--------- 2 files changed, 246 insertions(+), 128 deletions(-) create mode 100644 week7/homework/features/step_definitions/tic-tac-toe-renee.rb diff --git a/week7/homework/features/step_definitions/tic-tac-toe-renee.rb b/week7/homework/features/step_definitions/tic-tac-toe-renee.rb new file mode 100644 index 0000000..fc6b221 --- /dev/null +++ b/week7/homework/features/step_definitions/tic-tac-toe-renee.rb @@ -0,0 +1,130 @@ +class TicTacToe + SYMBOLS = [:X,:O] + attr_accessor :player, :board + + def initialize(current_player=nil,player_sym=nil) + setup_board + @current_player = current_player || [:computer, :player].sample + choose_player_symbol(player_sym) + end + + def computer_symbol + @player_symbol[:computer] + end + + def player_symbol + @player_symbol[:player] + end + + def current_player + {:computer => "Computer", + :player => @player}[@current_player] + end + + def welcome_player + "Welcome #{@player}" + end + + def indicate_palyer_turn + puts "#{@player}'s Move:" + end + + def get_player_move + gets.chomp + end + + def player_move + move = get_player_move.to_sym + until open_spots.include?(move) + move = get_player_move.to_sym + end + @board[move] = player_symbol + @current_player = :computer + move + end + + def computer_move + move = get_computer_move + @board[move] = computer_symbol + @current_player = :player + move + end + + def get_computer_move + @board.select{|k,v| v.to_s.strip.empty?}.map{|k,v| k}.sample + end + + def current_state + row1 = "#{@board[:A1]}|#{@board[:A2]}|#{@board[:A3]}\n" + row2 = "#{@board[:B1]}|#{@board[:B2]}|#{@board[:B3]}\n" + row3 = "#{@board[:C1]}|#{@board[:C2]}|#{@board[:C3]}\n" + row1 + "-"*row1.size+"\n"+ + row2 + "-"*row2.size+"\n"+ + row3 + "-"*row3.size+"\n"+ + "******" + end + + def determine_winner + p_spots = @board.select{|k,v| v==player_symbol} + c_spots = @board.select{|k,v| v==computer_symbol} + + player_spots = p_spots.map{|k,v| {k[0].to_sym => k[1].to_i} } + computer_spots = c_spots.map{|k,v| {k[0].to_sym => k[1].to_i} } + @player_win = false + @computer_win = false + [:A, :B, :C].each do |l| + return if @player_win = player_spots.map{|i| i[l]}.reject{|f| f.nil?}.sort == [1,2,3] + return if @computer_win = computer_spots.map{|i| i[l]}.reject{|f| f.nil?}.sort == [1,2,3] + end + + [1,2,3].each do |l| + return if @player_win = player_spots.map{|i| i.invert[l]}.reject{|f| f.nil?}.sort == [:A,:B,:C] + return if @computer_win = computer_spots.map{|i| i.invert[l]}.reject{|f| f.nil?}.sort == [:A,:B,:C] + end + + return if @player_win = p_spots.keys.sort.reject{|r| ![:A1,:B2,:C3].include? r} == [:A1,:B2,:C3] + return if @player_win = p_spots.keys.sort.reject{|r| ![:A3,:B2,:C1].include? r} == [:A3,:B2,:C1] + return if @computer_win = c_spots.keys.sort.reject{|r| ![:A1,:B2,:C3].include? r} == [:A1,:B2,:C3] + return if @computer_win = c_spots.keys.sort.reject{|r| ![:A3,:B2,:C1].include? r} == [:A3,:B2,:C1] + end + + def player_won? + !!@player_win + end + + def computer_won? + !!@computer_win + end + + def draw? + !player_won? && !computer_won? + end + + def over? + player_won? || computer_won? || !spots_open? + end + + def spots_open? + !open_spots.empty? + end + + def open_spots + @board.select{|k,v| v.to_s.strip.empty?}.map{|k,v| k} + end + +private + def setup_board + @board = {:A1 => ' ', :A2 => ' ', :A3 => ' ', + :B1 => ' ', :B2 => ' ', :B3 => ' ', + :C1 => ' ', :C2 => ' ', :C3 => ' '} + end + + def choose_player_symbol(player_sym=nil) + player_sym ||= SYMBOLS.sample + @player_symbol = { + :computer => SYMBOLS.reject{|s| s==player_sym}.first, + :player => player_sym + } + end +end +>>>>>>> c73757fbda1d04b82c1b199154af7ea85aa86d79 diff --git a/week7/homework/features/step_definitions/tic-tac-toe.rb b/week7/homework/features/step_definitions/tic-tac-toe.rb index fc6b221..8ddfcc6 100644 --- a/week7/homework/features/step_definitions/tic-tac-toe.rb +++ b/week7/homework/features/step_definitions/tic-tac-toe.rb @@ -1,130 +1,118 @@ +<<<<<<< HEAD +module Symbols + SYMBOLS = [:X , :O] +end + class TicTacToe - SYMBOLS = [:X,:O] - attr_accessor :player, :board - - def initialize(current_player=nil,player_sym=nil) - setup_board - @current_player = current_player || [:computer, :player].sample - choose_player_symbol(player_sym) - end - - def computer_symbol - @player_symbol[:computer] - end - - def player_symbol - @player_symbol[:player] - end - - def current_player - {:computer => "Computer", - :player => @player}[@current_player] - end - - def welcome_player - "Welcome #{@player}" - end - - def indicate_palyer_turn - puts "#{@player}'s Move:" - end - - def get_player_move - gets.chomp - end - - def player_move - move = get_player_move.to_sym - until open_spots.include?(move) - move = get_player_move.to_sym - end - @board[move] = player_symbol - @current_player = :computer - move - end - - def computer_move - move = get_computer_move - @board[move] = computer_symbol - @current_player = :player - move - end - - def get_computer_move - @board.select{|k,v| v.to_s.strip.empty?}.map{|k,v| k}.sample - end - - def current_state - row1 = "#{@board[:A1]}|#{@board[:A2]}|#{@board[:A3]}\n" - row2 = "#{@board[:B1]}|#{@board[:B2]}|#{@board[:B3]}\n" - row3 = "#{@board[:C1]}|#{@board[:C2]}|#{@board[:C3]}\n" - row1 + "-"*row1.size+"\n"+ - row2 + "-"*row2.size+"\n"+ - row3 + "-"*row3.size+"\n"+ - "******" - end - - def determine_winner - p_spots = @board.select{|k,v| v==player_symbol} - c_spots = @board.select{|k,v| v==computer_symbol} - - player_spots = p_spots.map{|k,v| {k[0].to_sym => k[1].to_i} } - computer_spots = c_spots.map{|k,v| {k[0].to_sym => k[1].to_i} } - @player_win = false - @computer_win = false - [:A, :B, :C].each do |l| - return if @player_win = player_spots.map{|i| i[l]}.reject{|f| f.nil?}.sort == [1,2,3] - return if @computer_win = computer_spots.map{|i| i[l]}.reject{|f| f.nil?}.sort == [1,2,3] - end - - [1,2,3].each do |l| - return if @player_win = player_spots.map{|i| i.invert[l]}.reject{|f| f.nil?}.sort == [:A,:B,:C] - return if @computer_win = computer_spots.map{|i| i.invert[l]}.reject{|f| f.nil?}.sort == [:A,:B,:C] - end - - return if @player_win = p_spots.keys.sort.reject{|r| ![:A1,:B2,:C3].include? r} == [:A1,:B2,:C3] - return if @player_win = p_spots.keys.sort.reject{|r| ![:A3,:B2,:C1].include? r} == [:A3,:B2,:C1] - return if @computer_win = c_spots.keys.sort.reject{|r| ![:A1,:B2,:C3].include? r} == [:A1,:B2,:C3] - return if @computer_win = c_spots.keys.sort.reject{|r| ![:A3,:B2,:C1].include? r} == [:A3,:B2,:C1] - end - - def player_won? - !!@player_win - end - - def computer_won? - !!@computer_win - end - - def draw? - !player_won? && !computer_won? - end - - def over? - player_won? || computer_won? || !spots_open? - end - - def spots_open? - !open_spots.empty? - end - - def open_spots - @board.select{|k,v| v.to_s.strip.empty?}.map{|k,v| k} - end - -private - def setup_board - @board = {:A1 => ' ', :A2 => ' ', :A3 => ' ', - :B1 => ' ', :B2 => ' ', :B3 => ' ', - :C1 => ' ', :C2 => ' ', :C3 => ' '} - end - - def choose_player_symbol(player_sym=nil) - player_sym ||= SYMBOLS.sample - @player_symbol = { - :computer => SYMBOLS.reject{|s| s==player_sym}.first, - :player => player_sym - } - end + include Symbols + + attr_accessor = :player + + def initialize(*args_in) + @board = { + :A1 => :X, :A2 => :X, :A3 => "", + :B1 => "", :B2 => "", :B3 => :X, + :C1 => "", :C2 => "", :C3 => "" + } + @player = {:computer =>"Computer",:player => ""} + + puts "What is your name" + @player[:player] = gets.chop + welcome_player + + #puts " #{args_in}" + + if args_in[0] == :player + @current_player = @player[:player] + elsif args_in[0] == :computer + @current_player = @player[:computer] + end + + end + + def welcome_player + puts "Welcome #{@player[:player]}" + end + + + def current_player + puts "current is: #{@current_player}" + @current_player + end + + def player=(name) + @player[:player] = name + @current_player = @player[:player] + end + + def player + rand = Random.new + num = rand(max=2) + puts "num: #{num}" + + if num == 1 + @current_player = @player[:player] + else + @current_player = @player[:computer] + end + end + + def player_symbol + SYMBOLS[1] + end + + def computer_symbol + SYMBOLS[0] + end + + def indicate_player_turn + "#{@current_player}'s' Move" + end + + def get_player_move + players_move = gets + #puts "move: #{players_move} current: #{@current_player}" + if @current_player != @player[:computer] + @current_player = @player[:computer] + end + player_move + end + + def player_move + end + + def determine_winner + xs = [] + @board.select {|k,v| if v == :X; puts "k= #{k} v= #{v}" end} + xs = @board.sort + puts "xs: #{xs}" + #player_won = FALSE + end + + def open_spots + opens = {} + opens = @board.select{|k,v| if v == ""; opens.store(k,v) end} + puts "open spots are: #{opens}" + computer_move(opens) + + end + + def computer_move(opens) + + rand = Random.new + num = rand(max=3) + if num == 1 + letter = "A" + elsif num == 2 + letter = "B" + else num = 3 + letter = "C" + end + + opens.fetch(":#{letter}#{num}", "#{letter}#{num} sorry, not open") + + end + + end ->>>>>>> c73757fbda1d04b82c1b199154af7ea85aa86d79 +======= From bc64348e4328577931ef7a1bcae5bc0a000a810d Mon Sep 17 00:00:00 2001 From: wsugg Date: Tue, 11 Dec 2012 18:52:09 -0800 Subject: [PATCH 11/14] renaming files --- week7/homework/features/step_definitions/tic-tac-toe.rb | 4 ++-- week7/homework/features/support/env.rb | 0 2 files changed, 2 insertions(+), 2 deletions(-) create mode 100644 week7/homework/features/support/env.rb diff --git a/week7/homework/features/step_definitions/tic-tac-toe.rb b/week7/homework/features/step_definitions/tic-tac-toe.rb index 8ddfcc6..91bad3b 100644 --- a/week7/homework/features/step_definitions/tic-tac-toe.rb +++ b/week7/homework/features/step_definitions/tic-tac-toe.rb @@ -1,4 +1,4 @@ -<<<<<<< HEAD + module Symbols SYMBOLS = [:X , :O] end @@ -115,4 +115,4 @@ def computer_move(opens) end -======= + diff --git a/week7/homework/features/support/env.rb b/week7/homework/features/support/env.rb new file mode 100644 index 0000000..e69de29 From 0f62aeaa63917b3267ed5ccc1287a07024bb39ea Mon Sep 17 00:00:00 2001 From: wsugg Date: Tue, 11 Dec 2012 18:52:51 -0800 Subject: [PATCH 12/14] removing old files --- .../step_definitions/tic-tac-toe-orig.rb | 118 ------------------ 1 file changed, 118 deletions(-) delete mode 100644 week7/homework/features/step_definitions/tic-tac-toe-orig.rb diff --git a/week7/homework/features/step_definitions/tic-tac-toe-orig.rb b/week7/homework/features/step_definitions/tic-tac-toe-orig.rb deleted file mode 100644 index 8ddfcc6..0000000 --- a/week7/homework/features/step_definitions/tic-tac-toe-orig.rb +++ /dev/null @@ -1,118 +0,0 @@ -<<<<<<< HEAD -module Symbols - SYMBOLS = [:X , :O] -end - -class TicTacToe - include Symbols - - attr_accessor = :player - - def initialize(*args_in) - @board = { - :A1 => :X, :A2 => :X, :A3 => "", - :B1 => "", :B2 => "", :B3 => :X, - :C1 => "", :C2 => "", :C3 => "" - } - @player = {:computer =>"Computer",:player => ""} - - puts "What is your name" - @player[:player] = gets.chop - welcome_player - - #puts " #{args_in}" - - if args_in[0] == :player - @current_player = @player[:player] - elsif args_in[0] == :computer - @current_player = @player[:computer] - end - - end - - def welcome_player - puts "Welcome #{@player[:player]}" - end - - - def current_player - puts "current is: #{@current_player}" - @current_player - end - - def player=(name) - @player[:player] = name - @current_player = @player[:player] - end - - def player - rand = Random.new - num = rand(max=2) - puts "num: #{num}" - - if num == 1 - @current_player = @player[:player] - else - @current_player = @player[:computer] - end - end - - def player_symbol - SYMBOLS[1] - end - - def computer_symbol - SYMBOLS[0] - end - - def indicate_player_turn - "#{@current_player}'s' Move" - end - - def get_player_move - players_move = gets - #puts "move: #{players_move} current: #{@current_player}" - if @current_player != @player[:computer] - @current_player = @player[:computer] - end - player_move - end - - def player_move - end - - def determine_winner - xs = [] - @board.select {|k,v| if v == :X; puts "k= #{k} v= #{v}" end} - xs = @board.sort - puts "xs: #{xs}" - #player_won = FALSE - end - - def open_spots - opens = {} - opens = @board.select{|k,v| if v == ""; opens.store(k,v) end} - puts "open spots are: #{opens}" - computer_move(opens) - - end - - def computer_move(opens) - - rand = Random.new - num = rand(max=3) - if num == 1 - letter = "A" - elsif num == 2 - letter = "B" - else num = 3 - letter = "C" - end - - opens.fetch(":#{letter}#{num}", "#{letter}#{num} sorry, not open") - - end - - -end -======= From 0bc80a783495e411bcb4bfe80a993f612c965dde Mon Sep 17 00:00:00 2001 From: wsugg Date: Tue, 11 Dec 2012 23:46:14 -0800 Subject: [PATCH 13/14] added to hi.rb --- week10/class_materials/hi.rb | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/week10/class_materials/hi.rb b/week10/class_materials/hi.rb index 0a722c9..0ba77a2 100644 --- a/week10/class_materials/hi.rb +++ b/week10/class_materials/hi.rb @@ -1,4 +1,5 @@ require 'sinatra' +require "sinatra/reloader" get '/hi' do "Hello World!"+ @@ -7,5 +8,11 @@ get '/' do "Renée is the coolest teacher ever!"+ - "
Click Here" + "
Click Here"+ + "
click here for red text" +end + +get '/bold-text' do + "Red Bold Text"+ + "
Go Back" end From 8dd7961e5efd81ebabc1da29612926e8759ce252 Mon Sep 17 00:00:00 2001 From: wsugg Date: Wed, 12 Dec 2012 01:04:09 -0800 Subject: [PATCH 14/14] removing the puts so that 'Welcome Renee' passes --- week7/homework/features/step_definitions/tic-tac-toe-renee.rb | 2 +- week7/homework/features/step_definitions/tic-tac-toe.rb | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/week7/homework/features/step_definitions/tic-tac-toe-renee.rb b/week7/homework/features/step_definitions/tic-tac-toe-renee.rb index fc6b221..3aeca81 100644 --- a/week7/homework/features/step_definitions/tic-tac-toe-renee.rb +++ b/week7/homework/features/step_definitions/tic-tac-toe-renee.rb @@ -127,4 +127,4 @@ def choose_player_symbol(player_sym=nil) } end end ->>>>>>> c73757fbda1d04b82c1b199154af7ea85aa86d79 + diff --git a/week7/homework/features/step_definitions/tic-tac-toe.rb b/week7/homework/features/step_definitions/tic-tac-toe.rb index 91bad3b..75120ce 100644 --- a/week7/homework/features/step_definitions/tic-tac-toe.rb +++ b/week7/homework/features/step_definitions/tic-tac-toe.rb @@ -14,6 +14,7 @@ def initialize(*args_in) :B1 => "", :B2 => "", :B3 => :X, :C1 => "", :C2 => "", :C3 => "" } + @player = {:computer =>"Computer",:player => ""} puts "What is your name" @@ -31,7 +32,7 @@ def initialize(*args_in) end def welcome_player - puts "Welcome #{@player[:player]}" + "Welcome #{@player[:player]}" end