Ejemplo n.º 1
0
        // return a list of legal moves that can be made on current board
        private List <PegMove> GetAvailableMoves()
        {
            List <PegMove> availableMoves = new List <PegMove>();

            // iterate thru list of all holes
            foreach (var hole in this.PegHoles)
            {
                if (hole.Occupied)
                {
                    foreach (var legalMove in LegalMoves)
                    {
                        if (hole.Label == legalMove.FromHoleLabel)
                        {
                            // get the data for to and jump holes
                            var toHole   = this.PegHoles.Where(h => h.Label == legalMove.ToHoleLabel).FirstOrDefault();
                            var jumpHole = this.PegHoles.Where(h => h.Label == legalMove.JumpHoleLabel).FirstOrDefault();

                            // if the hole is occupied and the jump hole is occupied and the to hole is not occupied - available move
                            if (toHole.Occupied == false && jumpHole.Occupied == true)
                            {
                                var goodMove = new PegMove();
                                goodMove.FromHole = hole;
                                goodMove.ToHole   = toHole;
                                goodMove.JumpHole = jumpHole;
                                availableMoves.Add(goodMove);
                            }
                        }
                    }
                }
            }
            return(availableMoves);
        }
Ejemplo n.º 2
0
        // compare the board after the move to prior boards
        private bool IsMoveGood(PegMove move)
        {
            bool retVal = true;

            // create a copy of the existing board
            List <PegHole> currHoles = new List <PegHole>();

            foreach (var hole in this.PegHoles)
            {
                currHoles.Add(hole);
            }

            // make the move on the current board
            MovePeg(currHoles, move);

            foreach (var board in PriorBoards)
            {
                // compare the list of holes to prior boards
                if (IsBoardMatch(board.PegHoles, currHoles))
                {
                    // board matches prior board, return not good move (false)
                    retVal = false;
                    break;
                }
            }
            return(retVal);
        }
Ejemplo n.º 3
0
        private void MovePeg(List <PegHole> holes, PegMove move)
        {
            // update the list of holes according to the move
            var fromHole = holes.Where(h => h.Label == move.FromHole.Label).First();

            fromHole.Occupied = false;

            var toHole = holes.Where(h => h.Label == move.ToHole.Label).First();

            toHole.Occupied = true;

            var jumpHole = holes.Where(h => h.Label == move.JumpHole.Label).First();

            jumpHole.Occupied = false;
        }
Ejemplo n.º 4
0
        public Outcome PlayGame()
        {
            bool    done   = false;
            Outcome retVal = Outcome.Lost;

            this.Moves = new List <PegMove>();

            while (!done)
            {
                // get list of legal moves
                List <PegMove> availableMoves = GetAvailableMoves();

                // if no move available, game over
                if (availableMoves.Count < 1)
                {
                    done = true;
                    break;
                }

                // TODO: pick next move at random
                PegMove nextMove = GetNextMove(availableMoves);
                if (nextMove != null)
                {
                    // make move
                    MovePeg(this.PegHoles, nextMove);

                    // save the move
                    this.Moves.Add(nextMove);
                }
                // if nextMove is null

                // if there is a single peg remaining, game won
                if (this.PegHoles.Where(h => h.Occupied).Count() == 1)
                {
                    retVal = Outcome.Win;

                    // if game won and number of moves less than ten, game champ
                    if (this.Moves.Count < 10)
                    {
                        retVal = Outcome.Champ;
                    }

                    done = true;
                }
            }
            return(retVal);
        }
Ejemplo n.º 5
0
        // choose the next move randomly from list
        private PegMove GetNextMove(List <PegMove> availableMoves)
        {
            PegMove retVal = null;

            List <int> alreadyChecked = new List <int>();
            var        random         = new Random();

            while (alreadyChecked.Count < availableMoves.Count)
            {
                int idx = random.Next(availableMoves.Count);
                if (!alreadyChecked.Contains(idx))
                {
                    retVal = availableMoves[idx];
                    break;
                }
            }
            return(retVal);
        }