From b54d09cebbc65c5e5b611efb94b03aa9204afede Mon Sep 17 00:00:00 2001 From: Sean Sehr Date: Mon, 2 Dec 2013 21:13:49 -0800 Subject: [PATCH 1/9] creating a final folder and getting Scenario: Begin Game resolved --- .../features/step_definitions/tic-tac-toe.rb | 22 ++++ .../step_definitions/tic-tac-toe_steps.rb | 124 ++++++++++++++++++ final/features/tic-tac-toe.feature | 57 ++++++++ final/play_game.rb | 22 ++++ 4 files changed, 225 insertions(+) create mode 100644 final/features/step_definitions/tic-tac-toe.rb create mode 100644 final/features/step_definitions/tic-tac-toe_steps.rb create mode 100644 final/features/tic-tac-toe.feature create mode 100644 final/play_game.rb diff --git a/final/features/step_definitions/tic-tac-toe.rb b/final/features/step_definitions/tic-tac-toe.rb new file mode 100644 index 0000000..207ed5b --- /dev/null +++ b/final/features/step_definitions/tic-tac-toe.rb @@ -0,0 +1,22 @@ +class TicTacToe + attr_accessor :player + attr_reader :current_player, :player_symbol, :computer_symbol + SYMBOLS = ["X", "O"] + + def initialize player + @player = "Player" + @current_player = @current_player || [@player, "Computer"].sample + if @current_player == "Computer" + @computer_symbol = SYMBOLS[0] + @player_symbol = SYMBOLS[1] + else + @computer_symbol = SYMBOLS[1] + @player_symbol = SYMBOLS[0] + end + end + + def welcome_player + "Welcome #{@player}" + end + +end diff --git a/final/features/step_definitions/tic-tac-toe_steps.rb b/final/features/step_definitions/tic-tac-toe_steps.rb new file mode 100644 index 0000000..a3287c1 --- /dev/null +++ b/final/features/step_definitions/tic-tac-toe_steps.rb @@ -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_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 diff --git a/final/features/tic-tac-toe.feature b/final/features/tic-tac-toe.feature new file mode 100644 index 0000000..6f3134d --- /dev/null +++ b/final/features/tic-tac-toe.feature @@ -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 diff --git a/final/play_game.rb b/final/play_game.rb new file mode 100644 index 0000000..0535830 --- /dev/null +++ b/final/play_game.rb @@ -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_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? From c93beb507fba79d4f9605464e5022b0cd447effc Mon Sep 17 00:00:00 2001 From: Sean Sehr Date: Tue, 3 Dec 2013 16:29:18 -0800 Subject: [PATCH 2/9] getting Scenario: My Turn passing --- .../features/step_definitions/tic-tac-toe.rb | 59 +++++++++++++++++-- .../step_definitions/tic-tac-toe_steps.rb | 57 +++++++++--------- 2 files changed, 83 insertions(+), 33 deletions(-) diff --git a/final/features/step_definitions/tic-tac-toe.rb b/final/features/step_definitions/tic-tac-toe.rb index 207ed5b..a2aa8b5 100644 --- a/final/features/step_definitions/tic-tac-toe.rb +++ b/final/features/step_definitions/tic-tac-toe.rb @@ -1,22 +1,69 @@ class TicTacToe - attr_accessor :player - attr_reader :current_player, :player_symbol, :computer_symbol + attr_accessor :board, :player + attr_reader :player_symbol, :computer_symbol SYMBOLS = ["X", "O"] - def initialize player - @player = "Player" - @current_player = @current_player || [@player, "Computer"].sample - if @current_player == "Computer" + def initialize player = "Player" + @player = player + + @computers_turn = rand() > 0.5 ? true : false + if @computers_turn @computer_symbol = SYMBOLS[0] @player_symbol = SYMBOLS[1] else @computer_symbol = SYMBOLS[1] @player_symbol = SYMBOLS[0] end + + @board = { + :A1 => false, :A2 => false, :A3 => false, + :B1 => false, :B2 => false, :B3 => false, + :C1 => false, :C2 => false, :C3 => false + } end def welcome_player "Welcome #{@player}" end + def current_player + @computers_turn ? "Computer" : @player + end + + def computer_move + spot = computer_find_spot + move(spot) + end + + def computer_find_spot + find_empty_spots.sample + end + + def indicate_player_turn + "#{@player}'s Move:" + end + + def get_player_move + STDOUT.flush + input = gets.chomp.upcase.to_sym + move(input) + end + + def find_empty_spots + spots = [] + @board.each{|k, v| spots << k if v == false } + spots + end + + def move spot + if @board[spot] == false + symbol = @computers_turn ? @computer_symbol : @player_symbol + @board[spot] = symbol + next_turn + end + end + + def next_turn + @computers_turn = !@computers_turn + end end diff --git a/final/features/step_definitions/tic-tac-toe_steps.rb b/final/features/step_definitions/tic-tac-toe_steps.rb index a3287c1..6b0bcfd 100644 --- a/final/features/step_definitions/tic-tac-toe_steps.rb +++ b/final/features/step_definitions/tic-tac-toe_steps.rb @@ -1,101 +1,104 @@ require 'rspec/mocks/standalone' require 'rspec/expectations' -Given /^I start a new Tic\-Tac\-Toe game$/ do +Given(/^I start a new Tic\-Tac\-Toe game$/) do @game = TicTacToe.new end -When /^I enter my name (\w+)$/ do |name| +When(/^I enter my name (\w+)$/) do |name| @game.player = name end -Then /^the computer welcomes me to the game with "(.*?)"$/ do |arg1| +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 +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 +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 +Given(/^I have a started Tic\-Tac\-Toe game$/) do @game = TicTacToe.new(:player) @game.player = "Renee" end -Given /^it is my turn$/ do +Given(/^it is my turn$/) do + if @game.current_player == "Computer" + @game.computer_move + end @game.current_player.should eq "Renee" end -Given /^the computer knows my name is Renee$/ do +Given(/^the computer knows my name is Renee$/) do @game.player.should eq "Renee" end -Then /^the computer prints "(.*?)"$/ do |arg1| +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| +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 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 +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 +Given(/^the computer is playing X$/) do @game.computer_symbol.should eq :X end -Then /^the board should have an X on it$/ do +Then(/^the board should have an X on it$/) do @game.current_state.should include 'X' end -Given /^I am playing X$/ do +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| +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| +When(/^"(.*?)" is not taken$/) do |arg1| @old_pos.should eq " " end -Then /^it is now the computer's turn$/ do +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 +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 +Then(/^I am declared the winner$/) do @game.determine_winner @game.player_won?.should be_true end -Then /^the game ends$/ do +Then(/^the game ends$/) do @game.over?.should be_true end -Given /^there are not three symbols in a row$/ do +Given(/^there are not three symbols in a row$/) do @game.board = { :A1 => :X, :A2 => :O, :A3 => :X, :B1 => :X, :B2 => :O, :B3 => :X, @@ -104,20 +107,20 @@ @game.determine_winner end -When /^there are no open spaces left on the board$/ do +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 +Then(/^the game is declared a draw$/) do @game.draw?.should be_true end -When /^"(.*?)" is taken$/ do |arg1| +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| +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 From cab74f2c5e6d7877a7cc07c6014912fc5d1442ea Mon Sep 17 00:00:00 2001 From: Sean Sehr Date: Tue, 3 Dec 2013 18:21:47 -0800 Subject: [PATCH 3/9] getting Scenario: Computer's Turn to pass --- .../features/step_definitions/tic-tac-toe.rb | 46 +++++++++++-------- 1 file changed, 27 insertions(+), 19 deletions(-) diff --git a/final/features/step_definitions/tic-tac-toe.rb b/final/features/step_definitions/tic-tac-toe.rb index a2aa8b5..10bdd1f 100644 --- a/final/features/step_definitions/tic-tac-toe.rb +++ b/final/features/step_definitions/tic-tac-toe.rb @@ -1,24 +1,18 @@ class TicTacToe attr_accessor :board, :player attr_reader :player_symbol, :computer_symbol - SYMBOLS = ["X", "O"] + SYMBOLS = [:X, :O] - def initialize player = "Player" - @player = player - - @computers_turn = rand() > 0.5 ? true : false - if @computers_turn - @computer_symbol = SYMBOLS[0] - @player_symbol = SYMBOLS[1] - else - @computer_symbol = SYMBOLS[1] - @player_symbol = SYMBOLS[0] - end + def initialize current_player = (rand() > 0.5 ? :computer : :player), symbol = SYMBOLS.sample + @player = "Player" + @computers_turn = current_player == :computer ? "Computer" : @player + @player_symbol = symbol.to_sym + @computer_symbol = @player_symbol == SYMBOLS[0] ? SYMBOLS[1] : SYMBOLS[0]; @board = { - :A1 => false, :A2 => false, :A3 => false, - :B1 => false, :B2 => false, :B3 => false, - :C1 => false, :C2 => false, :C3 => false + :A1 => " ", :A2 => " ", :A3 => " ", + :B1 => " ", :B2 => " ", :B3 => " ", + :C1 => " ", :C2 => " ", :C3 => " " } end @@ -36,7 +30,7 @@ def computer_move end def computer_find_spot - find_empty_spots.sample + open_spots.sample end def indicate_player_turn @@ -49,21 +43,35 @@ def get_player_move move(input) end - def find_empty_spots + def open_spots spots = [] - @board.each{|k, v| spots << k if v == false } + @board.each{|k, v| spots << k if v == " " } spots end def move spot - if @board[spot] == false + r = spot + if @board[spot] == " " symbol = @computers_turn ? @computer_symbol : @player_symbol @board[spot] = symbol next_turn end + r end def next_turn @computers_turn = !@computers_turn end + + def current_state + <<-eos + a b c" + + 1 #{@board[:A1]} | #{@board[:B1]} | #{@board[:C1]} + --- --- --- + 2 #{@board[:A2]} | #{@board[:B2]} | #{@board[:C2]} + --- --- --- + 3 #{@board[:A3]} | #{@board[:B3]} | #{@board[:C3]} + eos + end end From 608859d57c4d52a0a423a8d99d5d74aa08c0a864 Mon Sep 17 00:00:00 2001 From: Sean Sehr Date: Wed, 4 Dec 2013 08:41:04 -0800 Subject: [PATCH 4/9] getting Scenario: Making Moves to pass --- .../features/step_definitions/tic-tac-toe.rb | 36 ++++++++----------- .../step_definitions/tic-tac-toe_steps.rb | 6 ++-- final/play_game.rb | 2 +- 3 files changed, 17 insertions(+), 27 deletions(-) diff --git a/final/features/step_definitions/tic-tac-toe.rb b/final/features/step_definitions/tic-tac-toe.rb index 10bdd1f..e34dd7c 100644 --- a/final/features/step_definitions/tic-tac-toe.rb +++ b/final/features/step_definitions/tic-tac-toe.rb @@ -5,7 +5,7 @@ class TicTacToe def initialize current_player = (rand() > 0.5 ? :computer : :player), symbol = SYMBOLS.sample @player = "Player" - @computers_turn = current_player == :computer ? "Computer" : @player + @computers_turn = current_player == :computer ? true : false @player_symbol = symbol.to_sym @computer_symbol = @player_symbol == SYMBOLS[0] ? SYMBOLS[1] : SYMBOLS[0]; @@ -26,7 +26,9 @@ def current_player def computer_move spot = computer_find_spot - move(spot) + @board[spot] = @computer_symbol + @computers_turn = !@computers_turn + spot end def computer_find_spot @@ -38,9 +40,13 @@ def indicate_player_turn end def get_player_move - STDOUT.flush - input = gets.chomp.upcase.to_sym - move(input) + @input = gets.chomp.upcase.to_sym + player_move + end + + def player_move + @board[@input] = @player_symbol + @input end def open_spots @@ -49,29 +55,15 @@ def open_spots spots end - def move spot - r = spot - if @board[spot] == " " - symbol = @computers_turn ? @computer_symbol : @player_symbol - @board[spot] = symbol - next_turn - end - r - end - - def next_turn - @computers_turn = !@computers_turn - end - def current_state <<-eos a b c" - 1 #{@board[:A1]} | #{@board[:B1]} | #{@board[:C1]} + 1 #{@board[:A1].to_s} | #{@board[:B1].to_s} | #{@board[:C1].to_s} --- --- --- - 2 #{@board[:A2]} | #{@board[:B2]} | #{@board[:C2]} + 2 #{@board[:A2].to_s} | #{@board[:B2].to_s} | #{@board[:C2].to_s} --- --- --- - 3 #{@board[:A3]} | #{@board[:B3]} | #{@board[:C3]} + 3 #{@board[:A3].to_s} | #{@board[:B3].to_s} | #{@board[:C3].to_s} eos end end diff --git a/final/features/step_definitions/tic-tac-toe_steps.rb b/final/features/step_definitions/tic-tac-toe_steps.rb index 6b0bcfd..24bc13b 100644 --- a/final/features/step_definitions/tic-tac-toe_steps.rb +++ b/final/features/step_definitions/tic-tac-toe_steps.rb @@ -26,9 +26,6 @@ end Given(/^it is my turn$/) do - if @game.current_player == "Computer" - @game.computer_move - end @game.current_player.should eq "Renee" end @@ -72,7 +69,8 @@ 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.should_receive(:gets).and_return(arg1) + @game.get_player_move @game.player_move.should eq arg1.to_sym end diff --git a/final/play_game.rb b/final/play_game.rb index 0535830..7b99f10 100644 --- a/final/play_game.rb +++ b/final/play_game.rb @@ -10,7 +10,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 From 69e6537079a78a156733dc951f7b267da7cc0ebe Mon Sep 17 00:00:00 2001 From: Sean Sehr Date: Wed, 4 Dec 2013 16:37:10 -0800 Subject: [PATCH 5/9] getting Scenario: Making Bad Moves to pass --- .../features/step_definitions/tic-tac-toe.rb | 23 ++++++++++++++----- .../step_definitions/tic-tac-toe_steps.rb | 3 +-- 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/final/features/step_definitions/tic-tac-toe.rb b/final/features/step_definitions/tic-tac-toe.rb index e34dd7c..10c39a9 100644 --- a/final/features/step_definitions/tic-tac-toe.rb +++ b/final/features/step_definitions/tic-tac-toe.rb @@ -36,17 +36,28 @@ def computer_find_spot end def indicate_player_turn - "#{@player}'s Move:" + print "#{@player}'s Move:" end - def get_player_move - @input = gets.chomp.upcase.to_sym - player_move + def get_player_move i = gets + indicate_player_turn + i.chomp.upcase.to_sym end def player_move - @board[@input] = @player_symbol - @input + spot = get_player_move.to_sym + r = spot + if @board[spot] != " " + r = spot_taken + else + @board[spot] = @player_symbol + end + r + end + + def spot_taken + print "spot already taken" + player_move end def open_spots diff --git a/final/features/step_definitions/tic-tac-toe_steps.rb b/final/features/step_definitions/tic-tac-toe_steps.rb index 24bc13b..b68b9ed 100644 --- a/final/features/step_definitions/tic-tac-toe_steps.rb +++ b/final/features/step_definitions/tic-tac-toe_steps.rb @@ -69,8 +69,7 @@ When(/^I enter a position "(.*?)" on the board$/) do |arg1| @old_pos = @game.board[arg1.to_sym] - @game.should_receive(:gets).and_return(arg1) - @game.get_player_move + @game.should_receive(:get_player_move).and_return(arg1) @game.player_move.should eq arg1.to_sym end From b80d4e126275d9674347f7b9c7c121c0f5757132 Mon Sep 17 00:00:00 2001 From: Sean Sehr Date: Wed, 4 Dec 2013 19:04:35 -0800 Subject: [PATCH 6/9] getting Scenario: Game is a draw passing --- .../features/step_definitions/tic-tac-toe.rb | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/final/features/step_definitions/tic-tac-toe.rb b/final/features/step_definitions/tic-tac-toe.rb index 10c39a9..7e2e084 100644 --- a/final/features/step_definitions/tic-tac-toe.rb +++ b/final/features/step_definitions/tic-tac-toe.rb @@ -9,6 +9,19 @@ def initialize current_player = (rand() > 0.5 ? :computer : :player), symbol = S @player_symbol = symbol.to_sym @computer_symbol = @player_symbol == SYMBOLS[0] ? SYMBOLS[1] : SYMBOLS[0]; + @wins = [ + [:A1,:A2,:A3], + [:B1,:B2,:B3], + [:C1,:C2,:C3], + + [:A1,:B1,:C1], + [:A2,:B2,:C2], + [:A3,:B3,:C3], + + [:A1,:B2,:C3], + [:C1,:B2,:A3] + ] + @board = { :A1 => " ", :A2 => " ", :A3 => " ", :B1 => " ", :B2 => " ", :B3 => " ", @@ -66,6 +79,36 @@ def open_spots spots end + def determine_winner + @wins.each do |win| + if @board[win[0]] == @board[win[1]] && @board[win[1]] == @board[win[2]] + @player_won = @board[win[0]] == @player_symbol ? true : false + end + end + end + + def spots_open? + @board.values.inject(false){|r, v| r = true if v == " "} + end + + def player_won? + determine_winner + @player_won || nil + end + + def over? + !spots_open? || player_won? != nil ? true : false + end + + def draw? + r = false + if over? && player_won? == nil + puts "test" + r = true + end + r + end + def current_state <<-eos a b c" From 108e8cdab7a99296dafadbd547957cadc6ad9514 Mon Sep 17 00:00:00 2001 From: Sean Sehr Date: Tue, 10 Dec 2013 09:56:26 -0800 Subject: [PATCH 7/9] getting the game to work as expected --- .../features/step_definitions/tic-tac-toe.rb | 39 ++++++++++++++----- 1 file changed, 29 insertions(+), 10 deletions(-) diff --git a/final/features/step_definitions/tic-tac-toe.rb b/final/features/step_definitions/tic-tac-toe.rb index 7e2e084..5c200cf 100644 --- a/final/features/step_definitions/tic-tac-toe.rb +++ b/final/features/step_definitions/tic-tac-toe.rb @@ -8,6 +8,8 @@ def initialize current_player = (rand() > 0.5 ? :computer : :player), symbol = S @computers_turn = current_player == :computer ? true : false @player_symbol = symbol.to_sym @computer_symbol = @player_symbol == SYMBOLS[0] ? SYMBOLS[1] : SYMBOLS[0]; + @game_over = false + @player_won, @computer_won = false, false @wins = [ [:A1,:A2,:A3], @@ -40,7 +42,7 @@ def current_player def computer_move spot = computer_find_spot @board[spot] = @computer_symbol - @computers_turn = !@computers_turn + @computers_turn = false spot end @@ -64,12 +66,13 @@ def player_move r = spot_taken else @board[spot] = @player_symbol + @computers_turn = true end r end def spot_taken - print "spot already taken" + print "spot already taken:" player_move end @@ -81,29 +84,44 @@ def open_spots def determine_winner @wins.each do |win| - if @board[win[0]] == @board[win[1]] && @board[win[1]] == @board[win[2]] - @player_won = @board[win[0]] == @player_symbol ? true : false + if (@board[win[0]] == @board[win[1]] && @board[win[1]] == @board[win[2]]) && @board[win[0]] != " " + @game_over = true + if @board[win[0]] == @player_symbol + @player_won = true + else + @computer_won = true + end end end end def spots_open? - @board.values.inject(false){|r, v| r = true if v == " "} + r = false + @board.each do |k, v| + if v == " " + r = true + end + end + r + end + + def computer_won? + determine_winner + @computer_won end def player_won? determine_winner - @player_won || nil + @player_won end def over? - !spots_open? || player_won? != nil ? true : false + !spots_open? || @game_over ? true : false end def draw? r = false - if over? && player_won? == nil - puts "test" + if over? && player_won? == false && computer_won? == false r = true end r @@ -111,7 +129,8 @@ def draw? def current_state <<-eos - a b c" + + a b c" 1 #{@board[:A1].to_s} | #{@board[:B1].to_s} | #{@board[:C1].to_s} --- --- --- From 1b28f1fe1af72ffe9a37aef277d2b38a59bd513d Mon Sep 17 00:00:00 2001 From: Sean Sehr Date: Tue, 10 Dec 2013 10:05:51 -0800 Subject: [PATCH 8/9] moving final into week7 homework folder --- .../step_definitions/tic-tac-toe-steps.rb | 2 +- .../features/step_definitions/tic-tac-toe.rb | 142 ++++++++++++++++++ 2 files changed, 143 insertions(+), 1 deletion(-) 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..a9bd88e 100644 --- a/week7/homework/features/step_definitions/tic-tac-toe-steps.rb +++ b/week7/homework/features/step_definitions/tic-tac-toe-steps.rb @@ -35,7 +35,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..5c200cf --- /dev/null +++ b/week7/homework/features/step_definitions/tic-tac-toe.rb @@ -0,0 +1,142 @@ +class TicTacToe + attr_accessor :board, :player + attr_reader :player_symbol, :computer_symbol + SYMBOLS = [:X, :O] + + def initialize current_player = (rand() > 0.5 ? :computer : :player), symbol = SYMBOLS.sample + @player = "Player" + @computers_turn = current_player == :computer ? true : false + @player_symbol = symbol.to_sym + @computer_symbol = @player_symbol == SYMBOLS[0] ? SYMBOLS[1] : SYMBOLS[0]; + @game_over = false + @player_won, @computer_won = false, false + + @wins = [ + [:A1,:A2,:A3], + [:B1,:B2,:B3], + [:C1,:C2,:C3], + + [:A1,:B1,:C1], + [:A2,:B2,:C2], + [:A3,:B3,:C3], + + [:A1,:B2,:C3], + [:C1,:B2,:A3] + ] + + @board = { + :A1 => " ", :A2 => " ", :A3 => " ", + :B1 => " ", :B2 => " ", :B3 => " ", + :C1 => " ", :C2 => " ", :C3 => " " + } + end + + def welcome_player + "Welcome #{@player}" + end + + def current_player + @computers_turn ? "Computer" : @player + end + + def computer_move + spot = computer_find_spot + @board[spot] = @computer_symbol + @computers_turn = false + spot + end + + def computer_find_spot + open_spots.sample + end + + def indicate_player_turn + print "#{@player}'s Move:" + end + + def get_player_move i = gets + indicate_player_turn + i.chomp.upcase.to_sym + end + + def player_move + spot = get_player_move.to_sym + r = spot + if @board[spot] != " " + r = spot_taken + else + @board[spot] = @player_symbol + @computers_turn = true + end + r + end + + def spot_taken + print "spot already taken:" + player_move + end + + def open_spots + spots = [] + @board.each{|k, v| spots << k if v == " " } + spots + end + + def determine_winner + @wins.each do |win| + if (@board[win[0]] == @board[win[1]] && @board[win[1]] == @board[win[2]]) && @board[win[0]] != " " + @game_over = true + if @board[win[0]] == @player_symbol + @player_won = true + else + @computer_won = true + end + end + end + end + + def spots_open? + r = false + @board.each do |k, v| + if v == " " + r = true + end + end + r + end + + def computer_won? + determine_winner + @computer_won + end + + def player_won? + determine_winner + @player_won + end + + def over? + !spots_open? || @game_over ? true : false + end + + def draw? + r = false + if over? && player_won? == false && computer_won? == false + r = true + end + r + end + + def current_state + <<-eos + + a b c" + + 1 #{@board[:A1].to_s} | #{@board[:B1].to_s} | #{@board[:C1].to_s} + --- --- --- + 2 #{@board[:A2].to_s} | #{@board[:B2].to_s} | #{@board[:C2].to_s} + --- --- --- + 3 #{@board[:A3].to_s} | #{@board[:B3].to_s} | #{@board[:C3].to_s} + eos + end +end From c39971897e3e7481d6c315c1aec6304a64eb0271 Mon Sep 17 00:00:00 2001 From: Sean Sehr Date: Tue, 10 Dec 2013 10:07:43 -0800 Subject: [PATCH 9/9] removing final folder since it was moved into week7 homework --- .../features/step_definitions/tic-tac-toe.rb | 142 ------------------ .../step_definitions/tic-tac-toe_steps.rb | 124 --------------- final/features/tic-tac-toe.feature | 57 ------- final/play_game.rb | 22 --- 4 files changed, 345 deletions(-) delete mode 100644 final/features/step_definitions/tic-tac-toe.rb delete mode 100644 final/features/step_definitions/tic-tac-toe_steps.rb delete mode 100644 final/features/tic-tac-toe.feature delete mode 100644 final/play_game.rb diff --git a/final/features/step_definitions/tic-tac-toe.rb b/final/features/step_definitions/tic-tac-toe.rb deleted file mode 100644 index 5c200cf..0000000 --- a/final/features/step_definitions/tic-tac-toe.rb +++ /dev/null @@ -1,142 +0,0 @@ -class TicTacToe - attr_accessor :board, :player - attr_reader :player_symbol, :computer_symbol - SYMBOLS = [:X, :O] - - def initialize current_player = (rand() > 0.5 ? :computer : :player), symbol = SYMBOLS.sample - @player = "Player" - @computers_turn = current_player == :computer ? true : false - @player_symbol = symbol.to_sym - @computer_symbol = @player_symbol == SYMBOLS[0] ? SYMBOLS[1] : SYMBOLS[0]; - @game_over = false - @player_won, @computer_won = false, false - - @wins = [ - [:A1,:A2,:A3], - [:B1,:B2,:B3], - [:C1,:C2,:C3], - - [:A1,:B1,:C1], - [:A2,:B2,:C2], - [:A3,:B3,:C3], - - [:A1,:B2,:C3], - [:C1,:B2,:A3] - ] - - @board = { - :A1 => " ", :A2 => " ", :A3 => " ", - :B1 => " ", :B2 => " ", :B3 => " ", - :C1 => " ", :C2 => " ", :C3 => " " - } - end - - def welcome_player - "Welcome #{@player}" - end - - def current_player - @computers_turn ? "Computer" : @player - end - - def computer_move - spot = computer_find_spot - @board[spot] = @computer_symbol - @computers_turn = false - spot - end - - def computer_find_spot - open_spots.sample - end - - def indicate_player_turn - print "#{@player}'s Move:" - end - - def get_player_move i = gets - indicate_player_turn - i.chomp.upcase.to_sym - end - - def player_move - spot = get_player_move.to_sym - r = spot - if @board[spot] != " " - r = spot_taken - else - @board[spot] = @player_symbol - @computers_turn = true - end - r - end - - def spot_taken - print "spot already taken:" - player_move - end - - def open_spots - spots = [] - @board.each{|k, v| spots << k if v == " " } - spots - end - - def determine_winner - @wins.each do |win| - if (@board[win[0]] == @board[win[1]] && @board[win[1]] == @board[win[2]]) && @board[win[0]] != " " - @game_over = true - if @board[win[0]] == @player_symbol - @player_won = true - else - @computer_won = true - end - end - end - end - - def spots_open? - r = false - @board.each do |k, v| - if v == " " - r = true - end - end - r - end - - def computer_won? - determine_winner - @computer_won - end - - def player_won? - determine_winner - @player_won - end - - def over? - !spots_open? || @game_over ? true : false - end - - def draw? - r = false - if over? && player_won? == false && computer_won? == false - r = true - end - r - end - - def current_state - <<-eos - - a b c" - - 1 #{@board[:A1].to_s} | #{@board[:B1].to_s} | #{@board[:C1].to_s} - --- --- --- - 2 #{@board[:A2].to_s} | #{@board[:B2].to_s} | #{@board[:C2].to_s} - --- --- --- - 3 #{@board[:A3].to_s} | #{@board[:B3].to_s} | #{@board[:C3].to_s} - eos - end -end diff --git a/final/features/step_definitions/tic-tac-toe_steps.rb b/final/features/step_definitions/tic-tac-toe_steps.rb deleted file mode 100644 index b68b9ed..0000000 --- a/final/features/step_definitions/tic-tac-toe_steps.rb +++ /dev/null @@ -1,124 +0,0 @@ -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 diff --git a/final/features/tic-tac-toe.feature b/final/features/tic-tac-toe.feature deleted file mode 100644 index 6f3134d..0000000 --- a/final/features/tic-tac-toe.feature +++ /dev/null @@ -1,57 +0,0 @@ -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 diff --git a/final/play_game.rb b/final/play_game.rb deleted file mode 100644 index 7b99f10..0000000 --- a/final/play_game.rb +++ /dev/null @@ -1,22 +0,0 @@ -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?