Created
November 1, 2012 18:16
-
-
Save arn-e/3995495 to your computer and use it in GitHub Desktop.
find_diagonals_scratch
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#test array | |
class TestArray | |
def initialize | |
@array = Array.new(6).map{|i| Array.new(7)} | |
# populate_fields | |
populate_fields_numeric | |
end | |
def populate_fields_numeric | |
@array.each_with_index do |i,idx_i| | |
i.each_with_index do |j,idx_j| | |
@array[idx_i][idx_j] = "#{idx_i}, #{idx_j}" | |
end | |
end | |
end | |
def populate_fields | |
@array[1][1] = "x" | |
@array[1][3] = "x" | |
@array[1][2] = "o" | |
@array[1][4] = "x" | |
@array[1][5] = "o" | |
@array[1][6] = "x" | |
end | |
def print_array | |
@array.reverse.each {|i| p i} | |
end | |
def check_horizontal | |
print_array | |
@array.each do |i| | |
return true if i.to_s.match(/xxxx/) | |
end | |
end | |
def check_vertical | |
end | |
def check_diagonal | |
check_1 | |
chekc_2 | |
check_3 | |
check_4 | |
end | |
def check_diagonal(diags = [], x = 0) | |
@array.each_with_index do |i,idx| | |
y, x = idx, 0 | |
diags << [y,x] | |
while y > 0 | |
y, x = y - 1, x + 1 | |
diags << [y,x] | |
end | |
p diags | |
diags = [] | |
end | |
end | |
def check_diagonal_2(diags = [], x = 0) | |
@array.each_with_index do |i,idx| | |
y, x = idx, 6 | |
diags << [y,x] | |
while y < @array.length - 1 | |
y, x = y + 1, x - 1 | |
diags << [y,x] | |
end | |
p diags | |
diags = [] | |
end | |
end | |
def check_diagonal_3(diags = [], x = 0) | |
@array.each_with_index do |i,idx| | |
y, x = idx, 6 | |
diags << [y,x] | |
while y > 0 | |
y, x = y - 1, x - 1 | |
diags << [y,x] | |
end | |
p diags | |
diags = [] | |
end | |
end | |
def check_diagonal_4(diags = [], x = 0) | |
@array.each_with_index do |i,idx| | |
y, x = idx, 0 | |
diags << [y,x] | |
while y < @array.length - 1 | |
y, x = y + 1, x + 1 | |
diags << [y,x] | |
end | |
p diags | |
diags = [] | |
end | |
end | |
end | |
ta = TestArray.new | |
# ta.print_array | |
my_list = ta.check_diagonal | |
my_list | |
my_list_2 = ta.check_diagonal_2 | |
my_list_2 | |
my_list_3 = ta.check_diagonal_3 | |
my_list_3 | |
my_list_4 = ta.check_diagonal_4 | |
my_list_4 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment