From b0f9e92eac97109fbeb2dd838ca19e14cf5d6d85 Mon Sep 17 00:00:00 2001 From: Emory Taylor Date: Wed, 15 Jan 2014 18:22:26 -0800 Subject: [PATCH 1/7] Emory's homework for week 1 --- week1/homework/questions.txt | 7 +++++++ week1/homework/strings_and_rspec_spec.rb | 9 +++++---- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/week1/homework/questions.txt b/week1/homework/questions.txt index 2257bb9..9c54537 100644 --- a/week1/homework/questions.txt +++ b/week1/homework/questions.txt @@ -3,13 +3,20 @@ Chapter 3 Classes, Objects, and Variables p.86-90 Strings (Strings section in Chapter 6 Standard Types) 1. What is an object? + Everything in ruby is an object, it's a piece of data that can be manipulated or hold properties. 2. What is a variable? + A variable is name that is assigned to reference a piece of information to be called on later. 3. What is the difference between an object and a class? + A class is a definition for objects in it, it defines object properties. An object is an instance of a class. 4. What is a String? + A sequence of characters. 5. What are three messages that I can send to a string object? Hint: think methods + upcase, reverse, capitalize 6. What are two ways of defining a String literal? Bonus: What is the difference between them? + Using quotes or %. + diff --git a/week1/homework/strings_and_rspec_spec.rb b/week1/homework/strings_and_rspec_spec.rb index ea79e4c..a3ce92f 100644 --- a/week1/homework/strings_and_rspec_spec.rb +++ b/week1/homework/strings_and_rspec_spec.rb @@ -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.length.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.to_s.should eq("UTF-8") end end end From fd9cea4d6852545bbe3b4626872ce2aadb6880d3 Mon Sep 17 00:00:00 2001 From: Emory Taylor Date: Thu, 23 Jan 2014 17:15:23 -0800 Subject: [PATCH 2/7] Emory's homework for week 2 --- week2/homework/questions.txt | 5 +++++ week2/homework/simon_says.rb | 25 +++++++++++++++++++++++++ 2 files changed, 30 insertions(+) create mode 100644 week2/homework/simon_says.rb diff --git a/week2/homework/questions.txt b/week2/homework/questions.txt index 939e42d..ff85811 100644 --- a/week2/homework/questions.txt +++ b/week2/homework/questions.txt @@ -3,11 +3,16 @@ Containers, Blocks, and Iterators Sharing Functionality: Inheritance, Modules, and Mixins 1. What is the difference between a Hash and an Array? + You index arrays with integers, and index a hash with objects. 2. When would you use an Array over a Hash and vice versa? + Arrays are good for storing large sets of data with only 1 value, say like a list of names. A hash would be good for storing a set of data that would include more information about those 50 people, like their address and birthdate. You can then put the hashes into an array. 3. What is a module? Enumerable is a built in Ruby module, what is it? + A sort of library for methods and constants. Enumerable is a mixin that provides searching and sorting methods. 4. Can you inherit more than one thing in Ruby? How could you get around this problem? + No, ruby is a single inheritance language. You can get around this problem using mixins. 5. What is the difference between a Module and a Class? + A class is a collection of objects, a module is a collection of functions. diff --git a/week2/homework/simon_says.rb b/week2/homework/simon_says.rb new file mode 100644 index 0000000..fd2c1ed --- /dev/null +++ b/week2/homework/simon_says.rb @@ -0,0 +1,25 @@ +module SimonSays + + +def echo(word) + word +end + +def shout(word) + word.upcase +end + +def repeat(word, n = 2) + ( "#{word} " * n ).strip +end + +def start_of_word(word, n) + word[0,n] +end + +def first_word(string) + string.split[0] +end + + +end \ No newline at end of file From 48ed155845bd77fc17fe9c6490cb948c0f24fb66 Mon Sep 17 00:00:00 2001 From: Emory Taylor Date: Thu, 30 Jan 2014 17:52:42 -0800 Subject: [PATCH 3/7] Emory's homework 1/30/2014 --- week3/homework/calculator.rb | 32 +++++++++++++++++++++++++++++++ week3/homework/calculator_spec.rb | 2 +- week3/homework/questions.txt | 5 +++++ 3 files changed, 38 insertions(+), 1 deletion(-) create mode 100644 week3/homework/calculator.rb diff --git a/week3/homework/calculator.rb b/week3/homework/calculator.rb new file mode 100644 index 0000000..727aa54 --- /dev/null +++ b/week3/homework/calculator.rb @@ -0,0 +1,32 @@ +class Calculator + + +def new + initialize +end + + +def sum(array) + array.inject(:+).to_i + end + + +def multiply(*array) + array.flatten! + array.inject(:*) + +end + +def pow(num,exp) + num ** exp +end + +def fac(n) + if n == 0 + return 1 + end + else + n.downto(1).inject(:*) +end + +end \ No newline at end of file diff --git a/week3/homework/calculator_spec.rb b/week3/homework/calculator_spec.rb index 5a418ed..edad946 100644 --- a/week3/homework/calculator_spec.rb +++ b/week3/homework/calculator_spec.rb @@ -1,4 +1,4 @@ -require "#{File.dirname(__FILE__)}/calculator" +require "./calculator.rb" describe Calculator do diff --git a/week3/homework/questions.txt b/week3/homework/questions.txt index dfb158d..2dd1713 100644 --- a/week3/homework/questions.txt +++ b/week3/homework/questions.txt @@ -5,11 +5,16 @@ Please Read: - Chapter 22 The Ruby Language: basic types (symbols), variables and constants 1. What is a symbol? + A Ruby symbol is an identifier corresponding to a string of characters, often a name. Example: :symbol 2. What is the difference between a symbol and a string? + Symbols are not mutable, or changeable. Symbols also remain in memory while program is in operation, each new instance does not require additional memory / object ID. 3. What is a block and how do I call a block? + A block is a chunk of goes between braces or "do" and "end." Blocks are called my invoking a method. 4. How do I pass a block to a method? What is the method signature? + Each or yield are examples. "each &block" 5. Where would you use regular expressions? + When you are trying to do pattern matching or text processing. \ No newline at end of file From 09ffdb784175863f044973794b928b4a031f6616 Mon Sep 17 00:00:00 2001 From: Emory Taylor Date: Thu, 6 Feb 2014 17:53:23 -0800 Subject: [PATCH 4/7] Emory's week 4 homework --- week4/homework/questions.txt | 9 ++++++++- week4/homework/worker.rb | 14 ++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) create mode 100644 week4/homework/worker.rb diff --git a/week4/homework/questions.txt b/week4/homework/questions.txt index 187b3d3..b55b156 100644 --- a/week4/homework/questions.txt +++ b/week4/homework/questions.txt @@ -3,12 +3,19 @@ Chapter 10 Basic Input and Output The Rake Gem: http://rake.rubyforge.org/ 1. How does Ruby read files? +What I gleamed from the book is that basically it reads line by line from the object file, using the File.gets 2. How would you output "Hello World!" to a file called my_output.txt? +File.open("my_output.txt", "w") +File.puts "Hello world!" +File.close 3. What is the Directory class and what is it used for? +It contains objects that represent directories in a file system, the class is used for modifying, searching for, and listing files/directories. 4. What is an IO object? +An IO object is a bidirectional channel between a Ruby program and some external +resource. Something you read from and write to. 5. What is rake and what is it used for? What is a rake task? - +It's a build tool used to create and manage tasks, sort of like automating things? Nothing I've read has been exactly clear as to what a rake task is, but from what I have gathered it's a task that runs according to the rules and parameters you set in the RakeFile? Need this to be explained. diff --git a/week4/homework/worker.rb b/week4/homework/worker.rb new file mode 100644 index 0000000..7ec2bb9 --- /dev/null +++ b/week4/homework/worker.rb @@ -0,0 +1,14 @@ +class Worker + + def self.work(n=0) + result = nil + if n > 0 + n.times{ |stuff| result = yield(stuff) } + result + else + result = yield + end + +end + +end \ No newline at end of file From 49710bd8e818706143ef700f0143f3f1dc353b4e Mon Sep 17 00:00:00 2001 From: Emory Taylor Date: Thu, 27 Feb 2014 17:44:47 -0800 Subject: [PATCH 5/7] pirate translator --- week7/homework/PirateTranslator.rb | 2 ++ week7/homework/features/step_definitions/pirate.rb | 12 ++++++++++++ .../features/step_definitions/pirate_speak.rb | 4 ++++ 3 files changed, 18 insertions(+) create mode 100644 week7/homework/PirateTranslator.rb create mode 100644 week7/homework/features/step_definitions/pirate.rb create mode 100644 week7/homework/features/step_definitions/pirate_speak.rb diff --git a/week7/homework/PirateTranslator.rb b/week7/homework/PirateTranslator.rb new file mode 100644 index 0000000..572e66b --- /dev/null +++ b/week7/homework/PirateTranslator.rb @@ -0,0 +1,2 @@ +class PirateTranslator +end \ No newline at end of file diff --git a/week7/homework/features/step_definitions/pirate.rb b/week7/homework/features/step_definitions/pirate.rb new file mode 100644 index 0000000..3e5d1c6 --- /dev/null +++ b/week7/homework/features/step_definitions/pirate.rb @@ -0,0 +1,12 @@ +class PirateTranslator + + def say(words) + @words = words + end + + def translate + @words.gsub(/\w/, "Ahoy Matey\n Shiber Me Timbers You Scurvey Dogs!!") + end + + +end \ No newline at end of file diff --git a/week7/homework/features/step_definitions/pirate_speak.rb b/week7/homework/features/step_definitions/pirate_speak.rb new file mode 100644 index 0000000..87919a0 --- /dev/null +++ b/week7/homework/features/step_definitions/pirate_speak.rb @@ -0,0 +1,4 @@ +class PirateTranslator + + +end \ No newline at end of file From 1fab755203962d22f7451b63122ee6f0ece03f40 Mon Sep 17 00:00:00 2001 From: Emory Taylor Date: Thu, 27 Feb 2014 17:56:28 -0800 Subject: [PATCH 6/7] Week 7 homework --- week7/homework/questions.txt | 31 ++++++++++++++++++++++--------- 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/week7/homework/questions.txt b/week7/homework/questions.txt index d55387d..e612cda 100644 --- a/week7/homework/questions.txt +++ b/week7/homework/questions.txt @@ -1,9 +1,22 @@ - -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? -3. When would you use DuckTypeing? How would you use it to improve your code? -4. What is the difference between a class method and an instance method? What is the difference between instance_eval and class_eval? -5. What is the difference between a singleton class and a singleton method? + +Please Read Chapters 23 and 24 DuckTyping and MetaProgramming + +Questions: +1. What is method_missing and how can it be used? +A hook method that gets invoked when a method is called and Ruby can't find it. You can tell it to behave in a specific fashion when an unknown method is invoked, rather than ignore the calls or raise an exception. + + +2. What is and Eigenclass and what is it used for? Where Do Singleton methods live? +It's an anonymous class that is created to hold an object's singleton methods, which is a child class of object's original class. +Singleton methods are methods attached to a specific object, that are not available to other instances of that object's class. + +3. When would you use DuckTypeing? How would you use it to improve your code? +You can use it to process different kinds of object types that share the same method as if they were the same types. You can use it to simplify things, rather than have to write code for different objects, you can just write it for the objects that can respond to whatever you're trying to accomplish. I'm not sure how to word this in coder terms, but I think I understand the concept. + +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 are methods that are called on a class and instance methods are methods that are called on an instance of a class. +Class_eval sets things up as if you were in the body of a class definition, so method definitions will define instance methods. Calling instance_eval on a class acts as if you were working inside the singleton class of self. Therefore, any methods you define will become class methods. + +5. What is the difference between a singleton class and a singleton method? +A singleton method is a method that is attached to a specific object that cannot be accessed by other objects of the same class. A singleton class is the class that is able to call a singleton method? + From 4a87d73700c0869dfac6ae4634cfb64556f2e871 Mon Sep 17 00:00:00 2001 From: Emory Taylor Date: Thu, 13 Mar 2014 17:08:53 -0700 Subject: [PATCH 7/7] Can't get this test to pass without causing another to fail. Been working on it for a couple days now. --- .../step_definitions/tic-tac-toe-steps.rb | 248 +++++++++--------- .../features/step_definitions/tic-tac-toe.rb | 145 ++++++++++ week7/homework/features/tic-tac-toe.feature | 114 ++++---- week7/homework/play_game.rb | 44 ++-- 4 files changed, 348 insertions(+), 203 deletions(-) create mode 100644 week7/homework/features/step_definitions/tic-tac-toe.rb diff --git a/week7/homework/features/step_definitions/tic-tac-toe-steps.rb b/week7/homework/features/step_definitions/tic-tac-toe-steps.rb index a3287c1..3ea10ca 100644 --- a/week7/homework/features/step_definitions/tic-tac-toe-steps.rb +++ b/week7/homework/features/step_definitions/tic-tac-toe-steps.rb @@ -1,124 +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_palyer_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 +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 computers 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 diff --git a/week7/homework/features/step_definitions/tic-tac-toe.rb b/week7/homework/features/step_definitions/tic-tac-toe.rb new file mode 100644 index 0000000..f87f224 --- /dev/null +++ b/week7/homework/features/step_definitions/tic-tac-toe.rb @@ -0,0 +1,145 @@ +class TicTacToe + attr_accessor :player, :player_move, :get_player_move, :current_player, :player_symbol, :computer_symbol, :open_spots, :board + + SYMBOLS = [:X, :O] + + def initialize(arg1=nil, arg2=nil) + @board = {:A1=>" ",:A2=>" ",:A3=>" ", + :B1=>" ",:B2=>" ",:B3=>" ", + :C1=>" ",:C2=>" ",:C3=>" " + } + + + @win_rows = [ + [:A1,:A2,:A3], + [:B1,:B2,:B3], + [:C1,:C2,:C3], + [:A1,:B1,:C1], + [:A2,:B2,:C2], + [:A3,:B3,:C3], + [:A1,:B2,:C3], + [:C1,:B2,:A3] + ] + + @open_spots = ['a1','a2', 'a3', + 'b1', 'b2', 'b3', + 'c1', 'c2', 'c3'] + + @moves = ['a1','a2', 'a3', + 'b1', 'b2', 'b3', + 'c1', 'c2', 'c3'] + + + welcome_player + @current_player = arg1 + @player_symbol = arg2 + @draw = false + @over = false + @player_won = false + @computer_won = false + + if @player_symbol == :O + @computer_symbol = :X + else + @computer_symbol = :O + end + + unless @player_symbol + @player_symbol = SYMBOLS.sample + @computer_symbol = SYMBOLS.select {|symbol| symbol != @player_symbol}.first + end + + unless @current_player + @current_player = [:player, "Computer"].sample + end + + end + + def welcome_player + return "Welcome #{@player}" + end + +def current_player + puts @current_player + if @current_player == :player + return @player + else + return "Computer" + end + puts current_player + end + + def indicate_player_turn + puts "#{@player}'s Move:" + end + + def player_move + print "What is your move?\n" + @get_player_move = gets.chomp.to_sym + @board[@get_player_move] = @player_symbol.to_s + return @get_player_move + end + + + + def computer_move + move = @open_spots.sample + puts "Computer marks #{move.upcase}" + @moves.delete move + @board[move.to_sym] = @computer_symbol.to_s + return move + end + + def current_state + return @board.values + end + + def determine_winner + if !spots_open? + @draw = true + @over = true + return false + end + SYMBOLS.each do |i| + moves = @board.map{ |k,v| v == i ? k : nil }.compact + @win_rows.each do |row| + if row-moves == [] + declare_winner(i) + return i + end + end + end + end + + + def declare_winner winner + if winner == @player_symbol + @player_won = true + elsif winner == @computer_symbol + @computer_won = true + end + @over = true + end + + def over? + @over + end + + def draw? + @draw + end + + def player_won? + @player_won + end + + def computer_won? + computer_won + end + + def spots_open? + !@board.select {|val| val == " " } == {} || !@board.select {|val| val == " " } + end + +end + diff --git a/week7/homework/features/tic-tac-toe.feature b/week7/homework/features/tic-tac-toe.feature index 6f3134d..ca283a9 100644 --- a/week7/homework/features/tic-tac-toe.feature +++ b/week7/homework/features/tic-tac-toe.feature @@ -1,57 +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 +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 computers 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 diff --git a/week7/homework/play_game.rb b/week7/homework/play_game.rb index 0535830..dc0b454 100644 --- a/week7/homework/play_game.rb +++ b/week7/homework/play_game.rb @@ -1,22 +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_palyer_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? +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_palyer_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?