Exemple #1
0
        public List <IVertex> Adjacent(IVertex vertex)
        {
            List <IVertex> result = new List <IVertex>();

            uint[,] state = ((GameVertex)vertex).State;
            // Найдем пустую клеточку (ноль)
            int i, j = 0;

            for (i = 0; i < dimension; i++)
            {
                for (j = 0; j < dimension; j++)
                {
                    if (state[i, j] == 0)
                    {
                        break;
                    }
                }
                if (j != dimension && state[i, j] == 0)
                {
                    break;
                }
            }
            // Сформируем список возможных состояний, в которые можно перейти, передвинув фишку на свободное место
            GameVertex newVertex;

            uint[,] newState;
            if (i != 0)
            {
                newState           = (uint[, ])state.Clone();
                newState[i, j]     = newState[i - 1, j];
                newState[i - 1, j] = 0;
                newVertex          = new GameVertex(dimension, newState);
                result.Add(newVertex);
            }
            if (j != 0)
            {
                newState           = (uint[, ])state.Clone();
                newState[i, j]     = newState[i, j - 1];
                newState[i, j - 1] = 0;
                newVertex          = new GameVertex(dimension, newState);
                result.Add(newVertex);
            }
            if (i != dimension - 1)
            {
                newState           = (uint[, ])state.Clone();
                newState[i, j]     = newState[i + 1, j];
                newState[i + 1, j] = 0;
                newVertex          = new GameVertex(dimension, newState);
                result.Add(newVertex);
            }
            if (j != dimension - 1)
            {
                newState           = (uint[, ])state.Clone();
                newState[i, j]     = newState[i, j + 1];
                newState[i, j + 1] = 0;
                newVertex          = new GameVertex(dimension, newState);
                result.Add(newVertex);
            }
            return(result);
        }
Exemple #2
0
        private void buttonRun_Click(object sender, EventArgs e)
        {
            paths = new List <List <IVertex> >();
            times = new List <long>();
            System.Diagnostics.Stopwatch stopwatch = new System.Diagnostics.Stopwatch();
            GameGraph game = new GameGraph(Convert.ToUInt32(numericUpDownDimension.Value));

            uint[,] state = new uint[Convert.ToUInt32(numericUpDownDimension.Value), Convert.ToUInt32(numericUpDownDimension.Value)];
            for (int i = 0; i < dataGridViewField.RowCount; i++)
            {
                for (int j = 0; j < dataGridViewField.ColumnCount; j++)
                {
                    state[i, j] = Convert.ToUInt32(dataGridViewField[j, i].Value);
                }
            }
            GameVertex start = new GameVertex(Convert.ToUInt32(numericUpDownDimension.Value), state);

            uint[,] goalState = new uint[Convert.ToUInt32(numericUpDownDimension.Value), Convert.ToUInt32(numericUpDownDimension.Value)];
            uint k = 0;

            for (int i = 0; i < Convert.ToUInt32(numericUpDownDimension.Value); i++)
            {
                for (int j = 0; j < Convert.ToUInt32(numericUpDownDimension.Value); j++, k++)
                {
                    goalState[i, j] = k;
                }
            }
            GameVertex goal  = new GameVertex(Convert.ToUInt32(numericUpDownDimension.Value), goalState);
            AStar      astar = new AStar(Convert.ToDouble(textBoxOmega.Text), game, start, goal);
            bool       flag  = true;

            do
            {
                try
                {
                    stopwatch.Start();
                    paths.Add(astar.GetWay());
                    stopwatch.Stop();
                    times.Add(stopwatch.ElapsedMilliseconds);
                }
                catch (Exception)
                {
                    flag = false;
                }
            } while (flag);
            FormSolutions formSolutions = new FormSolutions();

            for (int i = 0; i < paths.Count; i++)
            {
                formSolutions.listBoxSolutions.Items.Add("Solution " + i);
                formSolutions.chartProfile.Series[0].Points.AddXY(times[i], Convert.ToDouble(paths.Last().Count) / Convert.ToDouble(paths[i].Count));
            }

            formSolutions.ShowDialog();
        }