From 6eb0eb8a4f48a5dfe37afc683af4ff63bf47457c Mon Sep 17 00:00:00 2001 From: Michal Vanzura Date: Thu, 17 Oct 2013 11:02:44 +0200 Subject: [PATCH 1/2] add tests --- test/tictactoe_test.rb | 65 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 64 insertions(+), 1 deletion(-) diff --git a/test/tictactoe_test.rb b/test/tictactoe_test.rb index 1d8d247..91e6983 100644 --- a/test/tictactoe_test.rb +++ b/test/tictactoe_test.rb @@ -1 +1,64 @@ -require 'test_helper' +require 'test_helper' + +describe TicTacToe do + + describe "there are X vertically" do + it "must say winner is X" do + ttt = TicTacToe.new([["X", "X", "X"], + ["O", "O", "_"], + ["O", "_","_"]]) + + ttt.winner.must_equal "X" + end + end + + describe "there are X horizontally" do + it "must say winner is X" do + ttt = TicTacToe.new([["X", "O", "_"], + ["X", "O", "O"], + ["X", "_","_"]]) + + ttt.winner.must_equal "X" + end + end + + describe "there are X diagonally" do + it "must say winner is X" do + ttt = TicTacToe.new([["X", "O", "_"], + ["O", "X", "_"], + ["O", "_","X"]]) + + ttt.winner.must_equal "X" + end + end + + describe "there are O vertically" do + it "must say winner is O" do + ttt = TicTacToe.new([["O", "O", "O"], + ["X", "X", "_"], + ["X", "_","_"]]) + + ttt.winner.must_equal "O" + end + end + + describe "there are O horizontally" do + it "must say winner is O" do + ttt = TicTacToe.new([["O", "X", "_"], + ["O", "X", "X"], + ["O", "_","_"]]) + + ttt.winner.must_equal "O" + end + end + + describe "there are O diagonally" do + it "must say winner is O" do + ttt = TicTacToe.new([["O", "X", "_"], + ["X", "O", "_"], + ["X", "_","O"]]) + + ttt.winner.must_equal "O" + end + end +end \ No newline at end of file From e436c02877c6fd95e7387f3dfb1e4fc65cd0fc86 Mon Sep 17 00:00:00 2001 From: Michal Vanzura Date: Thu, 17 Oct 2013 11:20:32 +0200 Subject: [PATCH 2/2] fixed tictactoe.rb to work in older ruby --- lib/tictactoe.rb | 70 ++++++++++++++++++++++++------------------------ 1 file changed, 35 insertions(+), 35 deletions(-) diff --git a/lib/tictactoe.rb b/lib/tictactoe.rb index 2b7b022..ea0a1a3 100644 --- a/lib/tictactoe.rb +++ b/lib/tictactoe.rb @@ -1,35 +1,35 @@ -class TicTacToe - def initialize(config) - @config = config.map { |row| row.map { |field| field.gsub(/[^XO]/,"") } } - @winning_count = 3 - end - - def winner - if in_row?(@config, "X") || in_coll?(@config, "X") || in_diag?(@config, "X") - "X" - elsif in_row?(@config, "O") || in_coll?(@config, "O") || in_diag?(@config, "O") - "O" - else - nil - end - end - - def in_row?(config, player) - config.include?([player] * @winning_count) - end - - def in_coll?(config, player) - in_row?(config.transpose, player) - end - - def in_diag?(config, player) - diag_indexes = [(0...size).zip((0...size))] - diag_indexes << (0...size).to_a.reverse.zip((0...size)) - diagonals = diag_indexes.map {|indexes| indexes.map { |x, y| config[x][y] } } - in_row?(diagonals, player) - end - - def size - @config.size - end -end +class TicTacToe + def initialize(config) + @config = config.map { |row| row.map { |field| field.gsub(/[^XO]/,"") } } + @winning_count = 3 + end + + def winner + if in_row?(@config, "X") || in_coll?(@config, "X") || in_diag?(@config, "X") + "X" + elsif in_row?(@config, "O") || in_coll?(@config, "O") || in_diag?(@config, "O") + "O" + else + nil + end + end + + def in_row?(config, player) + config.include?([player] * @winning_count) + end + + def in_coll?(config, player) + in_row?(config.transpose, player) + end + + def in_diag?(config, player) + diag_indexes = [(0...size).to_a.zip((0...size).to_a)] + diag_indexes << (0...size).to_a.reverse.zip((0...size).to_a) + diagonals = diag_indexes.map {|indexes| indexes.map { |x, y| config[x][y] } } + in_row?(diagonals, player) + end + + def size + @config.size + end +end