Skip to content
This repository was archived by the owner on Dec 1, 2021. It is now read-only.

Week10 tic-tac-toe #183

Open
wants to merge 22 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@ test/version_tmp
tmp
*.*~
*~
./week1
./week2
./week3
./midterm
./week4
*/Fall2012RubyMidTerm/*


# YARD artifacts
.yardoc
Expand Down
3 changes: 1 addition & 2 deletions week1/exercises/roster.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ Karen Keasler, [email protected], github.com/irissilvermoon, irissilvermoon, keas
Bryan Williams, [email protected], tallbryan, no twitter, [email protected]
Serene C., [email protected], serened, @grrlcoder, [email protected]
Peter E. Granger, [email protected], evac156, N/A, N/A
Price Hardman, [email protected], PriceHardman
Will Sugg, [email protected], wsugg, , [email protected]
Nicole Lewis, [email protected], nelewis, nil, [email protected]
Sol Wagner, [email protected], Soladin, n/a, [email protected]

18 changes: 10 additions & 8 deletions week1/exercises/rspec_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down
30 changes: 30 additions & 0 deletions week1/homework/questions.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,35 @@ 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?
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
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 = <<END_OF_STRING
line 1 of my string
line 2 of my string
line 3 of my string
<<END_OF_STRING

Bonus: What is the difference between the two?

An object is a representation in memory of a specific concept or thing that the Ruby interpreter knows about.

2. What is a variable?
Expand All @@ -22,3 +51,4 @@ split - returns an array of strings made up of the original string separated on
6. What are two ways of defining a String literal? Bonus: What is the difference between the two?
Single quotes ex: '' and Double quotes ex: "". The single qoutes allow for 2 escape characters: \' and \\ . The double qouted string literal allows for many different escaped special characters (like \n is a line break) and allows for string interpolation, or the injection of evaluated Ruby code into the string ex: "Hello #{my_name}". The single qouted string takes up much less memory than a doulbe qouted string with interpolation. Without interpolation, both are about the same.


19 changes: 19 additions & 0 deletions week1/homework/strings_and_rspec_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,33 @@
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(66).characters
end

it "should be able to split on the . charater" do
#pending
result = @my_string.split(/\./) #do something with @my_string here
result.should have(2).items
#just to check what the 2 items are print them out to stdout.
puts("\n")
puts(result[0])
puts(result[1])

it "should be able to count the charaters" do
@my_string.should have(@my_string.size).characters
end
it "should be able to split on the . charater" do
result = @my_string.split('.')
result.should have(2).items
end

it "should be able to give the encoding of the string" do

#pending

@my_string.encoding.should eq (Encoding.find("UTF-8"))
end
end
Expand Down
9 changes: 8 additions & 1 deletion week10/class_materials/hi.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'sinatra'
require "sinatra/reloader"

get '/hi' do
"<font color='pink'>Hello World!</font>"+
Expand All @@ -7,5 +8,11 @@

get '/' do
"<font color='green'>Ren&eacute;e</font> is the coolest teacher ever!"+
"<br/><a href='/hi'>Click Here</a>"
"<br/><a href='/hi'>Click Here</a>"+
"<br><a href='/bold-text'>click here for red text</a>"
end

get '/bold-text' do
"<font color='Red'><b>Red Bold Text</b></font>"+
"<br/><a href='/'>Go Back</a>"
end
6 changes: 4 additions & 2 deletions week6/class_materials/test_gem/Rakefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
require 'rspec/core/rake_task'

RSpec::Core::RakeTask.new('test')

task :default => :test
Rspec::Core::RakeTask.new('spec')

task :default => :spec

5 changes: 5 additions & 0 deletions week6/class_materials/test_gem/bin/test_gem.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!usr/bin/env ruby

puts ""Hello Word again"

puts ARGV[0]
3 changes: 2 additions & 1 deletion week6/class_materials/test_gem/lib/test_gem.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

puts "Hello World!"
class Hello
end
end
1 change: 1 addition & 0 deletions week6/class_materials/test_gem/spec/test_gem_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require 'test_gem'


describe "test_gem" do
it "Should make a new Hello" do
h = Hello.new
Expand Down
3 changes: 2 additions & 1 deletion week6/class_materials/test_gem/test_gem.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ Gem::Specification.new do |s|
s.email = '[email protected]'
s.homepage = 'http://rubygems.org/gems/test_gem'
s.files = ["lib/test_gem.rb"]
s.executables << 'test_gem'
s.executables << "test_gem.rb"

end
130 changes: 130 additions & 0 deletions week7/homework/features/step_definitions/tic-tac-toe-renee.rb
Original file line number Diff line number Diff line change
@@ -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

8 changes: 4 additions & 4 deletions week7/homework/features/step_definitions/tic-tac-toe-steps.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,15 @@

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|
@game.should_receive(:gets).and_return(arg1)
@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
Expand Down Expand Up @@ -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
Expand Down
Loading