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..d219185 100644 --- a/week7/homework/features/step_definitions/tic-tac-toe-steps.rb +++ b/week7/homework/features/step_definitions/tic-tac-toe-steps.rb @@ -1,5 +1,7 @@ require 'rspec/mocks/standalone' require 'rspec/expectations' +#require_relative '../../../../spec_helper' + Given /^I start a new Tic\-Tac\-Toe game$/ do @game = TicTacToe.new end @@ -13,11 +15,11 @@ end Then /^randomly chooses who goes first$/ do - [@game.player, "Computer"].should include @game.current_player + expect([@game.player, "Computer"]).to include(@game.current_player) end Then /^who is X and who is O$/ do - TicTacToe::SYMBOLS.should include @game.player_symbol, @game.computer_symbol + expect(TicTacToe::SYMBOLS).to include(@game.player_symbol, @game.computer_symbol) end Given /^I have a started Tic\-Tac\-Toe game$/ do @@ -35,7 +37,7 @@ 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| @@ -43,7 +45,7 @@ @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 @@ -77,22 +79,22 @@ @old_pos.should eq " " end -Then /^it is now the computer's turn$/ do +Then /^it is now the computers turn$/ do @game.current_player.should eq "Computer" end -When /^there are three X's in a row$/ do +When /^there are three Xs 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 + @game.player_won?.should be_truthy end Then /^the game ends$/ do - @game.over?.should be_true + @game.over?.should be_truthy end Given /^there are not three symbols in a row$/ do @@ -105,11 +107,11 @@ end When /^there are no open spaces left on the board$/ do - @game.spots_open?.should be_false + @game.spots_open?.should be_falsey end Then /^the game is declared a draw$/ do - @game.draw?.should be_true + @game.draw?.should be_truthy end When /^"(.*?)" is taken$/ do |arg1| 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..ce9365c --- /dev/null +++ b/week7/homework/features/step_definitions/tic_tac_toe.rb @@ -0,0 +1,146 @@ +class TicTacToe + attr_accessor :player, :player_symbol, :computer_symbol, :board + SYMBOLS = [:O, :X] + + def initialize(first_player = nil, player_symbol = nil) + @current_player_symbol = first_player || choose_current_symbol + @player_symbol = player_symbol || choose_player_symbol + @computer_symbol = (SYMBOLS - [player_symbol]).first + + @player_won = false + @computer_won = false + @over = false + @draw = false + + @board = { + :A1 => ' ', :A2 => ' ', :A3 => ' ', + :B1 => ' ', :B2 => ' ', :B3 => ' ', + :C1 => ' ', :C2 => ' ', :C3 => ' ' + } + end + + def welcome_player + "Welcome #{player}" + end + + def current_player + if current_player_symbol == :player + return player + else + return "Computer" + end + end + + def indicate_player_turn + puts "#{current_player}'s Move:" + end + + def get_player_move + player_position = gets.chomp + end + + def player_move + player_position = "" + while board[player_position.to_sym] != ' ' + player_position = get_player_move + end + place_board_move(player_position.to_sym, player_symbol) + current_player_symbol = :computer + player_position.to_sym + end + + def open_spots + board.keys.select{|key| board[key] == ' '} + end + + def computer_move + computer_position = open_spots.sample + place_board_move(computer_position, computer_symbol) + current_player_symbol = :player + computer_position + end + + def check_for_winner + winner_symbol = nil + if board[:A1] == board[:A2] && board[:A2] == board[:A3] && board[:A3] != ' ' + winner_symbol = board[:A3] + elsif board[:B1] == board[:B2] && board[:B2] == board[:B3] && board[:B3] != ' ' + winner_symbol = board[:B3] + elsif board[:C1] == board[:C2] && board[:C2] == board[:C3] && board[:C3] != ' ' + winner_symbol = board[:C3] + elsif board[:A1] == board[:B1] && board[:B1] == board[:C1] && board[:C1] != ' ' + winner_symbol = board[:C1] + elsif board[:A2] == board[:B2] && board[:B2] == board[:C2] && board[:C2] != ' ' + winner_symbol = board[:C2] + elsif board[:A3] == board[:B3] && board[:B3] == board[:C3] && board[:C3] != ' ' + winner_symbol = board[:C3] + elsif board[:A1] == board[:B2] && board[:B2] == board[:C3] && board[:C3] != ' ' + winner_symbol = board[:C3] + elsif board[:C1] == board[:B2] && board[:B2] == board[:A3] && board[:A3] != ' ' + winner_symbol = board[:A3] + end + winner_symbol + end + + def determine_winner + winner_symbol = check_for_winner + + if winner_symbol != nil + if winner_symbol == player_symbol + @player_won = true + @over = true + else + @computer_won = true + @over = true + end + else + if !spots_open? + @draw = true + @over = true + end + end + end + + def current_state + state = "#{board[:A1]}|#{board[:A2]}|#{board[:A3]}\n" + + "--------\n" + + "#{board[:B1]}|#{board[:B2]}|#{board[:B3]}\n" + + "--------\n" + + "#{board[:C1]}|#{board[:C2]}|#{board[:C3]}\n" + state + end + + def over? + @over + end + + def player_won? + @player_won + end + def computer_won? + @computer_won + end + + def draw? + @draw + end + + def spots_open? + open_spots.count != 0 + end + + private + attr_accessor :current_player_symbol + + def choose_current_symbol + [:player, :computer].sample + end + + def choose_player_symbol + SYMBOLS.sample + end + + def place_board_move(position, game_symbol) + board[position] = game_symbol.to_s + end +end \ No newline at end of file diff --git a/week7/homework/features/tic-tac-toe.feature b/week7/homework/features/tic-tac-toe.feature index 6f3134d..40d3cbd 100644 --- a/week7/homework/features/tic-tac-toe.feature +++ b/week7/homework/features/tic-tac-toe.feature @@ -17,9 +17,9 @@ Scenario: My Turn Then the computer prints "Renee's Move:" And waits for my input of "B2" -Scenario: Computer's Turn +Scenario: Computers Turn Given I have a started Tic-Tac-Toe game - And it is the computer's turn + 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 @@ -31,7 +31,7 @@ Scenario: Making Moves 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 + And it is now the computers turn Scenario: Making Bad Moves Given I have a started Tic-Tac-Toe game @@ -40,12 +40,12 @@ Scenario: Making Bad Moves 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 + And it is now the computers 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 + When there are three Xs in a row Then I am declared the winner And the game ends