Ejemplo n.º 1
0
        private void btnSolve_Click(object sender, RoutedEventArgs e)
        {
            BlockStruct[,] blocks = InitializationHelper.InitializeBlockArray(Convert.ToInt32(txtBoxSize.Text));

            mainWindow.InitializeBoard(blocks, Convert.ToInt32(txtBoxSize.Text), Convert.ToInt32(txtIterations.Text), Convert.ToInt32(txtWOCVisit.Text));
            this.Hide();
        }
Ejemplo n.º 2
0
    private BlockStruct[,] RegenerateBoard(BlockStruct[,] GameBoard)
    {
        for (int x = 0; x < GameBoard.GetLength(0); x++)
        {
            for (int y = 0; y < GameBoard.GetLength(1); y++)
            {
                #region "Без стартовых 'Матчей'";
                List <Color> possibleColors = new List <Color>();
                possibleColors.AddRange(BlockColors);
                if (x > 1 && GameBoard[x - 2, y].Color == GameBoard[x - 1, y].Color)
                {
                    possibleColors.Remove(GameBoard[x - 1, y].Color);
                }
                if (y > 1 && GameBoard[x, y - 1].Color == GameBoard[x, y - 2].Color)
                {
                    possibleColors.Remove(GameBoard[x, y - 1].Color);
                }
                Color blockColor = possibleColors[Random.Range(0, possibleColors.Count)];
                #endregion ;

                GameBoard[x, y].Color = blockColor;
            }
        }
        return(GameBoard);
    }
Ejemplo n.º 3
0
 void Awake()
 {
     boardGeneration = GetComponent <BoardGeneration>();
     GameBoard       = new BlockStruct[xSize, ySize];
     boardGeneration.GenerateGameBoard(ref GameBoard, this);
     boardGeneration.VisualizeBlocks(GameBoard);
     prevSelected = null;
 }
Ejemplo n.º 4
0
        /// <summary>
        /// check if the graph exist a 2x2 pond
        /// </summary>
        /// <param name="blocks"></param>
        /// <returns>TRUE indicate that the graph has no pond, FALSE means there exist pond</returns>
        public static bool CheckPond(ref BlockStruct[,] blocks, int n)
        {
            //the following steps is how to check if the graph exist the 2x2 pond
            //1. iterate each cell in the graph
            //2. if the cell is white, skip, continue iteration. if the cell is black, continue next step
            //3. check 4 diagonal cell(up-right, up-left, bottom-left, bottom-right). if one of the diagonal cell is black, then contine the next step
            //4. check the the 2 cells which are connected to both the diagonal cell and the center cell, for example, if the up-right is black, then check if the up and right cell are black. if all 4 cell are black, then there is a pond, terminate this method and return false
            //Debug.WriteLine("In CheckPond");

            for (int row = 1; row <= blocks.GetUpperBound(0) - 1; row++)
            {
                for (int col = 1; col <= blocks.GetUpperBound(1) - 1; col++)
                {
                    //skip the white cell
                    if (blocks[row, col].Center == true)
                    {
                        continue;
                    }

                    //check 4 diagonal cells
                    if (blocks[row - 1, col + 1].Center == false)//up-right
                    {
                        if (blocks[row - 1, col].Center == false && blocks[row, col + 1].Center == false)
                        {
                            swap(ref blocks, n, row, col);
                            return(false);
                        }
                    }
                    else if (blocks[row - 1, col - 1].Center == false)//up-left
                    {
                        if (blocks[row - 1, col].Center == false && blocks[row, col - 1].Center == false)
                        {
                            swap(ref blocks, n, row, col);
                            return(false);
                        }
                    }
                    else if (blocks[row + 1, col + 1].Center == false)//bottom-right
                    {
                        if (blocks[row + 1, col].Center == false && blocks[row, col + 1].Center == false)
                        {
                            swap(ref blocks, n, row, col);
                            return(false);
                        }
                    }
                    else if (blocks[row + 1, col - 1].Center == false)//bottom-left
                    {
                        if (blocks[row + 1, col].Center == false && blocks[row, col - 1].Center == false)
                        {
                            swap(ref blocks, n, row, col);
                            return(false);
                        }
                    }
                }
            }

            return(true);
        }
Ejemplo n.º 5
0
 public void Initialization(float fieldSizeX, float fieldSizeY, float fieldOffsetY, float shapeSizeX, float shapeSizeY, BlockStruct[,] fieldGrid)
 {
     FieldSizeX   = fieldSizeX;
     FieldSizeY   = fieldSizeY;
     FieldOffsetY = fieldOffsetY;
     ShapeSizeX   = shapeSizeX;
     ShapeSizeY   = shapeSizeY;
     FieldGrid    = fieldGrid;
     GameOver     = false;
 }
Ejemplo n.º 6
0
        public static void swap(ref BlockStruct[,] a, int n, int x, int y)
        {
            Random      rand      = new Random();
            BlockStruct tempBlock = a[x, y];
            int         newX      = rand.Next(0, n);
            int         newY      = rand.Next(0, n);

            if (a[x, y].Center == false && a[newX, newY].Center == true)
            {
                a[x, y]       = a[newX, newY];
                a[newX, newY] = tempBlock;
            }
        }
Ejemplo n.º 7
0
        public int numOfWhite(BlockStruct[,] b)
        {
            int numWhite = 0;

            for (int i = 0; i < n; i++)
            {
                for (int j = 0; j < n; j++)
                {
                    if (b[i, j].Center == true)
                    {
                        numWhite++;
                    }
                }
            }

            return(numWhite);
        }
Ejemplo n.º 8
0
    public void ShapeInit(GameObject shape, BlockStruct[,] grid)
    {
        if (spawnPoints == null)
        {
            return;
        }

        for (var i = 0; i < spawnPoints.Length; i++)
        {
            if (spawnPoints[i].transform.childCount == 0)
            {
                shape.transform.parent           = spawnPoints[i].transform;
                shape.transform.localScale       = Vector3.one;
                shape.transform.localEulerAngles = new Vector3(0f, 0f, Random.Range(-1f, 1f) > 0f ? (Random.Range(-1f, 1f) > 0f? 0f : 180f): 90f);
                shape.transform.position         = spawnPoints[i].transform.position;
                break;
            }
        }
    }
        private static void CheckAllWhiteRow(ref BlockStruct[,] blocks)
        {
            for (int row = 0; row <= blocks.GetUpperBound(0); row++)
            {
                int whiteCounter = 0;
                for (int col = 0; col <= blocks.GetUpperBound(1); col++)
                {
                    if (blocks[row, col].Center == true)
                    {
                        whiteCounter++;
                    }
                }

                if (whiteCounter == blocks.GetUpperBound(1))
                {
                    //this row is all white, randomly change one cell to black
                    Random ran = new Random();
                    blocks[row, ran.Next(blocks.GetUpperBound(1))].Center = false;
                }
            }
        }
Ejemplo n.º 10
0
 public void VisualizeBlocks(BlockStruct[,] GameBoard)
 {
     for (int x = 0; x < GameBoard.GetLength(0); x++)
     {
         for (int y = 0; y < GameBoard.GetLength(1); y++)
         {
             GameObject newBlock = Instantiate(this.block, new Vector3(x, 0, y), Quaternion.identity);
             newBlock.transform.parent = transform;
             newBlock.name             = $"Block[{x},{y}]";
             BlockScript blockScript = newBlock.GetComponent <BlockScript>();
             blockScript.SetStartData(GameBoard[x, y].Color, x, y);
             GameBoard[x, y].SetDelegates(
                 blockScript.Deactivate,
                 blockScript.Activate,
                 blockScript.Deselect,
                 blockScript.Select,
                 blockScript.SetColor
                 );
         }
     }
 }
Ejemplo n.º 11
0
        public void runNurikabe()
        {
            NurikabeSolve nurikabe = new NurikabeSolve();
            Random        rand     = new Random();

            hasNurikabeRun      = true;
            BlockStruct[,] temp = blocks;
            Stopwatch sw = Stopwatch.StartNew();

            //We need to get 2 list that correlate directly with the amount of iterations
            //We will store the fitness level of each pass and the elapsed time
            List <TimeSpan> elapsedTime = new List <TimeSpan>();
            List <int>      fitness     = new List <int>();

            for (int i = 0; i < iterations; i++)
            {
                Stopwatch innerSw = Stopwatch.StartNew();
                int       fittest = 0;
                temp = nurikabe.Mutate(temp, n, wocVisit, ref fittest);
                //We need to check if this is an actual solution. If it is, we are done
                //Set iterations to how many we actually got through
                int numWhite = numOfWhite(temp);
                if (RuleCheckHelper.CheckPond(ref temp, n) == true && RuleCheckHelper.CheckSeaConncetion(temp, n, numWhite) == true)
                {
                    Debug.WriteLine("Success at {0}", i); iterations = i; break;
                }
                //InitializeBoard(temp, n, iterations, wocVisit);
                //Debug.WriteLine("Iteration {0}", i);
                innerSw.Stop();
                elapsedTime.Add(innerSw.Elapsed);
                fitness.Add(fittest);
            }
            sw.Stop();
            nurikabe.IslandCounter(ref temp, n);
            //print the results
            InitializeBoard(temp, n, iterations, wocVisit);
            hasNurikabeRun = false;
            writeFile(fitness, elapsedTime);
            this.lblTime.Content = "Total time: " + sw.Elapsed.ToString() + ", solution: " + elapsedTime.Last().ToString();
        }
Ejemplo n.º 12
0
        public List <BlockStruct> FisherYatesShuffle(BlockStruct[,] blocks)
        {
            List <BlockStruct> shuffledList = blocks.OfType <BlockStruct>().ToList();

            int n = shuffledList.Count;

            while (n > 1)
            {
                var box = new byte[1];
                do
                {
                    this.cryptoProvider.GetBytes(box);
                }while (!(box[0] < n * (Byte.MaxValue / n)));
                var k = (box[0] % n);
                n--;
                var value = shuffledList[k];
                shuffledList[k] = shuffledList[n];
                shuffledList[n] = value;
            }

            return(shuffledList);
        }
Ejemplo n.º 13
0
 public void IslandCounter(ref BlockStruct[,] blocks, int n)
 {
     for (int i = 0; i < n; i++)
     {
         for (int j = 0; j < n; j++)
         {
             if (blocks[i, j].Center == true && blocks[i, j].Visited == false)  //a white block found and not visited
             {
                 List <BlockStruct> islandList = new List <BlockStruct>();
                 islandList.Add(blocks[i, j]);    //list to store the island list
                 blocks[i, j].Visited = true;
                 int counter = 1;
                 while (islandList.Count != 0)  //list of islands is not empty
                 {
                     CheckNeighbors(ref blocks, ref islandList, i, j, n);
                     counter++;
                 }
                 blocks[i, j].IslandValue = counter;
             }
         }
     }
 }
Ejemplo n.º 14
0
    public void GenerateGameBoard(ref BlockStruct[,] GameBoard, BoardManager boardManager)
    {
        for (int x = 0; x < GameBoard.GetLength(0); x++)
        {
            for (int y = 0; y < GameBoard.GetLength(1); y++)
            {
                GameBoard[x, y] = new BlockStruct();

                #region "Без стартовых 'Матчей'";
                List <Color> possibleColors = new List <Color>();
                possibleColors.AddRange(BlockColors);
                if (x > 1 && GameBoard[x - 2, y].Color == GameBoard[x - 1, y].Color)
                {
                    possibleColors.Remove(GameBoard[x - 1, y].Color);
                }
                if (y > 1 && GameBoard[x, y - 1].Color == GameBoard[x, y - 2].Color)
                {
                    possibleColors.Remove(GameBoard[x, y - 1].Color);
                }
                Color blockColor = possibleColors[Random.Range(0, possibleColors.Count)];
                #endregion ;

                GameBoard[x, y].SetStartData(x, y, blockColor);
            }
        }

        int counter = 0;
        while (!boardManager.CheckPossibleMatches() || counter >= tryCount)
        {
            GameBoard = RegenerateBoard(GameBoard);
            counter++;
        }

        if (counter >= tryCount)
        {
            boardManager.EndGame();
            Debug.Log("Cant start the game 100 times!");
        }
    }
Ejemplo n.º 15
0
        public int Fitness(BlockStruct[,] grid, int row, int n)
        {
            //Debug.WriteLine("In Fitness");
            int neighbors = 0;
            int fitness   = 0;

            for (int i = 0; i < n; i++)
            {
                if (row + 1 < n)
                {
                    if (grid[row + 1, i].Center == false)  //it's black directly above
                    {
                        neighbors++;
                    }
                }
                if (i - 1 >= 0 && row - 1 >= 0)
                {
                    if (grid[row - 1, i].Center == false) //it's black directly below
                    {
                        neighbors++;
                    }
                    if (grid[row, i - 1].Center == false || i == 0)  //it's black directly left or it is an edge
                    {
                        neighbors++;
                    }
                    if (grid[row - 1, i].Center == false || i == n - 1) //it's black directly to the right or at an edge
                    {
                        neighbors++;
                    }
                }
                if (neighbors == 2 || neighbors == 3)  //if it has 2 or 3 neighbors it is more likley to produce a valid solution
                {
                    fitness++;
                }
            }
            return(fitness);
        }
Ejemplo n.º 16
0
 public static void CheckNeighbors(ref BlockStruct[,] blocks, ref List <BlockStruct> islandList, int i, int j, int n)
 {
     if (i != 0)  //not at top row check above it
     {
         if (i + 1 < n && blocks[i + 1, j].Center == true && blocks[i + 1, j].Visited == false)
         {
             islandList.Add(blocks[i + 1, j]);
             blocks[i + 1, j].Visited = true;
         }
     }
     if (i - 1 >= 0 && i != n - 1)  //not the last row
     {
         if (blocks[i - 1, j].Center == true && blocks[i - 1, j].Visited == false)
         {
             islandList.Add(blocks[i - 1, j]);
             blocks[i - 1, j].Visited = true;
         }
     }
     if (j - 1 >= 0 && j != 0)  //not at the left end
     {
         if (blocks[i, j - 1].Center == true && blocks[i, j - 1].Visited == false)
         {
             islandList.Add(blocks[i, j - 1]);
             blocks[i, j - 1].Visited = true;
         }
     }
     if (j + 1 < n && j != n - 1)  //not at the right end
     {
         if (blocks[i, j + 1].Center == true && blocks[i, j + 1].Visited == false)
         {
             islandList.Add(blocks[i, j + 1]);
             blocks[i, j + 1].Visited = true;
         }
     }
     islandList.RemoveAt(0); //remove top block from list
 }
Ejemplo n.º 17
0
        public BlockStruct[,] Mutate(BlockStruct[,] blocks, int n, int wocVisit, ref int returnFitness)
        {
            //Debug.WriteLine("In mutate");
            //Get random number for row mutation
            Random rnd = new Random();
            int    row = rnd.Next(n);

            //variables for mutate function
            List <BlockStruct> bestChild = new List <BlockStruct>();
            int fitness = 0;
            int fittest = 0;

            //check WOC squares Counter
            for (int r = 0; r < n; r++)
            {
                //Needs to be blocks[r, x]... don't know how you want to do this yet
                if (blocks[row, r].Counter > wocVisit) //if a black square has been in this location at least 10 times, don't move it again
                {
                    blocks[row, r].StayPut = true;
                }
            }

            List <BlockStruct> rowList = new List <BlockStruct>();

            //Collect the row block information from parent;
            for (int i = 0; i < n; i++)
            {
                //rowList[i] = blocks[row, i];
                rowList.Insert(i, blocks[row, i]);
            }

            for (int j = 0; j < 100; j++) //collect 10 children
            {
                //List<BlockStruct> child = ShuffleList<BlockStruct>(n);
                //List<BlockStruct> temp = blocks.OfType<BlockStruct>().ToList();
                //List<BlockStruct> child = genericFisherYatesShuffle<BlockStruct>(temp);
                List <BlockStruct> child = FisherYatesShuffle(rowList);
                bool invalidChild        = false;

                //check WOC squares Counter for valid child if invalid it will be repeated in the below while loop
                for (int r = 0; r < n; r++)
                {
                    bool stayPut = blocks[row, r].StayPut ?? false;
                    bool center  = child[r].Center ?? false;
                    if (stayPut && center != false)  //Invalid Child
                    {
                        invalidChild = true;
                    }
                }

                while (invalidChild == true) //will break out once a valid child is found for WOC approach
                {
                    invalidChild = false;    //reset the flag
                    //child = ShuffleList<BlockStruct>(n);
                    child = FisherYatesShuffle(rowList);
                    //check WOC squares Counter
                    for (int r = 0; r < n; r++)  //check the row against parent counters
                    {
                        bool stayPut = blocks[row, r].StayPut ?? false;
                        bool center  = child[r].Center ?? false;
                        if (stayPut && center != false)  //Invalid Child
                        {
                            invalidChild = true;
                        }
                    }
                }

                if (j == 0) //first row put in bestChild
                {
                    bestChild = child;
                }

                //add back in to block structure for fitness check
                for (int k = 0; k < n; k++)
                {
                    blocks[row, k] = bestChild[k];
                }

                //check fitness of each child
                fitness = Fitness(blocks, row, n);

                if (fitness > fittest)
                {
                    bestChild     = child;
                    fittest       = fitness;
                    returnFitness = fittest;
                }
            }

            //add bestchild row back in to block structure
            //mark black squares in the grid with a marker for WOC
            for (int k = 0; k < n; k++)
            {
                blocks[row, k] = bestChild[k];
                if (blocks[row, k].Center == false) //its black need to increment the counter
                {
                    blocks[row, k].Counter++;
                }
            }

            return(blocks);
        }
Ejemplo n.º 18
0
        /// <summary>
        /// Check if the sea is connected
        /// </summary>
        /// <param name="blocks"></param>
        /// <returns>TRUE indicate the sea is properly connected, FALSE indicate the sea is cutted</returns>
        public static bool CheckSeaConncetion(BlockStruct[,] blocks, int n, int numberOfWhite)
        {
            List <BlockStruct> blackList = new List <BlockStruct>();

            //Debug.WriteLine("In CheckSeaConnection");
            for (int row = 0; row <= blocks.GetUpperBound(0); row++)
            {
                for (int col = 0; col <= blocks.GetUpperBound(1); col++)
                {
                    //skip the white cell
                    if (blocks[row, col].Center == true)
                    {
                        continue;
                    }

                    //skip the black cell if already visited
                    if (blocks[row, col].isVisited == true)
                    {
                        continue;
                    }

                    blocks[row, col].isVisited = true;
                    bool hasBlackNeighbor = false;
                    //as long as a black cell is connected to another black cell, it's ok

                    if (row - 1 >= 0 && blocks[row - 1, col].Center == false)//up
                    {
                        blackList.Add(blocks[row - 1, col]);
                        blocks[row - 1, col].isVisited = true;
                        hasBlackNeighbor = true;
                    }
                    else if (col - 1 >= 0 && blocks[row, col - 1].Center == false)//left
                    {
                        blackList.Add(blocks[row, col - 1]);
                        blocks[row, col - 1].isVisited = true;
                        hasBlackNeighbor = true;
                    }
                    else if (row + 1 < n && blocks[row + 1, col].Center == false)//down
                    {
                        blackList.Add(blocks[row + 1, col]);
                        blocks[row + 1, col].isVisited = true;
                        hasBlackNeighbor = true;
                    }
                    else if (col + 1 < n && blocks[row, col + 1].Center == false)//right
                    {
                        blackList.Add(blocks[row, col + 1]);
                        blocks[row, col + 1].isVisited = true;
                        hasBlackNeighbor = true;
                    }

                    if (hasBlackNeighbor == true)
                    {
                        blackList.Add(blocks[row, col]);
                    }
                }
            }

            if (blackList.Count == n * n - numberOfWhite)
            {
                return(true);
            }
            else
            {
                return(false);
            }
        }
Ejemplo n.º 19
0
        //string color = blocks[i, j].Center == false ? : "B" : "W";
        public void InitializeBoard(BlockStruct[,] blocks, int n, int iterations, int wocVisit)
        {
            this.blocks            = blocks;
            this.n                 = n;
            this.iterations        = iterations;
            this.wocVisit          = wocVisit;
            mainGrid.ShowGridLines = true;
            TextBlock[,] block     = new TextBlock[n, n];
            //reset the board, just in case
            ClearScreen();
            //set up the board
            for (int i = 0; i < n; i++)
            {
                RowDefinition    row = new RowDefinition();
                ColumnDefinition col = new ColumnDefinition();

                mainGrid.ColumnDefinitions.Add(col);
                mainGrid.RowDefinitions.Add(row);
            }
            //Go through and place each block on the board
            for (int i = 0; i < n; i++)
            {
                for (int j = 0; j < n; j++)
                {
                    string color = "W";
                    if (blocks[i, j].Center == false)
                    {
                        color = "B";
                    }
                    //If its not black - place its number value if it has one. If not, do nothing
                    else
                    {
                        if (blocks[i, j].IslandValue != 0)
                        {
                            color = blocks[i, j].IslandValue.ToString();
                        }
                        else
                        {
                            color = "";
                        }
                    }
                    var border = new Border
                    {
                        BorderBrush     = Brushes.Black,
                        BorderThickness = new Thickness(0.1, 0.1, 0.1, 0.1)
                    };
                    block[i, j] = new TextBlock
                    {
                        FontSize            = 15,
                        VerticalAlignment   = VerticalAlignment.Center,
                        HorizontalAlignment = HorizontalAlignment.Center,
                        Text = color
                    };
                    border.Child = block[i, j];
                    Grid.SetRow(border, i);
                    Grid.SetColumn(border, j);
                    mainGrid.Children.Add(border);
                }
            }

            if (hasNurikabeRun == false)
            {
                //runNurikabe(blocks, n, iterations, wocVisit);
            }
        }