Exemple #1
0
        private Board(Board source)
        {
            this.size = source.size;
            this.pegs = new Dictionary<Point, bool>();
            foreach (KeyValuePair<Point, bool> entry in source.pegs)
            {
                this.pegs.Add(entry.Key, entry.Value);
            }

            this.highlighted_pegs = new HashSet<Point>();
        }
        public void Setup()
        {
            System.Console.WriteLine("Please input board size");
            while(!int.TryParse(System.Console.ReadLine(), out size))
            {
                System.Console.WriteLine("That was invalid, please try again.");
            }
            System.Console.WriteLine("Please set up starting board:");
            start = CreateBoard(true);
            System.Console.WriteLine("Please set up finishing board:");
            finish = CreateBoard(false);

            setup_complete = true;
        }
 private Board CreateBoard(bool initial_value)
 {
     Board board = new Board(size, initial_value);
     while (true)
     {
         System.Console.WriteLine("Current State of Board:");
         System.Console.WriteLine(board);
         System.Console.WriteLine("Enter a pair to flip, X,Y: where 0,0 is apex,\n and 1,0 is second row first peg,\n or enter to accept");
         string input = System.Console.ReadLine();
         if (input.Length == 0)
         {
             if (board.PegCount(!initial_value) > 0)
             {
                 break;
             }
             System.Console.WriteLine("You need at least one toggled peg.");
             continue;
         }
         string[] tokens = input.Split(", ".ToCharArray()).Select(s => s.Trim()).ToArray<string>();
         if (tokens.Length != 2)
         {
             System.Console.WriteLine("Input is not in format X,Y");
             continue;
         }
         try
         {
             board.Toggle(new Point(int.Parse(tokens[0]), int.Parse(tokens[1])));
         }
         catch (InvalidPositionException e)
         {
             System.Console.WriteLine("Position not on board");
         }
         catch (FormatException e)
         {
             System.Console.WriteLine("Point not an integer");
         }
         catch (OverflowException e)
         {
             System.Console.WriteLine("Input out of range");
         }
     }
     return board;
 }
        private bool Search(Board board, Stack<Board> solution)
        {
            //System.Console.WriteLine(board);
            //System.Threading.Thread.Sleep(50);
            if (board.MatchState(finish))
            {
                return true;
            }
            foreach (Board move in board.NextStates())
            {
                if (Search(move, solution))
                {
                    solution.Push(move);
                    return true;
                }
            }

            return false;
        }
Exemple #5
0
        public bool MatchState(Board board)
        {
            if (pegs.Count != board.pegs.Count)
            {
                return false;
            }

            foreach (KeyValuePair<Point, bool> entry in pegs)
            {
                if (!board.pegs.ContainsKey(entry.Key))
                {
                    return false;
                }
                if (board.pegs[entry.Key] != entry.Value)
                {
                    return false;
                }
            }

            return true;
        }
Exemple #6
0
        private List<Board> TryStates(Point start)
        {
            List<Board> moves = new List<Board>();
            foreach (Point move in move_options)
            {
                Point move_a = new Point(start.X + move.X, start.Y + move.Y);
                Point move_b = new Point(start.X + (move.X * 2), start.Y + (move.Y * 2));

                if (pegs.ContainsKey(move_b) && pegs[move_a] && pegs[move_b])
                {
                    Board state = new Board(this);
                    state.pegs[start] = true;
                    state.highlighted_pegs.Add(start);
                    state.pegs[move_a] = false;
                    state.highlighted_pegs.Add(move_a);
                    state.pegs[move_b] = false;
                    state.highlighted_pegs.Add(move_b);
                    state.last_move = move_b;
                    state.move_options.Remove(move);
                    state.move_options.Insert(0, move);
                    moves.Add(state);
                }
            }

            return moves;
        }
 public GameSolver(Board start, Board finish)
 {
     this.start = start;
     this.finish = finish;
 }