From b796b0583a8bd1335a3f3b5d45aa926dfaa84c59 Mon Sep 17 00:00:00 2001 From: alex Date: Tue, 23 Jan 2018 02:43:28 +0100 Subject: [PATCH] Moved winning combos to a variable --- board.rb | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/board.rb b/board.rb index 048efe3..84c61c8 100644 --- a/board.rb +++ b/board.rb @@ -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