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

Final for Tic Tac Toe #171

Open
wants to merge 15 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
21 changes: 21 additions & 0 deletions week1/homework/questions_Yan_answers.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
Please read:
Chapter 3 Classes, Objects, and Variables
p.90-94 Strings

1. What is an object?
Everything we manipulate in Ruby is an object.

2. What is a variable?
Variables are used to keep track of objects; each variable holds a referece to an object.

3. What is the difference between an object and a class?
Every object in Ruby was generated either directly or indirectly from a class.

4. What is a String?
Ruby strings are simply sequences of characters. Strings are objects of class String.

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

6. What are two ways of defining a String literal? Bonus: What is the difference between the two?
The type of string delimiter determines the degree of substitution performed. Single-quoted strings and double-quoted strings.
12 changes: 9 additions & 3 deletions week1/homework/strings_and_rspec_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
# encoding: utf-8

# Please make these examples all pass
# You will need to change the 3 pending tests
# You will need to write a passing test for the first example
Expand All @@ -12,15 +11,22 @@
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(@my_string.size).characters
@my_string.should respond_to(:length)
#I should be able to do @my_string.length
end

it "should be able to split on the . charater" do
result = @my_string.split('.')
result = @my_string.split(/\./)
# '\' 'escapes' any character so that it just means that character
result.should have(2).items
end

it "should be able to give the encoding of the string" do
@my_string.encoding.should eq (Encoding.find("UTF-8"))
end
end
end


5 changes: 3 additions & 2 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 All @@ -70,6 +70,7 @@
When /^I enter a position "(.*?)" on the board$/ do |arg1|
@old_pos = @game.board[arg1.to_sym]
@game.should_receive(:get_player_move).and_return(arg1)

@game.player_move.should eq arg1.to_sym
end

Expand Down
106 changes: 106 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,106 @@
class TicTacToe
attr_accessor :player, :current_turn, :board, :player_symbol,
:computer_symbol
SYMBOLS = [:X, :O]

def initialize(player = nil, player_symbol = nil)
if player
@current_turn = player

if player_symbol
@player_symbol = player_symbol
@computer_symbol = SYMBOLS[SYMBOLS.index(player_symbol) - 1]
end

else
@current_turn = randomly_choose
end

randomly_assign_symbol unless (@player_symbol && @computer_symbol)
initialize_board
end

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

def randomly_choose
return [:player, :computer].sample
end

def current_player
return {:player => @player || "Player0", :computer => "Computer"}[@current_turn]
end

def indicate_player_turn
puts "#{current_player}'s Move:"
end

def player_move
move = get_player_move.to_sym

if @board[move] == " "
@board[move] = @player_symbol
else
return :B2 #need to change
end

return move
end

def get_player_move
gets.chomp
end

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

def winning_cases
@cases = [
['A1','B2','C3'],
['B1','B2','B3'],
['C1','C2','C3'],
['A1','B1','C1'],
['A2','B2','C2'],
['A3','B3','C3'],
['A1','B2','C3'],
['C1','B2','A3']
]
end

def randomly_assign_symbol
@computer_symbol = SYMBOLS.sample
@player_symbol = @computer_symbol == :X ? :O : :X
end

#@cpu = rand() > 0.5 ? 'X' : 'O'
#@user = @cpu == 'X' ? 'O' : 'X'

#@computer_symbol = SYMBOLS.sample
#@player_symbol = SYMBOLS[SYMBOLS.index(@computer_symbol) - 1]

def current_state
@player_symbol.to_s
end

def determine_winner
return "#{player_won?}"
end

def player_won?
return @board.values
end

def draw?
end

def spots_open?
end

def over?
end

end
2 changes: 1 addition & 1 deletion week7/homework/play_game.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
when "Computer"
@game.computer_move
when @game.player
@game.indicate_palyer_turn
@game.indicate_player_turn
@game.player_move
end
puts @game.current_state
Expand Down