How can I store a backgammon board in MongoDB? - mongodb

I am new to NoSQL databases, and even though I've looked for it a lot, I cannot find best practices for this sort of thing.
Backgammon, for those of you who don't know, is a game with a board that has twenty four pips (or slots), and a bar which can contain pieces of any color (pieces which get hit are placed on the bar). Plus, each pip (or slot) can contain one or more pieces of the same color.
The structure I'm looking for should be optimized to help me calculate two things quickly:
Is the move valid? For a move to be valid, the target slot must have less than two pieces of the opposing color. For example, if I want to move to slot 4, and I'm black, slot 4 must either have only 1 white piece (that will get hit after this move), or have no pieces, or have one or more black pieces.
What is every player's pip count? The pip count is the number of dice I must have, in total, to win the game. For this, I need to be able to quickly retrieve the location of pieces of one color all throughout the board. The calculation will be done locally, outside of the database. If you are curious about how the pip count will be calculated after I retrieve the query results, check out Beginner's Guide to Counting Pips.
The structure I thought of is something like this:
{
1: {
color: 'black',
pieces: 2
},
2: {
color: 'white',
pieces: 1
},
3: {}
// etc, until 24
24: {}
bar: {
{
color: 'white',
pieces: 1
}
}
}
The only problem with this approach, as you can no doubt see, is the bar. Since the bar can contain pieces of any color, I had to create another object inside the bar key, so that I could later on add pieces whose color is black if needed.
Is there a better structure? Also, I'd appreciate tips and tricks on how well/awful this structure will perform.

Considering the fact that you have only 1 document with a relative constant size, you can safely assume that this 1 document will be kept in memory, thus you shouldn't worry too much about performance issues more so that it's only one document - it's basically asking how good will my HashMap perform...
If it was up to me, I would split bar into 2 bars - bBar and wBar (as in real life by the way, each player has his own bar next to him to keep his own "eaten" pieces).
By the way, any calculations that involve best moves and such should happen in memory. You should not store intermediate calculation results in the DB

Related

FusionCharts/FusionTime stepline chart leaves off data points and binning aggregation distorts the data

I'm trying to reproduce a Pillow app timeline chart using FusionTime. Pillow tracks sleep quality and the different sleep stages using Apple Watch. The following is an example chart:
example Pillow chart
From the app you can export a json file with the data points. For example:
"sleepStageDataPoints": [
{
"stage": 1,
"timeStamp": 673239278.32947648
},
`Stage legend: 0=Deep, 1=Light, 2=REM, 3=Awake
I import the data into mysql, converting the Unix time in the process. I also increment the stage number so that Deep=1, etc., because the axis would be 0 in FusionTime.
So there is a start time and the level of the stage at that time, which remains the level until the next timestamp data point where the stage would change, etc.
Besides this metric, there will be other health related metrics I plan to capture that will model the data in a similar way. Only the starting time and the level at that time, which should remain the same until the next data point.
So that's the general plan. Now the problems...
I'm having trouble figuring out the best way. I think stepline would be the best chart type for this source data. I'm not expecting to be able to have the different stages have different colors. Just being able to see the height differences is sufficient.
But FusionTime doesn't show all of the data points and the ones it does show don't seem to be accurate. I'm blaming data binning.
FusionTime chart
As you can see the first point charted is on May 3rd, 12:24-12:26. But there are 5 data points from May 2nd that it just leaves off. I don't know why. And as you can see from comparing the two charts, the patterns don't match. For instance at 1am the Fusion chart says the level would be REM (3), but if you look at the Pillow chart, it would have to be either Deep (1) or Light (2). Looking at the data points in the database confirms that.
So it has to be the data binning aggregation that is changing the chart. I've tried every aggregation other than average and none of them makes a difference.
I'd like for the data to be 100% time accurate. I want to see every datapoint and not have the first ones disappear. If I mouse over 1am I should see the correct stage level for that time. As I zoom in or out, I'd like the columns to get more wide/narrow as appropriate, but the height should never be wrong. If I zoom too far out, I'd rather the data disappear than be wrong. I'd be fine to lock the zoom level if necessary, not that I know how to.
Is this possible with FusionCharts/FusionTime? Would there be a better chart type to accomplish what I'm trying to do? Is there a different charting software that would do this? I want to use FusionTime, because I want to have the multivariate charts to compare multiple metrics along the same timeline. And I do want to be able to zoom in and out because other metrics I'm tracking wouldn't be limited to this time frame. So I need to be able to zoom/scroll through the entire day.

Which color-key to use in a heat-map with a small range of values?

My data covers a small range, but I still like to make the small differences between the data points visible in a heat-map. What color-key is best to maximize color intensity (and not generating a greyish map) and how to set the range in pheatmap?
You didn't give enough information to give you an exact code example for your sample data, but something like the below is a way to get at the problem. In terms of what actual colours you want to use, I recommend you play around with it to see what looks best, I have just subbed in red and blue as a proxy.
pheatmap(yourdata, color = colorRampPalette(c("red", "blue"))(length(-12:12)),breaks=c(-12:12))
The length() is setting the range while the breaks=c(x:x) tells it where to make breaks. So let's say you wanted breaks every 0.2 from 0 to 1, you would modify it to be:
breaks=c(0,0.2,0.4,0.6,0.8,1.0)
You can play around with the break gradients to get something that works for your dataset.
This is my first attempt at answering a question on here, please let me know if something above does't work for you, or if you are confused by what I've written.

Star-Rating Feature in GWT: how to implement partially-filled stars?

How would one implement a star-rating feature like this in GWT? Does a library exist?
Main scenario: display a decimal rating as partially filled stars.
A call to server returns a [0, 5] decimal rating/grade, say 3.4.
UI display 3.4/5 filled stars, something looking like this .
By default, the best approach I can think of would be to create a number of small pre-filled images in a client bundle with a given rating resolution, say 0.5 (~ 10 images) or 0.1 (~ 50) images. And then write the mapping logic to display the appropriate star-filled image.
Is is the most efficient way of going about it?
A perhaps optimized version of this approach would be to have images of one partially filled star and then the display logic would assembled the 5 stars as function of rating -- e.g., for 3.4, 3 fully filled stars, one partially filled star to 0.4 level, one empty star.
Complement scenario: enter an integer rating {0, 1, 2, 3, 4, 5} stars.
Display five void stars.
When user mouse over a star, the star and all stars to the left fill up.
When user click, integer rating is registered.
Using a similar approach as above, two-images would suffice: one with no background, one filled.
Again, is there a widget/library out there wrapping such a common web-app feature?
It does not seem difficult to implement but no need to re-invent the wheel! A good library of such a common web-app feature could have more embedded functions like managing the rating list by rating and their corresponding display, entering review panel, etc, all performance optimized.
Create three star images: complete, empty and half-full.
Create a custom widget. Use FlowPanel as a container with star images inside.
Create method showRating() in this widget:
(a) Clear stars if you already had them.
(b) Add complete star images for each round point.
(c) Add half-empty star image for half-point, if necessary.
(d) Add empty star images if necessary to make 5 stars in total.
It's a fairly simply widget. You don't need a library for that.

Data structure for page layout like CSS floats

Suppose I have my own markup language that allows me to consume an array of TextElements and ImageElements. Each of these can have properties like "float: left/right/none" and "clear: none/left/right/both". Additionally, non-floated elements can flow around floated elements. Basically, all like CSS float layout.
For example:
Elem1 Elem2
Elem3
Elem4 Elem5 _Elem6
What's the best data structure to use to store these elements while I'm laying them out? I need something which makes it easy to answer questions like:
Is there enough space to fit Element2 left or right of Element1?
What's the page coordinates of Element1, origin and size?
I'll basically store a structure like:
{ Element, Origin(x, y), Size(w, h) }
for each already-laid-out element in this data structure.
There are things like RTrees, QuadTrees and such, but I want something simple to abstractly represent the layout of rectangles on a page for use in my layout algorithm.
NOTE: I am not doing this in HTML, it is for layout of elements in an iOS App, and I cannot use iOS 6 constraints since I need to support earlier iOS versions.
Thanks!
I believe that a binary heap could be made to work for what you want. Wikipedia explains binary heaps quite well.
The binary heap has two properties that make it nice for what you want:
It is a complete tree, which resembles structurally a laid-out page of elements.
The nodes are ordered such that each node is greater (or less, depending on which you want) than its children. In your case, "greater" means "placed further left and towards the top".
You have a list of elements that, in the absence of floats, would be laid out sequentially, wrapping lines as necessary. Thus, building a heap in the absence of floats would simply add nodes to the tree in the order they are received.
Floats complicate this, because they are placed higher than the nodes that preceded them (or lower than those that follow them in the case of right floats). Fortunately, building a heap allows for this by bubbling new nodes up to their proper place. So, as long as you can define a function that orders a floated element with respect to its peers, this can work.
It's that if that I'm not sure about, and unfortunately, I don't have enough time right now to explore the idea more completely.
Anyway, presuming you have a correct heap, repeatedly removing the topmost element should enumerate the nodes in the order they should be placed on the page.
I think CHDataStructures has a binary heap, but I can't verify that since the server where the documentation is kept seems to be offline at the time of posting.
Good luck and I hope this is helpful.

Is it possible to determine the current rendering position in Crystal Reports?

i would like to create a conditional keeptogether property depending on the space left on the current page, is it possible to get this value from within a formula field?
my goal is a visual clean report without wasting to much space, i already read about a method where you keep track of your position with a linenumber counter. unfortunately i cant use this approach because my lines vary heavy in height because of a note field which holds from 0 to 25 lines.
This is going to sound like a ridiculous and tedious suggestion, but it's the only way I know of making this work. You'll need to use that process of line counting (basically, keeping a running total of how many lines have been printed) that you heard of with one modification: calculate the number of lines Notes needs by the getting the length of that Notes string and dividing by a pre-determined count, which you'll need to do visually, of how many characters are in a line and make the result the line's line count in the overall formula. It's not going to be exactly right because there is no way to make it exactly right, but it will be close. Does that makes sense or do you need me to go into exact detail of how to do this?