public void CheckGetterandSetterViaTheNewIndex()
        {
            MatrixField fieldMatrix = new MatrixField(4, 4);

            fieldMatrix[8] = 12;
            Assert.AreEqual(12, fieldMatrix[8]);
        }
Пример #2
0
 public PathFinder(MatrixField field)
 {
     this.field = field;
     weights    = new int[field.height, field.width];
     queue      = new Queue <CellChain>();
     priorQueue = new PriorityQueue <CellChain>();
 }
        public void CheckGetterandSetter()
        {
            MatrixField fieldMatrix = new MatrixField(4, 4);

            fieldMatrix[1, 1] = 7;
            Assert.AreEqual(7, fieldMatrix[1, 1]);
        }
Пример #4
0
    private void Generate()
    {
        foreach (var x in walls)
        {
            Destroy(x.gameObject);
        }
        walls.Clear();

        noizeLabyrintGenerator.Generate(noizeField);
        field = noizeLabyrintGenerator.field;

        CalculateStartPoints();

        startGrounCell = InstacneCell(colorCell2, startCell);
        endGroundCell  = InstacneCell(colorCell3, endCell);

        InstacneCharacter(startCell);

        enemyInstance1 = InstanceEnemy(enemy1StartCell, enemyPath1);
        enemyInstance2 = InstanceEnemy(enemy2StartCell, enemyPath2);

        acusitcDetector.Initialize(this);
        InstanceBlocksFromField(field.matrix);

        isGenerated = true;
    }
        public void CheckNewFieldMatrixConstructorCorrectSize()
        {
            MatrixField fieldMatrix = new MatrixField(4, 4);

            Assert.AreEqual(4, fieldMatrix.Rows);
            Assert.AreEqual(4, fieldMatrix.Columns);
        }
        public void CheckNewFieldMatrixLengthPropertyValidity()
        {
            MatrixField fieldMatrix = new MatrixField(4, 4);
            int         testlength  = 16;

            Assert.AreEqual(testlength, fieldMatrix.Length);
        }
Пример #7
0
        public void InitRestoredGame()
        {
            var data = _gameLogic.RestoreGame();

            MatrixField            = data.MatrixField;
            MatrixField.LiveCells  = data.LiveCells;
            MatrixField.Iterations = data.Iterations;
        }
Пример #8
0
        public void InitNewGame(int x, int y)
        {
            MatrixField = new MatrixField(x, y);

            var cells = _matrixFieldLogic.GetFirstGeneration(x, y);

            MatrixField.Cells = cells;
        }
Пример #9
0
 public void Initialize(GameField gameField, Cell position)
 {
     this.gameField = gameField;
     matrixField    = gameField.field;
     mPosition      = new Cell(position.i, position.j);
     //step = gameField.baseCell.size;
     isMoving = false;
     isInit   = true;
 }
Пример #10
0
 public void OutputField(MatrixField MatrixField)
 {
     for (int x = 0; x < MatrixField.DimX; x++)
     {
         for (int y = 0; y < MatrixField.DimY; y++)
         {
             Console.Write((int)MatrixField.Cells[x, y].State);
         }
         Console.WriteLine();
     }
 }
 public void CheckNewFieldMatrixConstructorCorrectInitialization()
 {
     MatrixField fieldMatrix = new MatrixField(4, 4);
     foreach (var cell in fieldMatrix)
     {
         if (cell != 0)
         {
             throw new ArgumentException("When initialized, a cell value must be equal to zero!");
         }
     }
 }
Пример #12
0
    public NoizedLabyrintGenerator(int height, int width)
    {
        this.height = height;
        this.width  = width;

        field = new MatrixField(height, width);

        random = new System.Random();
        stack  = new Stack <Cell>();

        currNeight = new List <Cell>(4);
    }
        public void CheckNewFieldMatrixConstructorCorrectInitialization()
        {
            MatrixField fieldMatrix = new MatrixField(4, 4);

            foreach (var cell in fieldMatrix)
            {
                if (cell != 0)
                {
                    throw new ArgumentException("When initialized, a cell value must be equal to zero!");
                }
            }
        }
Пример #14
0
        public void Test()
        {
            var matrixField = new MatrixField(new int[, ]
            {
                { 0, 0, 1 },
                { 1, 1, 1 },
                { 1, 2, 3 }
            });

            var inverseRuben = matrixField.InverseRuben();
            var inverse      = matrixField.Inverse;

            Assert.Equal(inverse, inverseRuben);
        }
Пример #15
0
        public static MatrixField CreateParityMatrix(List <Block <byte> > dataBlocks, int parityBlockCount)
        {
            int totalBlocks = dataBlocks.Count + parityBlockCount;

            if (totalBlocks > 256)
            {
                throw new InvalidOperationException("A total of more then 256 blocks is not supported");
            }

            var identityMatrix   = MatrixField.CreateIdentityMatrix(dataBlocks.Count);
            var parityOnlyMatrix = CreateParityOnlyMatrix(dataBlocks, parityBlockCount);

            var combinedMatrix = identityMatrix.AddRowsAtTheEnd(parityOnlyMatrix);

            return(combinedMatrix);
        }
Пример #16
0
        public int CountLiveCells(MatrixField matrixField)
        {
            int liveCells = 0;

            for (int i = 0; i < matrixField.DimY; i++)
            {
                for (int j = 0; j < matrixField.DimX; j++)
                {
                    if (State.Alive.Equals(matrixField.Cells[j, i].State))
                    {
                        liveCells++;
                    }
                }
            }
            return(liveCells);
        }
        public void CheckLinearIndex()
        {
            MatrixField fieldMatrix = new MatrixField(4, 4);
            int counter = 0;
            for (int i = 0; i < fieldMatrix.Rows; i++)
            {
                for (int j = 0; j < fieldMatrix.Columns; j++)
                {
                    if (counter != j + (fieldMatrix.Rows * i))
                    {
                        throw new ArgumentException("Incorrect linear index!");
                    }

                    counter++;
                }
            }
        }
Пример #18
0
        void AStar_RunOnStable3On3Filed_ReturnCorrectPath()
        {
            /* The way
             * |F.W|
             * |W..|
             * |WWF|
             */

            var matrix       = new MatrixField();
            var search       = new AStarSearch(matrix.Start, matrix.Goal);
            var expectedPath = "s.W\nW..\nWWg\n";

            search.Run();
            var foundPath = matrix.Print(search.GetPath());

            foundPath.Should().Be(expectedPath);
        }
        public void CheckLinearIndex()
        {
            MatrixField fieldMatrix = new MatrixField(4, 4);
            int         counter     = 0;

            for (int i = 0; i < fieldMatrix.Rows; i++)
            {
                for (int j = 0; j < fieldMatrix.Columns; j++)
                {
                    if (counter != j + (fieldMatrix.Rows * i))
                    {
                        throw new ArgumentException("Incorrect linear index!");
                    }

                    counter++;
                }
            }
        }
Пример #20
0
        public Cell[,] ApplyNextGenerationRulesOnField(MatrixField matrixField)
        {
            var nextCells = new Cell[matrixField.DimX, matrixField.DimY];

            for (int y = 0; y < matrixField.DimY; y++)
            {
                for (int x = 0; x < matrixField.DimX; x++)
                {
                    var  aliveNeighbours = _cellLogic.GetAliveNeighbours(x, y, matrixField);
                    var  oldState        = matrixField.Cells[x, y].State;
                    Cell cell            = new Cell()
                    {
                        State = _cellLogic.DecideState(aliveNeighbours, oldState)
                    };
                    nextCells[x, y] = cell;
                }
            }
            return(nextCells);
        }
Пример #21
0
        public int GetAliveNeighbours(int x, int y, MatrixField matrixField)
        {
            int alive = 0;

            for (int i = -1; i <= 1; i++)
            {
                for (int j = -1; j <= 1; j++)
                {
                    if (i != 0 || j != 0)
                    {
                        int width  = x + j;
                        int height = y + i;


                        if (width < 0)
                        {
                            width = matrixField.DimY - 1;
                        }
                        else if (width > matrixField.DimY - 1)
                        {
                            width = 0;
                        }

                        if (height < 0)
                        {
                            height = matrixField.DimX - 1;
                        }
                        else if (height > matrixField.DimX - 1)
                        {
                            height = 0;
                        }

                        if (State.Alive.Equals(matrixField.Cells[height, width].State))
                        {
                            alive++;
                        }
                    }
                }
            }

            return(alive);
        }
Пример #22
0
        public static List <Block <byte> > RecoverData(List <Block <byte> > dataBlocks, List <Block <byte> > recoveryBlocks, int parityBlockCount)
        {
            var combinedData = dataBlocks.Concat(recoveryBlocks).ToList();
            var combinedDataWithoutMissingData = combinedData.Where(t => t.Data != null).ToList();
            int dataLengthInsideBlock          = combinedData.First(t => t.Data != null).Data.Length;


            var parMatrix = ParityAlgorithm.CreateParityMatrix(dataBlocks, parityBlockCount);
            //var parMatrixOnly = ParityAlgorithm.CreateParityOnlyMatrix(dataBlocks, parityBlockCount);


            var missingDataElements = new List <int>();
            var missingRows         = new List <Field[]>();
            var nonMissingRows      = new List <Field[]>();

            for (int i = 0; i < combinedData.Count; i++)
            {
                var dataBlock = combinedData[i];
                if (dataBlock.Data == null)
                {
                    missingDataElements.Add(i);
                    missingRows.Add(parMatrix.Array[i]);
                }
                else
                {
                    nonMissingRows.Add(parMatrix.Array[i]);
                }
            }

            if (missingDataElements.Count > parityBlockCount)
            {
                throw new InvalidOperationException("Can't recover this data as too much blocks are damaged");
            }



            //var subspace = new MatrixField(new int[,] {
            //        { 0, 0, 1, 0, 0 },
            //        { 0, 0, 0, 1, 0 },
            //    });
            //If there's more repair data then we need, from all the blocks, just take the amount of data blocks
            var rowsNeeded = nonMissingRows.Take(dataBlocks.Count).ToArray();
            var subspace   = new MatrixField(rowsNeeded);

            Console.WriteLine($"Subspace:\n\r{subspace}");

            var inverse  = subspace.InverseRuben();
            var inverse2 = subspace.Inverse;

            Console.WriteLine($"Inverse:\n\r{inverse}");


            if (inverse2 != inverse)
            {
                Console.WriteLine("NU");
            }


            foreach (var dataBlock in dataBlocks)
            {
                if (dataBlock.Data == null)
                {
                    dataBlock.Data = new byte[dataLengthInsideBlock];
                }
            }



            for (int i = 0; i < dataLengthInsideBlock; i++)
            {
                var data = new List <Field>();
                //If there's more repair data then we need, from all the blocks, just take the amount of data blocks
                foreach (var dataBlock in combinedDataWithoutMissingData.Take(dataBlocks.Count))
                {
                    data.Add(new Field(dataBlock.Data[i]));
                }

                var toArray = data.ToArray();
                var vector  = new CoolVectorField(toArray);

                var res = inverse * vector;


                //Console.WriteLine($"Recovered data:\n\r{res}");
                for (int y = 0; y < res.Length; y++)
                {
                    dataBlocks[y].Data[i] = res.Data[y].Value;
                }
            }


            return(dataBlocks);
        }
Пример #23
0
        public void SaveGame(MatrixField matrixField)
        {
            var Data = new Data(matrixField, matrixField.Iterations, matrixField.LiveCells);

            _jsonLogger.SaveGameToLogFile(Data);
        }
Пример #24
0
        //public static List<Block<byte>> RecoverData(GFTable gfTable, List<Block<byte>> dataBlocks, List<Block<byte>> recoveryBlocks, int parityBlockCount)
        //{
        //    var combinedData = dataBlocks.Concat(recoveryBlocks).ToList();
        //    var combinedDataWithoutMissingData = combinedData.Where(t => t.Data != null).ToList();
        //    int dataLengthInsideBlock = combinedData.First(t => t.Data != null).Data.Length;


        //    var parMatrix = CreateParityMatrix(gfTable, dataBlocks.Count, parityBlockCount);
        //    //var parMatrixOnly = CreateParityOnlyMatrix(dataBlocks, parityBlockCount);


        //    var missingDataElements = new List<int>();
        //    var missingRows = new List<GField[]>();
        //    var nonMissingRows = new List<GField[]>();

        //    for (int i = 0; i < combinedData.Count; i++)
        //    {
        //        var dataBlock = combinedData[i];
        //        if (dataBlock.Data == null)
        //        {
        //            missingDataElements.Add(i);
        //            missingRows.Add(parMatrix.Data[i]);
        //        }
        //        else
        //        {
        //            nonMissingRows.Add(parMatrix.Data[i]);
        //        }
        //    }

        //    if (missingDataElements.Count > parityBlockCount)
        //    {
        //        throw new InvalidOperationException("Can't recover this data as too much blocks are damaged");
        //    }



        //    //var subspace = new MatrixGField(new int[,] {
        //    //        { 0, 0, 1, 0, 0 },
        //    //        { 0, 0, 0, 1, 0 },
        //    //    });
        //    //If there's more repair data then we need, from all the blocks, just take the amount of data blocks
        //    var rowsNeeded = nonMissingRows.Take(dataBlocks.Count).ToArray();
        //    var subspace = new MatrixGField(rowsNeeded);
        //    Console.WriteLine($"Subspace:\n\r{subspace}");

        //    var inverse = subspace.InverseRuben();
        //    Console.WriteLine($"Inverse:\n\r{inverse}");



        //    foreach (var dataBlock in dataBlocks)
        //    {
        //        if (dataBlock.Data == null)
        //        {
        //            dataBlock.Data = new byte[dataLengthInsideBlock];
        //        }
        //    }



        //    for (int i = 0; i < dataLengthInsideBlock; i++)
        //    {
        //        var data = new List<GField>();
        //        //If there's more repair data then we need, from all the blocks, just take the amount of data blocks
        //        foreach (var dataBlock in combinedDataWithoutMissingData.Take(dataBlocks.Count))
        //        {
        //            var newField = gfTable.CreateField(dataBlock.Data[i]);
        //            data.Add(newField);
        //        }

        //        var toArray = data.ToArray();
        //        //var vector = new CoolVectorField(toArray);

        //        var res = inverse.Multiply(toArray);


        //        //Console.WriteLine($"Recovered data:\n\r{res}");
        //        for (int y = 0; y < res.Length; y++)
        //        {
        //            dataBlocks[y].Data[i] = (byte)res[y].Value;
        //        }
        //    }


        //    return dataBlocks;
        //}



        public static List <Block <byte> > RecoverData2(GFTable gfTable, List <Block <byte> > dataBlocks, List <Block <byte> > recoveryBlocks, int parityBlockCount)
        {
            var combinedData = dataBlocks.Concat(recoveryBlocks).ToList();
            var combinedDataWithoutMissingData = combinedData.Where(t => t.Data != null).ToList();
            int dataLengthInsideBlock          = combinedData.First(t => t.Data != null).Data.Length;


            var parMatrix = CreateParityMatrix2(gfTable, dataBlocks.Count, parityBlockCount);
            //var parMatrixOnly = CreateParityOnlyMatrix(dataBlocks, parityBlockCount);

            var missingDataElements = new List <int>();
            var missingRows         = new List <GField[]>();
            var nonMissingRows      = new List <GField[]>();

            for (int i = 0; i < combinedData.Count; i++)
            {
                var dataBlock = combinedData[i];
                if (dataBlock.Data == null)
                {
                    missingDataElements.Add(i);
                    missingRows.Add(parMatrix.Data[i]);
                }
                else
                {
                    nonMissingRows.Add(parMatrix.Data[i]);
                }
            }

            if (missingDataElements.Count > parityBlockCount)
            {
                throw new InvalidOperationException("Can't recover this data as too much blocks are damaged");
            }



            //var subspace = new MatrixGField(new int[,] {
            //        { 0, 0, 1, 0, 0 },
            //        { 0, 0, 0, 1, 0 },
            //    });
            //If there's more repair data then we need, from all the blocks, just take the amount of data blocks
            var rowsNeeded = nonMissingRows.Take(dataBlocks.Count).ToArray();
            var subspace   = new MatrixGField(rowsNeeded);

            Console.WriteLine($"Subspace:\n\r{subspace}");

            var inverse = subspace.InverseRuben();

            Console.WriteLine($"Inverse:\n\r{inverse}");

            var testje = subspace * inverse;

            var blah     = subspace.Data.Select(t => t.Select(z => new Field((byte)z.Value)).ToArray()).ToArray();
            var mf       = new MatrixField(blah);
            var inversed = mf.Inverse;

            var realInverseData = inversed.Array.Select(t => t.Select(z => gfTable.CreateField((uint)z)).ToArray()).ToArray();
            var realInverse     = new MatrixGField(realInverseData);



            foreach (var dataBlock in dataBlocks)
            {
                if (dataBlock.Data == null)
                {
                    dataBlock.Data = new byte[dataLengthInsideBlock];
                }
            }



            for (int i = 0; i < dataLengthInsideBlock; i++)
            {
                var data = new List <GField>();
                //If there's more repair data then we need, from all the blocks, just take the amount of data blocks
                foreach (var dataBlock in combinedDataWithoutMissingData.Take(dataBlocks.Count))
                {
                    var newField = gfTable.CreateField(dataBlock.Data[i]);
                    data.Add(newField);
                }

                var toArray = data.ToArray();
                //var vector = new CoolVectorField(toArray);

                var res = inverse.Multiply(toArray);


                //Console.WriteLine($"Recovered data:\n\r{res}");
                for (int y = 0; y < res.Length; y++)
                {
                    dataBlocks[y].Data[i] = (byte)res[y].Value;
                }
            }


            return(dataBlocks);
        }
 public void CheckGetterandSetter()
 {
     MatrixField fieldMatrix = new MatrixField(4, 4);
     fieldMatrix[1, 1] = 7;
     Assert.AreEqual(7, fieldMatrix[1, 1]);
 }
 public void CheckGetterandSetterViaTheNewIndex()
 {
     MatrixField fieldMatrix = new MatrixField(4, 4);
     fieldMatrix[8] = 12;
     Assert.AreEqual(12, fieldMatrix[8]);
 }
Пример #27
0
        public static void GoParStuff()
        {
            //usage example
            Field f1 = new Field(15);
            Field f2 = new Field(8);
            Field f3 = f1 + f2;

            Console.WriteLine(f3);

            var f4 = f3 - f2;

            Console.WriteLine(f4);


            //var v1 = new CoolVector(1, 2, 3);
            //var v2 = new CoolVector(2, 1, 3);

            //var ar2 = new int[,] {
            //    { 1, 2, 3 },
            //    { 4, 5, 6 },
            //    { 7, 8, 9 }
            //};


            //var matrix = new Matrix(ar2);

            //var matrix2 = new Matrix(new int[,] {
            //    { 2 },
            //    { 1 },
            //    { 3 }
            //});



            //var result = matrix * v2;
            //Console.WriteLine(result);

            //var result2 = matrix * matrix2;
            //Console.WriteLine(result2);



            // 10
            // 3

            var ar1 = new int[, ] {
                { 1, 0, 0, 0, 0 },
                { 0, 1, 0, 0, 0 },
                { 0, 0, 1, 0, 0 },
                { 0, 0, 0, 1, 0 },
                { 0, 0, 0, 0, 1 },
                { 1, 1, 1, 1, 1 },
                { 1, 2, 3, 4, 5 }
            };


            var matrixField  = new MatrixField(ar1);
            var dataForField = new MatrixField(new int[, ] {
                { 10 },
                { 5 },
                { 8 },
                { 13 },
                { 2 }
            });
            var dataForField2 = new CoolVectorField(
                10,
                5,
                8,
                13,
                2
                );

            var result3 = matrixField * dataForField2;

            Console.WriteLine(result3);


            int parityBlocks = 2;

            var data = new List <Block <byte> >()
            {
                new Block <byte>()
                {
                    Data = new byte[] { 10 }
                },
                new Block <byte>()
                {
                    Data = new byte[] { 5 }
                },
                new Block <byte>()
                {
                    Data = new byte[] { 8 }
                },
                new Block <byte>()
                {
                    Data = new byte[] { 13 }
                },
                new Block <byte>()
                {
                    Data = new byte[] { 2 }
                },
            };


            var parMatrix     = ParityAlgorithm.CreateParityMatrix(data, parityBlocks);
            var parMatrixOnly = ParityAlgorithm.CreateParityOnlyMatrix(data, parityBlocks);

            Console.WriteLine();
            Console.Write(parMatrix);
            Console.WriteLine();
            Console.Write(parMatrixOnly);

            var recoveryData = ParityAlgorithm.GenerateParityData(data, parityBlocks);

            Console.WriteLine(string.Join(Environment.NewLine, recoveryData.Select(t => string.Join(",", t.Data))));



            //var totalData = data.Concat(recoveryData).ToList();
            //data[0].Data = null;
            data[1].Data = null;
            //data[2].Data = null;
            //data[3].Data = null;
            //data[4].Data = null;
            //recoveryData[0].Data = null;
            recoveryData[1].Data = null;
            ParityAlgorithm.RecoverData(data, recoveryData, parityBlocks);
            //ParityAlgorithm.RecoverDataV2(data, recoveryData, parityBlocks);



            var missing = new MatrixField(new int[, ]
            {
                { 1 },
                { 1 },
                { 1 },
                { 1 },
                { 1 },
                { 1 },
                { 1 }
            });
        }
 public void CheckNewFieldMatrixLengthPropertyValidity()
 {
     MatrixField fieldMatrix = new MatrixField(4, 4);
     int testlength = 16;
     Assert.AreEqual(testlength, fieldMatrix.Length);
 }
 public void CheckNewFieldMatrixConstructorCorrectSize()
 {
     MatrixField fieldMatrix = new MatrixField(4, 4);
     Assert.AreEqual(4, fieldMatrix.Rows);
     Assert.AreEqual(4, fieldMatrix.Columns);
 }