I wanted to write an algorithm which could generate a 'maze' like structure within a closed room. [This is not a typical maze. I just want some walls here and there within the room.]
The catch is that I don't want any 'cycles'.
eg:
I want this:-
I do not want this:- [Here the bot is stuck as it cant access the rest of the room]
I understand this as not having cycles in the wall structure. So I thought of one solution: Generate a wall segment and then after generation check for cycles (if there are cycles, regenerate), but that seemed tedious as I'd have to encode stuff in a graph, so I thought of another solution.
Generate a wall segment and then choose an empty cell and see if you can reach all other empty cells from that cells (if not, regenerate). This one seemed promising but I did not know where to start.
Moreover these solutions don't address the elephant in the room: to generate the walls correctly in the first place! Moreover, one can't truly talk about the time complexity of the former algorithms.
How should I proceed with this problem?
P.S: I am using doing this in Unity with C#.
The recursive division maze generation method does what you want. https://en.wikipedia.org/wiki/Maze_generation_algorithm#Recursive_division_method
From the pictures you posted, you want wide open 'rooms', so you will want to stop the algorithm early. Instead of "until all chambers are minimum sized" you can specify required minimum size greated than 1.
Generate a wall segment and then choose an empty cell and see if you can reach all other empty cells from that cells
If you still wanted to use that idea, then one way to do that is to use a flood-fill algorithm to count the reachable tiles from the start location and confirm that it is the same as the number of empty tiles in total.
This page is part of a larger tutorial that contains a more detailed description of this idea. See the “Banishing disconnected islands (a roguelike developer's greatest enemy)” section.
Your problem is a little different than the standard "maze generation" algorithms, because you want to allow cycles in the path, just no cycles in the walls, and I think this is an important part of the game.
So, I would solve this using a variant of Kruskal's algorithm that satisfies this requirement.
Level 1
Initialize a disjoint set data structure. Make a set for every cell. Whenever we fill a cell with wall, we will merge its set with the sets of all adjacent filled cells (including diagonal neighbors).
Fill in all the border cells and merge their sets as indicated above.
Make a list of all the unfilled cells
Repeat the following until the list of unfilled cells is empty:
Choose an unfilled cell from the list at random.
Check the sets of its 8 neighbors. If any two of them are in the same set, then filling this cell would create a wall cycle. Discard it.
Otherwise, fill the cell, merging its set with its filled neighbors, and go back to step 4.
When you're done, you will have a pretty dense maze -- it will be impossible to fill any empty cell without creating a wall cycle. If you want a bit more space, you can remember and undo the last fills, or you can just stop filling after a certain number of cells are filled.
Level 2
Instead of using just one list of unfilled cells, divide them into buckets based on the pattern of their neighbors -- eight neighbors filled or unfilled makes 256 possible neighbor patterns.
Then, instead of choosing from the whole bunch randomly, assign different weights to each pattern and assign cells in that bucket a different probability.
This gives you a lot of power to adjust the character of the mazes you create until you find one that's right for you. Maybe you want avoid filling cells adjacent to walls, because that makes your maze too blocky. Maybe you want to prefer filling cells that continue the end of an existing path. Maybe you want to avoid filling cells that make diagonal connections. You can play with the weights you want until you get mazes you like.
I've done a similar thing with more traditional mazes here. Try adjusting the weights.
Note that this algorithm is very fast, with or without level 2. There is no backtracking/retrying, and operations on the disjoint set structure are effectively constant time, which makes the whole thing pretty much O(n)
Related
I wanted to program a Crossword in C# with this conditions:
Words cannot be repeated.
Words can be placed horizontally but only run left to right.
Words can be placed vertically but only run high to low.
A horizontal word must intersect one or more vertical words.
A vertical word must intersect one or more horizontal words.
Each word must be delimited by spaces or the grid edge.
The field size isn't given. Considering the wordlist, the algorithm should find the best crossword Output and the fieldsize should be minimum. My Problem is the variable fieldsize. Have someone an idea considering how the algorithm could look like?
This is a very broad question, I think you need to spend some time thinking about the design before you start trying to code the solution.
Some possible sub problems you will need to solve:
You'll need some data store of words, will you make your own or use something from online?
You will need some way of checking the letters in these words to ensure the words have common letters you can use to cross the words over each other
You will need to measure the lengths of words and ensure the words do not run over each other (unless they are supposed to cross over!) or fit within the screen. You will need to retest these rules each time you generate a grid to ensure it is valid
You could store the grid as an n x n array, words will need to be written either in a single row or column, assuming you're not using diagonals.
To ensure your words go left to right, or top to bottom, you must ensure the starting letters index is always less then the letter at the end.
You will probably also need to store a count of words per row or column, to ensure there is an even spacing of words across the grid
I am trying to create a WPF application using C# to run on Pixelsense that is basic version of the tangram puzzle. I am able to draw my 7 shapes and translate and rotate them all around the screen.
Could anyone give me advise regarding how I should go about saving the pattern (with shapes in specific positions and orientations) so that when a user creates the pattern next time, the application can match it to the saved one and tell the user if it's correct.
It's a pattern matching and recognition problem that I am trying to solve.
I have been stuck on this for a while now :(
Define the solution as a collection of objects with shapeType, position, and orientation properties. Have the solution include one shape at position 0, 0 and an orientation of 0. Now loop over all the shapes the user has actually placed to find the ones with a shapeType that matches the shape your solution has at 0,0,0. Calculate the position and orientation of every other shape relative to where the user put this one. Compare those values to the rest of your solution. You'll need to experiment with how much tolerance to allow because this stuff is not precise - to make the game fun, err on the side of having high tolerances. If needed, you can follow this up with some performance optimizations to only re-evaluate pieces that moved.
Hopefully you are using physical shape prices with tags on them instead of this purely a virtual game. I always wanted to build this when I was on the Surface team but it never happened. One challenge you will run into is defining how the tag's position/orientation relates to the actual shape. If you'll be putting tag stickers on multiple tangram sets, you almost certainly won't get the on precisely the same each time so you may need to add a "calibration" mode to your app (have the user place each piece in a specific spot and then push a button so you can record where the tag is relative to those spots). The TagVisualizer WPF control should help a lot for building your UI - definitely look into using it (this scenario was top of mind when we designed that API). The default behavior of that control (if you tell it the ID of a tag to look for but not how to visualize it) is a "crosshair" that can help you find tune your offset values.
Good luck! If you wouldn't mind recording a YouTube video when you are done and posting a comment here linking to it, I'd really appreciate that
You can use ObservableCollection or List of a custom class. That class can consist of various values such as position, orientation etc as properties.
When a new pattern is drawn or when the pattern change its position you can update that particular object stored in the collection. As you have all the details of the pattern(positions and orientation) you can iterate the for loop and check the position of the new pattern when added.
I have multiple series which share the same x-axis but some values are repeated as they have different number of data points. Since this is the case I want to set the same number of data points for all my series.
Is setting empty data points a solution to make all series have the same number of data points or are there any other solutions? If setting empty data points is a solution, how do I use it? My series aren't fixed and vary according to user selection.
They follow:
Chart1.Series[i].XValueMember = "Receipt date";
Chart1.Series[i].YValueMembers = "AvgAgingDays";
Is setting empty data points a solution?
Well, it certainly will achieve the counts to be the same. But how it looks is a different matter.
One question here is where do you inset them (X-Value), probably where they are missing, right?
The other question is what ChartType do your series have? Here are a few typical types:
Point, Bars, Columns: That is fine, just make the Color of the 'empty' Points Transparent!
Line, Area: That is more tricky. You don't want gaps in the lines so you need to keep them visible. And you want the lines to go straight so you need to calculate the Y-Values from the neighbours. Simple for one missing Point, a little bit more work for larger gaps. Not possible for points missing at the start or end. Those should be invisible again..
Spline: Next to impossible to get really right. Either put in some more work or live with some inaccuracies!
If you have a Line chart, to fully document the situation, you may consider adding a Point Series on top with the same data, but with the missing Points invisible.
Btw: If you have correctly set the XValueType as DateTime, all this ought to be unnecessary, as then the missing dates won't matter and the DataPoints all sit at their respective dates. They only shift if you don't have a valid X-Value and/or fitting XValueType.
This is a rather comon mistake because at first it all looks fine but without setting the type it will be string and then you run into trouble when you want to act on the values or rely on their positions or even just format them..
Btw: While it is possible to AddXY the missing points afterwards, it makes things a lot easier if you can detect and add them while adding the real points..
I have a project where I need to calculate how many "widgets" i can remove from an area defined by the user.
for example, if i have a piece of paper... 13in x 23in. How many business cards I print when the business card dimensions are 5in x 3in.
I need something like this...
http://www.copel.com.py/calculadora-de-corte.html
But the best I can do is use a WrapPanel, which does a good job, but if I need to rotate an object, it leaves me with blank space. meaning that wrap panel works by rows, and each row takes the space of the largest object. leaving wastage around the smaller ones.
How do you suggest I attack this problem?
C#, WPF, XAML.!
I'm designing a Point and Figure Charting program, and my first version used the DataGridView control, which I found to be too big and bloated for my needs. All I need is a simple control that displays a square grid which will be filled with X's and O's.
The data is parsed from yahoo finance as Open, High, Low, Close data, sorted by a set of rules and converted to an Int Array, which will correspond with the index of the rows, so the simpler the control is, and the less bloat is has the more efficient it will be when chewing through large amounts of data.
I also need to be able to easily adjust the size of the squares in the grid, to zoom in and out of the data.
I am unfamiliar with creating custom controls (But willing to learn), and I'm not having a lot of luck with the search terms I'm using, so any help pointing in the right direction would be appreciated.
I've implemented a couple of custom controls like this before usually colour/graphics related stuff and they usually end up being more work than you imagine.
In the last project where I needed to do custom display stuff (a massive matrix of TCPConnection status between many different machines) I just used Xceed's gridControl and dynamically added the columns to the control. I kept an internal dictionary of the index of the column I added so that subsequent rows could benefit from direct reference to the column.
There are many different grid controls that you could probably utilise. Or if you want to get down and dirty with a Custom Control using the *Pain*t stuff you can do that too.
property for Columns, Rows .. Calculate the Space available then draw your Horiz/Verticals then draw you other values in the correct spaces, but eek prepare to invest quite a bit of time getting it "just right"