Example #1
0
        private void ButtonTree_Click(object sender, EventArgs e)
        {
            FormTreeView TreeWIndow = new FormTreeView(ref moveList);

            TreeWIndow.ShowDialog();
            buttonPlay.Focus();
        }
Example #2
0
        private void buttonPlay_Click(object sender, EventArgs e)
        {
            if (!playing && character != null && finalXY.X > -1)
            {   //Empezar
                moveList.Clear();
                resetMapNumbers();
                buttonExamine.Enabled     = false;
                buttonInitialCord.Enabled = false;
                buttonPlay.Text           = "STOP";
                GroundButton.Enabled      = false;
                buttonCharacter.Enabled   = false;
                buttonFinalCoord.Enabled  = false;
                buttonUp.Enabled          = false;
                buttonDown.Enabled        = false;

                ButtonTree.Enabled = true;
                playing            = !playing;

                character.currentStep = 1;
                character.coordinateX = initXY.X;
                character.coordinateY = initXY.Y;
                mapa[character.coordinateY][character.coordinateX].listSteps.Add(character.currentStep);
                unveilKnown();
                panelMap.Refresh();

                if (checkBoxBacktracking.Checked == true)
                {
                    visitedBT = new List <Point>();
                    routeBT   = new List <Point>();
                    //Backtracking
                    visitedBT.Add(new Point(character.coordinateX, character.coordinateY));
                    routeBT.Add(new Point(character.coordinateX, character.coordinateY));
                    doBackTrack();
                    string result = "";
                    for (int i = 0; i < routeBT.Count; i++)
                    {
                        char c = 'A';
                        c      += (char)routeBT[i].X;
                        result += c.ToString() + "," + (routeBT[i].Y + 1);
                        if (i < routeBT.Count - 1)
                        {
                            result += " -> ";
                        }
                    }
                    labelSteps.Text = result;
                    ButtonTree.PerformClick();
                }
                else if (checkBoxAStar.Checked == true)
                {
                    if (checkBoxRepeat.Checked == true)
                    {
                        //AStar Repeat
                    }
                    else
                    {
                        //AStar no repeat
                        while (playing)
                        {
                            int    minimum = -1;
                            double minFn   = -1;
                            for (int i = 0; i < expantionOrder.Count; i++)
                            {
                                switch (expantionOrder[i])
                                {
                                //UP
                                case 0:
                                    for (int j = 0; j < character.idCostList.Count; j++)
                                    {
                                        //Si encuentra el id
                                        if ((character.coordinateY - 1) > -1 && character.idCostList[j].id == mapa[character.coordinateY - 1][character.coordinateX].TerrainId)
                                        {
                                            // y este no es N/A
                                            if (character.idCostList[j].cost > -1)
                                            {
                                                double manhattan = Math.Sqrt(Math.Pow(finalXY.X - character.coordinateX, 2) + Math.Pow(finalXY.Y - (character.coordinateY - 1), 2));
                                                int    child     = 0;
                                                child += checkChild(-1, 1);
                                                child += checkChild(-1, -1);
                                                child += checkChild(0, 0);
                                                child += checkChild(-2, 0);
                                                moveList.Add(new Move(character.coordinateX, character.coordinateY - 1, child));
                                                if (minimum == -1)
                                                {
                                                    minimum = expantionOrder[i];
                                                    minFn   = manhattan;
                                                }
                                                else if (manhattan < minFn)
                                                {
                                                    minimum = expantionOrder[i];
                                                    minFn   = manhattan;
                                                }
                                            }
                                            break;
                                        }
                                    }
                                    break;

                                //DOWN
                                case 1:
                                    for (int j = 0; j < character.idCostList.Count; j++)
                                    {
                                        //Si encuentra el id
                                        if ((character.coordinateY + 1) < mapa.Count && character.idCostList[j].id == mapa[character.coordinateY + 1][character.coordinateX].TerrainId)
                                        {
                                            // y este no es N/A
                                            if (character.idCostList[j].cost > -1)
                                            {
                                                double manhattan = Math.Sqrt(Math.Pow(finalXY.X - character.coordinateX, 2) + Math.Pow(finalXY.Y - (character.coordinateY + 1), 2));
                                                if (minimum == -1)
                                                {
                                                    minimum = expantionOrder[i];
                                                    minFn   = manhattan;
                                                }
                                                else if (manhattan < minFn)
                                                {
                                                    minimum = expantionOrder[i];
                                                    minFn   = manhattan;
                                                }
                                            }
                                            break;
                                        }
                                    }
                                    break;

                                //LEFT
                                case 2:
                                    for (int j = 0; j < character.idCostList.Count; j++)
                                    {
                                        //Si encuentra el id
                                        if ((character.coordinateX - 1) > -1 && character.idCostList[j].id == mapa[character.coordinateY][character.coordinateX - 1].TerrainId)
                                        {
                                            // y este no es N/A
                                            if (character.idCostList[j].cost > -1)
                                            {
                                                double manhattan = Math.Sqrt(Math.Pow(finalXY.X - (character.coordinateX - 1), 2) + Math.Pow(finalXY.Y - character.coordinateY, 2));
                                                if (minimum == -1)
                                                {
                                                    minimum = expantionOrder[i];
                                                    minFn   = manhattan;
                                                }
                                                else if (manhattan < minFn)
                                                {
                                                    minimum = expantionOrder[i];
                                                    minFn   = manhattan;
                                                }
                                            }
                                            break;
                                        }
                                    }
                                    break;

                                //RIGHT
                                case 3:
                                    for (int j = 0; j < character.idCostList.Count; j++)
                                    {
                                        //Si encuentra el id
                                        if ((character.coordinateX + 1) < mapa[0].Count && character.idCostList[j].id == mapa[character.coordinateY][character.coordinateX + 1].TerrainId)
                                        {
                                            // y este no es N/A
                                            if (character.idCostList[j].cost > -1)
                                            {
                                                double manhattan = Math.Sqrt(Math.Pow(finalXY.X - (character.coordinateX + 1), 2) + Math.Pow(finalXY.Y - character.coordinateY, 2));
                                                if (minimum == -1)
                                                {
                                                    minimum = expantionOrder[i];
                                                    minFn   = manhattan;
                                                }
                                                else if (manhattan < minFn)
                                                {
                                                    minimum = expantionOrder[i];
                                                    minFn   = manhattan;
                                                }
                                            }
                                            break;
                                        }
                                    }
                                    break;
                                }
                            }
                            switch (minimum)
                            {
                            case 0:
                                MoveCharacterUp();
                                break;

                            case 1:
                                MoveCharacterDown();
                                break;

                            case 2:
                                MoveCharacterLeft();
                                break;

                            case 3:
                                MoveCharacterRight();
                                break;
                            }
                            Thread.Sleep(slp);
                        }
                    }
                }
            }
            else if (playing && character != null)
            {   //Dejar de jugar
                buttonExamine.Enabled     = true;
                buttonInitialCord.Enabled = true;
                buttonPlay.Text           = "Play";
                GroundButton.Enabled      = true;
                buttonCharacter.Enabled   = true;
                buttonFinalCoord.Enabled  = true;
                buttonUp.Enabled          = true;
                buttonDown.Enabled        = true;
                playing = !playing;

                FormTreeView TreeWindow = new FormTreeView(ref moveList);
                TreeWindow.ShowDialog();
            }
            if (character == null)
            {
                MessageBox.Show("Debe haber un personaje para jugar", "Info", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else if (finalXY.X == -1)
            {
                MessageBox.Show("Debe haber un estado final", "Info", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            panelMap.Refresh();
        }