⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 bookbuild.java

📁 简单的迷宫生成算法、复杂的迷宫生成算法、简单的迷宫搜索算法、复杂的迷宫搜索算法
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
                                        break;
                                //2= south
                                case 2: 
                                        doneSouth = true;
                                        if(firstPair.y==mazeGrid.getSizeY()-1)
                                        {
                                                partner = selectPartner(doneNorth, doneEast, doneSouth, doneWest);
                                                numpartners++;
                                                break;
                                        }
                                        if (ourLocation != mazeGrid.getElement(firstPair.x, firstPair.y+1).getBuildLocation())
                                        {
                                                mazeGrid.getElement(firstPair.x, firstPair.y+1).setNorth(false);
                                                mazeGrid.getElement(firstPair.x, firstPair.y).setSouth(false);
                                                newLocation = mazeGrid.getElement(firstPair.x, firstPair.y+1).getBuildLocation();
                                                foundPartner = true;
                                        } else {
                                                partner = selectPartner(doneNorth, doneEast, doneSouth, doneWest);
                                                numpartners++;
                                        }
                                        break;
                                //3= west
                                case 3: 
                                        doneWest = true;
                                        if(firstPair.x==0)
                                        {
                                                partner = selectPartner(doneNorth, doneEast, doneSouth, doneWest);
                                                numpartners++;
                                                break;
                                        }
                                        if(ourLocation  != mazeGrid.getElement(firstPair.x-1, firstPair.y).getBuildLocation())
                                        {
                                                foundPartner = true;
                                                mazeGrid.getElement(firstPair.x-1, firstPair.y).setEast(false);
                                                mazeGrid.getElement(firstPair.x, firstPair.y).setWest(false);
                                                newLocation = mazeGrid.getElement(firstPair.x-1, firstPair.y).getBuildLocation();
                                        } else {
                                                partner = selectPartner(doneNorth, doneEast, doneSouth, doneWest);
                                                numpartners++;
                                        }
                                        break;
                                case -1:
                                        numpartners = 4;
                                        break;
                                }
                                
                        }
                        //if all walls have been searched, then it is time for a new element to checkup!
                        if(numpartners > 3)
                        {
                                continue;
                        }
                        
                        //there is a location (ourlocation) and another location that has to be added.
                        //by adding the location, we should add the complete list to it!
                        
                        //search for the list that has the newlocation in it;
                        for (int k=0; k<gridLocator.size(); k++)
                        {
                                CoordinatePair searchPair= (CoordinatePair)gridLocator.get(k).getFirst();
                                if (mazeGrid.getElement(searchPair.x, searchPair.y).getBuildLocation()== newLocation)
                                {
                                        //get an iterator to traverse through the linked list which contains the newlocation;
                                         ListIterator<CoordinatePair> itr= gridLocator.get(k).listIterator(0);
                                         //iterate through the linkedlist and modify all elements on the mazegrid to have the
                                         //same buildlocation as the "ourlocation" has.
                                         while (itr.hasNext())
                                         {
                                                 CoordinatePair modifyPair = (CoordinatePair) itr.next();
                                                 mazeGrid.getElement(modifyPair.x, modifyPair.y).setBuildLocation(ourLocation);                                                                                          
                                         }
                                        
                                        //add the list from the newlocation to the list from the ourlocation
                                         gridLocator.get(i).addAll(gridLocator.get(k));
                                        //gridLocator.get(i).add(gridLocator.get(k));
                                        //remove the newlocationlist from the arraylist.
                                        gridLocator.remove(k);
                                }
                        }
                        //tick has been succesful, no more trial here.
                        tickIsDone=true;
                        
                }
                //report tick succesful
                return true;
        }
        
        // Selects a random partner, based on the available choices.
        private int selectPartner(boolean doneNorth, boolean doneEast, boolean doneSouth, boolean doneWest)
        {
                int numchoices = 0;
                
                // No partners left to do?
                if(doneNorth && doneEast && doneSouth && doneWest)
                        return -1;
                
                // Add a choice for each still unchecked partner.
                if(!doneNorth)
                        numchoices++;
                if(!doneEast)
                        numchoices++;
                if(!doneSouth)
                        numchoices++;
                if(!doneWest)
                        numchoices++;
                
                // Choose
                int choice = (int) (Math.random() * numchoices);
                
                // If a partner is available, it was added to the choices
                if(!doneNorth)
                {
                        // This is our choice
                        if(choice == 0)
                                return 0;
                        // Prepare for the next
                        else
                                choice--;
                }
                if(!doneEast)
                {
                        if(choice == 0)
                                return 1;
                        else
                                choice--;
                }
                if(!doneSouth)
                {
                        if(choice == 0)
                                return 2;
                        else
                                choice--;
                }
                if(!doneWest)
                {
                        if(choice == 0)
                                return 3;
                        else
                                choice--;
                }
                /*!NOTREACHED*/
                return -1;
        }
        
        // this is an encapsulated class which contains the x and y positions
        
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -