This repository has been 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
final added #111
Open
stevenbxu
wants to merge
6
commits into
UWE-Ruby:master
Choose a base branch
from
stevenbxu:master
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.
Open
final added #111
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
cb46f6e
homework 1
stevenbxu 1c68cd0
adding homework for week2
stevenbxu 44f2d8c
week 3 homework
stevenbxu 1ed72b4
added week4 homework
stevenbxu 1317f1f
week 7 hw added
stevenbxu 5746f1b
tictactoe.rb added for final
stevenbxu 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 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,127 @@ | ||
class TicTacToe | ||
SYMBOLS = [:X,:O] | ||
attr_accessor :player, :board | ||
|
||
private | ||
def board_constructor | ||
@board = {:A1 => ' ', :A2 => ' ', :A3 => ' ', | ||
:B1 => ' ', :B2 => ' ', :B3 => ' ', | ||
:C1 => ' ', :C2 => ' ', :C3 => ' '} | ||
end | ||
|
||
def player_is(player_sym=nil) | ||
player_sym ||= SYMBOLS.sample | ||
@player_symbol = { | ||
:computer => SYMBOLS.reject{|s| s==player_sym}.first, | ||
:player => player_sym | ||
} | ||
end | ||
|
||
public | ||
def initialize(current_player=nil,player_sym=nil) | ||
board_constructor | ||
@current_player = current_player || [:computer, :player].sample | ||
player_is(player_sym) | ||
@player_win = false | ||
@computer_win = false | ||
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_player_turn | ||
puts "#{@player}'s Move:" | ||
end | ||
|
||
def get_player_move | ||
gets.chomp.capitalize | ||
end | ||
|
||
def player_move | ||
move = get_player_move.to_sym | ||
until open_spots.include?(move) | ||
puts "Please select another location." | ||
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?}.collect{|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"+ | ||
"+"*row1.size | ||
end | ||
|
||
def determine_winner | ||
p = @board.select{|k,v| v==player_symbol} | ||
c = @board.select{|k,v| v==computer_symbol} | ||
|
||
played = p.collect{|k,v| {k[0].to_sym=>k[1].to_i}} | ||
computed = c.collect{|k,v| {k[0].to_sym=>k[1].to_i}} | ||
[:A, :B, :C].each do |l| | ||
return if @player_win = played.collect{|i| i[l]}.reject{|f| f.nil?}.sort == [1,2,3] | ||
return if @computer_win = computed.collect{|i| i[l]}.reject{|f| f.nil?}.sort == [1,2,3] | ||
end | ||
[1,2,3].each do |l| | ||
return if @player_win = played.collect{|i| i.invert[l]}.reject{|f| f.nil?}.sort == [:A,:B,:C] | ||
return if @computer_win = computed.collect{|i| i.invert[l]}.reject{|f| f.nil?}.sort == [:A,:B,:C] | ||
end | ||
return if @player_win = p.keys.sort.reject{|r| ![:A1,:B2,:C3].include? r} == [:A1,:B2,:C3] | ||
return if @player_win = p.keys.sort.reject{|r| ![:A3,:B2,:C1].include? r} == [:A3,:B2,:C1] | ||
return if @computer_win = c.keys.sort.reject{|r| ![:A1,:B2,:C3].include? r} == [:A1,:B2,:C3] | ||
return if @computer_win = c.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?}.collect{|k,v| k} | ||
end | ||
end |
This file contains 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 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 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,32 @@ | ||
Please Read The Chapters on: | ||
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 object references, indexed | ||
by integers. A hash is also a collection of object references, | ||
but its elements can be indexed with objects of any type, e.g., | ||
symbols, strings, regular expressions, etc. | ||
|
||
2. When would you use an Array over a Hash and vice versa? | ||
An array is useful for breaking objects down into simple elements of a list, whereas | ||
a hash would be more advantageous for parsing through that list and indexing the | ||
elements by the number of times they appear. | ||
In the example where one would be looking to calculate the word frequency | ||
in a given block of text, an array would be advantageous to split the strings | ||
in the text into individual word elements in an array. The hash object would | ||
be created to index by the words in the array. | ||
|
||
3. What is a module? Enumerable is a built in Ruby module, what is it? | ||
Modules are a way of grouping together methods, classes, and constants. Modules | ||
provide a namespace and prevent name clashes, as well as suppoprting the mixin facility. | ||
Enumerable is a module that supports operations such as mapping, sorting, searching, | ||
and ordering elements. | ||
|
||
4. Can you inherit more than one thing in Ruby? How could you get around this problem? | ||
No. Ruby classes have only one direct parent. This can be avoided by including | ||
a "mixin" (a partial class difinition), thereby introducing multiple-inheritance-like | ||
capability. | ||
|
||
5. What is the difference between a Module and a Class? | ||
Modules cannot have instances. |
This file contains 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,21 @@ | ||
module SimonSays | ||
def echo anything #to pass echo tests | ||
anything | ||
end | ||
|
||
def shout anything #to pass shout tests | ||
anything.upcase | ||
end | ||
|
||
def start_of_word (anything,n) #to pass array tests | ||
anything[0...n] | ||
end | ||
|
||
def first_word anything #to pass first word test | ||
anything.split.first | ||
end | ||
|
||
def repeat (anything,n=2) #to pass word repeat tests | ||
([anything]*n).join(' ') | ||
end | ||
end |
This file contains 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,48 @@ | ||
module Summable | ||
def sum(whatever) | ||
if whatever != [] | ||
whatever = whatever.flatten | ||
whatever.inject(:+) | ||
else | ||
whatever = 0 | ||
end | ||
end | ||
end | ||
|
||
module Empowerable | ||
def pow(whatever, anything) | ||
whatever**anything | ||
end | ||
end | ||
|
||
module Multipliable | ||
def multiply(whatever,anything) | ||
if anything != nil | ||
anything*whatever | ||
else | ||
anything = 1 | ||
whatever = whatever.flatten | ||
whatever.inject(:*)*anything | ||
end | ||
end | ||
end | ||
|
||
module Factorialize | ||
def fac(whatever) | ||
if whatever<=1 | ||
1 | ||
else | ||
whatever * fac(whatever-1) | ||
end | ||
end | ||
end | ||
|
||
|
||
class Calculator | ||
include Enumerable | ||
include Summable | ||
include Empowerable | ||
include Multipliable | ||
include Factorialize | ||
end | ||
|
This file contains 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,30 @@ | ||
Please Read: | ||
- Chapter 6 Standard Types | ||
- Review Blocks | ||
- Chapter 7 Regular Expressions | ||
- Chapter 22 The Ruby Language: basic types (symbols), variables and constants | ||
|
||
1. What is a symbol? | ||
A symbol is something used to correspond to a | ||
string of characters, often a name. | ||
|
||
2. What is the difference between a symbol and a string? | ||
Symbols of the same name are initialized and exist in | ||
memory only once during a session of Ruby. | ||
|
||
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'. A block is invoked by | ||
a Ruby iterator; it may only appear in the source adjacent | ||
to a method call; the block is written starting on the | ||
same line as the method's last parameter. | ||
|
||
4. How do I pass a block to a method? What is the method signature? | ||
A block is passed to a method when a yield statement is | ||
executed. | ||
|
||
5. Where would you use regular expressions? | ||
Regular expressions would be used in tests to see whether | ||
strings match a desired pattern, extract parts of code | ||
that match the desired pattern, or find and replace | ||
patterns in given strings with a desired set of characers. |
This file contains 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 @@ | ||
Please Read: | ||
Chapter 10 Basic Input and Output | ||
The Rake Gem: http://rake.rubyforge.org/ | ||
|
||
1. How does Ruby read files? | ||
file = File.open("file_name","r") opens an existing file named "file_name", starting at the beginning of the file, and reads it (r). | ||
|
||
2. How would you output "Hello World!" to a file called my_output.txt? | ||
file = File.open("my_output.txt","r+") do |text| | ||
text << "Hello World!" | ||
end | ||
|
||
3. What is the Directory class and what is it used for? | ||
The Directory class allows you to manipulate things to do with directories (folders) -- primarily navigating within directories, making new directories, and determining what is contained within a directory. | ||
|
||
4. What is an IO object? | ||
Ruby defines a single base class, IO, to handle input and output. An IO object is a bidrectional 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 Ruby Gem. It is used for task management. A rake task is a unit of work in a Rakefile, which has its associated actions and respective prerequisites. |
This file contains 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,11 @@ | ||
module Worker | ||
|
||
def self.work n=1 #sets a default value, thus making param optional | ||
|
||
results = nil #initialize results to neutral format | ||
n.times {results = yield} #to satisfy third test | ||
results #show results | ||
|
||
end | ||
|
||
end |
This file contains 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,18 @@ | ||
class PirateTranslator | ||
|
||
PIRATE_VOCABULARY = {"Hello Friend" => "Ahoy Matey"} | ||
|
||
def say(what) | ||
@heard = listen_pirate(what) | ||
end | ||
|
||
def translate | ||
@heard + "\n Shiber Me Timbers You Scurvey Dogs!!" | ||
end | ||
|
||
private | ||
def listen_pirate(what) | ||
PIRATE_VOCABULARY[what] | ||
end | ||
end | ||
|
This file contains 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,18 @@ | ||
|
||
Please Read Chapters 23 and 24 DuckTyping and MetaProgramming | ||
|
||
Questions: | ||
1. What is method_missing and how can it be used? | ||
Method_missing is a method that receives the symbol of a non-existent method that has been called, an array of the arguments that were passed in the original call, and any block passed to the original method. It can be used to gracefully intercept any unanswerable messages that are sent to your code and return an error message that displays the problem with your input, specifically -- that the method in question is nonexistent. | ||
|
||
2. What is and Eigenclass and what is it used for? Where Do Singleton methods live? | ||
An eigenclass is used to capture the creation of signleton methods and the objects for which they are defined. Singleton methods live within the eigenclass. | ||
|
||
3. When would you use DuckTypeing? How would you use it to improve your code? | ||
DuckTyping is used in cases where the focus is what on the code should be doing and not what the code is in and of itself. If the purpose of a class is to act like a String, then we treat it as such regardless of its innate "type". It can be used to make code interface more efficiently with larger data sets. | ||
|
||
4. What is the difference between a class method and an instance method? What is the difference between instance_eval and class_eval? | ||
Class methods can only be called on classes and instance methods can only be caled on an instance of a class. Instance_eval is used to define class methods. Class_eval is used to define instance methods. | ||
|
||
5. What is the difference between a singleton class and a singleton method? | ||
A singleton method is a method that is specific to a particular object; the resultant anonymous class that gets created in the interim -- behind the scenes -- is the singleton class. |
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!