How to show legal moves - wpf

I want to make a game where the playing pieces can move around. When I click on a playing piece it shows all available moves were I can go like this:
Red represents legal moves and dark gray represents a wall.
I have created a grid and buttons in it. When I click on a button, I subtract or add the available step count to the button index in the grid. For example if my piece can move 3 places, I subtract 3 from the index of the button and get the available position to the left of the piece. Then I do the same for all other directions. It looks something like this:
For each i as button in grid
Select case grid.indexof(i)
Case grid.getindex(currentPlayingPiece) - 3 'Left
i.background = brushes.red
Case grid.getindex(currentPlayingPiece) + 3 'Right
i.background = brushes.red
Case grid.getindex(currentPlayingPiece) - grid.columndefinitions.count 'Top
i.background = brushes.red
...
Next
Using this method it is very slow and cumbersome to get all available moves and not intuitive to get top and bottom moves. Also if there is a wall in the way and behind there's a available spot it shows that I can move to it. So my question is: is there a better method to accomplish this?

If you prefer not to use an "advanced" algorithm involving graphs or similar solutions, there are a couple of simpler things you might try.
Firstly, creating a variable instead of calling getIndex() a bunch of times might save a few cycles.
Secondly, I would try to use the absolute of each position to evaluate, and so calculate the distance without having to check once for each direction (+ or -); Maybe something like this:
dim currentButtonIndex as Integer = grid.getIndex(currentPlayingPiece)
For each i as button in grid
dim indexToEvaluate as Integer = grid.indexOf(i)
If (Math.Abs(indexToEvaluate - currentButtonIndex) = 3) Then
i.background = brushes.red
End If
Next

You can create a graph from your grid where each cell in your grid will be adjacent to its neighborhood (top-bottom-left-right) then you should specify a value for each vertex (for example 1 if occupied and 0 in not). When the user clicks a particular cell in your grid you find all the possible paths starting from there having a total weight of 0.

Related

Graphs the same size in gnuplot multiplot when each take up most of its canvas and one has labels

I've reedited this question a few times: I've made some good progress!
So, as I understand it, multiplot splits the whole canvas up into equal sized parts as needed. This is a little weird when your different plots have different dimensions, as in my case, but it works. The problem might come in when the graph are supposed to be very close together (e.g. each takes up most of its canvas), but one of them has labels. In that case, it seems the plot with labels must resize to be smaller so everything can fit. That's where I am now.
I see a few options.
make all the plots farther apart-- but I don't want to do that.
somehow make the label not part of the multiplot-- I would totally do this, but I don't know how. It's possible even just the axis tics themselves would be too big, but I can probably deal with that or compromise just that amount on the spacing.
So my question is, how can I put words in a gnuplot graph, completely separately from a plot?
(The picture is also giant, which is unfortunate, it was the only way I could make the formatting work)
Two things:
Multiplot has a convenience mode layout <rows>, <columns> that, as you say, splits the page into equal rectangles. But you do not have to use this convenience mode; you can assign each sub-plot to any arbitrary rectangle on the page, even one that overlaps or is interior to another rectangle. Here is an example from the online demo set that is close to what you show:
Demo of multiple plots with explicit alignment of borders
Placing text anywhere on the page: The set label command allows you to position the text using screen coordinates rather than plot coordinates. For example, to place a single large label centered at the top of a page that contains multiple plots:
set label 1 "This label is positioned independent of all plots"
set label 1 at screen 0.5, screen 0.95 center
set label 1 font "Times,20"

Accessing or omitting non-existing data

I'm performing some geographical computations in a grid with squares (i.e. regions). I'm using Delphi, but the logic could probably be applied to C++ too. Let me first explain what I want to do.
The following image is a portion of my grid, which is represented by a two-dimensional array Square that denotes the centre point in each square, and the "movement through the layers":
The green square has an X and Y coordinate of 2, so that is Square[2,2]. The actual coordinates are stored in Square[2,2].Latitude and Square[2,2].Longitude as wel as extra information in e.g. Square[2,2].Info that I use for computations.
Now comes the purpose: I need to do some computations on the surrounding areas. How many of the surrounding areas can be called "neighbours", depends on how many "layers" I have defined. In the image above, I used two of these "layers". That means that when starting from the green cell, I go around it once (blue arrows) and then again in the second layer (red arrows).
Now comes the problem: if I would have started in Square[1,1] (green square) instead of Square[2,2] as in the image below, the second layer (in red) would try to access data on the left side and at the bottom that does not exist (i.e. in the "-1" column and row). See the image below. This problem occurs at all borders of course.
I probably can make exceptions with IF-statements for every scenario, but I was wondering if there are common programming "tricks" that can handle such situations where you try to access data does not exist.
For example, I imagine it would be very handy if I can follow the pattern of the arrows depicted in the first image to access all the neighbouring squares every single time, even if there are non-existing squares. So, looking at the first image, after Square[3,0] you'd go to something like Square[3,-1] etc. and then eventually come back into the "feasible" zone in Square[0,3].
To visit neighborhood, you can use some kind of BFS (breadth-first search).
But for sparse structure (like the last picture shows) it is worth to use some data structure to organize cells in a good way. Perhaps kd-tree is suitable - you add all existing cells in the tree and make range search around given cell to get other cells in its vicinity.
Also look at another spatial data structures (see list at the bottom of kd-tree page).

WPF Custom Panels - Split the final element's visual between the end and start of a panel region

I am currently attempting to write a control(s) that will display a collection of elements, each of which have a start time between 0 and 1. The desired visual look we're aiming for is something akin to a simple timeline.
I've already created a FrameworkElement that renders an individual element as a line (this line represents the transition between one element and the next, e.g. y(x) = x) and my intention is to use this element as part of the DataTemplate for my custom ItemsControl. By flipping the odd elements horizontally and butting them together in a panel, it should be possible to see a continuous series of transitions between the elements. I'm having difficulty in deciding how to implement this panel, in particular with filling 'dead space' that might occur at the start of the panel.
The collection of elements that I'm trying to draw represents a looping effect, but the first element may not start at time T=0. Because the effect loops back on itself this means that the gap between T=0 and the first effect's start time is actually made up of the final part of the last element in the collection. This means I have to find a way of chopping up the last visual element in the panel so that the front part sits at the end of the panel and the back part sits at the start.
Another way to think of this is in terms of a circle/pie chart - if element A starts at 15% and element B starts at 50%, element B would occupy the regions 50-100% and 0%-15% continuously.
I'm really looking for a way to get this done in WPF visually rather than by modifying the collection of items (e.g. by adding a fake 'padding' element between 0 and the first element) as this would create complications down the line when it comes to things like modifying/selecting items, etc.
Someone has suggested drawing this as a 0-1 effect (removing any offset on the first element if there is one) then recreating the offset using a pair of cameras/viewports, which is something I'm not familiar with and seems a bit overkill. Can anyone suggest a simpler or more elegant way?
The 3D XAML question posed below contains code that achieves the kind of effect I was looking for, with some modifications. I set it up as an orthographic camera and made the position and texture co-ordinates of the MeshGeometry3D into dependency properties:
Why does TextureCoordinates work as expected for a Viewport2DVisual3D, but not for a GeometryModel3D?

Complex depth sorting issue based on MC location

I need help rescuing a project concerning some extreme depth management. We're out of ideas!
The project concerns stacking boxes of various sizes, each box is isometric - so thats where the depth comes in to play (we need to weave this box in front of that box, but on top of two boxes and below another, etc - anything goes!)
Here's the project as it stands:
http://clearlytrained.com/depth/
You can drag the boxes then let go over the grid if the grid space is green - if you were to take the two boxes that are one grid space high, place then next to eachother, then take a wider box (2x2) and place it over both (so the gap in the two lower boxes is in the middle of the box you placed on top) you'll see that the far right lower box's depth is now higher and above the box on top. I completley understand why this happens, but no matter how we sweep through the grid in order, this will happen to some extent the way it currently stands.
The way we're currently sorting depth, is sweeping through the grid array, which stores the name of every box on screen, based on the direction we sweep through the rows and columns we get an order to which we then set each box's depth. The problem is that even though a box might take up 6 grid spaces, we don't set the depth 6 times - we only set it the first time we come to its instance name in the array - so if we're sweeping through the grid bottom to top, left to right, a box that's physically under another box, yet further to the right will always have a higher depth. not good!
The only way i can see to fix this is to figure out some sort of box to box comparison of first X then Y values, setting up some sort of double for loop/if statemenet conditions, and more or less, programatically hand place each boxe's depth every time we move or place a new box. So the complexity goes from setting depth by order of box instance name, to comparing this box to every other box, then every other box to every other box, and coming up with the real order we need to stack their depth. In some cases we might go left to right, then bottom to top, then back to left to right - there's no smooth pattern.
Please play around with the link provided, let me know if you have any other questions or need more info, I'm desperate!
I would add all boxes to an array and sort that by both X and Y value (tile X and Y). It's not a large set, so you don't really have to worry about performance.
Sort by X, then if X is the same, sort by Y. That shoooould solve it, unless I missed something obvious.

Reversing an animation in Silverlight on button press or any other event

How would I reverse a double animation in Silverlight on an event? For example, lets say I have an ellipse as a path and I am moving a shape along this path in an infinite loop. If I press a button, I want to reverse the direction of the spin (clockwise <-> counterclockwise).
To be more specific, I am using the PathListBox object that is new in SL4. It has a start property from 0 to 100% (double 0 - 1) that will place the first item at the specified location along the path. By animating this property from 0 to 1 and 1 to 0, I can make an object spin clockwise and counter clockwise.
Lets say I have a button and on the button press, I want to reverse the direction. How do I go about doing it? I've tried pausing the animation and swapping the To and From values of the animation and resuming it, but this causes the animation to jump. So if its at "3 o'clock" on the ellipse, it jumps to "9 o'clock" and goes backwards from there. I tried doing something like setting the storyboard's CurrentTime property to "Duration - CurrentTime", but that property doesn't seem to be settable.
So, in conclusion, any ideas about how to reverse an animation on demand?
PS: I know there is a PathListBoxUtils that provides scroll behaviors for PathListBox, but that doesn't quite implement what I want. Or to put it another way, I am looking to modify the PathListBoxUtils to have an infinitely rotating, reversible carousel rather than scrolling one item at a time.
Ok, the way I got it working was by pausing the animation, swapping the To and From values, and using the Seek method to move the animation forward by a set amount. The amount I needed it to move forward is "Duration.TimeSpan() - GetCurrentTime()", but accessing Duration seems to throw an exception (Operation not valid for the current state of the object).
For now, I just hard coded the Duration value for a test and seems to be working fine. I guess for a real implementation the Duration can be bound to some value and the calculation can use that value too.

Resources