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

tic tac toe #136

Open
wants to merge 9 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
10 changes: 10 additions & 0 deletions week1/homework/questions.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,23 @@ Chapter 3 Classes, Objects, and Variables
p.86-90 Strings (Strings section in Chapter 6 Standard Types)

1. What is an object?
A instance or 'thing' of any type that is defined in memory within the context of the program

2. What is a variable?
A name that is given to a specific object

3. What is the difference between an object and a class?
An object is a specific instance of a class. A class may have specific attributes that are themselves objects of another class.

4. What is a String?
A string is one or more characters. A string object is an object that is a member of the string class.

5. What are three messages that I can send to a string object? Hint: think methods
length (return length of string)
index (return first character location of a hit on a substring or regex pattern)
encode (convert character encoding)

6. What are two ways of defining a String literal? Bonus: What is the difference between the two?
single (') and double (") quotes...
single quotes don't interpolate variables and only require escaping the single-quote(') or backslash(\)
double quotes allow variable interpolation and have many more 'special' characters that either require escaping or that can be created through the use of backslash(\)
9 changes: 5 additions & 4 deletions week1/homework/strings_and_rspec_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 count the charaters" do
@my_string.size.should eq 66
end
it "should be able to split on the . charater" do
pending
result = #do something with @my_string here
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
Expand Down
13 changes: 13 additions & 0 deletions week2/homework/questions.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,24 @@ Containers, Blocks, and Iterators
Sharing Functionality: Inheritance, Modules, and Mixins

1. What is the difference between a Hash and an Array?
An array is a collection of objects that are indexed by their order in memory and will therefore be returned in a predictable order
A hash is a collection of objects indexed by an arbitrary hash key. Items may be returned in an unpredictable order

2. When would you use an Array over a Hash and vice versa?
An array is ideal for creating a push/pop stack and anytime an integer indexed key makes logical sense.
Arrays are also generally more memory efficient than hashes, so anytime memory limitations are a concern.
Otherwise, hashes are generally more flexible and lookup speeds are generally faster.

3. What is a module? Enumerable is a built in Ruby module, what is it?
Modules act as libraries and are a way to create methods/functions that will be used in multiple classes.


4. Can you inherit more than one thing in Ruby? How could you get around this problem?
No, Ruby does not have multiple inheritance.
To get around the problem 'include' can be used to mixin multiple modules into a single class.

5. What is the difference between a Module and a Class?
Generally, a module is about methods and a class is about objects...
Objects can be instances of a class, modules cannot be instantiated.
Class can inherit from other classes but cannot be 'included' in a class.
Modules have no pure inheritance, but can be 'included' in other classes and modules (using include)
21 changes: 21 additions & 0 deletions week2/homework/simon_says.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
module SimonSays
def echo(input)
input
end

def shout(input)
input.upcase
end

def repeat(input, times=2)
((input + ' ') * times).chop
end

def start_of_word(input,last)
input[0...last]
end

def first_word(input)
input.split(' ')[0]
end
end
19 changes: 19 additions & 0 deletions week3/homework/calculator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class Calculator

def sum (input)
input.inject(0,:+)
end

def multiply (*input)
input.flatten.inject(:*)
end

def pow(base,exp)
(1...exp).to_a.inject(base){|result| result*=base}
end

def fac(n)
(1..n).to_a.inject(1){|result,i| result*=i}
end

end
14 changes: 14 additions & 0 deletions week3/homework/questions.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,25 @@ Please Read:
- Chapter 22 The Ruby Language: basic types (symbols), variables and constants

1. What is a symbol?
A symbol is a unique, fixed static name

2. What is the difference between a symbol and a string?
A string is an object and the character contents of that object can be changed at any time.
Multiple string object instances can contain the same string value.
A symbol is a collection of characters (like a string), but the name and value of a symbol are the same, they cannot be changed, and are always unique.
Every reference to a specific symbol points to the same memory object while individual references to a specific string value point to individual in-memory string object instances.

3. What is a block and how do I call a block?
A block is an unnamed function or section of code which can be called with the yield function.

4. How do I pass a block to a method? What is the method signature?
A block is delineated by a pair of curly-braces {} or by do/end keywords.
A block is passed to a method by placing the block immediately after the method call. All methods receive blocks, which can be called with yield.
A method signature is part of the method declaration. It is the combination of the method name and the parameter list.
Blocks can be defined in the method signature by using the ampersand & prefix and then accessed within the method by using the 'call' method.
Performance is generally better using 'yield' vs 'call' so method signature block declaration is generally only used when necessary.

5. Where would you use regular expressions?
Regexes are used to match (and/or replace/extract) potentially complex pattern conditions within text.
Regexes are generally less efficient than simpler equality matches, and should thus be reserved for situations where they are required.

11 changes: 11 additions & 0 deletions week4/homework/questions.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,18 @@ Chapter 10 Basic Input and Output
The Rake Gem: http://rake.rubyforge.org/

1. How does Ruby read files?
Files are read using the Ruby built-in File class which is a subclass of the IO class

2. How would you output "Hello World!" to a file called my_output.txt?
File.open('my_output.txt', 'w+') {|fileout| fileout.puts "Hello World!"}

3. What is the Directory class and what is it used for?
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.

4. What is an IO object?
An IO object is any instance of an IO class. Since the File class is a subclass of IO, all File objects are IO objects.

5. What is rake and what is it used for? What is a rake task?
rake = ruby make
It is a build tool to manage dependencies. They are used wherever make files would normally be used but use ruby syntax instead of make syntax
A rake task is a logical group of ruby statements. Tasks can have prerequisites and depend on other tasks. In this manner, tasks can be used to group other tasks into sequences.
108 changes: 108 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,108 @@
class TicTacToe
attr_accessor :board, :player, :player_symbol, :computer_symbol

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

def initialize (player=[:player, :computer].sample, symbol=[:X,:O].sample)
@player = ''
@board = {
:A1 => :' ', :A2 => :' ', :A3 => :' ',
:B1 => :' ', :B2 => :' ', :B3 => :' ',
:C1 => :' ', :C2 => :' ', :C3 => :' '
}
@winner = nil
@next_turn = player
if symbol == :X
other_symbol=:O
else
other symbol = :X
end
if player == :player
@player_symbol = symbol
@computer_symbol = other_symbol
else
@player_symbol = other_symbol
@computer_symbol = symbol
end
end

def current_player
if @next_turn == :computer
return 'Computer'
else
@player
end
end

def welcome_player
puts "Welcome #{@player}"
end

def indicate_palyer_turn
puts "#{@player}'s Move:"
end

def open_spots
@board.select {|k,v| v==:' '}.keys
end

def spots_open?
if self.open_spots.length > 0
true
else
false
end
end

def get_player_move
player_move = gets.chomp
until self.open_spots.include?(player_move)
puts "That spot is taken."
self.indicate_palyer_turn
player_move=gets.chomp
end
@board[player_move => @player_symbol]
@next_turn = :computer
end

def computer_move
@board[self.open_spots.sample => @computer_symbol]
@next_turn = :player
end

def determine_winner
@winner = :player if board.select {|k,v| v==@player_symbol}.keys.permutation(3).any? {|combo| @WINNING_COMBOS.include?(combo)}
@winner = :computer if board.select {|k,v| v==@player_symbol}.keys.permutation(3).any? {|combo| @WINNING_COMBOS.include?(combo)}
end

def player_won?
if @winner == :player
true
else false
end
end

def computer_won?
if @winner == :computer
true
else false
end
end

def over?
if self.spots_open? == false or @winner
true
else
false
end
end

def draw?
if @winner.nil? and self.over?
true
else
false
end
end

end