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

week 7 homework #104

Open
wants to merge 70 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
70 commits
Select commit Hold shift + click to select a range
440397f
answer week 1 questions
nbirnel Jan 22, 2014
e39ea1f
passed 1st 2
nbirnel Jan 22, 2014
06a74ac
pass all week1 rspec
nbirnel Jan 22, 2014
9814036
week 1 tidy up
nbirnel Jan 22, 2014
9b13d01
restore questions to original format
nbirnel Jan 22, 2014
bc3e4bf
answer week2 1 + 2
nbirnel Jan 22, 2014
24c32c0
Answer week2 questions
nbirnel Jan 23, 2014
1f25c74
passed all tests
nbirnel Jan 23, 2014
70d1757
pass my additional test
nbirnel Jan 23, 2014
9e9cfb0
pull vim junk out of homework
nbirnel Jan 23, 2014
c7e640a
Merge branch 'master' of https://github.com/UWE-Ruby/RubyWinter2014
nbirnel Jan 24, 2014
c11d1a9
undo my stupid pull
nbirnel Jan 24, 2014
2855e2b
answer 1, 2, 5
nbirnel Jan 28, 2014
e533453
pass addition tests, multiply array test.
nbirnel Jan 28, 2014
21627f7
pass multiplication
nbirnel Jan 28, 2014
c2d2c2e
week 3 all tests passed
nbirnel Jan 29, 2014
3b81ac4
tidy up and comment calculator.rb
nbirnel Jan 29, 2014
fd2ed62
answer week 3 3 , ½ of 4
nbirnel Jan 29, 2014
af7f083
refactor week3 calculator homework
nbirnel Jan 29, 2014
e5776f4
unsatisfactory answer to week 3 question 4
nbirnel Jan 29, 2014
c6fd070
Merge branch 'master' of https://github.com/UWE-Ruby/RubyWinter2014
nbirnel Jan 31, 2014
03679d5
answer week4 1,2,4
nbirnel Feb 1, 2014
12e31d3
week4 add worker.rb and Makefile
nbirnel Feb 1, 2014
2574151
refactor Makefile
nbirnel Feb 1, 2014
23e1491
pass specs 1-3 with self.work
nbirnel Feb 1, 2014
8b4217f
pass 4th test using local variables.
nbirnel Feb 3, 2014
176efe7
make result a method variable, and comment out 1st def
nbirnel Feb 3, 2014
b990889
comment on method vs global variable
nbirnel Feb 3, 2014
2abdb6b
answer last question - now to add rakefile
nbirnel Feb 3, 2014
94edc67
add wrong Rakefile
nbirnel Feb 3, 2014
dc80831
make rakefile work. Sort of.
nbirnel Feb 3, 2014
fc728b5
answer q3 with Dir, not Directory, in hopes this is desired
nbirnel Feb 3, 2014
1cad665
add rake v make comparison (rake compare not done)
nbirnel Feb 5, 2014
c2c8638
skip blanks and comments in rake - done
nbirnel Feb 5, 2014
3e3e095
comment refactor
nbirnel Feb 5, 2014
cafebd2
question 5 rake vs make commentary
nbirnel Feb 5, 2014
9def693
Merge branch 'master' of https://github.com/UWE-Ruby/RubyWinter2014
nbirnel Feb 7, 2014
f0662c3
add the inject version of week 4 excercise
nbirnel Feb 8, 2014
02d82f2
Merge branch 'master' of https://github.com/UWE-Ruby/RubyWinter2014
nbirnel Feb 14, 2014
72e5bdc
Merge branch 'master' of https://github.com/UWE-Ruby/RubyWinter2014
nbirnel Feb 21, 2014
f1b78bc
Merge branch 'master' of https://github.com/UWE-Ruby/RubyWinter2014
nbirnel Feb 21, 2014
7882af4
add pirate translator:say
nbirnel Feb 24, 2014
da2d0d8
make pirate pass in a stupid manner
nbirnel Feb 24, 2014
d317dfd
make pirate pass in a less ugly manner
nbirnel Feb 24, 2014
5c0ae8d
attribute source of heredoc trick
nbirnel Feb 24, 2014
b11401c
pass begin game scenario
nbirnel Feb 24, 2014
e59d383
more attempts
nbirnel Feb 26, 2014
9bfca5f
Merge branch 'master' of github.com:nbirnel/RubyWinter2014
nbirnel Feb 26, 2014
f87eb8a
pass who goes first
nbirnel Feb 26, 2014
1606e4d
padd who is first for real
nbirnel Feb 26, 2014
510fd96
pass X and O by cheating
nbirnel Feb 26, 2014
f816c9e
pass x and o correctly, grossly
nbirnel Feb 26, 2014
8783b90
pass Renee's move print
nbirnel Feb 26, 2014
1b01eb5
pass My Turn (get_player_move does nothing yet)
nbirnel Feb 26, 2014
4b74416
pass computer playing x - 2nd param is player symb
nbirnel Feb 26, 2014
6ae4c83
hold here for understanding game board
nbirnel Feb 26, 2014
1b661bf
make open_spots work
nbirnel Feb 26, 2014
df27f2a
add computer_move, refactory zto1 >dien
nbirnel Feb 26, 2014
7b2c12e
pass computer's turn
nbirnel Feb 26, 2014
d4f0d62
get player move
nbirnel Feb 27, 2014
d492da9
change nil open spaces to ' '
nbirnel Feb 27, 2014
ac224fe
rough answer to week7 q 1
nbirnel Feb 27, 2014
6eb55b3
rough answer to w7 q2
nbirnel Feb 27, 2014
9d14ee7
answer w7 q3
nbirnel Feb 27, 2014
4796f92
partial answer w7 q4
nbirnel Feb 27, 2014
4d811eb
answer w7 q5
nbirnel Feb 27, 2014
846471c
rough answer w7 q5
nbirnel Feb 27, 2014
fa99353
Merge branch 'master' of https://github.com/UWE-Ruby/RubyWinter2014
nbirnel Feb 28, 2014
d009d1d
Merge branch 'master' of https://github.com/UWE-Ruby/RubyWinter2014
nbirnel Mar 7, 2014
59397b2
add tictactoe - this has it's own git repo
nbirnel Mar 13, 2014
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
124 changes: 124 additions & 0 deletions tictactoe/features/step_definitions/tic-tac-toe-steps.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
require 'rspec/mocks/standalone'
require 'rspec/expectations'
Given /^I start a new Tic\-Tac\-Toe game$/ do
@game = TicTacToe.new
end

When /^I enter my name (\w+)$/ do |name|
@game.player = name
end

Then /^the computer welcomes me to the game with "(.*?)"$/ do |arg1|
@game.welcome_player.should eq arg1
end

Then /^randomly chooses who goes first$/ do
[@game.player, "Computer"].should include @game.current_player
end

Then /^who is X and who is O$/ do
TicTacToe::SYMBOLS.should include @game.player_symbol, @game.computer_symbol
end

Given /^I have a started Tic\-Tac\-Toe game$/ do
@game = TicTacToe.new(:player)
@game.player = "Renee"
end

Given /^it is my turn$/ do
@game.current_player.should eq "Renee"
end

Given /^the computer knows my name is Renee$/ do
@game.player.should eq "Renee"
end

Then /^the computer prints "(.*?)"$/ do |arg1|
@game.should_receive(:puts).with(arg1)
@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
@game = TicTacToe.new(:computer, :O)
@game.current_player.should eq "Computer"
end

Then /^the computer randomly chooses an open position for its move$/ do
open_spots = @game.open_spots
@com_move = @game.computer_move
open_spots.should include(@com_move)
end

Given /^the computer is playing X$/ do
@game.computer_symbol.should eq :X
end

Then /^the board should have an X on it$/ do
@game.current_state.should include 'X'
end

Given /^I am playing X$/ do
@game = TicTacToe.new(:computer, :X)
@game.player_symbol.should eq :X
end

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

When /^"(.*?)" is not taken$/ do |arg1|
@old_pos.should eq " "
end

Then /^it is now the computer's turn$/ do
@game.current_player.should eq "Computer"
end

When /^there are three X's in a row$/ do
@game = TicTacToe.new(:computer, :X)
@game.board[:C1] = @game.board[:B2] = @game.board[:A3] = :X
end

Then /^I am declared the winner$/ do
@game.determine_winner
@game.player_won?.should be_true
end

Then /^the game ends$/ do
@game.over?.should be_true
end

Given /^there are not three symbols in a row$/ do
@game.board = {
:A1 => :X, :A2 => :O, :A3 => :X,
:B1 => :X, :B2 => :O, :B3 => :X,
:C1 => :O, :C2 => :X, :C3 => :O
}
@game.determine_winner
end

When /^there are no open spaces left on the board$/ do
@game.spots_open?.should be_false
end

Then /^the game is declared a draw$/ do
@game.draw?.should be_true
end

When /^"(.*?)" is taken$/ do |arg1|
@game.board[arg1.to_sym] = :O
@taken_spot = arg1.to_sym
end

Then /^computer should ask me for another position "(.*?)"$/ do |arg1|
@game.board[arg1.to_sym] = ' '
@game.should_receive(:get_player_move).twice.and_return(@taken_spot, arg1)
@game.player_move.should eq arg1.to_sym
end
146 changes: 146 additions & 0 deletions tictactoe/features/step_definitions/tic-tac-toe.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
class TicTacToe
SYMBOLS = [:X, :O]
PLAYERS = [:player, :computer]
RED = "\e[31m"
GREEN = "\e[32m"
OFF = "\e[0m"
attr_reader :player, :last_taken
attr_accessor :player_symbol, :computer_symbol, :players, :whose_turn, :board

def initialize starts=PLAYERS.sample, s=SYMBOLS.sample
@name = {:player => 'Player', :computer => 'Computer'}
@whose_turn = PLAYERS.index starts
@whose_symb = SYMBOLS.index s
@symbol = {
#PLAYERS[@whose_turn] => s,
:player => s,
:computer => SYMBOLS[(SYMBOLS.index s)^1]
}
@board = {
:A1 => ' ', :A2 => ' ', :A3 => ' ',
:B1 => ' ', :B2 => ' ', :B3 => ' ',
:C1 => ' ', :C2 => ' ', :C3 => ' '
}
@draw = @game_over = false
end

def player= name
@player = @name[:player] = name
end

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

[:player, :computer].each do |s|

define_method("#{s}_symbol") do
instance_variable_get("@symbol")[s]
end

define_method("#{s}_won?") do
ws = instance_variable_get("@winning_symbol")
ss = instance_variable_get("@symbol")[s]
ws == ss
end

end

def welcome_player
"Welcome #{@player}"
end

def current_player
@name[PLAYERS[@whose_turn]]
end

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

def get_player_move
move = gets.chomp.upcase
end

def player_move
pos = get_player_move.to_sym
until self.open_spots.include? pos
puts "#{pos} is taken, yo. Try again."
pos = get_player_move.to_sym
end
@board[pos] = @symbol[:player]
@last_taken = pos
pos
end

def computer_move
puts "My move, yo:"
pos = open_spots.sample
@board[pos] = @symbol[:computer]
@last_taken = pos
pos
end

def current_state
b = @board.map{|k,v| k == @last_taken ? colorize(v, OFF) : colorize(v)}

#b = @board.values.map{|v| v.to_s}
cs = ''
[0,3,6].each do |row|
b[row...row+3].each{|cell| cs << cell}
cs << "\n"
end
cs
end

def over?
@game_over
end

def spots_open?
open_spots.length > 0
end

def determine_winner

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

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

[:A1, :B2, :C3],
[:A3, :B2, :C1],
]
[player_symbol, computer_symbol].each do |sym|
owns = @board.select{|k,v| v == sym}.keys
winning_line.each do |line|
if (line & owns).length == 3
@winning_symbol = sym
@game_over = true
end
end
end

@game_over = true unless spots_open?
toggle_current_player
end

def draw?
!(computer_won? || player_won?)
end

private

def toggle_current_player
self.whose_turn = self.whose_turn^1
end

def colorize cell, color = cell == :X ? RED : GREEN
"#{color}#{cell.to_s}#{OFF}"
end

end
57 changes: 57 additions & 0 deletions tictactoe/features/tic-tac-toe.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
Feature: Tic-Tac-Toe Game
As a game player I like tic-tac-toe
In order to up my skills
I would like to play agaist the computer

Scenario: Begin Game
Given I start a new Tic-Tac-Toe game
When I enter my name Renee
Then the computer welcomes me to the game with "Welcome Renee"
And randomly chooses who goes first
And who is X and who is O

Scenario: My Turn
Given I have a started Tic-Tac-Toe game
And it is my turn
And the computer knows my name is Renee
Then the computer prints "Renee's Move:"
And waits for my input of "B2"

Scenario: Computer's Turn
Given I have a started Tic-Tac-Toe game
And it is the computer's turn
And the computer is playing X
Then the computer randomly chooses an open position for its move
And the board should have an X on it

Scenario: Making Moves
Given I have a started Tic-Tac-Toe game
And it is my turn
And I am playing X
When I enter a position "A1" on the board
And "A1" is not taken
Then the board should have an X on it
And it is now the computer's turn

Scenario: Making Bad Moves
Given I have a started Tic-Tac-Toe game
And it is my turn
And I am playing X
When I enter a position "A1" on the board
And "A1" is taken
Then computer should ask me for another position "B2"
And it is now the computer's turn

Scenario: Winning the Game
Given I have a started Tic-Tac-Toe game
And I am playing X
When there are three X's in a row
Then I am declared the winner
And the game ends

Scenario: Game is a draw
Given I have a started Tic-Tac-Toe game
And there are not three symbols in a row
When there are no open spaces left on the board
Then the game is declared a draw
And the game ends
22 changes: 22 additions & 0 deletions tictactoe/play_game.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
require './features/step_definitions/tic-tac-toe.rb'

@game = TicTacToe.new
puts "What is your name?"
@game.player = gets.chomp
puts @game.welcome_player

until @game.over?
case @game.current_player
when "Computer"
@game.computer_move
when @game.player
@game.indicate_player_turn
@game.player_move
end
puts @game.current_state
@game.determine_winner
end

puts "You Won!" if @game.player_won?
puts "I Won!" if @game.computer_won?
puts "DRAW!" if @game.draw?
28 changes: 25 additions & 3 deletions week1/homework/questions.txt
Original file line number Diff line number Diff line change
@@ -1,15 +1,37 @@
Please read:
Chapter 3 Classes, Objects, and Variables
Please read:
Chapter 3 Classes, Objects, and Variables
p.86-90 Strings (Strings section in Chapter 6 Standard Types)

1. What is an object?
A representation of a "thing" in a program.

2. What is a variable?
In ruby, a named pointer to an object. In some other languages, more like a
container for a value.

3. What is the difference between an object and a class?
A class is a representation of a "type of thing" - e.g. the class of all
toasters. An object is a representation of an individual "thing" - e.g. the
toaster in my kitchen. This is not the same as a Platonic Ideal Form vs a Real
Object - ruby objects are not second rate to classes; each is a correct
"instance" of its class.

4. What is a String?
A sequence of characters. In ruby, an object of the class String. And in ruby,
it is mutable. This still freaks me out and I would like to understand if this
is considered to be a deliberate feature or a side-effect of something.

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

6. What are two ways of defining a String literal? Bonus: What is the difference between them?
'single quoted, which has #{no interpration}, except
\\\\ renders as \\, and \\\' renders as \''

interpretation='lots of interprations'
"double quoted, which has #{interpretation}"

%q{wrapped in some arbitrary brace pair, allowing one to embed plenty of
nasties like ', ", and \, without escapes}
Loading