Пример #1
0
        private static void PrintSolution(PegBoard pegBoard, IEnumerable <Peg> pegBestInitialPositions)
        {
            Console.WriteLine("Process done\n\n");
            var solution = pegBestInitialPositions.First();

            Console.WriteLine($"El espacio inicial que presenta mayor probabilidad de llegar al espacio meta {pegBoard.Goal}, es el {solution.Column} con un probabilidad de {solution.Probability * 100}%");
            Console.WriteLine("\n\n");
        }
Пример #2
0
        private void NewGame(PegGameType gameType)
        {
            gameBoard = GameBoards[gameType].Clone() as PegBoard;

            controller = PegControllerFactory.CreateForGameType(gameType, gameBoard);

            controller.LoadView(MainView);
        }
        private static void SetHoleFilled(PegBoard board, int fromNumber, bool filled)
        {
            var hole = board.Holes.FirstOrDefault(x => x.Number == fromNumber);

            if (hole != null)
            {
                hole.Filled = filled;
            }
        }
        private static void MakeMove(PegBoard board, LegacyPegMove historicalMove)
        {
            var fromNumber   = Conversion.ConvertToHoleNumber(historicalMove.fromLocation);
            var toNumber     = Conversion.ConvertToHoleNumber(historicalMove.toLocation);
            var middleNumber = Conversion.ConvertToHoleNumber(historicalMove.middleLocation);

            SetHoleFilled(board, fromNumber, false);
            SetHoleFilled(board, toNumber, true);
            SetHoleFilled(board, middleNumber, false);
        }
Пример #5
0
    public Peg(PegBoard Controller, UnityEngine.Vector3 Position)
    {
        _Template           = Controller.PegTemplate;
        _instantiatedObject = PegBoard.Instantiate(_Template);
        _instantiatedObject.transform.position = Position;
        _instantiatedObject.transform.Rotate(new UnityEngine.Vector3(0, 0, -180));
        PegCollision _pegCollision = _instantiatedObject.AddComponent <PegCollision>();

        _collider = _instantiatedObject.GetComponent <MeshCollider>();
    }
Пример #6
0
        static void Main(string[] args)
        {
            if (args[0] != null)
            {
                // input of number of games to play
                maxGames = Int32.Parse(args[0]);
            }

            // Read any previous boards
            List <BoardImage> PriorBoards = GetSavedBoarImages();

            // Create list of legal moves
            List <LegalMove> LegalMoves = GetLegalMoves();

            bool done  = false;
            int  count = 0;

            while (!done)
            {
                // Create new board
                PegBoard board = new PegBoard();
                board.LegalMoves  = LegalMoves;
                board.PriorBoards = PriorBoards;

                // Play Game
                //logger.Info("**New Game**");
                PegBoard.Outcome outcome = board.PlayGame();

                // add the board to the array of images
                PriorBoards.Add(BuildBoardImage(board, PriorBoards.Count + 1, outcome));

                // if game lost or number of moves greater than ten, save board
                if (outcome == PegBoard.Outcome.Champ)
                {
                    done = true;
                }

                // display the board moves
                //logger.Info("Board moves:");
                //foreach (var move in board.Moves)
                //{
                //    logger.Info(move.ToString());
                //}
                logger.Info($"Game: {count + 1} Outcome : {outcome.ToString()} Pegs Remaining: {board.PegsRemaining()}");

                count++;
                if (count == maxGames)
                {
                    done = true;
                }
            }
            // save the collection of boards
            SaveBoardImages(PriorBoards);
        }
Пример #7
0
        public void PegBoardGetGoalPegTest()
        {
            // Arrange
            var pegBoard = PegBoard.FromSource(_mockPegBoardSource.Object);

            // Act
            var actual = pegBoard.GoalPeg;

            // Assert
            Assert.AreEqual(Peg.Create(5, 1), actual);
        }
Пример #8
0
 static public PegController CreateForGameType(PegGameType gameType, PegBoard gameBoard)
 {
     if (gameType == PegGameType.Cross)
     {
         return(new RectanglePegController(new RectanglePegView(), gameBoard));
     }
     else if (gameType == PegGameType.Triangle)
     {
         return(new TrianglePegController(new TrianglePegView(), gameBoard));
     }
     return(null);
 }
Пример #9
0
    private void OnCollisionEnter(Collision collision)
    {
        if (ControlBoard == null)
        {
            while (ControlBoard == null)
            {
                ControlBoard = GameObject.Find("PegBoard").GetComponent <PegBoard>();
            }
        }

        AddCoins(1);
    }
Пример #10
0
        public void PegBoardGetPegNeighboursFromBoardTest()
        {
            // Arrange
            var pegBoard = PegBoard.FromSource(TestBoardSource.CreateDefault());

            // Act
            var actual = pegBoard.GetPegUpNeighboursFrom(Peg.Create(4, 0));

            // Assert
            Assert.AreEqual(1, actual.Count());
            Assert.IsTrue(actual.Contains(Peg.Create(3, 1)));
            Assert.AreEqual(Peg.PegProbality, actual.ElementAt(0).Probability);
        }
Пример #11
0
        public void PegBoardGetPegNeighboursFrom33Test()
        {
            // Arrange
            var pegBoard = PegBoard.FromSource(TestBoardSource.CreateDefault());

            // Act
            var actual = pegBoard.GetPegUpNeighboursFrom(Peg.Create(3, 3));

            // Assert
            Assert.AreEqual(2, actual.Count());
            Assert.IsTrue(actual.Contains(Peg.Create(1, 3)));
            Assert.IsTrue(actual.Contains(Peg.Create(2, 4)));
        }
Пример #12
0
        public async Task ShouldIndicateSuccessfulSolve_WhenBoardIsFilled()
        {
            var pegBoard = new PegBoard();

            pegBoard.Holes[0].Filled = false;

            var result = await _appFixture.SendAsync(new SolvePegBoardQuery
            {
                PegBoard = pegBoard,
            });

            result.Should().NotBeNull();
            result.SuccessfullySolved.Should().BeTrue();
        }
Пример #13
0
    public Peg(PegBoard Controller, GameObject UnregisteredPegObject)
    {
        _Controller         = Controller;
        _instantiatedObject = UnregisteredPegObject;
        PegCollision _pegCollision = UnregisteredPegObject.GetComponent <PegCollision>();

        if (_pegCollision == null)
        {
            _pegCollision = _instantiatedObject.AddComponent <PegCollision>();
        }
        _pegCollision.TakeDamage += ProcessObjectCollision;

        _collider = _instantiatedObject.GetComponent <MeshCollider>();
    }
        public void PoorManMemoizingPegsSolutionResolveWith111x111Test()
        {
            // Arrange
            var pegBoard          = PegBoard.FromSource(new TestBoardSource(111, 111, 33, null));
            var pegsBoardIterator = PoorManMemoizingPegsSolution.Create(pegBoard);

            // Act
            pegsBoardIterator.Resolve();
            var actual = pegsBoardIterator.GetBestPositions();

            // Assert
            Assert.IsNotNull(actual);
            Assert.IsTrue(actual.ElementAt(0).Probability <= 1);
        }
Пример #15
0
 public PegRow(PegBoard Controller, Vector3 RootPosition, int PegCount, float Spacing, bool doubleSpaced)
 {
     this._RootPosition = RootPosition;
     for (int i = 0; i < PegCount; i++)
     {
         float _xOffset = Spacing * i;
         if (doubleSpaced)
         {
             _xOffset += Spacing;
         }
         Vector3 _pegPosition = new Vector3(RootPosition.x + _xOffset, RootPosition.y, RootPosition.z);
         this.Controller = Controller;
         this.Pegs[i]    = new Peg(this.Controller, _pegPosition);
     }
 }
        public void PoorManMemoizingPegsSolutionResolveTest()
        {
            // Arrange
            var pegBoard          = PegBoard.FromSource(TestBoardSource.CreateDefault());
            var pegsBoardIterator = PoorManMemoizingPegsSolution.Create(pegBoard);

            // Act
            pegsBoardIterator.Resolve();
            var actual = pegsBoardIterator.GetBestPositions();

            // Assert
            Assert.IsNotNull(actual);
            Assert.AreEqual(Peg.Create(1, 1), actual.ElementAt(0));
            Assert.AreEqual(0.625, actual.ElementAt(0).Probability);
        }
Пример #17
0
        public async Task ShouldReturnTheCorrectNumberOfMoves_WhenTheBoardIsFilled()
        {
            var pegBoard = new PegBoard();

            pegBoard.Holes[0].Filled = false;

            var result = await _appFixture.SendAsync(new SolvePegBoardQuery
            {
                PegBoard = pegBoard,
            });

            result.Should().NotBeNull();
            result.Moves.Should().NotBeNullOrEmpty();
            result.Moves.Should().HaveCount(13);
        }
Пример #18
0
        public async Task ShouldHaveBoardsOnMoves_WhenTheBoardIsFilled()
        {
            var pegBoard = new PegBoard();

            pegBoard.Holes[0].Filled = false;

            var result = await _appFixture.SendAsync(new SolvePegBoardQuery
            {
                PegBoard = pegBoard,
            });

            result.Should().NotBeNull();
            result.Moves.Should().NotBeNullOrEmpty();
            result.Moves.ForEach(x => x.Board.Should().NotBeNull());
        }
Пример #19
0
        public async Task ShouldReturnSolvedMoves_WhenBoardIsInProgress()
        {
            var pegBoard = new PegBoard();

            pegBoard.Holes[7].Filled  = false;
            pegBoard.Holes[11].Filled = false;

            var result = await _appFixture.SendAsync(new SolvePegBoardQuery
            {
                PegBoard = pegBoard,
            });

            result.Should().NotBeNull();
            result.Moves.Should().NotBeNullOrEmpty();
            result.Moves.Should().HaveCount(12);
        }
Пример #20
0
        public void PegBoardCreatePegBoardTest()
        {
            // Arrange

            // Act
            var pegBoard = PegBoard.FromSource(_mockPegBoardSource.Object);

            // Assert
            Assert.AreEqual(Columns * 2 - 1, pegBoard.Columns);
            Assert.AreEqual(Rows, pegBoard.Rows);
            Assert.AreEqual(Goal, pegBoard.Goal);
            Assert.AreEqual(MissingPegs, pegBoard.MissingPegs.Count());
            Assert.AreEqual(Peg.Create(1, 3), pegBoard.MissingPegs.ElementAt(0));
            Assert.AreEqual(Peg.Create(2, 2), pegBoard.MissingPegs.ElementAt(1));
            Assert.AreEqual(Peg.Create(3, 5), pegBoard.MissingPegs.ElementAt(2));
        }
        public async Task ShouldBeValidMove_WhenOnePegIsRemoved()
        {
            var pegBoard = new PegBoard();

            pegBoard.Holes[0].Filled = false;

            var result = await _appFixture.SendAsync(new MakeMoveCommand
            {
                PegBoard = pegBoard,
                From     = pegBoard.Holes[3],
                To       = pegBoard.Holes[0]
            });

            result.Should().NotBeNull();
            result.IsValidMove.Should().BeTrue();
        }
Пример #22
0
        private static BoardImage BuildBoardImage(PegBoard board, int arrSize, PegBoard.Outcome outcome)
        {
            BoardImage boardImage = new BoardImage();

            boardImage.BoardId  = arrSize.ToString("0000#");
            boardImage.Outcome  = outcome.ToString();
            boardImage.PegHoles = board.PegHoles;

            // list of moves
            List <string> moveList = new List <string>();

            foreach (var move in board.Moves)
            {
                moveList.Add(move.ToString());
            }
            boardImage.Moves = moveList;
            return(boardImage);
        }
        public async Task ShouldHaveNewBoardWithRemovedPeg_WhenOnePegIsRemoved()
        {
            var pegBoard = new PegBoard();

            pegBoard.Holes[0].Filled = false;

            var result = await _appFixture.SendAsync(new MakeMoveCommand
            {
                PegBoard = pegBoard,
                From     = pegBoard.Holes[3],
                To       = pegBoard.Holes[0]
            });

            result.Should().NotBeNull();
            result.NewBoard.Holes[0].Filled.Should().BeTrue();
            result.NewBoard.Holes[1].Filled.Should().BeFalse();
            result.NewBoard.Holes[3].Filled.Should().BeFalse();
        }
Пример #24
0
        public async Task ShouldReturnWithFailedToSolve_WhenTheBoardIsUnSolvable()
        {
            var pegBoard = new PegBoard();

            pegBoard.Holes.ForEach(x => x.Filled = false);
            pegBoard.Holes[2].Filled             = true;
            pegBoard.Holes[3].Filled             = true;
            pegBoard.Holes[12].Filled            = true;
            pegBoard.Holes[14].Filled            = true;

            var result = await _appFixture.SendAsync(new SolvePegBoardQuery
            {
                PegBoard = pegBoard,
            });

            result.Should().NotBeNull();
            result.Moves.Should().BeEmpty();
            result.SuccessfullySolved.Should().BeFalse();
        }
        public void PoorManMemoizingPegsSolutionResolveWith7x7Test()
        {
            // Arrange
            var pegBoard = PegBoard.FromSource(new TestBoardSource(7, 7, 1, new[]
            {
                Peg.CreateMissingPeg(1, 1),
                Peg.CreateMissingPeg(2, 1),
                Peg.CreateMissingPeg(3, 2),
            }));
            var pegsBoardIterator = PoorManMemoizingPegsSolution.Create(pegBoard);

            // Act
            pegsBoardIterator.Resolve();
            var actual = pegsBoardIterator.GetBestPositions();

            // Assert
            Assert.IsNotNull(actual);
            Assert.IsTrue(actual.ElementAt(0).Probability <= 1);
        }
        public void PoorManMemoizingPegsSolutionResolveWithFreefallSolutionTest()
        {
            // Arrange
            var pegBoard = PegBoard.FromSource(new TestBoardSource(5, 5, 1, new[]
            {
                Peg.CreateMissingPeg(1, 1),
                Peg.CreateMissingPeg(2, 1),
                Peg.CreateMissingPeg(3, 1),
            }));
            var pegsBoardIterator = PoorManMemoizingPegsSolution.Create(pegBoard);

            // Act
            pegsBoardIterator.Resolve();
            var actual = pegsBoardIterator.GetBestPositions();

            // Assert
            Assert.IsNotNull(actual);
            Assert.AreEqual(Peg.Create(1, 3), actual.ElementAt(0));
            Assert.AreEqual(1, actual.ElementAt(0).Probability);
        }
Пример #27
0
        private static void StartGame()
        {
            while (true)
            {
                var filePath = ReadFilePath();
                var pegBoard = PegBoard.FromSource(
                    PegBoardFileSource.ParseFile(filePath)
                    );

                var pegsBoardSolution = PoorManMemoizingPegsSolution.Create(pegBoard);
                pegsBoardSolution.Resolve();
                PrintSolution(pegBoard, pegsBoardSolution.GetBestPositions());

                Console.WriteLine("Would you like to play one more time? yes/no");
                var answer = Console.ReadLine();
                if (answer == "no" || answer == "n")
                {
                    break;
                }
            }
        }
Пример #28
0
 public void DestroyPeg()
 {
     PegBoard.Destroy(_instantiatedObject);
     ProcessPegUpdate();
 }
Пример #29
0
 internal _BPositions(PegBoard parent)
 {
     this.parent = parent;
 }
Пример #30
0
 public void SetModel(PegBoard pegBoard)
 {
     _pegBoard = pegBoard;
 }