Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add tests #11

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 35 additions & 35 deletions lib/tictactoe.rb
Original file line number Diff line number Diff line change
@@ -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
65 changes: 64 additions & 1 deletion test/tictactoe_test.rb
Original file line number Diff line number Diff line change
@@ -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