예제 #1
0
파일: Form1.cs 프로젝트: giladsch/Maze
        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Escape)
            {
                Application.Exit();
            }

            else if (currPos != null)
            {
                int x = currPos.x;
                int y = currPos.y;

                if (e.KeyCode == Keys.R)
                {
                    //Create a previously searched array
                    bool[,] alreadySearched = new bool[numOfBlocks, numOfBlocks];
                    //Starts the recursive solver at tile (0,0). If false maze can not be solved.
                    if (!solveMazeRecursion(0, 0, alreadySearched))
                    {
                        MessageBox.Show("Maze can not be solved.");
                    }
                    clearRec = true;
                }
                else if (e.KeyCode == Keys.A)
                {
                    aStar();
                    clearRec = true;
                }
                else if (e.KeyCode == Keys.C)
                {
                    clearGrid();
                    ShowGrid();
                }
                else if (e.KeyCode == Keys.Up)
                {
                    if (!currPos.walls[0])
                    {
                        currPos.resetBackColor();
                        currPos.show();
                        currPos.LineVisited();
                        currPos.BackColor(x, --y);
                        currPos = grid[x, y];
                    }
                }
                else if (e.KeyCode == Keys.Left)
                {
                    if (!currPos.walls[1])
                    {
                        currPos.resetBackColor();
                        currPos.show();
                        currPos.LineVisited();
                        currPos.BackColor(--x, y);
                        currPos = grid[x, y];
                    }
                }
                else if (e.KeyCode == Keys.Down)
                {
                    if (!currPos.walls[2])
                    {
                        currPos.resetBackColor();
                        currPos.show();
                        currPos.LineVisited();
                        currPos.BackColor(x, ++y);
                        currPos = grid[x, y];
                    }
                }
                else if (e.KeyCode == Keys.Right)
                {
                    if (!currPos.walls[3])
                    {
                        currPos.resetBackColor();
                        currPos.show();
                        currPos.LineVisited();
                        currPos.BackColor(++x, y);
                        currPos = grid[x, y];
                    }
                }
            }
        }