Esempio n. 1
0
        public void ConstructorTest()
        {
            Cell cell = new Cell(new Location(23, 34));

            Assert.AreEqual(23, cell.X);
            Assert.AreEqual(34, cell.Y);
            Assert.AreEqual(Occupied.Empty, cell.IsOccupied);
        }
Esempio n. 2
0
        public void CopyConstructorTest()
        {
            Cell cell = new Cell(new Location(0, 0));
            Cell newCell = new Cell(cell);

            Assert.AreEqual(cell.X, newCell.X);
            Assert.AreEqual(cell.Y, newCell.Y);
            Assert.AreEqual(cell.IsOccupied, newCell.IsOccupied);
        }
Esempio n. 3
0
        public void IsOccupiedYTest()
        {
            Cell cell = new Cell(new Location(0, 0));

            cell.IsOccupied = Occupied.PlayerY;
            bool returnValue = cell.IsEmpty();
            Assert.IsFalse(returnValue);

            returnValue = cell.IsPlayer(true);
            Assert.IsFalse(returnValue);

            returnValue = cell.IsPlayer(false);
            Assert.IsTrue(returnValue);
        }
Esempio n. 4
0
        /// <summary>
        /// number of steps needed to reach a cell is the min of the steps
        /// needed to reach a neighbour, plus one (unles it's filled)
        /// </summary>
        /// <param name="cell">the cell to update</param>
        /// <returns>true if work was done</returns>
        private bool UpdateCellFromNeighbour(Cell cell)
        {
            var neighbours = board.Neighbours(cell);
            int closest = this.MinVal(neighbours);

            if (cell.IsEmpty() && closest < PathLengthConstants.OffPath)
            {
                closest++;
            }

            if (closest < Value(cell.Location))
            {
                this.SetValByLoc(cell.Location, closest);
                return true;
            }

            return false;
        }
Esempio n. 5
0
 public CellEventArgs(Cell cell)
 {
     this.cell = cell;
 }
Esempio n. 6
0
        private bool IsInLastRow(Cell cell)
        {
            if (playerX)
            {
                return cell.Y == (board.Size - 1);
            }

            return cell.X == (this.board.Size - 1);
        }
Esempio n. 7
0
        private void AddInProgressCell(Cell cell, int value)
        {
            // is this cell in progress already?
            int existingIndex = this.inProgressCells.IndexOf(cell);
            if (existingIndex > -1)
            {
                if (value >= this.inProgressCellDistances[existingIndex])
                {
                    // we've found another route to a cell already in progress
                    // but it's not shorter, so it's of no interest
                    return;
                }

                // a new and shorter route to an in-progress cell
                // remove it from it from its current position
                // and add it at the new position as below
                this.inProgressCells.RemoveAt(existingIndex);
                this.inProgressCellDistances.RemoveAt(existingIndex);
            }

            // add the cell, in the right sorted position
            bool added = false;
            int loopMax = this.inProgressCells.Count; // does not vary during loop

            for (int loopIndex = 0; loopIndex < loopMax; loopIndex++)
            {
                if (value < this.inProgressCellDistances[loopIndex])
                {
                    this.inProgressCells.Insert(loopIndex, cell);
                    this.inProgressCellDistances.Insert(loopIndex, value);
                    added = true;
                    break;
                }
            }

            if (!added)
            {
                // put it on the end
                this.inProgressCells.Add(cell);
                this.inProgressCellDistances.Add(value);
            }

            vals[cell.X, cell.Y] = value;
        }
Esempio n. 8
0
        /// <summary>
        /// get the start (or end) row for player 1 (or 2)
        /// </summary>
        /// <param name="playerX">is the player queried player X</param>
        /// <param name="start">start row or end row</param>
        /// <returns>the cells in the row</returns>
        public Cell[] Row(bool playerX, bool start)
        {
            Cell[] result = new Cell[this.Size];

            // first or last row/col
            int col = start ? 0 : this.Size - 1;

            for (int loopIndex = 0; loopIndex < this.Size; loopIndex++)
            {
                if (playerX)
                {
                    result[loopIndex] = this.GetCellAt(loopIndex, col);
                }
                else
                {
                    result[loopIndex] = this.GetCellAt(col, loopIndex);
                }
            }

            return result;
        }
Esempio n. 9
0
 public Cell[][] Neighbours2(Cell cell)
 {
     return this.Neighbours2(cell.Location);
 }
Esempio n. 10
0
        private static void TestNeighbour2Triplet(Cell cell, Cell[] triplet)
        {
            Assert.AreEqual(3, triplet.Length);
            NoNullsInCellArray(triplet);

            var testBoard = new HexBoardNeighbours(BoardSize);
            var testLoc = cell.Location;
            var neighbour2 = triplet[0].Location;
            var between1 = triplet[1].Location;
            var between2 = triplet[2].Location;

            Assert.IsTrue(testBoard.AreNeighbours(between1, between2));

            Assert.IsTrue(testBoard.AreNeighbours(testLoc, between1));
            Assert.IsTrue(testBoard.AreNeighbours(testLoc, between2));

            Assert.IsTrue(testBoard.AreNeighbours(neighbour2, between1));
            Assert.IsTrue(testBoard.AreNeighbours(neighbour2, between2));

            // but not neighbours to each other
            Assert.IsFalse(testBoard.AreNeighbours(testLoc, neighbour2));
        }
Esempio n. 11
0
        /// <summary>
        /// get an array of cells for an array of locations
        /// </summary>
        /// <param name="locations">the locations to process</param>
        /// <returns>the cells at the locations</returns>
        public Cell[] GetCellAt(Location[] locations)
        {
            if (locations == null)
            {
                return new Cell[0];
            }

            Cell[] result = new Cell[locations.Length];

            for (int loopIndex = 0; loopIndex < locations.Length; loopIndex++)
            {
                result[loopIndex] = this.GetCellAt(locations[loopIndex]);
            }

            return result;
        }
Esempio n. 12
0
        public void ToStringWithPlayerX()
        {
            Cell cell = new Cell(new Location(2, 3));
            cell.IsOccupied = Occupied.PlayerX;

            string result = cell.ToString();

            Assert.AreEqual("2,3 X", result);
        }
Esempio n. 13
0
 public void ToStringTest()
 {
     Cell cell = new Cell(new Location(0, 0));
     string outValue = cell.ToString();
     Assert.IsFalse(String.IsNullOrEmpty(outValue));
 }
Esempio n. 14
0
 public void IsEmptyTest()
 {
     Cell cell = new Cell(new Location(0, 0));
     bool returnValue = cell.IsEmpty();
     Assert.IsTrue(returnValue);
 }
Esempio n. 15
0
        private static bool IsIncluded(Cell testCell, HexBoard board)
        {
            int boardSize = board.Size;
            int x = testCell.X;
            int y = testCell.Y;

            // corner cells are always in
            if (((x == 0) || (x == (boardSize - 1))) &&
                ((y == 0) || (y == boardSize - 1)))
            {
                return true;
            }

            // cells along the edges
            if ((x == 0) || (x == (boardSize - 1)))
            {
                if ((y % EdgeInterval) == 0)
                {
                    return true;
                }
            }

            if ((y == 0) || (y == (boardSize - 1)))
            {
                if ((x % EdgeInterval) == 0)
                {
                    return true;
                }
            }

            // interior cells
            if ((x != 0) && (x != (boardSize - 1)) && (y != 0) && (y != (boardSize - 1)))
            {
                if (((x - 1) % InternalInterval == 0) && ((y - 1) % InternalInterval == 0))
                {
                    return true;
                }
            }

            // are any neighbours filled?
            var neighbours = board.Neighbours(testCell);
            foreach (Cell neighbourCell in neighbours)
            {
                if (!neighbourCell.IsEmpty())
                {
                    return true;
                }
            }

            return false;
        }
Esempio n. 16
0
        public Cell[][] GetCellsAt(Location[][] locs)
        {
            Cell[][] result = new Cell[locs.GetLength(0)][];

            for (int loopIndex = 0; loopIndex < locs.GetLength(0); loopIndex++)
            {
                result[loopIndex] = this.GetCellAt(locs[loopIndex]);
            }

            return result;
        }
Esempio n. 17
0
        private static bool HasFilledNeighbour(Cell testCell, HexBoard board)
        {
            Cell[] neighbours = board.Neighbours(testCell);

            foreach (Cell neighbour in neighbours)
            {
                if (!neighbour.IsEmpty())
                {
                    return true;
                }
            }

            Cell[][] neighbours2 = board.Neighbours2(testCell);

            foreach (Cell[] triple in neighbours2)
            {
                if (!triple[0].IsEmpty())
                {
                    return true;
                }
            }

            return false;
        }
Esempio n. 18
0
        private static void DoTestNeighbour(Cell cell, Cell neibCell, HexBoard board)
        {
            Assert.IsTrue(cell.Location.ManhattanDistance(neibCell.Location) < 3, "Neigbour is too far away");

            var neighboursTest = new HexBoardNeighbours(BoardSize);

            Assert.IsTrue(neighboursTest.AreNeighbours(cell.Location, neibCell.Location));

            // reflexive. If B is a neighbour of A, then B's neighbours must include A
            var neibs = board.Neighbours(neibCell);
            int index = Array.IndexOf(neibs, cell);
            Assert.IsTrue(index >= 0, "Cell is not neighbour's neighbour");
        }