/// <summary>
        /// Returns true iff <c>object</c> is a
        /// <c>FieldMatrix</c> instance with the same dimensions as this
        /// and all corresponding matrix entries are equal.
        /// </summary>
        /// <param name="obj">the object to test equality against.</param>
        /// <returns>true if object equals this</returns>
        public override Boolean Equals(Object obj)
        {
            if (obj == this)
            {
                return(true);
            }
            if (obj is FieldMatrix <T> )
            {
                return(false);
            }
            FieldMatrix <T> m     = (FieldMatrix <T>)obj;
            int             nRows = getRowDimension();
            int             nCols = getColumnDimension();

            if (m.getColumnDimension() != nCols || m.getRowDimension() != nRows)
            {
                return(false);
            }
            for (int row = 0; row < nRows; ++row)
            {
                for (int col = 0; col < nCols; ++col)
                {
                    if (!getEntry(row, col).Equals(m.getEntry(row, col)))
                    {
                        return(false);
                    }
                }
            }
            return(true);
        }
Ejemplo n.º 2
0
        private bool FilledCoordinateNotSelf(List <Coordinate> coordinates, Coordinate coordinate)
        {
            FieldMatrix matrix = field.FieldMatrix;

            return(!(matrix.ValidCoordinate(coordinate) &&
                     (matrix[coordinate] == null || matrix[coordinate] == Brick)));
        }
 /// <summary>
 /// Check if a matrix is multiplication compatible with the instance.
 /// </summary>
 /// <param name="m">Matrix to check.</param>
 /// <exception cref="DimensionMismatchException"> if the matrix is not
 /// multiplication-compatible with instance.</exception>
 protected void checkMultiplicationCompatible(FieldMatrix <T> m)
 {
     if (getColumnDimension() != m.getRowDimension())
     {
         throw new DimensionMismatchException(m.getRowDimension(), getColumnDimension());
     }
 }
Ejemplo n.º 4
0
    public void OnPointerClick(PointerEventData eventData)
    {
        if (_dragging || Input.touchCount > 1 || eventData.button != PointerEventData.InputButton.Left)
        {
            return;
        }

        if (nextClickOverride != null)
        {
            nextClickOverride();
            nextClickOverride = null;
            return;
        }

        Utils.GetInputCoords(out var x, out var y);
        if (FieldMatrix.Get(x, y, out var block))
        {
            block.logic.Click(eventData);
        }
        else
        {
            PixelDriver.Add(PixelRoad.Circle(Colors.GetPalette(-1)[3],
                                             3f, 3f, 0.05f, 0.5f, x, y).SetWeight(0.05f));
        }
    }
        /// <inheritdoc/>
        public FieldMatrix <T> multiply(FieldMatrix <T> m)
        {
            // safety check
            checkMultiplicationCompatible(m);

            int             nRows = getRowDimension();
            int             nCols = m.getColumnDimension();
            int             nSum  = getColumnDimension();
            FieldMatrix <T> outp  = createMatrix(nRows, nCols);

            for (int row = 0; row < nRows; ++row)
            {
                for (int col = 0; col < nCols; ++col)
                {
                    T sum = field.getZero();
                    for (int i = 0; i < nSum; ++i)
                    {
                        sum = sum.add(getEntry(row, i).multiply(m.getEntry(i, col)));
                    }
                    outp.setEntry(row, col, sum);
                }
            }

            return(outp);
        }
        /// <summary>
        /// Convert a <see cref="FieldMatrix"/>/<see cref="BigFraction"/> matrix to a
        /// <see cref="RealMatrix"/>.
        /// </summary>
        /// <param name="m">Matrix to convert.</param>
        /// <returns>the converted matrix.</returns>
        public static Array2DRowRealMatrix bigFractionMatrixToRealMatrix(FieldMatrix <BigFraction> m)
        {
            BigFractionMatrixConverter converter = new BigFractionMatrixConverter();

            m.walkInOptimizedOrder(converter);
            return(converter.getConvertedMatrix());
        }
Ejemplo n.º 7
0
        public void SnakeGame_GameIteration_SnakeCrash()
        {
            Position head = new Position(), tail = new Position();

            eCellType[] field = new eCellType[64];

            FieldMatrix.Seed(field, head, tail);
            eDirectionType currentDirection = eDirectionType.None, nextDirection = eDirectionType.Right;

            FieldMatrix.SetCellTypeByPosition(field, new Position()
            {
                row = head.row, col = (byte)(head.col + 1)
            }, eCellType.SnakeDown);

            Assert.ThrowsException <CrashedInSnakeException>(() => GameEngine.ApplyChange(field, head, tail, currentDirection, nextDirection, out bool expanded), "Did not crash moving to the right");

            FieldMatrix.Seed(field, head, tail);

            FieldMatrix.SetCellTypeByPosition(field, new Position()
            {
                row = (byte)(head.row + 1), col = head.col
            }, eCellType.SnakeHead);
            nextDirection = eDirectionType.Down;

            Assert.ThrowsException <CrashedInSnakeException>(() => GameEngine.ApplyChange(field, head, tail, currentDirection, nextDirection, out bool expanded), "Did not crash moving down");
        }
Ejemplo n.º 8
0
    static void DragFromTo(int fromX, int fromY, int toX, int toY)
    {
        if (!FieldMatrix.Get(fromX, fromY, out var fromBlock))
        {
            return;
        }
        if (FieldMatrix.Get(toX, toY, out var toBlock))
        {
            var bind           = BindMatrix.GetBind(fromBlock, toBlock);
            var newBlockOffset = new Vector2(toX - fromX, toY - fromY);
            if (bind == null)
            {
                BindMatrix.AddBind(fromBlock, toBlock, newBlockOffset, Bind.BlockBindStrength);
                return;
            }

            if (bind.First != fromBlock)
            {
                if (!(fromBlock is RootBlock) && BindMatrix.GetOutBindsCount(fromBlock) == 0)
                {
                    fromBlock.Destroy();
                    return;
                }

                bind.Break();
                BindMatrix.AddBind(fromBlock, toBlock, newBlockOffset, Bind.BlockBindStrength);
                return;
            }
            return;
        }
        NodeBlock.Create(toX, toY, fromBlock);
    }
Ejemplo n.º 9
0
    void FinishPlacing()
    {
        if (_placing)
        {
            return;
        }
        var position = cam.transform.position;
        int x = Mathf.RoundToInt(position.x), y = Mathf.RoundToInt(position.y);

        if (FieldMatrix.Get(x, y, out _))
        {
            Animator.Interpolate(
                new Color(0.3f, 0f, 0f, DarkenAlpha),
                new Color(0f, 0f, 0f, DarkenAlpha), 1f)
            .Type(InterpolationType.Square)
            .PassValue(v => background.color = v);
            return;
        }
        _placing = true;
        Animator.Interpolate(DarkenAlpha, 0f, 1f).Type(InterpolationType.Linear)
        .PassValue(v =>
        {
            background.color = new Color(0, 0, 0, v);
        })
        .WhenDone(() =>
        {
            gameObject.SetActive(false);
            Roots.CreateRoot(x, y, -1, palette.ColorsId);
            _placing = false;
        });
    }
Ejemplo n.º 10
0
 // Use this for initialization
 void OnEnable()
 {
     fieldMatrix = GetComponent <FieldMatrix>();
     field       = fieldMatrix.playingfield;
     x           = fieldMatrix.x;
     z           = fieldMatrix.z;
     SpawnInField();
 }
 /// <summary>
 /// Check if a matrix is subtraction compatible with the instance.
 /// </summary>
 /// <param name="m">Matrix to check.</param>
 /// <exception cref="MatrixDimensionMismatchExceptio">n if the matrix is not
 /// subtraction-compatible with instance.</exception>
 protected void checkSubtractionCompatible(FieldMatrix <T> m)
 {
     if ((getRowDimension() != m.getRowDimension()) ||
         (getColumnDimension() != m.getColumnDimension()))
     {
         throw new MatrixDimensionMismatchException(m.getRowDimension(), m.getColumnDimension(),
                                                    getRowDimension(), getColumnDimension());
     }
 }
        /// <inheritdoc/>
        public FieldMatrix <T> transpose()
        {
            int             nRows = getRowDimension();
            int             nCols = getColumnDimension();
            FieldMatrix <T> outp  = createMatrix(nCols, nRows);

            walkInOptimizedOrder(new DefaultFieldMatrixPreservingVisitorAnonymous2 <T>(field.getZero(), outp));

            return(outp);
        }
Ejemplo n.º 13
0
 public void ClearField()
 {
     foreach (var root in Roots.Root.Values.ToArray())
     {
         root.Destroy();
     }
     foreach (var block in FieldMatrix.GetAllAsList())
     {
         block.Destroy();
     }
 }
        /// <summary>
        /// Returns a diagonal matrix with specified elements.
        /// </summary>
        /// <typeparam name="T">the type of the field elements</typeparam>
        /// <param name="diagonal">diagonal elements of the matrix (the array elements
        /// will be copied)</param>
        /// <returns>diagonal matrix</returns>
        public static FieldMatrix <T> createFieldDiagonalMatrix <T>(T[] diagonal) where T : FieldElement <T>
        {
            FieldMatrix <T> m =
                createFieldMatrix(diagonal[0].getField(), diagonal.Length, diagonal.Length);

            for (int i = 0; i < diagonal.Length; ++i)
            {
                m.setEntry(i, i, diagonal[i]);
            }
            return(m);
        }
        /// <inheritdoc/>
        public FieldMatrix <T> getColumnMatrix(int column)
        {
            checkColumnIndex(column);
            int             nRows = getRowDimension();
            FieldMatrix <T> outp  = createMatrix(nRows, 1);

            for (int i = 0; i < nRows; ++i)
            {
                outp.setEntry(i, 0, getEntry(i, column));
            }

            return(outp);
        }
 /// <summary>
 /// Returns the P rows permutation matrix.
 /// <para>P is a sparse matrix with exactly one element set to 1.0 in
 /// each row and each column, all other elements being set to 0.0.</para>
 /// <para>The positions of the 1 elements are given by the <see cref="getPivot()">
 /// pivot permutation vector</see>.</para>
 /// </summary>
 /// <returns>the P rows permutation matrix (or null if decomposed matrix is singular)
 /// </returns>
 /// <remarks>
 /// See <see cref="getPivot()"/>
 /// </remarks>
 public FieldMatrix <T> getP()
 {
     if ((cachedP == null) && !singular)
     {
         int m = pivot.Length;
         cachedP = new Array2DRowFieldMatrix <T>(field, m, m);
         for (int i = 0; i < m; ++i)
         {
             cachedP.setEntry(i, pivot[i], field.getOne());
         }
     }
     return(cachedP);
 }
        /// <inheritdoc/>
        public FieldMatrix <T> getRowMatrix(int row)
        {
            checkRowIndex(row);
            int             nCols = getColumnDimension();
            FieldMatrix <T> outp  = createMatrix(1, nCols);

            for (int i = 0; i < nCols; ++i)
            {
                outp.setEntry(0, i, getEntry(row, i));
            }

            return(outp);
        }
        /// <inheritdoc/>
        public FieldMatrix <T> getSubMatrix(int[] selectedRows, int[] selectedColumns)
        {
            // safety checks
            checkSubMatrixIndex(selectedRows, selectedColumns);

            // copy entries
            FieldMatrix <T> subMatrix =
                createMatrix(selectedRows.Length, selectedColumns.Length);

            subMatrix.walkInOptimizedOrder(new DefaultFieldMatrixChangingVisitorAnonymous1 <T>(field.getZero(), selectedRows, selectedColumns, this));

            return(subMatrix);
        }
Ejemplo n.º 19
0
    public static GameSerialized Create()
    {
        var result = new GameSerialized
        {
            NodeBlocks    = new List <NodeBlockSerialized>(),
            RootBlocks    = new List <RootBlockSerialized>(),
            Binds         = new List <BindSerialized>(),
            SoundsPlayers = new List <SoundsPlayerSerialized>(),
        };

        foreach (var block in FieldMatrix.GetAllAsList())
        {
            switch (block)
            {
            case NodeBlock nodeBlock:
            {
                if (NodeBlockSerialized.Create(nodeBlock, out var t))
                {
                    result.NodeBlocks.Add(t);
                }
                break;
            }

            case RootBlock rootBlock:
            {
                if (RootBlockSerialized.Create(rootBlock, out var t))
                {
                    result.RootBlocks.Add(t);
                }
                break;
            }
            }
        }
        foreach (var bind in BindMatrix.GetAllAsList())
        {
            if (BindSerialized.Create(bind, out var t))
            {
                result.Binds.Add(t);
            }
        }
        foreach (var root in Roots.Root.Values)
        {
            if (SoundsPlayerSerialized.Create(root.block.soundsPlayer, root.block.rootId, out var t))
            {
                result.SoundsPlayers.Add(t);
            }
        }
        return(result);
    }
        /// <inheritdoc/>
        public FieldMatrix <T> scalarAdd(T d)
        {
            int             rowCount    = getRowDimension();
            int             columnCount = getColumnDimension();
            FieldMatrix <T> outp        = createMatrix(rowCount, columnCount);

            for (int row = 0; row < rowCount; ++row)
            {
                for (int col = 0; col < columnCount; ++col)
                {
                    outp.setEntry(row, col, getEntry(row, col).add(d));
                }
            }
            return(outp);
        }
Ejemplo n.º 21
0
    public static PixelRoad NodeBackground()
    {
        var pr = new PixelRoad(-1f);

        pr._colorFunc = (x, y) =>
        {
            if (FieldMatrix.Get(x, y, out var block))
            {
                return(new WeightedColor(Roots.Root[block.rootId].block.view.PrimaryPainter.palette.GetColor(2),
                                         pr._weight));
            }
            return(WeightedColor.Clear);
        };
        return(pr);
    }
Ejemplo n.º 22
0
    public void OnBeginDrag(PointerEventData eventData)
    {
        if (eventData.button != PointerEventData.InputButton.Left || Input.touchCount > 1)
        {
            return;
        }
        _dragging = true;

        Utils.GetInputCoords(out var x, out var y);
        if (FieldMatrix.Get(x, y, out _draggedBlock))
        {
            _draggingBlock = true;
            _draggedBlock.logic.BeginDrag(eventData);
            BlockEditor.OnBlockDragStart(_draggedBlock);
        }
    }
        /// <inheritdoc/>
        public FieldMatrix <T> getSubMatrix(int startRow, int endRow, int startColumn, int endColumn)
        {
            checkSubMatrixIndex(startRow, endRow, startColumn, endColumn);

            FieldMatrix <T> subMatrix = createMatrix(endRow - startRow + 1, endColumn - startColumn + 1);

            for (int i = startRow; i <= endRow; ++i)
            {
                for (int j = startColumn; j <= endColumn; ++j)
                {
                    subMatrix.setEntry(i - startRow, j - startColumn, getEntry(i, j));
                }
            }

            return(subMatrix);
        }
Ejemplo n.º 24
0
        public void SnakeGame_GameIteration_TurnUp()
        {
            Position head = new Position(), tail = new Position();

            eCellType[] field = new eCellType[64];

            FieldMatrix.Seed(field, head, tail);
            eDirectionType currentDirection = eDirectionType.None, nextDirection = eDirectionType.Up;

            GameEngine.ApplyChange(field, head, tail, currentDirection, nextDirection, out bool expanded);

            Assert.AreEqual(3, field.Count(c => c != eCellType.None));

            Assert.AreEqual(eCellType.SnakeRight, field[TestOffset(3, 3)], "Tail did not move to the right");
            Assert.AreEqual(eCellType.SnakeUp, field[TestOffset(3, 4)], "Body did not move to the right");
            Assert.AreEqual(eCellType.SnakeHead, field[TestOffset(2, 4)], "Head did not move up");
        }
 /// <summary>
 /// Returns the matrix U of the decomposition.
 /// <para>U is an upper-triangular matrix</para>
 /// </summary>
 /// <returns>the U matrix (or null if decomposed matrix is singular)</returns>
 public FieldMatrix <T> getU()
 {
     if ((cachedU == null) && !singular)
     {
         int m = pivot.Length;
         cachedU = new Array2DRowFieldMatrix <T>(field, m, m);
         for (int i = 0; i < m; ++i)
         {
             T[] luI = lu[i];
             for (int j = i; j < m; ++j)
             {
                 cachedU.setEntry(i, j, luI[j]);
             }
         }
     }
     return(cachedU);
 }
        /// <inheritdoc/>
        public void setColumnMatrix(int column, FieldMatrix <T> matrix)
        {
            checkColumnIndex(column);
            int nRows = getRowDimension();

            if ((matrix.getRowDimension() != nRows) ||
                (matrix.getColumnDimension() != 1))
            {
                throw new MatrixDimensionMismatchException(matrix.getRowDimension(),
                                                           matrix.getColumnDimension(),
                                                           nRows, 1);
            }
            for (int i = 0; i < nRows; ++i)
            {
                setEntry(i, column, matrix.getEntry(i, 0));
            }
        }
        /// <inheritdoc/>
        public void setRowMatrix(int row, FieldMatrix <T> matrix)
        {
            checkRowIndex(row);
            int nCols = getColumnDimension();

            if ((matrix.getRowDimension() != 1) ||
                (matrix.getColumnDimension() != nCols))
            {
                throw new MatrixDimensionMismatchException(matrix.getRowDimension(),
                                                           matrix.getColumnDimension(),
                                                           1, nCols);
            }
            for (int i = 0; i < nCols; ++i)
            {
                setEntry(row, i, matrix.getEntry(0, i));
            }
        }
 /// <summary>
 /// Returns the matrix L of the decomposition.
 /// <para>L is a lower-triangular matrix</para>
 /// </summary>
 /// <returns>the L matrix (or null if decomposed matrix is singular)</returns>
 public FieldMatrix <T> getL()
 {
     if ((cachedL == null) && !singular)
     {
         int m = pivot.Length;
         cachedL = new Array2DRowFieldMatrix <T>(field, m, m);
         for (int i = 0; i < m; ++i)
         {
             T[] luI = lu[i];
             for (int j = 0; j < i; ++j)
             {
                 cachedL.setEntry(i, j, luI[j]);
             }
             cachedL.setEntry(i, i, field.getOne());
         }
     }
     return(cachedL);
 }
        /// <inheritdoc/>
        public FieldMatrix <T> subtract(FieldMatrix <T> m)
        {
            // safety check
            checkSubtractionCompatible(m);

            int             rowCount    = getRowDimension();
            int             columnCount = getColumnDimension();
            FieldMatrix <T> outp        = createMatrix(rowCount, columnCount);

            for (int row = 0; row < rowCount; ++row)
            {
                for (int col = 0; col < columnCount; ++col)
                {
                    outp.setEntry(row, col, getEntry(row, col).subtract(m.getEntry(row, col)));
                }
            }

            return(outp);
        }
        /// <summary>
        /// Creates a column <see cref="FieldMatrix"/> using the data from the input
        /// array.
        /// </summary>
        /// <typeparam name="T">the type of the field elements</typeparam>
        /// <param name="columnData">the input column data</param>
        /// <returns>a columnData x 1 FieldMatrix</returns>
        /// <exception cref="NoDataException"> if <c>data</c> is empty.</exception>
        /// <exception cref="NullArgumentException"> if <c>columnData</c> is <c>null</c>.
        /// </exception>
        public static FieldMatrix <T> createColumnFieldMatrix <T>(T[] columnData) where T : FieldElement <T>
        {
            if (columnData == null)
            {
                throw new NullArgumentException();
            }
            int nRows = columnData.Length;

            if (nRows == 0)
            {
                throw new NoDataException(new LocalizedFormats("AT_LEAST_ONE_ROW"));
            }
            FieldMatrix <T> m = createFieldMatrix(columnData[0].getField(), nRows, 1);

            for (int i = 0; i < nRows; ++i)
            {
                m.setEntry(i, 0, columnData[i]);
            }
            return(m);
        }