This repository was archived by the owner on Dec 1, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 29
tic-tac-toe from week 7 for our final Craig Adams #117
Open
ghost
wants to merge
28
commits into
UWE-Ruby:master
Choose a base branch
from
unknown repository
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+540
−22
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
46cea6d
answered the questions so i'll better understand the material
d411a87
made the tests pass so i'll better understand how rspec works
ff2b085
merging
f782fc1
answered HW 2 questions to reinforce what I've learned
4ea453c
created module to contain methods for simon says
8221ead
tweeks to accomodate requiring simon_says.rb
6f0dc5d
Merge branch 'master' of github.com:UWE-Ruby/RubyFall2013
d5e576f
answered homework 3 questions to reinforce what I read
62ce8b3
updated one of the homework 3 answers
1f0d51d
homework 3, created calculator.rb to make spec pass
64e4570
Merge branch 'master' of github.com:UWE-Ruby/RubyFall2013
a5d0696
answered homework 4 questions to reinforce what I've read
605b2cb
created worker.rb for homework 4 to make the rspec pass
425801d
homework worker.rb added comments with prior attempts
17e9252
Merge branch 'master' of github.com:UWE-Ruby/RubyFall2013
7ebb76c
Merge branch 'master' of github.com:UWE-Ruby/RubyFall2013
a6e19e4
Merge branch 'master' of github.com:UWE-Ruby/RubyFall2013
45ecfc4
Merge branch 'master' of github.com:UWE-Ruby/RubyFall2013
dc63986
Merge branch 'master' of github.com:UWE-Ruby/RubyFall2013
b956587
answered questions and got pirate features to pass
294f148
Refined class file
ee122a3
Merge branch 'master' of github.com:UWE-Ruby/RubyFall2013
1060365
Merge branch 'master' of github.com:UWE-Ruby/RubyFall2013
b6fd233
Merge branch 'master' of github.com:UWE-Ruby/RubyFall2013
4d43e96
initial checkin, 31 steps passing
e86e7a1
initial commit
93436ab
minor modifications, but still no joy
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
week7/homework/features/step_definitions/pirate_translator.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
229 changes: 229 additions & 0 deletions
229
week7/homework/features/step_definitions/tic-tac-toe.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
|
||
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. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Very good.