Sha256: 102fbb6f02333f2af36bd1a56ed8c59e63fd4190b06b16c481ab4742477b5f03

Contents?: true

Size: 1.96 KB

Versions: 1

Compression:

Stored size: 1.96 KB

Contents

require_relative 'line_folder'
require "Matrix"
# A game board keeps and manipulates the state of the game.
class GameBoardFolder

  def initialize(line_folder = LineFolder.new )
    @line_folder = line_folder
  end

  # Folds the game board from right to left.
  def fold_left(state)
    new_state = []
    state.row_vectors.each_index {|i|
      values = state.row_vectors[i].to_a
      values = @line_folder.fold(values)
      new_state << values
    }
    Matrix.rows(new_state)
  end

  # Folds the game board from left to right.
  def fold_right(state)
    new_state = []
    state.row_vectors.each {|row|
      values = row.to_a.reverse
      values = @line_folder.fold(values)
      new_state << values.reverse
    }
    Matrix.rows(new_state)
  end

  # Folds the game board upwards.
  def fold_up(state)
    new_state = []
    state.column_vectors.each {|column|
      values = column.to_a
      values = @line_folder.fold(values)
      new_state << values
    }
    Matrix.columns(new_state)
  end

  # Folds the game board downwards.
  def fold_down(state)
    new_state = []
    state.column_vectors.each {|column|
      values = column.to_a.reverse
      values = @line_folder.fold(values)
      new_state << values.reverse
    }
    Matrix.columns(new_state)
  end

  def can_fold_left?(state)
    foldable = false
    state.row_vectors.each{|line|
      foldable = (foldable or @line_folder.can_fold?(line.to_a))
    }
    foldable
  end

  def can_fold_right?(state)
    foldable = false
    state.row_vectors.each{|line|
      foldable = (foldable or @line_folder.can_fold?(line.to_a.reverse))
    }
    foldable
  end

  def can_fold_up?(state)
    foldable = false
    state.column_vectors.each{|line|
      foldable = (foldable or @line_folder.can_fold?(line.to_a))
    }
    foldable
  end

  def can_fold_down?(state)
    foldable = false
    state.column_vectors.each{|line|
      foldable = (foldable or @line_folder.can_fold?(line.to_a.reverse))
    }
    foldable
  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
threesmodel-0.0.3 lib/game_board_folder.rb