This commit is contained in:
Alexander Munch-Hansen 2018-01-25 20:41:58 +01:00
commit eb5e6771b6

View File

@ -10,11 +10,12 @@ end
class Board class Board
attr_reader :spaces, :def_val attr_reader :spaces, :def_val, :winning_scens
def initialize def initialize
@def_val = "---" @def_val = " "
@spaces = Array.new(9, @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 end
def to_s def to_s
@ -39,8 +40,8 @@ class Board
end end
def any_winner? 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]} tmp = s.map {|i| @spaces[i]}
if (not tmp.include? @def_val) and tmp.same_values? then if (not tmp.include? @def_val) and tmp.same_values? then
return true return true
@ -50,8 +51,8 @@ class Board
end end
def is_winner? player 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]} tmp = s.map {|i| @spaces[i]}
if (not tmp.include? @def_val) and tmp.same_values? and tmp[0] == player then if (not tmp.include? @def_val) and tmp.same_values? and tmp[0] == player then
return true return true
@ -63,17 +64,4 @@ class Board
def is_full? def is_full?
get_free.empty? get_free.empty?
end end
def is_over?
if any_winner? then
system "clear"
print self.to_s
print "\nSomeone won!\n"
elsif is_full? then
system "clear"
print self.to_s
print "\nGame is done!\n"
end
end
end end