Moved winning combos to a variable
This commit is contained in:
parent
df1d3f59ae
commit
b796b0583a
15
board.rb
15
board.rb
|
@ -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,6 +64,4 @@ class Board
|
||||||
def is_full?
|
def is_full?
|
||||||
get_free.empty?
|
get_free.empty?
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue
Block a user