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
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,17 +64,4 @@ class Board
def is_full?
get_free.empty?
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