Ejemplo n.º 1
0
        public bool DoneEditing(ToolboxForm toolbox)
        {
            if (!_boardControl.EditingMode)
                return false;

            var filename = _saveBoard();
            if (filename == null)
            {
                MessageBox.Show("You must save your Board before continuing!", "!", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return false;
            }

            _currentBoard = BoardIO.ReadFile(filename);
            _refreshControls();

            Menu.MenuItems["boardMenu"].Enabled = true;
            Menu.MenuItems["solveMenu"].Enabled = true;

            _boardControl.Toolbox = null;
            _boardControl.EditingMode = false;

            if (!toolbox.IsDisposed)
                toolbox.Close();

            return true;
        }
Ejemplo n.º 2
0
        public Block(Board board, Orientation orientation, int size, Point position, int type = 0)
        {
            Id = ++_lastId;
            Type = type;

            _board = board;
            Orientation = orientation;
            Size = size;
            Postition = position;
        }
Ejemplo n.º 3
0
        public BlockControl(Board board, Block block, BoardControl boardControl)
        {
            Id = block.Id;

            _board = board;
            _block = block;
            _boardControl = boardControl;
            BackColor = Color.Transparent;

            InitializeComponent();
            _blockControlInit();
        }
Ejemplo n.º 4
0
        private void CreateButton_Click(object sender, EventArgs e)
        {
            var width = (int)CBWidth.SelectedItem;
            var height = (int)CBHeight.SelectedItem;

            var redBlockPos = new Point(width - 2, (int)CBFinishRow.SelectedItem - 1);
            var blocks = new List<Block>();

            blocks.Add(new Block(null, Orientation.Horizontal, 2, redBlockPos, 1));

            Board = new Board(width, height, blocks, redBlockPos);
            Close();
        }
Ejemplo n.º 5
0
        public BoardControl(MainForm mainForm, Board board)
        {
            _mainForm = mainForm;
            _board = board;
            Width = Board.BOX_SQUARE_SIZE * _board.Width + 1;
            Height = Board.BOX_SQUARE_SIZE * _board.Height + 1;

            InitializeComponent();
            Paint += new PaintEventHandler(_paint);
            _boardControlInit();

            EditingMode = false;

            MouseClick += new MouseEventHandler(_mouseClick);
        }
Ejemplo n.º 6
0
 public static void WriteFile(Board board, string filename)
 {
     var writer = new StreamWriter(filename);
     string boardText = board.ToText();
     writer.Write(boardText);
     writer.Close();
 }
Ejemplo n.º 7
0
        public static Board TestBoard()
        {
            var width = 6;
            var height = 6;
            var endPoint = new Point(4, 2);

            var blocks = new List<Block>() {
                new Block(null, Orientation.Horizontal, 2, new Point(2, 2), 1),
                new Block(null, Orientation.Horizontal, 3, new Point(3, 0)),
                new Block(null, Orientation.Horizontal, 2, new Point(2, 1)),
                new Block(null, Orientation.Vertical, 3, new Point(0, 1)),
                new Block(null, Orientation.Vertical, 3, new Point(4, 1)),
                new Block(null, Orientation.Horizontal, 3, new Point(3, 4)),
                new Block(null, Orientation.Horizontal, 3, new Point(3, 5)),
                new Block(null, Orientation.Vertical, 2, new Point(2, 4)),
            };

            var board = new Board(width, height, blocks, endPoint);
            return board;
        }
Ejemplo n.º 8
0
        private static BoardSolution solve(Board initialBoard, Algorithm algorithm)
        {
            Board solvedBoard = null;
            IEnumerable<Board> boards = null;

            Action<IEnumerable<Board>, Board> PushF = null;
            Func<IEnumerable<Board>, Board> PopF = null;
            Func<IEnumerable<Board>, int> CountF = null;
            Action<IEnumerable<Board>> ClearF = null;

            if (algorithm == Algorithm.DFS)
            {
                boards = new Stack<Board>();
                PushF = (bs, b) => ((Stack<Board>)bs).Push(b);
                PopF = (bs) => ((Stack<Board>)bs).Pop();
                CountF = bs => ((Stack<Board>)bs).Count;
                ClearF = bs => ((Stack<Board>)bs).Clear();
            }
            else if (algorithm == Algorithm.BFS)
            {
                boards = new Queue<Board>();
                PushF = (bs, b) => ((Queue<Board>)bs).Enqueue(b);
                PopF = (bs) => ((Queue<Board>)bs).Dequeue();
                CountF = bs => ((Queue<Board>)bs).Count;
                ClearF = bs => ((Queue<Board>)bs).Clear();
            }

            string start = null;
            string finish = null;

            var visited = new Dictionary<string, Movement>();

            PushF(boards, initialBoard);

            // int i = 1;
            var goal = false;
            while (CountF(boards) != 0 && !goal)
            {
                var currentBoard = PopF(boards);

                if (start == null)
                    start = currentBoard.ToString();

                // currentBoard.PrintMatrix();
                // Console.WriteLine(i++);

                goal = currentBoard.GoalReached();
                if (goal)
                {
                    solvedBoard = currentBoard;
                    finish = solvedBoard.ToString();
                    break;
                }

                foreach (var block in currentBoard.BlockList)
                {
                    foreach (var direction in directions)
                    {
                        if (block.ValidMove(direction))
                        {
                            var board = new Board(currentBoard.Width, currentBoard.Height, currentBoard.BlockList, currentBoard.EndPoint);
                            int times = 0;
                            while (board.BlockList[block.Id - 1].ValidMove(direction))
                            {
                                board.BlockList[block.Id - 1].Move(direction);
                                times++;
                                if (!visited.ContainsKey(board.ToString()))
                                {
                                    visited[board.ToString()] = new Movement
                                    {
                                        PrevNode = currentBoard.ToString(),
                                        BlockId = block.Id,
                                        Direction = direction,
                                        Times = times,
                                    };

                                    PushF(boards, board);
                                }
                            }
                        }
                    }
                }
            }

            ClearF(boards);
            return new BoardSolution()
            {
                SolvedBoard = solvedBoard,
                Path = createPath(start, finish, visited),
            };
        }
Ejemplo n.º 9
0
 public static BoardSolution DFSSolve(Board initialBoard)
 {
     return solve(initialBoard, Algorithm.DFS);
 }
Ejemplo n.º 10
0
        private void _createBoard(object s, EventArgs e)
        {
            var createDlg = new CreateDialog();
            createDlg.ShowDialog();

            if (createDlg.Board == null)
                return;

            var toolbox = new ToolboxForm(this);
            toolbox.Location = new Point(Location.X + Width, Location.Y);
            toolbox.Show();

            _currentBoard = createDlg.Board;
            _refreshControls();
            InfoBox.Instance.StopTimer();

            Menu.MenuItems["boardMenu"].Enabled = false;
            Menu.MenuItems["solveMenu"].Enabled = false;
            _boardControl.Toolbox = toolbox;
            _boardControl.EditingMode = true;
        }
Ejemplo n.º 11
0
        private void _openBoard(object s, EventArgs e)
        {
            var dlg = new OpenFileDialog();
            dlg.Filter = "Board Text Files (.txt)|*.txt|All Files (*.*)|*.*";
            dlg.FilterIndex = 1;

            var result = dlg.ShowDialog();
            if (result == DialogResult.OK)
            {
                _currentBoard = BoardIO.ReadFile(dlg.FileName);
                _refreshControls();
            }
        }
Ejemplo n.º 12
0
        private void _mainFormInit()
        {
            _mainMenuInit();

            _mainPanel = new FlowLayoutPanel();
            _mainPanel.AutoSize = true;
            _mainPanel.Padding = new Padding(10);

            _currentBoard = BoardIO.ReadFile("StartupDefault.txt");
            _boardControl = new BoardControl(this, _currentBoard);

            _mainPanel.Controls.Add(_boardControl);
            _mainPanel.Controls.Add(InfoBox.Instance);

            Controls.Add(_mainPanel);
            AutoSize = true;
            AutoSizeMode = AutoSizeMode.GrowAndShrink;
        }