From 39b4c41dca1bfe3e1ba874ab6146e228f0c9760a Mon Sep 17 00:00:00 2001 From: Jon Parten Date: Sat, 29 Nov 2014 11:18:39 -0800 Subject: [PATCH 1/2] player select logic working --- .../step_definitions/tic-tac-toe-steps.rb | 8 +- .../features/step_definitions/tic_tac_toe.rb | 77 +++++++++++++++++++ 2 files changed, 82 insertions(+), 3 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..d85c92c 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| 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..52dca71 --- /dev/null +++ b/week7/homework/features/step_definitions/tic_tac_toe.rb @@ -0,0 +1,77 @@ +class TicTacToe + attr_accessor :player + attr_accessor :player_symbol + attr_accessor :computer_symbol + attr_reader :board + SYMBOLS = [:O, :X] + + def initialize(first_player = nil, player_symbol = nil) + @player = "Player1" + @current_player_symbol = first_player || choose_current_symbol + @player_symbol = player_symbol || choose_player_symbol + @computer_symbol = choose_computer_symbol + 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_move = gets + player_move = player_move.upcase.to_sym + place_board_move(player_move, player_symbol) + switch_player + end + + def open_spots + @board.map{[key,value] } + end + + private + + attr_accessor :current_player_symbol + + def choose_current_symbol + [:player, :computer].sample + end + + def choose_player_symbol + SYMBOLS.sample + end + + def choose_computer_symbol + (SYMBOLS - [player_symbol]).first + end + + def place_board_move(position, game_symbol) + @board[position] = game_symbol + end + + def init_board + @board = { + :A1 => nil, :A2 => nil, :A3 => nil, + :B1 => :X, :B2 => nil, :B3 => nil, + :C1 => :O, :C2 => nil, :C3 => nil + } + end + + def switch_player + if current_player_symbol == :player + current_player_symbol = :computer + else + current_player_symbol = :player + end + end +end \ No newline at end of file From f4e8790a96daae782fa3959abd22065c588bd370 Mon Sep 17 00:00:00 2001 From: Jon Parten Date: Fri, 5 Dec 2014 23:21:46 -0800 Subject: [PATCH 2/2] finished tictactoe --- .../step_definitions/tic-tac-toe-steps.rb | 14 +- .../features/step_definitions/tic_tac_toe.rb | 135 +++++++++++++----- week7/homework/features/tic-tac-toe.feature | 10 +- 3 files changed, 114 insertions(+), 45 deletions(-) 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 d85c92c..d219185 100644 --- a/week7/homework/features/step_definitions/tic-tac-toe-steps.rb +++ b/week7/homework/features/step_definitions/tic-tac-toe-steps.rb @@ -45,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 @@ -79,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 @@ -107,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 index 52dca71..ce9365c 100644 --- a/week7/homework/features/step_definitions/tic_tac_toe.rb +++ b/week7/homework/features/step_definitions/tic_tac_toe.rb @@ -1,15 +1,22 @@ class TicTacToe - attr_accessor :player - attr_accessor :player_symbol - attr_accessor :computer_symbol - attr_reader :board + attr_accessor :player, :player_symbol, :computer_symbol, :board SYMBOLS = [:O, :X] def initialize(first_player = nil, player_symbol = nil) - @player = "Player1" @current_player_symbol = first_player || choose_current_symbol @player_symbol = player_symbol || choose_player_symbol - @computer_symbol = choose_computer_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 @@ -29,49 +36,111 @@ def indicate_player_turn end def get_player_move - player_move = gets - player_move = player_move.upcase.to_sym - place_board_move(player_move, player_symbol) - switch_player + 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.map{[key,value] } + board.keys.select{|key| board[key] == ' '} end - private + def computer_move + computer_position = open_spots.sample + place_board_move(computer_position, computer_symbol) + current_player_symbol = :player + computer_position + end - attr_accessor :current_player_symbol + 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 choose_current_symbol - [:player, :computer].sample + 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 choose_player_symbol - SYMBOLS.sample + 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 choose_computer_symbol - (SYMBOLS - [player_symbol]).first + def over? + @over end - def place_board_move(position, game_symbol) - @board[position] = game_symbol + def player_won? + @player_won + end + def computer_won? + @computer_won + end + + def draw? + @draw end - def init_board - @board = { - :A1 => nil, :A2 => nil, :A3 => nil, - :B1 => :X, :B2 => nil, :B3 => nil, - :C1 => :O, :C2 => nil, :C3 => nil - } + def spots_open? + open_spots.count != 0 end - def switch_player - if current_player_symbol == :player - current_player_symbol = :computer - else - current_player_symbol = :player - 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