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

tic-tac-toe from week 7 for our final Craig Adams #117

Open
wants to merge 28 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
a10acde
added values for Craig
Oct 11, 2013
46cea6d
answered the questions so i'll better understand the material
Oct 12, 2013
d411a87
made the tests pass so i'll better understand how rspec works
Oct 12, 2013
ff2b085
merging
Oct 16, 2013
f782fc1
answered HW 2 questions to reinforce what I've learned
Oct 22, 2013
4ea453c
created module to contain methods for simon says
Oct 22, 2013
8221ead
tweeks to accomodate requiring simon_says.rb
Oct 22, 2013
6f0dc5d
Merge branch 'master' of github.com:UWE-Ruby/RubyFall2013
Oct 23, 2013
d5e576f
answered homework 3 questions to reinforce what I read
Oct 29, 2013
62ce8b3
updated one of the homework 3 answers
Oct 29, 2013
1f0d51d
homework 3, created calculator.rb to make spec pass
Oct 29, 2013
64e4570
Merge branch 'master' of github.com:UWE-Ruby/RubyFall2013
Oct 29, 2013
a5d0696
answered homework 4 questions to reinforce what I've read
Nov 6, 2013
605b2cb
created worker.rb for homework 4 to make the rspec pass
Nov 6, 2013
425801d
homework worker.rb added comments with prior attempts
Nov 6, 2013
17e9252
Merge branch 'master' of github.com:UWE-Ruby/RubyFall2013
Nov 6, 2013
7ebb76c
Merge branch 'master' of github.com:UWE-Ruby/RubyFall2013
Nov 6, 2013
a6e19e4
Merge branch 'master' of github.com:UWE-Ruby/RubyFall2013
Nov 13, 2013
45ecfc4
Merge branch 'master' of github.com:UWE-Ruby/RubyFall2013
Nov 20, 2013
dc63986
Merge branch 'master' of github.com:UWE-Ruby/RubyFall2013
Nov 23, 2013
b956587
answered questions and got pirate features to pass
Nov 26, 2013
294f148
Refined class file
Nov 26, 2013
ee122a3
Merge branch 'master' of github.com:UWE-Ruby/RubyFall2013
Nov 27, 2013
1060365
Merge branch 'master' of github.com:UWE-Ruby/RubyFall2013
Dec 4, 2013
b6fd233
Merge branch 'master' of github.com:UWE-Ruby/RubyFall2013
Dec 4, 2013
4d43e96
initial checkin, 31 steps passing
Dec 5, 2013
e86e7a1
initial commit
Dec 11, 2013
93436ab
minor modifications, but still no joy
Dec 11, 2013
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
33 changes: 33 additions & 0 deletions week1/homework/questions.txt
Original file line number Diff line number Diff line change
@@ -4,12 +4,45 @@ p.86-90 Strings (Strings section in Chapter 6 Standard Types)

1. What is an object?

An object has three components: a set of flags, some instance variables, and an associated class. In Ruby,
everthing is an object.

2. What is a variable?

A variable is use to keep track of objects. Each variable holds a reference to an object. Its type is
defined by the messages that the object referenced by the variable responds. There are different types of
variables like constant, global, instance, and class.

3. What is the difference between an object and a class?

Wow, that is a tought one. In some respects, there is no real difference between them because they are actually
the same thing at different points in time (taken from http://ruby.learncodethehardway.org/book/ex42.html). In
Ruby, everything is an object, hence there is no difference. With that said though, an object is a kind of class.
Just like a Salmon (object) is a kind of Fish (class).

A Ruby class is an object of class Class

4. What is a String?

A String is a sequence of characters. They normally hold printable characters, but they can also hold binary
data. Strings are objects of class String. Strings are often created using string literals, i.e. sequences
of characters between delimiters.

5. What are three messages that I can send to a string object? Hint: think methods

String#split
String#chomp
String#scan

6. What are two ways of defining a String literal? Bonus: What is the difference between the two?

Single quote
Double quote

The difference between them is significant. Using single quotes, you can escape things like single characters,
e.g. 'display one escape \\'. "\\" will be replaced with a single "\". Double quotes support lots of escape
sequences. For example, \n for newline character. Also you can imbed #{expr} in double quoted strings. This
provides the ability to perform math, e.g. "#{4*5/2}", repeat sets of characters, e.g. "#{'Ho! '*3}", or use global,
class, or instance variable, e.g. "#$SAFE".

Ruby does much more processing on the string while constructing the literal when using double quotes.
11 changes: 6 additions & 5 deletions week1/homework/strings_and_rspec_spec.rb
Original file line number Diff line number Diff line change
@@ -12,14 +12,15 @@
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 split on the . charater" do
pending
result = #do something with @my_string here
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
result = @my_string.split(".")
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"))'
@my_string.encoding.should eq (Encoding.find("UTF-8"))
end
end
end
35 changes: 35 additions & 0 deletions week2/homework/questions.txt
Original file line number Diff line number Diff line change
@@ -4,10 +4,45 @@ Sharing Functionality: Inheritance, Modules, and Mixins

1. What is the difference between a Hash and an Array?

A hash is an indexed collection of object references. You index a hash with object types like
symbols, strings, regular expressions, etc. When you store values in a hash, you supply two objects,
the index (or key) and the entry to be stored with that key, e.g. a['salmon'] = 'fish.' You can
retrieve an object by indexing the hash with the key value you used to store it, e.g. a['salmon'].

An array holds a collection of object references where each reference occupies a position in the array.
That position is identified by a non-negative integer index that starts with 0. When you store a single
value in an array, you supply the integer position and the object for that position, e.g. a[0] = 10. To
retrieve an object from the array, you supply its position, e.g. a[0]. Arrays can also be indexed by
supplying a pair of numbers [start, count] and a range where you provide a start and end positions. The
start and end positions are seperated by two or three dots.

2. When would you use an Array over a Hash and vice versa?

You would use an array over a hash when you wanted to treat the data as stacks, sets, queues, dequeues,
and FIFO queues. Arrays are ordered while Hashes are not.

You would use a hash over an array when you want to use an object as the index.

3. What is a module? Enumerable is a built in Ruby module, what is it?

A module is a way of grouping together methods, classes, and constraints. Modules give you two major
benefits: they provide a namespace and prevent name clashes and they support mixin facility. They also
define a namespace, a sandbox in which your methods and constraints can play without having to worry
about being stepped on by other methods and constraints. Modules eliminate the need for inheritance.

Enumerable is a standard mixin, that implements a number of methods in terms of the host class's each
method. An example of a mixin is inject. This method applies a function or operation to the first two
elements in the collection and then applied the operation to the result of this computation and to the
third element and so on until all elements in the collection have been used.

4. Can you inherit more than one thing in Ruby? How could you get around this problem?

No, Ruby is a single-inheritance language. Ruby classes can include the functionality of any number of
mixins. A mixin is like a partial class definition. This provides a controlled multiple-inheritance-like
capability.

5. What is the difference between a Module and a Class?

A Module can't have instances, but a Class can. A Module isn't a Class. A Class can contain Modules.
Modules can't be inherited, but Classes can. A module can be included in a class definition. When this
occurs, all of the module's instance methods are available as methods in the class.
23 changes: 23 additions & 0 deletions week2/homework/simon_says.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
module SimonSays

def SimonSays.echo(message)
@message = message
end

def SimonSays.shout(message)
@message = message.upcase
end

def SimonSays.repeat(message, repeats = 2)
Array.new(repeats, message).join(" ")
end

def SimonSays.start_of_word(message, index)
@message = message[0,index]
end

def SimonSays.first_word(message)
@message = message.split(" ")[0]
end

end
24 changes: 13 additions & 11 deletions week2/homework/simon_says_spec.rb
Original file line number Diff line number Diff line change
@@ -2,46 +2,48 @@
require "./simon_says.rb"

describe SimonSays do
include SimonSays # Hint: Inclusion is different than SimonSays.new (read about modules)
#include SimonSays # Hint: Inclusion is different than SimonSays.new (read about modules)

# Hint: We are just calling methods, we are not passing a message to a SimonSays object.


it "should echo hello" do
echo("hello").should == "hello"
SimonSays.echo("hello").should == "hello"
end

it "should echo bye" do
echo("bye").should == "bye"
SimonSays.echo("bye").should == "bye"
end

it "should shout hello" do
shout("hello").should == "HELLO"
SimonSays.shout("hello").should == "HELLO"
end

it "should shout multiple words" do
shout("hello world").should == "HELLO WORLD"
SimonSays.shout("hello world").should == "HELLO WORLD"
end

it "should repeat" do
repeat("hello").should == "hello hello"
SimonSays.repeat("hello").should == "hello hello"
end

it "should repeat a number of times" do
repeat("hello", 3).should == "hello hello hello"
SimonSays.repeat("hello", 3).should == "hello hello hello"
end

it "should return the first letter" do
start_of_word("hello", 1).should == "h"
SimonSays.start_of_word("hello", 1).should == "h"
end

it "should return the first two letters" do
start_of_word("Bob", 2).should == "Bo"
SimonSays.start_of_word("Bob", 2).should == "Bo"
end

it "should tell us the first word of 'Hello World' is 'Hello'" do
first_word("Hello World").should == "Hello"
SimonSays.first_word("Hello World").should == "Hello"
end

it "should tell us the first word of 'oh dear' is 'oh'" do
first_word("oh dear").should == "oh"
SimonSays.first_word("oh dear").should == "oh"
end
end
20 changes: 20 additions & 0 deletions week3/homework/calculator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
class Calculator

def sum(sum_arg)
sum_arg.inject(0) { |sum, a| sum + a}
end

def multiply(*product_arg)
product_arg.flatten.inject { |product, a| product * a}
end

def pow(base,power)
(base ** power)
end

def fac(fact)
return 1 if fact.zero?
fact.downto(1).inject { | product, a | product * a}
end

end
11 changes: 6 additions & 5 deletions week3/homework/calculator_spec.rb
Original file line number Diff line number Diff line change
@@ -36,10 +36,12 @@
end
end

it "raises one number to the power of another number" do
p = 1
32.times{ p *= 2 }
@calculator.pow(2,32).should eq p
describe "#power" do
it "raises one number to the power of another number" do
p = 1
32.times{ p *= 2 }
@calculator.pow(2,32).should eq p
end
end

# http://en.wikipedia.org/wiki/Factorial
@@ -64,5 +66,4 @@
end

end

end
43 changes: 43 additions & 0 deletions week3/homework/questions.txt
Original file line number Diff line number Diff line change
@@ -6,10 +6,53 @@ Please Read:

1. What is a symbol?

A symbol is an identifier that corresponds to a string of characters, often a
name. A symbol is constructed for a name by proceeding the name with a colon.
A symbol can be constructed for an arbitrary string by proceeding a string
literal with a colon. A particular string or name will always generate the
same symbol, regardless of how the name is used within the program. To create
a symbol, you can use the %s delimited notation.

2. What is the difference between a symbol and a string?

A string can be changed while a symbol can not. A string is mutable. When a
new string is created, it creates a new object with a new object id. The same
symbol on the other hand, will always use the same object id.

A great discuss of the differences can be found here:
http://www.robertsosinski.com/2009/01/11/the-difference-between-ruby-symbols-and-strings

3. What is a block and how do I call a block?

A block is a chunk of code enclosed between either braces or the keywords do
and end. Typically, braces are used for blocks that fit on one line and do/end
keywords are used when the block spans multiple lines. A block can be thought
of somewhat like the body of an anonymous method. A block can also take parameters.
Those parameters appear at the beginning of the block between to vertical bars.
The body of the block is not executed when Ruby first sees it. It is saved away
to be called later.

A block is called by a method that proceeds the block. For example, lets say you
have an array ["a", "b", "c"]. A block could be called by the "each" method once
for each element in the array. Each element would be passed to the block via a
parameter, e.g. ["a", "b", "c"].each do | letter |.

4. How do I pass a block to a method? What is the method signature?

A block may be invoked within a method using the yield statement. When yield
is executed, it invokes the code in the block.

The method signature look like this:

def hi_there
yield
end

hi_there {"Hi there!"}

5. Where would you use regular expressions?

Regular expressions are great for testing a string to see if it matches a pattern.
They can be used to extract from a string the sections that match all or part of a
pattern. They can also be used to change the string, replacing parts that match a
pattern.
42 changes: 42 additions & 0 deletions week4/exercises/worker.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# first attempt that works
#class Worker
# def self.work(loop_count = 0)
# if loop_count != 0
# for i in 1...loop_count
# yield
# end
# end
# yield
# end
#end
#
# 2nd attempt a little better
#class Worker
# def self.work(loop_count = 0)
# i = 1
# begin
# my_test = yield
# i += 1
# end until i > loop_count
# return my_test
# end
#end
#
# 3rd attempt a little different
#class Worker
# def self.work(loop_count = 1)
# i = 0
# while i < loop_count
# my_test = yield
# i += 1
# end
# return my_test
# end
#end

#oh, best of all
class Worker
def self.work(loop_count = 1)
(0..loop_count).inject {yield}
end
end
32 changes: 32 additions & 0 deletions week4/homework/questions.txt
Original file line number Diff line number Diff line change
@@ -3,7 +3,39 @@ Chapter 10 Basic Input and Output
The Rake Gem: http://rake.rubyforge.org/

1. How does Ruby read files?

Ruby defines a single base class called IO. This class is used to handle reading files in addition
to writing files. IO is subclassed by classes File and BasicSocket. To read a file in Ruby, you'll
read from an IO stream. To open the stream, you could invoke File.open with the filename and the file
mode string of 'r'.

2. How would you output "Hello World!" to a file called my_output.txt?

hello.rb
File.open("my_output.txt", "w") do | myfile |
myfile.puts "Hello World!"
end

ruby hello.rb

3. What is the Directory class and what is it used for?

The directory class is 'Dir'. Objects of class Dir are directory streams representing directories in
the underlying file system. They provide a variety of ways to list directories and their contents. There
are a number of methods that can be used to make and delete directories, change to a directory, or open
a directory to read its content.

4. What is an IO object?

An IO object is a bidirectional channel between a Ruby program and some external resource.

5. What is rake and what is it used for? What is a rake task?

Rake is a simple Ruby build program similar to make. It is an internal DLS or Domain Specific Language
using Ruby. It is used to manage software tasks. It allows you to specify tasks and describe dependencies.
It also allows you to group tasks in a namespace.

A task is made up of a chunk of Ruby code. It begins with the keyword "task" and is followed by a symbol
which represents the name of the task. That is followed by a list of pre-requisite task(s) and a do/end
block of Ruby code. There are two types of tasks; regular and file. A task performs a specific function
in the build process.
10 changes: 10 additions & 0 deletions week7/homework/features/step_definitions/pirate_translator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class PirateTranslator

def initialize
@pirate_string = "Ahoy Matey\n Shiber Me Timbers You Scurvey Dogs!!"
end

def send(*args)
return @pirate_string
end
end
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require 'rspec/mocks/standalone'
require 'rspec/expectations'

Given /^I start a new Tic\-Tac\-Toe game$/ do
@game = TicTacToe.new
end
229 changes: 229 additions & 0 deletions week7/homework/features/step_definitions/tic-tac-toe.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,229 @@
class TicTacToe

include Enumerable

SYMBOLS = [:X, :O]

attr_accessor :player, :computer, :board, :outside_player

def initialize player = "", symbol = ""
@player = player
@outside_player = false
@outside_player = true if @player != ""
@symbol = symbol

@board = {
A1:" ",A2:" ",A3:" ",
B1:" ",B2:" ",B3:" ",
C1:" ",C2:" ",C3:" "
}

@winning_moves = [
[:A1, :B1, :C1],
[:A2, :B2, :C2],
[:A3, :B3, :C3],

[:A1, :A2, :A3],
[:B1, :B2, :B3],
[:C1, :C2, :C3],

[:A1, :B2, :C3],
[:A3, :B2, :C1]
]
@@over = false
@draw = false
@player_won = false
@computer_won = false
@spots_are_open = true
end

# Welcome and set opponents symbols
def welcome_player
@@player_new = true
@@contestants = { :player => @player, :computer => "Computer" }
@@the_players = @@contestants.values
# newly added
player_symbol
computer_symbol
@the_sybmols = { :player => @a,
:computer => @b
}
puts
return "Welcome #{@player}"
end

# Determine who the current player is
def current_player
if @player.is_a? Symbol
@player = @@contestants[@player]
end
if @outside_player == false
if @@player_new
new_player = @@contestants.values.sample
@@player_new = false
@player = new_player
else
next_player = @@the_players.reject{|i| i == @player}.join
@player = next_player
end
else
return @player
end
end

# Notify it is the player's turn
def indicate_palyer_turn
return "#{@player}\'s Move:"
end

# Set player symbol
def player_symbol
if @symbol == ""
@a = SYMBOLS.sample
if @a.is_a? Symbol
@a = @a.to_sym
end
return @a
else
puts "the symbol: \"#{@symbol}\""
return @symbol
end
end

# Set computer symbol
def computer_symbol
@b = SYMBOLS.reject{ |symbols| symbols == @a}.first
if @b.is_a? Symbol
@b = @b.to_sym
end
return @b
end

def over?
return @@over
end

# Get players input
def get_player_move
printf "\n#{@player}\'s Move:"
@the_move = gets.chomp.to_sym
check_move
return @the_move
end

# Player makes a move
def player_move
get_player_move
@board[@the_move] = @a
return @the_move
end

# Computer makes a move
def computer_move
cm_open = self.open_spots
cm_ramdom = cm_open.sample
@board[cm_ramdom.to_sym] = @b
printf "\nComputer chose: #{cm_ramdom}\n\n"
return cm_ramdom
end

# See if the position is already taken
def check_move
if @board[@the_move] != " "
puts "Position already taken, choose another"
get_player_move
end
end

# Draw the board
def current_state
puts " A B C\n"
puts " 1 #{@board[:A1]} | #{@board[:B1]} | #{@board[:C1]} "
puts " --- --- ---"
puts " 2 #{@board[:A2]} | #{@board[:B2]} | #{@board[:C2]} "
puts " --- --- ---"
puts " 3 #{@board[:A3]} | #{@board[:B3]} | #{@board[:C3]} \n"
end

# Determine who the winner is
def determine_winner
if @player == "Computer" || @player == :computer
sym_to_check = @b
else
sym_to_check = @a
end
get_moves_array = @board.select{|k, v| v == sym_to_check}.keys

count_winning_sets = 0
count_my_moves = 0
count_success = 0
a_winner = false

while count_winning_sets < @winning_moves.length
while count_my_moves < get_moves_array.length
if @winning_moves[count_winning_sets].include? get_moves_array[count_my_moves]
count_success += 1
end
if count_success == 3
a_winner = true
count_my_moves = get_moves_array.length
count_winning_sets = @winning_moves.length
end
count_my_moves += 1
end
count_success = 0
count_winning_sets += 1
count_my_moves = 0
end

# Set the winner
if a_winner == true
if @player == "Computer" || @player == :computer
@computer_won = true
else
@player_won = true
end
@@over = true
end
check_open = spots_open?
end

# Check for open spots
def spots_open?
spots_hash = @board.reject{|k, v| v != " "}
if spots_hash.length == 0
@spots_are_open = false
@draw = true
@@over = true
end
return @spots_are_open
end

# Neither won
def draw?
@draw
end

# Player won
def player_won?
@player
end

# Computer won
def computer_won?
@player
end

# Determine where the open spots are
def open_spots
spots_hash = @board.reject{|k, v| v != " "}
if spots_hash.length == 0
@spots_are_open = false
@draw = true
@@over = true
else
spots_array = spots_hash.keys
return spots_array
end
end
end
48 changes: 47 additions & 1 deletion week7/homework/questions.txt
Original file line number Diff line number Diff line change
@@ -3,7 +3,53 @@ 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?

If a method is a called and it doesn't exist in receiver's object class hierarchy,
Ruby invokes a method called method_missing on the original object. The default
behavior is to report an error and terminate the program. Since missing_method is
simply a Ruby method, you can override it in your own class to handle calls in an
application specific way.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very good.


2. What is an Eigenclass and what is it used for? Where Do Singleton methods live?

An Eigenclass is an anonymous class created to hold an object's singleton methods.
This anonymous class assumes the role of the object's class and the original class
is re-designated as the superclass of that anonymous class. The normal method lookup
pattern is unaltered. Both the singleton methods and the instance methods will be
found along the method lookup path when the object receives a method call.

Singleton methods are methods that you define that are specific to a particular object.
They live in the Singleton class or Eigenclass.

3. When would you use DuckTypeing? How would you use it to improve your code?

In DuckTyping, an object's type is defined by what it can do, not by what it is.
Ruby is less concerned with the class of an object and more concerned with what
methods can be called on it and what operations it can perform. With this in mind,
you should use DuckTyping all the time.

By using DuckTyping, you would write less and more efficient code. Since you won't
be checking the class of an object or type of an arguement, the method call will just
work or it will throw an exception.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Excellent!


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 doesn't deal with an individual instance of a class. An instance
method only works with an instance. You have to create a new instance to use
them. Hence, class methods can only be called on classes and instance methods
can only be called on an instance of a class.

instance_eval and class_eval differ in the way they setup the environment for the
method definition. class_eval sets things up as if you were in the body of a class
definition, so method definitions define instance methods. instance_eval acts as if
you were working inside a singleton call of self. Any methods defined would become
class methods. class_eval defines instance methods whereas instance_eval defines
class methods.

5. What is the difference between a singleton class and a singleton method?

A singleton class is an anonymous class. When you add a method to a specific object
Ruby inserts a new anonymous class into the inheritance hierarchy as a container to
hold special methods unique only to that object. The singleton class has no name
and is not accessible through a constant like other classes. A singleton method
is a method definition that only exists for a single object and not a class of objects.