From 25ddc5637d6817cdd1aba410bfd5e84026c80326 Mon Sep 17 00:00:00 2001 From: Adam Hendricksen Date: Tue, 3 Dec 2013 18:47:03 -0800 Subject: [PATCH 01/45] Adding tic tac toe class to work with --- week7/homework/features/tic-tac-toe.rb | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 week7/homework/features/tic-tac-toe.rb diff --git a/week7/homework/features/tic-tac-toe.rb b/week7/homework/features/tic-tac-toe.rb new file mode 100644 index 0000000..03b7b66 --- /dev/null +++ b/week7/homework/features/tic-tac-toe.rb @@ -0,0 +1,4 @@ +class TicTacToe + def initialize + end +end \ No newline at end of file From 0a6da714b441a266b7107bed317585c8493436ef Mon Sep 17 00:00:00 2001 From: Adam Hendricksen Date: Tue, 3 Dec 2013 18:52:43 -0800 Subject: [PATCH 02/45] Accepting a player local variable --- week7/homework/features/tic-tac-toe.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/week7/homework/features/tic-tac-toe.rb b/week7/homework/features/tic-tac-toe.rb index 03b7b66..8839601 100644 --- a/week7/homework/features/tic-tac-toe.rb +++ b/week7/homework/features/tic-tac-toe.rb @@ -1,4 +1,5 @@ class TicTacToe + attr_accessor :player def initialize end end \ No newline at end of file From 695eafb2d2f3b56f2b65f4d2f8c6391fba135838 Mon Sep 17 00:00:00 2001 From: Adam Hendricksen Date: Tue, 3 Dec 2013 18:59:45 -0800 Subject: [PATCH 03/45] Welcoming player --- week7/homework/features/tic-tac-toe.rb | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/week7/homework/features/tic-tac-toe.rb b/week7/homework/features/tic-tac-toe.rb index 8839601..1259cf2 100644 --- a/week7/homework/features/tic-tac-toe.rb +++ b/week7/homework/features/tic-tac-toe.rb @@ -2,4 +2,9 @@ class TicTacToe attr_accessor :player def initialize end + + + def welcome_player + "Welcome #{@player}" + end end \ No newline at end of file From 1385fae7d783eac858bb9ed2df014392b400b830 Mon Sep 17 00:00:00 2001 From: Adam Hendricksen Date: Tue, 3 Dec 2013 19:01:30 -0800 Subject: [PATCH 04/45] Don't need the initialize --- week7/homework/features/tic-tac-toe.rb | 3 --- 1 file changed, 3 deletions(-) diff --git a/week7/homework/features/tic-tac-toe.rb b/week7/homework/features/tic-tac-toe.rb index 1259cf2..6cfaf26 100644 --- a/week7/homework/features/tic-tac-toe.rb +++ b/week7/homework/features/tic-tac-toe.rb @@ -1,8 +1,5 @@ class TicTacToe attr_accessor :player - def initialize - end - def welcome_player "Welcome #{@player}" From 9168f4c2c691203c9bfe935ed6d7a0dfee78d427 Mon Sep 17 00:00:00 2001 From: Adam Hendricksen Date: Tue, 3 Dec 2013 19:11:51 -0800 Subject: [PATCH 05/45] Sampling an array for a random player --- week7/homework/features/tic-tac-toe.rb | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/week7/homework/features/tic-tac-toe.rb b/week7/homework/features/tic-tac-toe.rb index 6cfaf26..3b9491d 100644 --- a/week7/homework/features/tic-tac-toe.rb +++ b/week7/homework/features/tic-tac-toe.rb @@ -4,4 +4,8 @@ class TicTacToe def welcome_player "Welcome #{@player}" end + + def current_player + [@player, "Computer"].sample + end end \ No newline at end of file From ebaa8e5b6d8cd59b065acce23a4438050617bbcb Mon Sep 17 00:00:00 2001 From: Adam Hendricksen Date: Tue, 3 Dec 2013 19:26:56 -0800 Subject: [PATCH 06/45] SYMBOLS should be defined with setter methods --- week7/homework/features/tic-tac-toe.rb | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/week7/homework/features/tic-tac-toe.rb b/week7/homework/features/tic-tac-toe.rb index 3b9491d..1d98763 100644 --- a/week7/homework/features/tic-tac-toe.rb +++ b/week7/homework/features/tic-tac-toe.rb @@ -1,6 +1,7 @@ class TicTacToe - attr_accessor :player - + attr_accessor :player, :player_symbol, :computer_symbol + SYMBOLS = @player_symbol, @computer_symbol + def welcome_player "Welcome #{@player}" end From d6fb7c22b2be0f4e5eccef1f2b77aa91436502e5 Mon Sep 17 00:00:00 2001 From: Adam Hendricksen Date: Tue, 3 Dec 2013 19:57:27 -0800 Subject: [PATCH 07/45] Turns out initialize is needed --- week7/homework/features/tic-tac-toe.rb | 3 +++ 1 file changed, 3 insertions(+) diff --git a/week7/homework/features/tic-tac-toe.rb b/week7/homework/features/tic-tac-toe.rb index 1d98763..427a7df 100644 --- a/week7/homework/features/tic-tac-toe.rb +++ b/week7/homework/features/tic-tac-toe.rb @@ -9,4 +9,7 @@ def welcome_player def current_player [@player, "Computer"].sample end + + def initialize name + end end \ No newline at end of file From 21e7719cc325e31935aa47eaf22617f923f29f41 Mon Sep 17 00:00:00 2001 From: Adam Hendricksen Date: Tue, 3 Dec 2013 20:03:40 -0800 Subject: [PATCH 08/45] Initialize has variable numbers of arguments --- week7/homework/features/tic-tac-toe.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/week7/homework/features/tic-tac-toe.rb b/week7/homework/features/tic-tac-toe.rb index 427a7df..05a9e3d 100644 --- a/week7/homework/features/tic-tac-toe.rb +++ b/week7/homework/features/tic-tac-toe.rb @@ -10,6 +10,6 @@ def current_player [@player, "Computer"].sample end - def initialize name + def initialize *name end end \ No newline at end of file From 211028b1e59795c465d9ef08ad0c2f00da1d4ed0 Mon Sep 17 00:00:00 2001 From: Adam Hendricksen Date: Tue, 3 Dec 2013 20:31:56 -0800 Subject: [PATCH 09/45] I think I need to keep track of the current player --- week7/homework/features/tic-tac-toe.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/week7/homework/features/tic-tac-toe.rb b/week7/homework/features/tic-tac-toe.rb index 05a9e3d..cdcd010 100644 --- a/week7/homework/features/tic-tac-toe.rb +++ b/week7/homework/features/tic-tac-toe.rb @@ -1,13 +1,14 @@ class TicTacToe attr_accessor :player, :player_symbol, :computer_symbol SYMBOLS = @player_symbol, @computer_symbol + @current def welcome_player "Welcome #{@player}" end def current_player - [@player, "Computer"].sample + @current = [@player, "Computer"].sample end def initialize *name From 6759f4cec07c3c55fbf1a131fc32994bfc695c9f Mon Sep 17 00:00:00 2001 From: Adam Hendricksen Date: Thu, 5 Dec 2013 13:09:22 -0800 Subject: [PATCH 10/45] Fixing typo --- week7/homework/play_game.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/week7/homework/play_game.rb b/week7/homework/play_game.rb index 0535830..7b99f10 100644 --- a/week7/homework/play_game.rb +++ b/week7/homework/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 601503058e024c9d67c0a749816adf0dc1c80921 Mon Sep 17 00:00:00 2001 From: Adam Hendricksen Date: Thu, 5 Dec 2013 13:11:57 -0800 Subject: [PATCH 11/45] Turns must alternate --- week7/homework/features/tic-tac-toe.rb | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/week7/homework/features/tic-tac-toe.rb b/week7/homework/features/tic-tac-toe.rb index cdcd010..88f32e7 100644 --- a/week7/homework/features/tic-tac-toe.rb +++ b/week7/homework/features/tic-tac-toe.rb @@ -1,16 +1,19 @@ class TicTacToe attr_accessor :player, :player_symbol, :computer_symbol SYMBOLS = @player_symbol, @computer_symbol - @current + @current = "Computer" def welcome_player "Welcome #{@player}" end def current_player - @current = [@player, "Computer"].sample + @current = @player if @current == "Computer" + @current = "Computer" if @current == @player + @current end def initialize *name + @current = [@player, "Computer"].sample unless name[0] end end \ No newline at end of file From 8cab71977495803a871e11a790c86465419bb75c Mon Sep 17 00:00:00 2001 From: Adam Hendricksen Date: Thu, 5 Dec 2013 13:13:38 -0800 Subject: [PATCH 12/45] Was changing player back into computer --- week7/homework/features/tic-tac-toe.rb | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/week7/homework/features/tic-tac-toe.rb b/week7/homework/features/tic-tac-toe.rb index 88f32e7..e242298 100644 --- a/week7/homework/features/tic-tac-toe.rb +++ b/week7/homework/features/tic-tac-toe.rb @@ -8,8 +8,11 @@ def welcome_player end def current_player - @current = @player if @current == "Computer" - @current = "Computer" if @current == @player + if @current == "Computer" + @current = @player + else + @current = "Computer" + end @current end From dbb444c7793d2541493ae6693a4fad3ceaa03000 Mon Sep 17 00:00:00 2001 From: Adam Hendricksen Date: Thu, 5 Dec 2013 13:16:01 -0800 Subject: [PATCH 13/45] Apparantly my logic was backwards --- week7/homework/features/tic-tac-toe.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/week7/homework/features/tic-tac-toe.rb b/week7/homework/features/tic-tac-toe.rb index e242298..fc159d1 100644 --- a/week7/homework/features/tic-tac-toe.rb +++ b/week7/homework/features/tic-tac-toe.rb @@ -1,7 +1,7 @@ class TicTacToe attr_accessor :player, :player_symbol, :computer_symbol SYMBOLS = @player_symbol, @computer_symbol - @current = "Computer" + @current def welcome_player "Welcome #{@player}" @@ -9,9 +9,9 @@ def welcome_player def current_player if @current == "Computer" - @current = @player - else @current = "Computer" + else + @current = @player end @current end From b9f10e41db80acfee12ef173806538b8eef4a4f6 Mon Sep 17 00:00:00 2001 From: Adam Hendricksen Date: Thu, 5 Dec 2013 13:21:34 -0800 Subject: [PATCH 14/45] Adding methods --- .../homework/features/step_definitions/tic-tac-toe-steps.rb | 2 +- week7/homework/features/tic-tac-toe.rb | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) 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/tic-tac-toe.rb b/week7/homework/features/tic-tac-toe.rb index fc159d1..9f17e90 100644 --- a/week7/homework/features/tic-tac-toe.rb +++ b/week7/homework/features/tic-tac-toe.rb @@ -19,4 +19,10 @@ def current_player def initialize *name @current = [@player, "Computer"].sample unless name[0] end + + def indicate_player_turn + end + + def get_player_move + end end \ No newline at end of file From baa8758e081197b93c036dcb59f00c94827d3cdb Mon Sep 17 00:00:00 2001 From: Adam Hendricksen Date: Thu, 5 Dec 2013 13:30:25 -0800 Subject: [PATCH 15/45] Think this is actually the right logic, with a computer instance variable --- week7/homework/features/tic-tac-toe.rb | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/week7/homework/features/tic-tac-toe.rb b/week7/homework/features/tic-tac-toe.rb index 9f17e90..0538fd0 100644 --- a/week7/homework/features/tic-tac-toe.rb +++ b/week7/homework/features/tic-tac-toe.rb @@ -1,6 +1,7 @@ class TicTacToe attr_accessor :player, :player_symbol, :computer_symbol SYMBOLS = @player_symbol, @computer_symbol + @computer = "computer" @current def welcome_player @@ -8,16 +9,16 @@ def welcome_player end def current_player - if @current == "Computer" - @current = "Computer" - else + if @current == @computer @current = @player + else + @current = @computer end @current end def initialize *name - @current = [@player, "Computer"].sample unless name[0] + @current = [@player, @computer].sample unless name[0] end def indicate_player_turn From 4d62cdafe354e7ba587423573e4d09ef8736535b Mon Sep 17 00:00:00 2001 From: Adam Hendricksen Date: Thu, 5 Dec 2013 13:47:11 -0800 Subject: [PATCH 16/45] Adding player symbols --- week7/homework/features/tic-tac-toe.rb | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/week7/homework/features/tic-tac-toe.rb b/week7/homework/features/tic-tac-toe.rb index 0538fd0..649176c 100644 --- a/week7/homework/features/tic-tac-toe.rb +++ b/week7/homework/features/tic-tac-toe.rb @@ -3,6 +3,8 @@ class TicTacToe SYMBOLS = @player_symbol, @computer_symbol @computer = "computer" @current + @player_symbol + @computer_symbol def welcome_player "Welcome #{@player}" @@ -19,6 +21,13 @@ def current_player def initialize *name @current = [@player, @computer].sample unless name[0] + if name[1] == 'X' + @player_symbol = :O + @computer_symbol = :X + else + @player_symbol = :X + @computer_symbol = :O + end end def indicate_player_turn From 085ff69595510267280c7cbad65999d4386eaeb2 Mon Sep 17 00:00:00 2001 From: Adam Hendricksen Date: Thu, 5 Dec 2013 14:17:40 -0800 Subject: [PATCH 17/45] I'm shooting in the dark, no idea what I'm hitting... --- week7/homework/features/tic-tac-toe.rb | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/week7/homework/features/tic-tac-toe.rb b/week7/homework/features/tic-tac-toe.rb index 649176c..968e674 100644 --- a/week7/homework/features/tic-tac-toe.rb +++ b/week7/homework/features/tic-tac-toe.rb @@ -1,10 +1,7 @@ class TicTacToe - attr_accessor :player, :player_symbol, :computer_symbol + attr_accessor :player, :computer, :player_symbol, :computer_symbol SYMBOLS = @player_symbol, @computer_symbol - @computer = "computer" @current - @player_symbol - @computer_symbol def welcome_player "Welcome #{@player}" @@ -20,14 +17,9 @@ def current_player end def initialize *name - @current = [@player, @computer].sample unless name[0] - if name[1] == 'X' - @player_symbol = :O - @computer_symbol = :X - else - @player_symbol = :X - @computer_symbol = :O - end + symbols = [:X, :O] + name[1] ||= symbols.sample + @player_symbol = symbols.fetch(symbols.index(name[1])) end def indicate_player_turn From 5c9905fd84b24700efce7df486f93bc8ab231c5e Mon Sep 17 00:00:00 2001 From: Adam Hendricksen Date: Thu, 5 Dec 2013 14:23:05 -0800 Subject: [PATCH 18/45] Finally making computer accepted as a player --- week7/homework/features/tic-tac-toe.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/week7/homework/features/tic-tac-toe.rb b/week7/homework/features/tic-tac-toe.rb index 968e674..c97375c 100644 --- a/week7/homework/features/tic-tac-toe.rb +++ b/week7/homework/features/tic-tac-toe.rb @@ -1,6 +1,6 @@ class TicTacToe attr_accessor :player, :computer, :player_symbol, :computer_symbol - SYMBOLS = @player_symbol, @computer_symbol + SYMBOLS = [@player_symbol, @computer_symbol] @current def welcome_player @@ -20,6 +20,8 @@ def initialize *name symbols = [:X, :O] name[1] ||= symbols.sample @player_symbol = symbols.fetch(symbols.index(name[1])) + @computer_symbol = symbols.delete(symbols.index(@player_symbol)) + @player = name.first.to_s.capitalize end def indicate_player_turn From 8b21e6b1dccff59d6699f8f7152c68617a133bf6 Mon Sep 17 00:00:00 2001 From: Adam Hendricksen Date: Thu, 5 Dec 2013 14:54:10 -0800 Subject: [PATCH 19/45] Figuring out a robust way to handle play symbols --- week7/homework/features/tic-tac-toe.rb | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/week7/homework/features/tic-tac-toe.rb b/week7/homework/features/tic-tac-toe.rb index c97375c..60a1e0e 100644 --- a/week7/homework/features/tic-tac-toe.rb +++ b/week7/homework/features/tic-tac-toe.rb @@ -1,6 +1,6 @@ class TicTacToe attr_accessor :player, :computer, :player_symbol, :computer_symbol - SYMBOLS = [@player_symbol, @computer_symbol] + SYMBOLS = [] @current def welcome_player @@ -19,9 +19,10 @@ def current_player def initialize *name symbols = [:X, :O] name[1] ||= symbols.sample - @player_symbol = symbols.fetch(symbols.index(name[1])) - @computer_symbol = symbols.delete(symbols.index(@player_symbol)) + @player_symbol = (symbols.reject {|s| s != name[1]}).first + @computer_symbol = (symbols.reject {|s| s == @player_symbol}).first @player = name.first.to_s.capitalize + SYMBOLS << @player_symbol << @computer_symbol end def indicate_player_turn From 247fbc0a584f0fd1cd1970205ecb956681087d36 Mon Sep 17 00:00:00 2001 From: Adam Hendricksen Date: Thu, 5 Dec 2013 15:44:26 -0800 Subject: [PATCH 20/45] Trying to setup the game board --- week7/homework/features/tic-tac-toe.rb | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/week7/homework/features/tic-tac-toe.rb b/week7/homework/features/tic-tac-toe.rb index 60a1e0e..665c4b8 100644 --- a/week7/homework/features/tic-tac-toe.rb +++ b/week7/homework/features/tic-tac-toe.rb @@ -1,7 +1,10 @@ class TicTacToe - attr_accessor :player, :computer, :player_symbol, :computer_symbol + attr_accessor :player, :computer, :player_symbol, :computer_symbol, :board SYMBOLS = [] @current + @board = {A1: ' ', A2: ' ', A3: ' ', + B1: ' ', B2: ' ', B3: ' ', + C1: ' ', C2: ' ', C3: ' '} def welcome_player "Welcome #{@player}" From 80e1dda09529abce8558bedcf5a6f9b3ec10eb79 Mon Sep 17 00:00:00 2001 From: Adam Hendricksen Date: Fri, 6 Dec 2013 14:31:24 -0800 Subject: [PATCH 21/45] Making use of that SYMBOLS constant... good refactor --- week7/homework/features/tic-tac-toe.rb | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/week7/homework/features/tic-tac-toe.rb b/week7/homework/features/tic-tac-toe.rb index 665c4b8..8e40fb6 100644 --- a/week7/homework/features/tic-tac-toe.rb +++ b/week7/homework/features/tic-tac-toe.rb @@ -1,6 +1,6 @@ class TicTacToe attr_accessor :player, :computer, :player_symbol, :computer_symbol, :board - SYMBOLS = [] + SYMBOLS = [:X, :O] @current @board = {A1: ' ', A2: ' ', A3: ' ', B1: ' ', B2: ' ', B3: ' ', @@ -20,12 +20,10 @@ def current_player end def initialize *name - symbols = [:X, :O] - name[1] ||= symbols.sample - @player_symbol = (symbols.reject {|s| s != name[1]}).first - @computer_symbol = (symbols.reject {|s| s == @player_symbol}).first + name[1] ||= SYMBOLS.sample + @player_symbol = (SYMBOLS.reject {|s| s != name[1]}).first + @computer_symbol = (SYMBOLS.reject {|s| s == @player_symbol}).first @player = name.first.to_s.capitalize - SYMBOLS << @player_symbol << @computer_symbol end def indicate_player_turn From a1c7e3286e479674a5b332ec53ba8904bc699a28 Mon Sep 17 00:00:00 2001 From: Adam Hendricksen Date: Fri, 6 Dec 2013 14:38:05 -0800 Subject: [PATCH 22/45] Setting up methods for computer move. --- week7/homework/features/tic-tac-toe.rb | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/week7/homework/features/tic-tac-toe.rb b/week7/homework/features/tic-tac-toe.rb index 8e40fb6..2e89353 100644 --- a/week7/homework/features/tic-tac-toe.rb +++ b/week7/homework/features/tic-tac-toe.rb @@ -31,4 +31,13 @@ def indicate_player_turn def get_player_move end + + def computer_move + # Should sample all the open spots, and fill it on the board with the computer_symbol + end + + def open_spots + # Should work through the board, making an array of all open spots + + end end \ No newline at end of file From a6977e44921918b032ab03a1cbc62bde62986d39 Mon Sep 17 00:00:00 2001 From: Adam Hendricksen Date: Fri, 6 Dec 2013 14:48:53 -0800 Subject: [PATCH 23/45] Successfully generating array of empty spots --- week7/homework/features/tic-tac-toe.rb | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/week7/homework/features/tic-tac-toe.rb b/week7/homework/features/tic-tac-toe.rb index 2e89353..7688e32 100644 --- a/week7/homework/features/tic-tac-toe.rb +++ b/week7/homework/features/tic-tac-toe.rb @@ -2,7 +2,7 @@ class TicTacToe attr_accessor :player, :computer, :player_symbol, :computer_symbol, :board SYMBOLS = [:X, :O] @current - @board = {A1: ' ', A2: ' ', A3: ' ', + BOARD = {A1: ' ', A2: ' ', A3: ' ', B1: ' ', B2: ' ', B3: ' ', C1: ' ', C2: ' ', C3: ' '} @@ -38,6 +38,8 @@ def computer_move def open_spots # Should work through the board, making an array of all open spots - + BOARD.map do |spot, fill| + spot if fill == ' ' + end end end \ No newline at end of file From b6be44f8a508c3a0a718bd90526ebf7cb25aaa21 Mon Sep 17 00:00:00 2001 From: Adam Hendricksen Date: Fri, 6 Dec 2013 14:52:22 -0800 Subject: [PATCH 24/45] Passing test with random computer move --- week7/homework/features/tic-tac-toe.rb | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/week7/homework/features/tic-tac-toe.rb b/week7/homework/features/tic-tac-toe.rb index 7688e32..1b7e3f0 100644 --- a/week7/homework/features/tic-tac-toe.rb +++ b/week7/homework/features/tic-tac-toe.rb @@ -2,7 +2,7 @@ class TicTacToe attr_accessor :player, :computer, :player_symbol, :computer_symbol, :board SYMBOLS = [:X, :O] @current - BOARD = {A1: ' ', A2: ' ', A3: ' ', + BOARD = { A1: ' ', A2: ' ', A3: ' ', B1: ' ', B2: ' ', B3: ' ', C1: ' ', C2: ' ', C3: ' '} @@ -33,11 +33,10 @@ def get_player_move end def computer_move - # Should sample all the open spots, and fill it on the board with the computer_symbol + open_spots.sample end def open_spots - # Should work through the board, making an array of all open spots BOARD.map do |spot, fill| spot if fill == ' ' end From d3610dc66cddae635da2c61e745e73c3eef62236 Mon Sep 17 00:00:00 2001 From: Adam Hendricksen Date: Fri, 6 Dec 2013 15:13:21 -0800 Subject: [PATCH 25/45] Building displayable board --- week7/homework/features/tic-tac-toe.rb | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/week7/homework/features/tic-tac-toe.rb b/week7/homework/features/tic-tac-toe.rb index 1b7e3f0..2487e54 100644 --- a/week7/homework/features/tic-tac-toe.rb +++ b/week7/homework/features/tic-tac-toe.rb @@ -33,7 +33,9 @@ def get_player_move end def computer_move - open_spots.sample + move = open_spots.sample + BOARD[move] = @computer_symbol.to_s + move end def open_spots @@ -41,4 +43,16 @@ def open_spots spot if fill == ' ' end end + + def current_state + #current state is a display method, building a string that pleasingly displays the board + <<-board + 1 2 3 + A #{BOARD[:A1]} | #{BOARD[:A2]} | #{BOARD[:A3]} + --- --- --- + B #{BOARD[:B1]} | #{BOARD[:B2]} | #{BOARD[:B3]} + --- --- --- + C #{BOARD[:C1]} | #{BOARD[:C2]} | #{BOARD[:C3]} + board + end end \ No newline at end of file From b845badbba4998d9c23ef11de7e75cbf9d46be71 Mon Sep 17 00:00:00 2001 From: Adam Hendricksen Date: Fri, 6 Dec 2013 17:27:08 -0800 Subject: [PATCH 26/45] Making use of attr_accessor to modify board --- week7/homework/features/tic-tac-toe.rb | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/week7/homework/features/tic-tac-toe.rb b/week7/homework/features/tic-tac-toe.rb index 2487e54..64f77d6 100644 --- a/week7/homework/features/tic-tac-toe.rb +++ b/week7/homework/features/tic-tac-toe.rb @@ -2,9 +2,7 @@ class TicTacToe attr_accessor :player, :computer, :player_symbol, :computer_symbol, :board SYMBOLS = [:X, :O] @current - BOARD = { A1: ' ', A2: ' ', A3: ' ', - B1: ' ', B2: ' ', B3: ' ', - C1: ' ', C2: ' ', C3: ' '} + def welcome_player "Welcome #{@player}" @@ -24,6 +22,9 @@ def initialize *name @player_symbol = (SYMBOLS.reject {|s| s != name[1]}).first @computer_symbol = (SYMBOLS.reject {|s| s == @player_symbol}).first @player = name.first.to_s.capitalize + @board = {A1: ' ', A2: ' ', A3: ' ', + B1: ' ', B2: ' ', B3: ' ', + C1: ' ', C2: ' ', C3: ' '} end def indicate_player_turn @@ -34,25 +35,25 @@ def get_player_move def computer_move move = open_spots.sample - BOARD[move] = @computer_symbol.to_s + @board[move] = @computer_symbol.to_s move end def open_spots - BOARD.map do |spot, fill| + @board.map do |spot, fill| spot if fill == ' ' end end def current_state - #current state is a display method, building a string that pleasingly displays the board + #current state is a display method, building a string that pleasingly displays the @board <<-board 1 2 3 - A #{BOARD[:A1]} | #{BOARD[:A2]} | #{BOARD[:A3]} + A #{@board[:A1]} | #{@board[:A2]} | #{@board[:A3]} --- --- --- - B #{BOARD[:B1]} | #{BOARD[:B2]} | #{BOARD[:B3]} + B #{@board[:B1]} | #{@board[:B2]} | #{@board[:B3]} --- --- --- - C #{BOARD[:C1]} | #{BOARD[:C2]} | #{BOARD[:C3]} + C #{@board[:C1]} | #{@board[:C2]} | #{@board[:C3]} board end end \ No newline at end of file From 39a6f82c8c8169436f2b1f0aa51ecb3b43c484fa Mon Sep 17 00:00:00 2001 From: Adam Hendricksen Date: Fri, 6 Dec 2013 17:30:39 -0800 Subject: [PATCH 27/45] Using player move to change board --- week7/homework/features/tic-tac-toe.rb | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/week7/homework/features/tic-tac-toe.rb b/week7/homework/features/tic-tac-toe.rb index 64f77d6..f439321 100644 --- a/week7/homework/features/tic-tac-toe.rb +++ b/week7/homework/features/tic-tac-toe.rb @@ -39,6 +39,12 @@ def computer_move move end + def player_move + move = get_player_move + @board[move] = @player_symbol.to_s + move.to_sym + end + def open_spots @board.map do |spot, fill| spot if fill == ' ' From 230b0ebc67b23d907028b8426a6ed14491dff88b Mon Sep 17 00:00:00 2001 From: Adam Hendricksen Date: Fri, 6 Dec 2013 18:31:43 -0800 Subject: [PATCH 28/45] Making sure that player move is valid --- week7/homework/features/tic-tac-toe.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/week7/homework/features/tic-tac-toe.rb b/week7/homework/features/tic-tac-toe.rb index f439321..4b67e70 100644 --- a/week7/homework/features/tic-tac-toe.rb +++ b/week7/homework/features/tic-tac-toe.rb @@ -40,7 +40,7 @@ def computer_move end def player_move - move = get_player_move + move = get_player_move unless open_spots.include? get_player_move @board[move] = @player_symbol.to_s move.to_sym end From 7dd6814bfbf0dd369c2f3cc025cdc4c822354cb4 Mon Sep 17 00:00:00 2001 From: Adam Hendricksen Date: Fri, 6 Dec 2013 18:45:07 -0800 Subject: [PATCH 29/45] Player input must be a symbol to key the hash --- week7/homework/features/tic-tac-toe.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/week7/homework/features/tic-tac-toe.rb b/week7/homework/features/tic-tac-toe.rb index 4b67e70..95980e9 100644 --- a/week7/homework/features/tic-tac-toe.rb +++ b/week7/homework/features/tic-tac-toe.rb @@ -31,6 +31,7 @@ def indicate_player_turn end def get_player_move + gets end def computer_move @@ -40,7 +41,7 @@ def computer_move end def player_move - move = get_player_move unless open_spots.include? get_player_move + move = get_player_move.to_sym unless open_spots.include? get_player_move @board[move] = @player_symbol.to_s move.to_sym end From b648e6ab9793aa99a19e9c776ad6f0ff1d178143 Mon Sep 17 00:00:00 2001 From: Adam Hendricksen Date: Sat, 7 Dec 2013 18:34:51 -0800 Subject: [PATCH 30/45] Developing private method to assess winning situations --- week7/homework/features/tic-tac-toe.rb | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/week7/homework/features/tic-tac-toe.rb b/week7/homework/features/tic-tac-toe.rb index 95980e9..3c2c4c0 100644 --- a/week7/homework/features/tic-tac-toe.rb +++ b/week7/homework/features/tic-tac-toe.rb @@ -63,4 +63,26 @@ def current_state C #{@board[:C1]} | #{@board[:C2]} | #{@board[:C3]} board end + + def determine_winner + puts "Game won: #{winning_lines :X}" + end + + private + def winning_lines symbol + winning_lines = [[:A1, :A2, :A3], + [:B1, :B2, :B3], + [:C1, :C2, :C3], + [:A1, :B1, :C1], + [:A2, :B2, :C2], + [:A3, :B3, :C3], + [:A3, :B2, :C1], + [:A1, :B2, :C3]] + winning_lines.inject(false) do |won, line| + line_wins = line.inject(true) do |winning, spot| + winning = winning and @board[spot] == symbol + end + won = won or line_wins + end + end end \ No newline at end of file From b3f19e7da3d4ddee34c1073fc2827c4444188dc2 Mon Sep 17 00:00:00 2001 From: Adam Hendricksen Date: Sat, 7 Dec 2013 18:37:51 -0800 Subject: [PATCH 31/45] Commenting because the logical math may not be intuitive --- week7/homework/features/tic-tac-toe.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/week7/homework/features/tic-tac-toe.rb b/week7/homework/features/tic-tac-toe.rb index 3c2c4c0..db75b22 100644 --- a/week7/homework/features/tic-tac-toe.rb +++ b/week7/homework/features/tic-tac-toe.rb @@ -80,8 +80,10 @@ def winning_lines symbol [:A1, :B2, :C3]] winning_lines.inject(false) do |won, line| line_wins = line.inject(true) do |winning, spot| + # A single false creates a false value winning = winning and @board[spot] == symbol end + # A single true creates a true value won = won or line_wins end end From e882bbd581bf0e16ed506f799b8dcf9945b61843 Mon Sep 17 00:00:00 2001 From: Adam Hendricksen Date: Sat, 7 Dec 2013 18:48:46 -0800 Subject: [PATCH 32/45] First pass at declaring a winner and game over --- week7/homework/features/tic-tac-toe.rb | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/week7/homework/features/tic-tac-toe.rb b/week7/homework/features/tic-tac-toe.rb index db75b22..ca1a088 100644 --- a/week7/homework/features/tic-tac-toe.rb +++ b/week7/homework/features/tic-tac-toe.rb @@ -1,6 +1,8 @@ class TicTacToe attr_accessor :player, :computer, :player_symbol, :computer_symbol, :board SYMBOLS = [:X, :O] + @winner = false + @game_over = false @current @@ -65,7 +67,19 @@ def current_state end def determine_winner - puts "Game won: #{winning_lines :X}" + [:X, :O].each do |token| + puts "Player #{token} just won!" if winning_lines token + @winner = true + @game_over = true + end + end + + def player_won? + @winner + end + + def over? + @game_over end private From f71cd2f574c05e96d7a10cb0362a011d740d8a1b Mon Sep 17 00:00:00 2001 From: Adam Hendricksen Date: Sat, 7 Dec 2013 19:08:49 -0800 Subject: [PATCH 33/45] Determined where draws occur --- week7/homework/features/tic-tac-toe.rb | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/week7/homework/features/tic-tac-toe.rb b/week7/homework/features/tic-tac-toe.rb index ca1a088..b20e2eb 100644 --- a/week7/homework/features/tic-tac-toe.rb +++ b/week7/homework/features/tic-tac-toe.rb @@ -67,9 +67,13 @@ def current_state end def determine_winner - [:X, :O].each do |token| - puts "Player #{token} just won!" if winning_lines token - @winner = true + if spots_open? + [:X, :O].each do |token| + @winner = true + @game_over = true + end + else + @winner = false @game_over = true end end @@ -82,6 +86,14 @@ def over? @game_over end + def draw? + @game_over == true and @winner == false + end + + def spots_open? + @board.has_value?(' ') + end + private def winning_lines symbol winning_lines = [[:A1, :A2, :A3], From b556e04f1e45d85be3ad3054eb6bb96ba8478790 Mon Sep 17 00:00:00 2001 From: Adam Hendricksen Date: Sat, 7 Dec 2013 20:43:14 -0800 Subject: [PATCH 34/45] Can not play the game, despite tests passing. Checking functionality --- week7/homework/features/tic-tac-toe.rb | 7 ++++++- week7/homework/play_game.rb | 2 +- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/week7/homework/features/tic-tac-toe.rb b/week7/homework/features/tic-tac-toe.rb index b20e2eb..df739a9 100644 --- a/week7/homework/features/tic-tac-toe.rb +++ b/week7/homework/features/tic-tac-toe.rb @@ -3,7 +3,6 @@ class TicTacToe SYMBOLS = [:X, :O] @winner = false @game_over = false - @current def welcome_player @@ -30,9 +29,11 @@ def initialize *name end def indicate_player_turn + "Your Turn" end def get_player_move + puts "#{@player}'s Move:" gets end @@ -82,6 +83,10 @@ def player_won? @winner end + def computer_won? + @winner + end + def over? @game_over end diff --git a/week7/homework/play_game.rb b/week7/homework/play_game.rb index 7b99f10..a28dc5c 100644 --- a/week7/homework/play_game.rb +++ b/week7/homework/play_game.rb @@ -1,4 +1,4 @@ -require './features/step_definitions/tic-tac-toe.rb' +require './features/tic-tac-toe.rb' @game = TicTacToe.new puts "What is your name?" From 117330d6923cd1dad84cba6f59dba2ef8623e77a Mon Sep 17 00:00:00 2001 From: Adam Hendricksen Date: Sat, 7 Dec 2013 20:51:05 -0800 Subject: [PATCH 35/45] Reworking current_player and making sure tests pass --- week7/homework/features/tic-tac-toe.rb | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/week7/homework/features/tic-tac-toe.rb b/week7/homework/features/tic-tac-toe.rb index df739a9..c41a25b 100644 --- a/week7/homework/features/tic-tac-toe.rb +++ b/week7/homework/features/tic-tac-toe.rb @@ -1,8 +1,9 @@ class TicTacToe - attr_accessor :player, :computer, :player_symbol, :computer_symbol, :board + attr_accessor :player, :player_symbol, :computer_symbol, :board SYMBOLS = [:X, :O] @winner = false @game_over = false + @current def welcome_player @@ -10,12 +11,12 @@ def welcome_player end def current_player - if @current == @computer - @current = @player + @current ||= [@player, "Computer"].sample + if @current == @player + @current = "Computer" else - @current = @computer + @current = @player end - @current end def initialize *name @@ -29,7 +30,7 @@ def initialize *name end def indicate_player_turn - "Your Turn" + end def get_player_move From 8ab618e9267d7ba0b3b3703fa3e56b137bf7c5e6 Mon Sep 17 00:00:00 2001 From: Adam Hendricksen Date: Sat, 7 Dec 2013 20:59:54 -0800 Subject: [PATCH 36/45] Fixing lack of specifing starting player --- week7/homework/features/tic-tac-toe.rb | 4 +++- week7/homework/play_game.rb | 1 + 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/week7/homework/features/tic-tac-toe.rb b/week7/homework/features/tic-tac-toe.rb index c41a25b..3c310b4 100644 --- a/week7/homework/features/tic-tac-toe.rb +++ b/week7/homework/features/tic-tac-toe.rb @@ -7,6 +7,7 @@ class TicTacToe def welcome_player + current_state "Welcome #{@player}" end @@ -20,6 +21,7 @@ def current_player end def initialize *name + @current ||= name[0] name[1] ||= SYMBOLS.sample @player_symbol = (SYMBOLS.reject {|s| s != name[1]}).first @computer_symbol = (SYMBOLS.reject {|s| s == @player_symbol}).first @@ -31,10 +33,10 @@ def initialize *name def indicate_player_turn + #puts "#{@player}'s Move:" end def get_player_move - puts "#{@player}'s Move:" gets end diff --git a/week7/homework/play_game.rb b/week7/homework/play_game.rb index a28dc5c..1c9f6d0 100644 --- a/week7/homework/play_game.rb +++ b/week7/homework/play_game.rb @@ -9,6 +9,7 @@ case @game.current_player when "Computer" @game.computer_move + @game.current_state when @game.player @game.indicate_player_turn @game.player_move From dc5b75483a0665a15c5b88e8f7c1a05cf0a91cd3 Mon Sep 17 00:00:00 2001 From: Adam Hendricksen Date: Sat, 7 Dec 2013 21:49:08 -0800 Subject: [PATCH 37/45] The computer wastes moves on empty map values, so injecting into an array instead --- week7/homework/features/tic-tac-toe.rb | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/week7/homework/features/tic-tac-toe.rb b/week7/homework/features/tic-tac-toe.rb index 3c310b4..8b400e5 100644 --- a/week7/homework/features/tic-tac-toe.rb +++ b/week7/homework/features/tic-tac-toe.rb @@ -53,8 +53,9 @@ def player_move end def open_spots - @board.map do |spot, fill| - spot if fill == ' ' + @board.inject(Array.new) do |remaining, (spot, fill)| + remaining << spot if fill == ' ' + remaining end end From 8214e1a1e7ddd561c28cba12f1ea5be404dbf480 Mon Sep 17 00:00:00 2001 From: Adam Hendricksen Date: Sat, 7 Dec 2013 21:51:43 -0800 Subject: [PATCH 38/45] Actually breaking tests, but making game not fail after first turn --- week7/homework/features/tic-tac-toe.rb | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/week7/homework/features/tic-tac-toe.rb b/week7/homework/features/tic-tac-toe.rb index 8b400e5..ff81c7f 100644 --- a/week7/homework/features/tic-tac-toe.rb +++ b/week7/homework/features/tic-tac-toe.rb @@ -32,8 +32,7 @@ def initialize *name end def indicate_player_turn - - #puts "#{@player}'s Move:" + puts "#{@player}'s Move:" end def get_player_move @@ -74,8 +73,8 @@ def current_state def determine_winner if spots_open? [:X, :O].each do |token| - @winner = true - @game_over = true + @winner = winning_lines token + @game_over = winning_lines token end else @winner = false From 06d2c1737ea9a34625621e81c78e1784c2e10308 Mon Sep 17 00:00:00 2001 From: Adam Hendricksen Date: Sat, 7 Dec 2013 22:07:24 -0800 Subject: [PATCH 39/45] Fixing the ability to detect a winner --- week7/homework/features/tic-tac-toe.rb | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/week7/homework/features/tic-tac-toe.rb b/week7/homework/features/tic-tac-toe.rb index ff81c7f..1ad3fce 100644 --- a/week7/homework/features/tic-tac-toe.rb +++ b/week7/homework/features/tic-tac-toe.rb @@ -73,8 +73,9 @@ def current_state def determine_winner if spots_open? [:X, :O].each do |token| - @winner = winning_lines token - @game_over = winning_lines token + puts "winning_lines #{winning_lines token}" + @winner = (@winner or winning_lines token) + @game_over = (@winner or winning_lines token) end else @winner = false @@ -105,20 +106,20 @@ def spots_open? private def winning_lines symbol winning_lines = [[:A1, :A2, :A3], - [:B1, :B2, :B3], - [:C1, :C2, :C3], - [:A1, :B1, :C1], - [:A2, :B2, :C2], - [:A3, :B3, :C3], - [:A3, :B2, :C1], - [:A1, :B2, :C3]] + [:B1, :B2, :B3], + [:C1, :C2, :C3], + [:A1, :B1, :C1], + [:A2, :B2, :C2], + [:A3, :B3, :C3], + [:A3, :B2, :C1], + [:A1, :B2, :C3]] winning_lines.inject(false) do |won, line| line_wins = line.inject(true) do |winning, spot| # A single false creates a false value - winning = winning and @board[spot] == symbol + winning = (winning and @board[spot] == symbol) end # A single true creates a true value - won = won or line_wins + won = (won or line_wins) end end end \ No newline at end of file From 4fa417562318889562cd61fd8741dc87bcc8f1cc Mon Sep 17 00:00:00 2001 From: Adam Hendricksen Date: Sat, 7 Dec 2013 22:08:52 -0800 Subject: [PATCH 40/45] Removing debugging statement --- week7/homework/features/tic-tac-toe.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/week7/homework/features/tic-tac-toe.rb b/week7/homework/features/tic-tac-toe.rb index 1ad3fce..d6a7d29 100644 --- a/week7/homework/features/tic-tac-toe.rb +++ b/week7/homework/features/tic-tac-toe.rb @@ -73,7 +73,6 @@ def current_state def determine_winner if spots_open? [:X, :O].each do |token| - puts "winning_lines #{winning_lines token}" @winner = (@winner or winning_lines token) @game_over = (@winner or winning_lines token) end From 4cc04f1d1e1c8bbe4e25fc764bd8ac2d7e52b2e2 Mon Sep 17 00:00:00 2001 From: Adam Hendricksen Date: Sat, 7 Dec 2013 22:29:15 -0800 Subject: [PATCH 41/45] Fixing the bad moves with recursion --- week7/homework/features/tic-tac-toe.rb | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/week7/homework/features/tic-tac-toe.rb b/week7/homework/features/tic-tac-toe.rb index d6a7d29..4a924d3 100644 --- a/week7/homework/features/tic-tac-toe.rb +++ b/week7/homework/features/tic-tac-toe.rb @@ -36,7 +36,7 @@ def indicate_player_turn end def get_player_move - gets + gets.upcase end def computer_move @@ -46,9 +46,16 @@ def computer_move end def player_move - move = get_player_move.to_sym unless open_spots.include? get_player_move + entered_move = get_player_move.to_sym + puts entered_move + if open_spots.include? entered_move + move = entered_move + else + puts "you suck" + move = player_move + end @board[move] = @player_symbol.to_s - move.to_sym + move end def open_spots From 6bc48d598753bf16d2a9284d8777dd05ebda8c45 Mon Sep 17 00:00:00 2001 From: Adam Hendricksen Date: Sat, 7 Dec 2013 22:43:31 -0800 Subject: [PATCH 42/45] Sooo, not working because the carriage return turns into part of the symbol --- week7/homework/features/tic-tac-toe.rb | 4 +--- week7/homework/play_game.rb | 1 - 2 files changed, 1 insertion(+), 4 deletions(-) diff --git a/week7/homework/features/tic-tac-toe.rb b/week7/homework/features/tic-tac-toe.rb index 4a924d3..60aa703 100644 --- a/week7/homework/features/tic-tac-toe.rb +++ b/week7/homework/features/tic-tac-toe.rb @@ -36,7 +36,7 @@ def indicate_player_turn end def get_player_move - gets.upcase + gets.chomp.upcase end def computer_move @@ -47,11 +47,9 @@ def computer_move def player_move entered_move = get_player_move.to_sym - puts entered_move if open_spots.include? entered_move move = entered_move else - puts "you suck" move = player_move end @board[move] = @player_symbol.to_s diff --git a/week7/homework/play_game.rb b/week7/homework/play_game.rb index 1c9f6d0..a28dc5c 100644 --- a/week7/homework/play_game.rb +++ b/week7/homework/play_game.rb @@ -9,7 +9,6 @@ case @game.current_player when "Computer" @game.computer_move - @game.current_state when @game.player @game.indicate_player_turn @game.player_move From 96f9ac826fa1896b50f1ae9f28b54591d2905b1a Mon Sep 17 00:00:00 2001 From: Adam Hendricksen Date: Sun, 8 Dec 2013 10:09:09 -0800 Subject: [PATCH 43/45] Making sure correct winner is shown at the end of match --- week7/homework/features/tic-tac-toe.rb | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/week7/homework/features/tic-tac-toe.rb b/week7/homework/features/tic-tac-toe.rb index 60aa703..fa4cc5f 100644 --- a/week7/homework/features/tic-tac-toe.rb +++ b/week7/homework/features/tic-tac-toe.rb @@ -50,6 +50,7 @@ def player_move if open_spots.include? entered_move move = entered_move else + puts "Sorry, that spot isn't available." move = player_move end @board[move] = @player_symbol.to_s @@ -77,9 +78,9 @@ def current_state def determine_winner if spots_open? - [:X, :O].each do |token| - @winner = (@winner or winning_lines token) - @game_over = (@winner or winning_lines token) + {@player_symbol=>@player, @computer_symbol=>"Computer"}.each do |symbol, winner| + @winner = winner if (@winner or winning_lines symbol) + @game_over = (@winner or winning_lines symbol) end else @winner = false @@ -88,11 +89,11 @@ def determine_winner end def player_won? - @winner + @winner == @player end def computer_won? - @winner + @winner == "Computer" end def over? From 1740bd2bb11ae24e4b279dcfecc49765d94c1bb0 Mon Sep 17 00:00:00 2001 From: Adam Hendricksen Date: Sun, 8 Dec 2013 11:59:23 -0800 Subject: [PATCH 44/45] Fixed draw logic to work in play mode --- week7/homework/features/tic-tac-toe.rb | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/week7/homework/features/tic-tac-toe.rb b/week7/homework/features/tic-tac-toe.rb index fa4cc5f..04a7a3c 100644 --- a/week7/homework/features/tic-tac-toe.rb +++ b/week7/homework/features/tic-tac-toe.rb @@ -1,8 +1,8 @@ class TicTacToe attr_accessor :player, :player_symbol, :computer_symbol, :board SYMBOLS = [:X, :O] - @winner = false - @game_over = false + + @game_over @current @@ -77,13 +77,16 @@ def current_state end def determine_winner - if spots_open? - {@player_symbol=>@player, @computer_symbol=>"Computer"}.each do |symbol, winner| - @winner = winner if (@winner or winning_lines symbol) - @game_over = (@winner or winning_lines symbol) - end - else - @winner = false + @winner = false + winner_hash = {@player_symbol=>@player, @computer_symbol=>"Computer"} + winner_hash.each do |symbol, winner| + winning_play = winning_lines symbol + @winner = winner if winning_play + puts "#{symbol}: #{@game_over}, #{winning_play}" + @game_over = (@game_over or winning_play) + end + puts "open #{spots_open?}, player: #{player_won?}, Computer: #{computer_won?}, Game over: #{over?}, Draw: #{draw?}" + if spots_open? == false and @winner == false @game_over = true end end @@ -101,7 +104,8 @@ def over? end def draw? - @game_over == true and @winner == false + puts "Draw?: #{@game_over}, #{@winner}" + (@game_over == true) and (@winner == false) end def spots_open? @@ -121,7 +125,7 @@ def winning_lines symbol winning_lines.inject(false) do |won, line| line_wins = line.inject(true) do |winning, spot| # A single false creates a false value - winning = (winning and @board[spot] == symbol) + winning = (winning and @board[spot].to_sym == symbol) end # A single true creates a true value won = (won or line_wins) From d9d230148cedc7fafcbd456aa8a630ec006df500 Mon Sep 17 00:00:00 2001 From: Adam Hendricksen Date: Sun, 8 Dec 2013 12:01:29 -0800 Subject: [PATCH 45/45] Tearing out debugging puts --- week7/homework/features/tic-tac-toe.rb | 3 --- 1 file changed, 3 deletions(-) diff --git a/week7/homework/features/tic-tac-toe.rb b/week7/homework/features/tic-tac-toe.rb index 04a7a3c..dd83639 100644 --- a/week7/homework/features/tic-tac-toe.rb +++ b/week7/homework/features/tic-tac-toe.rb @@ -82,10 +82,8 @@ def determine_winner winner_hash.each do |symbol, winner| winning_play = winning_lines symbol @winner = winner if winning_play - puts "#{symbol}: #{@game_over}, #{winning_play}" @game_over = (@game_over or winning_play) end - puts "open #{spots_open?}, player: #{player_won?}, Computer: #{computer_won?}, Game over: #{over?}, Draw: #{draw?}" if spots_open? == false and @winner == false @game_over = true end @@ -104,7 +102,6 @@ def over? end def draw? - puts "Draw?: #{@game_over}, #{@winner}" (@game_over == true) and (@winner == false) end