public void OverlapTechniqueGivesSolutionWhenHintGivenCompleteInformation_5_2_2()
        {
            // Testing: Finds solution for odd-numbered width
            // Expected Results:
            // Width: 5
            // Hint: 2, 2
            //
            // Solution:
            // XX XX

            const int width = 5;
            var       n     = new Nonogram(width, 1);
            var       line  = n.Row(0);

            line.Hints.AddRange(new int[] { 2, 2 });

            Cell.CellState[] expected = new Cell.CellState[width] {
                Cell.CellState.Filled,
                Cell.CellState.Filled,
                Cell.CellState.Blank,
                Cell.CellState.Filled,
                Cell.CellState.Filled
            };

            var t = new OverlapTechnique();

            t.Apply(line);

            for (int i = 0; i < width; ++i)
            {
                Assert.AreEqual(expected[i], line[i].State);
            }
        }
        public void OverlapTechniqueFindsSolutionWhenGivenPartialInformation_5_3_last()
        {
            // Testing: Finds information when a partial solution is in place
            // Expected Results:
            // Width: 5
            // Hint: 3
            // Pre-given information: ????X
            // Solution: __XXX

            const int width = 5;
            var       n     = new Nonogram(width, 1);
            var       line  = n.Row(0);

            line.Hints.AddRange(new int[] { 3 });

            line[4].State = Cell.CellState.Filled;

            Cell.CellState[] expected = new Cell.CellState[width] {
                Cell.CellState.Blank,
                Cell.CellState.Blank,
                Cell.CellState.Filled,
                Cell.CellState.Filled,
                Cell.CellState.Filled
            };

            var t = new OverlapTechnique();

            t.Apply(line);

            for (int i = 0; i < width; ++i)
            {
                Assert.AreEqual(expected[i], line[i].State);
            }
        }
示例#3
0
    public List <List <Cell> > LoadMapFromString(string map)
    {
        string[]            csvLines = map.Split('\n');
        List <List <Cell> > cells    = new List <List <Cell> >();
        Camera     cam = Camera.main;
        float      worldSpace_ScreenHeight = 2f * cam.orthographicSize;
        float      worldSpace_ScreenWidth  = worldSpace_ScreenHeight * cam.aspect;
        Vector2    LowerLeftPos            = new Vector2(-(worldSpace_ScreenWidth / 2.0f), -(worldSpace_ScreenHeight / 2.0f));
        Vector2Int cellCount = new Vector2Int(Mathf.CeilToInt(worldSpace_ScreenWidth / cellSize), Mathf.FloorToInt(worldSpace_ScreenHeight / cellSize) - 1);

        for (int i = 0; i < cellCount.y; i++)
        {
            List <Cell> aRowCells = new List <Cell>();
            string[]    aline     = csvLines[i + 1].Split(',');
            for (int j = 0; j < cellCount.x; j++)
            {
                Transform cellTrns = ObjectPoolManager.Instance.GetAnObject(cellObjectKey);
                cellTrns.parent = cellsParent;
                Vector2 relativePos = new Vector3(j * cellSize + cellSize / 2, i * cellSize + cellSize / 2);
                cellTrns.position = LowerLeftPos + relativePos;
                Cell           cell  = cellTrns.GetComponent <Cell>();
                Cell.CellState state = (Cell.CellState) int.Parse(aline[j]);
                cell.Setup(new Vector2Int(i, j), state);
                aRowCells.Add(cell);
            }
            cells.Add(aRowCells);
        }
        return(cells);
    }
        public void OverlapTechniqueDoesNothingWhenGivenConflictingInformation()
        {
            // Testing: Finds information when a partial solution is in place
            // Expected Results:
            // Width: 5
            // Hint: 3
            // Pre-given information: X???X

            const int width = 5;
            var       n     = new Nonogram(width, 1);
            var       line  = n.Row(0);

            line.Hints.AddRange(new int[] { 3 });

            line[0].State = Cell.CellState.Filled;
            line[4].State = Cell.CellState.Filled;

            Cell.CellState[] expected = new Cell.CellState[width] {
                Cell.CellState.Filled,
                Cell.CellState.Unknown,
                Cell.CellState.Unknown,
                Cell.CellState.Unknown,
                Cell.CellState.Filled
            };

            var t = new OverlapTechnique();

            t.Apply(line);

            for (int i = 0; i < width; ++i)
            {
                Assert.AreEqual(expected[i], line[i].State);
            }
        }
示例#5
0
        public void CellLineMinFunctionsWithAllUnknownCells_5_2_1()
        {
            Nonogram n = new Nonogram(5, 1);
            CellLine l = n.Row(0);

            l.Hints.AddRange(new int[] { 2, 1 });

            var expectedStates = new Cell.CellState[5] {
                Cell.CellState.Filled,
                Cell.CellState.Filled,
                Cell.CellState.Blank,
                Cell.CellState.Filled,
                Cell.CellState.Blank
            };

            var expectedFlags = new int[5] {
                0,
                0,
                0,
                1,
                1
            };

            var min = l.Min();

            Assert.IsNotNull(min);

            for (int i = 0; i < 5; ++i)
            {
                Assert.AreEqual(expectedStates[i], min[i].State);
                Assert.AreEqual(expectedFlags[i], min[i].Flag);
            }
        }
示例#6
0
        public void FillingPartOfALineThatHasStateOutsideThatPartWithStateAndFlagFillsTheSectionWithThatStateAndFlagAndReturnsFalse()
        {
            Nonogram n = new Nonogram(5, 1);
            CellLine l = n.Row(0);

            l[0].State = Cell.CellState.Blank;

            Assert.IsTrue(l.Fill(1, 2, Cell.CellState.Filled, 42));

            var expectedStates = new Cell.CellState[5] {
                Cell.CellState.Blank,
                Cell.CellState.Filled,
                Cell.CellState.Filled,
                Cell.CellState.Unknown,
                Cell.CellState.Unknown
            };

            var expectedFlags = new int[5] {
                0,
                42,
                42,
                0,
                0
            };

            for (int i = 0; i < 5; i++)
            {
                Assert.AreEqual(expectedStates[i], l[i].State);
                Assert.AreEqual(expectedFlags[i], l[i].Flag);
            }
        }
        public void OverlapTechniqueGivesSolutionWhenHintGivenCompleteInformation_1_1()
        {
            // Testing: Finds solution for simple problem
            // Expected Results:
            // Width: 1
            // Hint: 1
            //
            // Solution:
            // X

            const int width = 1;
            var       n     = new Nonogram(width, 1);
            var       line  = n.Row(0);

            line.Hints.AddRange(new int[] { 1 });

            Cell.CellState[] expected = new Cell.CellState[width] {
                Cell.CellState.Filled
            };

            var t = new OverlapTechnique();

            t.Apply(line);

            for (int i = 0; i < width; ++i)
            {
                Assert.AreEqual(expected[i], line[i].State);
            }
        }
示例#8
0
        public void CellLineMaxFunctionsWithSomeBlockedCells()
        {
            Nonogram n = new Nonogram(5, 1);
            CellLine l = n.Row(0);

            l.Hints.AddRange(new int[] { 2, 1 });
            l[2].State = Cell.CellState.Blank;

            var expectedStates = new Cell.CellState[5] {
                Cell.CellState.Filled,
                Cell.CellState.Filled,
                Cell.CellState.Blank, // forced blank
                Cell.CellState.Blank,
                Cell.CellState.Filled
            };

            var expectedFlags = new int[5] {
                0,
                0,
                0,
                1,
                1
            };

            var max = l.Max();

            Assert.IsNotNull(max);

            for (int i = 0; i < 5; ++i)
            {
                Assert.AreEqual(expectedStates[i], max[i].State);
                Assert.AreEqual(expectedFlags[i], max[i].Flag);
            }
        }
示例#9
0
        private bool IsValidPosition(int position, int length, Cell.CellState desiredState)
        {
            var temp = DeepClone();

            temp.Fill(position, length, desiredState);
            return(temp.IsValid());
        }
示例#10
0
        public void CellLineMaxFunctionsWithSomeKnownCells_5_3_last()
        {
            Nonogram n = new Nonogram(5, 1);
            CellLine l = n.Row(0);

            l.Hints.AddRange(new int[] { 3 });
            l[4].State = Cell.CellState.Filled;

            var expectedStates = new Cell.CellState[5] {
                Cell.CellState.Blank,
                Cell.CellState.Blank,
                Cell.CellState.Filled,
                Cell.CellState.Filled,
                Cell.CellState.Filled
            };

            var expectedFlags = new int[5] {
                0,
                1,
                0,
                0,
                0
            };

            var max = l.Max();

            Assert.IsNotNull(max);

            for (int i = 0; i < 5; ++i)
            {
                Assert.AreEqual(expectedStates[i], max[i].State);
                Assert.AreEqual(expectedFlags[i], max[i].Flag);
            }
        }
示例#11
0
        public void Then_the_cell_should_be_dead(int xPos, int yPos, Cell.CellState state)
        {
            var cell = lifeGame.GetCell(xPos, yPos);

            Assert.IsNotNull(cell);
            Assert.IsTrue(cell.State == state);
        }
示例#12
0
 public void SetAllCellState(Cell.CellState state)
 {
     foreach (Cell cell in cells)
     {
         cell.State = state;
     }
 }
示例#13
0
    // Returns the frontier cells at distance 2 and at the desired state of the given cell
    public List <IndexPair> GetFrontierCellAtDistance2(int row, int column, Cell.CellState desiredState)
    {
        List <IndexPair> frontierCells = new List <IndexPair>();

        // Determine which frontier cells are whithin the grid
        Cell topCell    = (row - 2) >= 0 ? grid[row - 2, column] : null;
        Cell bottomCell = (row + 2) < rows ? grid[row + 2, column] : null;
        Cell leftCell   = (column - 2) >= 0 ? grid[row, column - 2] : null;
        Cell rightCell  = (column + 2) < columns ? grid[row, column + 2] : null;

        // Check the state of the frontier cells whithin the grid
        if (topCell != null && topCell.state == desiredState)
        {
            IndexPair newPair = new IndexPair(row - 2, column);
            frontierCells.Add(newPair);
        }
        if (bottomCell != null && bottomCell.state == desiredState)
        {
            IndexPair newPair = new IndexPair(row + 2, column);
            frontierCells.Add(newPair);
        }
        if (leftCell != null && leftCell.state == desiredState)
        {
            IndexPair newPair = new IndexPair(row, column - 2);
            frontierCells.Add(newPair);
        }
        if (rightCell != null && rightCell.state == desiredState)
        {
            IndexPair newPair = new IndexPair(row, column + 2);
            frontierCells.Add(newPair);
        }

        return(frontierCells);
    }
示例#14
0
        private void UpdateState()
        {
            if (cell.State == Cell.CellState.HIDDEN)
            {
                if (prevState != Cell.CellState.HIDDEN)
                {
                    scale.BeginAnimation(ScaleTransform.ScaleXProperty, new DoubleAnimation {
                        From = scale.ScaleX, To = 0, Duration = TimeSpan.FromMilliseconds(ANIMATION_DURATION_MS)
                    });
                }

                rectBackground.Fill = Brushes.LightGoldenrodYellow;
            }

            if (cell.State == Cell.CellState.REVEALED)
            {
                scale.BeginAnimation(ScaleTransform.ScaleXProperty, new DoubleAnimation {
                    From = scale.ScaleX, To = 1, Duration = TimeSpan.FromMilliseconds(ANIMATION_DURATION_MS)
                });

                rectBackground.Fill = Brushes.Orange;
            }

            if (cell.State == Cell.CellState.SHOWN)
            {
                scale.BeginAnimation(ScaleTransform.ScaleXProperty, new DoubleAnimation {
                    From = scale.ScaleX, To = 1, Duration = TimeSpan.FromMilliseconds(ANIMATION_DURATION_MS)
                });

                rectBackground.Fill = Brushes.LimeGreen;
            }

            prevState = cell.State;
        }
示例#15
0
        // IsTargetCellValid(): given a base piece and a position (Vector2Int), return true if target cell is a valid move for piece
        public bool IsTargetCellValid(BasePiece piece, Vector2Int position)
        {
            Cell targetCell = mAllCells[position.x, position.y];

            Cell.CellState targetCellState = Cell.InquireCellState(piece, targetCell);

            return(targetCellState == Cell.CellState.Empty || targetCellState == Cell.CellState.Enemy);
        }
        public void OverlapTechniqueFindsSolutionForGreedyPitfall()
        {
            // Hint: 2 3 4 2
            // Given:    ???????XX???XXXX????
            // Expected: ???????XX??_XXXX_?X?
            const int width = 20;
            var       n     = new Nonogram(width, 1);
            var       line  = n.Row(0);

            line.Hints.AddRange(new int[] { 2, 3, 4, 2 });
            line[7].State                      =
                line[8].State                  =
                    line[12].State             =
                        line[13].State         =
                            line[14].State     =
                                line[15].State =
                                    Cell.CellState.Filled;

            Cell.CellState[] expected = new Cell.CellState[width] {
                Cell.CellState.Unknown,
                Cell.CellState.Unknown,
                Cell.CellState.Unknown,
                Cell.CellState.Unknown,
                Cell.CellState.Unknown,

                Cell.CellState.Unknown,
                Cell.CellState.Unknown,
                Cell.CellState.Filled,
                Cell.CellState.Filled,
                Cell.CellState.Unknown,

                Cell.CellState.Unknown,
                Cell.CellState.Blank,
                Cell.CellState.Filled,
                Cell.CellState.Filled,
                Cell.CellState.Filled,

                Cell.CellState.Filled,
                Cell.CellState.Blank,
                Cell.CellState.Unknown,
                Cell.CellState.Filled,
                Cell.CellState.Unknown
            };

            var t = new OverlapTechnique();

            t.Apply(line);

            for (int i = 0; i < width; ++i)
            {
                Assert.AreEqual(expected[i], line[i].State);
            }
        }
示例#17
0
        Cell.CellState ReverseState(Cell.CellState color)
        {
            if (color == Cell.CellState.BLUE)
            {
                return(Cell.CellState.RED);
            }

            else if (color == Cell.CellState.RED)
            {
                return(Cell.CellState.BLUE);
            }

            return(Cell.CellState.DEAD);
        }
示例#18
0
        public static bool TurnMatchesColor(Player.PlayerColor color, Cell.CellState state)
        {
            if (color == Player.PlayerColor.BLUE && state == Cell.CellState.BLUE)
            {
                return(true);
            }

            if (color == Player.PlayerColor.RED && state == Cell.CellState.RED)
            {
                return(true);
            }

            return(false);
        }
示例#19
0
        // IsEnemyInCell(): given a position (Vector2Int) and a piece, return true if cell contains a enemy piece
        public bool IsEnemyInCell(BasePiece piece, Vector2Int position)
        {
            Cell targetCell = mAllCells[position.x, position.y];

            Cell.CellState targetCellState = Cell.InquireCellState(piece, targetCell);

            if (targetCellState == Cell.CellState.Enemy)
            {
                return(true);
            }
            else
            {
                return(false);
            }
        }
示例#20
0
        public bool Fill(int startingPosition, int length, Cell.CellState desiredState)
        {
            bool ret = true;

            for (int i = startingPosition; i < startingPosition + length; i++)
            {
                if (cells[i].State != Cell.CellState.Unknown && cells[i].State != desiredState)
                {
                    ret = false;
                }

                cells[i].State = desiredState;
            }

            return(ret);
        }
        public void OverlapTechniqueGivesInformationWhenHintGivenIncompleteInformation_15_8()
        {
            // Testing: Edge case, odd width with single filled block
            // Expected Results:
            // Width: 15
            // Hint: 8
            //
            // Min/Max:
            // XXXXXXXX???????
            // ???????XXXXXXXX
            //
            // Overlap:
            // ???????X???????

            const int width = 15;
            var       n     = new Nonogram(width, 1);
            var       line  = n.Row(0);

            line.Hints.AddRange(new int[] { 8 });

            Cell.CellState[] expected = new Cell.CellState[width] {
                Cell.CellState.Unknown,
                Cell.CellState.Unknown,
                Cell.CellState.Unknown,
                Cell.CellState.Unknown,
                Cell.CellState.Unknown,
                Cell.CellState.Unknown,
                Cell.CellState.Unknown,
                Cell.CellState.Filled,
                Cell.CellState.Unknown,
                Cell.CellState.Unknown,
                Cell.CellState.Unknown,
                Cell.CellState.Unknown,
                Cell.CellState.Unknown,
                Cell.CellState.Unknown,
                Cell.CellState.Unknown
            };

            var t = new OverlapTechnique();

            t.Apply(line);

            for (int i = 0; i < width; ++i)
            {
                Assert.AreEqual(expected[i], line[i].State);
            }
        }
示例#22
0
        // IsEnemyKingInCell(): given a position (Vector2Int) and a piece, return true if cell contains the piece opposing king
        public bool IsEnemyKingInCell(BasePiece piece, Vector2Int position)
        {
            Cell targetCell = mAllCells[position.x, position.y];

            Cell.CellState targetCellState = Cell.InquireCellState(piece, targetCell);

            if (targetCellState == Cell.CellState.Enemy)
            {
                BasePiece targetCellPiece = targetCell.GetCurrentPiece();
                if (targetCellPiece is King)
                {
                    return(true);
                }
            }

            return(false);
        }
        public void OverlapTechniqueGivesInformationWhenHintGivenIncompleteInformationAndPartialSolution_10_1_1_1_1_1()
        {
            // Testing: Finds information when a partial solution is in place
            // Expected Results:
            // Width: 10
            // Hint: 1, 1, 1, 1, 1
            // Pre-given information: ??????? ??
            //
            // Min/Max:
            // ??????? ??
            // X X X X X
            // X X X X  X
            //
            // Overlap:
            // X X X X ??

            const int width = 10;
            var       n     = new Nonogram(width, 1);
            var       line  = n.Row(0);

            line.Hints.AddRange(new int[] { 1, 1, 1, 1, 1 });

            line[7].State = Cell.CellState.Blank;

            Cell.CellState[] expected = new Cell.CellState[width] {
                Cell.CellState.Filled,
                Cell.CellState.Blank,
                Cell.CellState.Filled,
                Cell.CellState.Blank,
                Cell.CellState.Filled,
                Cell.CellState.Blank,
                Cell.CellState.Filled,
                Cell.CellState.Blank,
                Cell.CellState.Unknown,
                Cell.CellState.Unknown
            };

            var t = new OverlapTechnique();

            t.Apply(line);

            for (int i = 0; i < width; ++i)
            {
                Assert.AreEqual(expected[i], line[i].State);
            }
        }
        private void EnsureNonogramIsSameAsPicture(Nonogram n, string picture)
        {
            picture = picture.Trim();
            var lines = picture.Split(new string[] { "\r\n" }, StringSplitOptions.None);

            Assert.AreEqual(lines.Length, n.height);
            Assert.AreEqual(lines[0].Length, n.width);

            for (int y = 0; y < lines.Length; y++)
            {
                for (int x = 0; x < lines[0].Length; x++)
                {
                    Cell.CellState expected = lines[y][x] == 'X' ? Cell.CellState.Filled : Cell.CellState.Blank;

                    Assert.AreEqual(expected, n[x, y].State);
                }
            }
        }
示例#25
0
        /// <summary>
        /// Finds the next index that will fit a line of cells with the given cell state
        /// in this CellLine.
        /// </summary>
        /// <param name="startingPosition">The position to start searching from.</param>
        /// <param name="length">The number of cells to look for an opening for.</param>
        /// <param name="desiredState">The state that we're looking for an opening for.</param>
        /// <returns>The next index that will fit the given cell state. Null if no opening exists.</returns>
        private int?FindNextOpening(int startingPosition, int length, Cell.CellState desiredState)
        {
            if (length == 0)
            {
                return(startingPosition);
            }

            for (int i = startingPosition; i <= Length - length; i++)
            {
                if (IsClear(i, length, desiredState) && IsValidPosition(i, length, desiredState))
                {
                    return(i);
                }

                // TODO: Optimization - Find the obstruction and move forward until i is past it.
            }

            return(null);
        }
示例#26
0
        public static string PrintCellState(Cell.CellState state)
        {
            switch (state)
            {
            case Cell.CellState.Bombed:
                return("x");

            case Cell.CellState.Missed:
                return("o");

            case Cell.CellState.Empty:
                return(" ");

            case Cell.CellState.Ship:
                return("◘");
            }

            return("");
        }
        public void OverlapTechniqueGivesInformationWhenHintGivenIncompleteInformation_10_2_2_3()
        {
            // Testing: Odd number of hints with even width
            // Expected Results:
            // Width: 10
            // Hint: 2, 2, 3
            //
            // Min/Max:
            // XX XX XXX?
            // ?XX XX XXX
            //
            // Overlap:
            // ?X??X??XX?

            const int width = 10;
            var       n     = new Nonogram(width, 1);
            var       line  = n.Row(0);

            line.Hints.AddRange(new int[] { 2, 2, 3 });

            Cell.CellState[] expected = new Cell.CellState[width] {
                Cell.CellState.Unknown,
                Cell.CellState.Filled,
                Cell.CellState.Unknown,
                Cell.CellState.Unknown,
                Cell.CellState.Filled,
                Cell.CellState.Unknown,
                Cell.CellState.Unknown,
                Cell.CellState.Filled,
                Cell.CellState.Filled,
                Cell.CellState.Unknown
            };

            var t = new OverlapTechnique();

            t.Apply(line);

            for (int i = 0; i < width; ++i)
            {
                Assert.AreEqual(expected[i], line[i].State);
            }
        }
示例#28
0
        public void FillingPartOfALineWithStateFillsTheSectionWithThatStateAndReturnsTrue()
        {
            Nonogram n = new Nonogram(5, 1);
            CellLine l = n.Row(0);

            Assert.IsTrue(l.Fill(1, 2, Cell.CellState.Filled));

            var expectedStates = new Cell.CellState[5] {
                Cell.CellState.Unknown,
                Cell.CellState.Filled,
                Cell.CellState.Filled,
                Cell.CellState.Unknown,
                Cell.CellState.Unknown
            };

            for (int i = 0; i < 5; i++)
            {
                Assert.AreEqual(expectedStates[i], l[i].State);
            }
        }
示例#29
0
        /// <summary>
        /// Returns whether or not the next x cells starting from a given position
        /// can be filled with desiredState without issues occurring. For example,
        /// there is a state of a different value contained within, or the given
        /// length would run off the edge of the line.
        /// </summary>
        /// <param name="position">The position to start checking from</param>
        /// <param name="length">The number of positions to check</param>
        /// <returns>Whether or not the given positions are able to be filled</returns>
        private bool IsClear(int position, int length, Cell.CellState desiredState)
        {
            for (int i = 0; i < length; i++)
            {
                int curPos = position + i;

                // Check bounds
                if (curPos >= Length || position < 0 || curPos < 0)
                {
                    return(false);
                }

                // Check for non-compatible cell.
                if (cells[curPos].State != Cell.CellState.Unknown &&
                    cells[curPos].State != desiredState)
                {
                    return(false);
                }
            }

            return(true);
        }
        public void OverlapTechniqueGivesInformationWhenHintGivenIncompleteInformation_5_2_1()
        {
            // Testing: Finds information when given incomplete hints, variation 2
            // Expected Results:
            // Width: 5
            // Hint: 2, 1
            //
            // Min/Max:
            // XX X?
            // ?XX X
            //
            // Overlap:
            // ?X???

            const int width = 5;
            var       n     = new Nonogram(width, 1);
            var       line  = n.Row(0);

            line.Hints.AddRange(new int[] { 2, 1 });

            Cell.CellState[] expected = new Cell.CellState[width] {
                Cell.CellState.Unknown,
                Cell.CellState.Filled,
                Cell.CellState.Unknown,
                Cell.CellState.Unknown,
                Cell.CellState.Unknown
            };

            var t = new OverlapTechnique();

            t.Apply(line);

            for (int i = 0; i < width; ++i)
            {
                Assert.AreEqual(expected[i], line[i].State);
            }
        }