Moved winning combos to a variable

This commit is contained in:
Alexander Munch-Hansen 2018-01-23 02:43:28 +01:00
parent df1d3f59ae
commit b796b0583a

View File

@ -10,11 +10,12 @@ end
class Board
attr_reader :spaces, :def_val
attr_reader :spaces, :def_val, :winning_scens
def initialize
@def_val = "---"
@def_val = " "
@spaces = Array.new(9, @def_val)
@winning_scens = [[0,1,2],[3,4,5],[6,7,8],[0,3,6],[1,4,7],[2,5,8],[0,4,8],[2,4,6]]
end
def to_s
@ -39,8 +40,8 @@ class Board
end
def any_winner?
winning_combos = [[0,1,2],[3,4,5],[6,7,8],[0,3,6],[1,4,7],[2,5,8],[0,4,8],[2,4,6]]
winning_combos.each do |s|
@winning_scens.each do |s|
tmp = s.map {|i| @spaces[i]}
if (not tmp.include? @def_val) and tmp.same_values? then
return true
@ -50,8 +51,8 @@ class Board
end
def is_winner? player
winning_combos = [[0,1,2],[3,4,5],[6,7,8],[0,3,6],[1,4,7],[2,5,8],[0,4,8],[2,4,6]]
winning_combos.each do |s|
@winning_scens.each do |s|
tmp = s.map {|i| @spaces[i]}
if (not tmp.include? @def_val) and tmp.same_values? and tmp[0] == player then
return true
@ -63,6 +64,4 @@ class Board
def is_full?
get_free.empty?
end
end