Algorithm to find word on Boggle board - vb.net

I'm building a boggle game in vb .net. Right now, my dices are as a 2d array (0,0 0,1 ) etc...
What I want it to do is, as I'm typing the word, that it highlights it on the board using the button(x,y).doclick sub which highlights it. Right now my implementation finds the first letter, then keeps trying each letter until it meets the 8 corner condition (ie it is neighbored to the last one) but this does not always work. If there are say 2 "G"'s on the board and I want the bottom one, this will not work. Can somebody give me an example of psuedocode of what needs to happen. I've been stumped for almost 6 hours trying to figure this out. Thanks

If I understand correctly, given a string you want to highlight one path through the dice that matches the string. Sometimes there are several possible choices, so adding a letter may completely change what is highlighted. It may be a good approach here to keep results from the previous substring, so we don't have to start over. Then a reasonable thing to do would be to compute all possible paths.
The answer for a given string s would be a list of paths, where a path is a list of grid coordinates. Each path is something you could reasonably highlight, so you just highlight the first one. When adding a letter to the string, you find paths you can expand and remove the ones you can't expand.
I'm afraid I don't know how to write vb code. Since you asked for pseudocode, here's some rough python-like pseudocode instead. I'm coding the boggle grid as a list of 16 items. The neighbors(x) function returns a list of the neighboring positions (except for edge cases that's going to be [x-1, x+1, x-4, x+4]).
def firstLetter(typed):
answer = []
for pos in range(16): if grid[pos]==typed: answer += [pos]
return answer
def addletter(partialanswer, typed):
answer2 = []
for partial in partialanswer:
for neighbor in neighbors(partial[-1]):
if grid[neighbor]==typed:
# partial+[neighbor] is a list. answer2 is a list of such lists.
answer2 += partial + [neighbor]
return answer2
If the player types "go", for example, then
(a) player types "g", code calls firstletter("g") and gets a list "answer" of the positions in the grid that have a "g" in them. Highlight, say, the first one.
(b) player types "o", code calls addletter(answer, "o") and gets a list of the paths in the grid that say "go". Again, highlight the first one.

Related

Referencing text from a 1D array for use in a line of code (vb)

I'm working on a small project, and one of the components utilizes an animated dice. I've got the mechanics of the dice down, however, I wish to make the dice colour change each time it rolls.
As of now, I have to manually set the colour
(e.g. pnl1.Backcolor = system.Drawing.color.Red).
I've already set up an array with the various different colours and intend to reference them randomly using the random number function, but my question is how can I refer to an item in the array in such a way that that makes the above mentioned pnl1.Backcolor match said colour?
I'm well aware I can't just use system.Drawing.color.Colours(1), so how might I go about this/what are some possible alternate options to an array?
Any and all help is massively appreciated and I do apologise if the way I've formatted this question is not in line with that which the website demands (I'm relatively new).
Thanks,
~ John
I'm not sure whether you have a different panel for each side of the die or just one with a picture change, nevertheless, below is an example of something you could do. Change as needed (I'm assuming just one panel with color change - pnl1).
Dim PanColor() As Color = {Color.White, Color.Red, Color.Green, Color.Blue, Color.Purple, Color.Yellow}
pnl1.BackColor = PanColor(put_random_number_here_0_to_5)

2-dimensional bin-packing in Excel

I’m trying to find a solution for the 2-dimensional packing problem using Excel (Formulas, Solver, VBA).
But apart from finding said solution, I would like to bring back this topic as base for discussion, because I realized during my extended web-searches that this problem (or variations of it) creates headaches for many people – novice and professional users.
The explanation for my problem:
I am trying to fit rectangular packages in rectangular containers. Usually there is one larger box and 2-5 smaller boxes to ship.
On average, there is still capacity of 30-50% left in the containers, so I want to calculate how many additional standardized boxes would fit in this free space to fill up the container.
There are no constraints, as long as the boxes fit into the container.
Height and weight are irrelevant.
The Boxes can be rotated by 90°.
One 40’ container is 1203cm long and 233cm wide.
The standardized boxes are 85cm x 70cm
The other boxes have different sizes.
I checked bin-packing algorithms but as of now I was not able to implement any solution in excel. I’d prefer a way to calculate this using Excel Solver or VBA, but my VBA-programming knowledge is limited.
The knapsack problem does not apply here in my opinion, although it is mentioned many times in this context.
In my case, I would be happy with a solution giving me something like: “You can fit at least x additional Boxes in the container”. Some inaccuracy does not matter – meaning up to 25% less boxes than possible. Too much boxes, on the other hand, are a no-go.
Now, do you guys have any idea how to get started here or even accomplish this? Maybe there is even a super-simple approximation I don’t know of?
Thanks!
UPDATE
After quite some time, I finally found some hours to get into this problem again.
I read Erwin Kalvelagen ‘s Blogposts and some papers on bin packing algorithms.
Also, the solver option is off the table.
I decided to go for a Bottom-Left-Algorithm (BLT) with some restraints (not just greedy).
Quick explanation of the BLT-Algorithm: Each box is placed in the bottom-most and left-most possible position in a given area (container). When a box is placed, it creates two new “corners” where the remaining boxes can be placed. Initially, the boxes are sorted by length (to start with the longest box) and place them in a 2-dimensional array. Then the starting point will be set in an Array (x, y coordinates) – the first coordinates are obviously 0, 0 as we start in an empty container. Then the algorithm would try to place the first box in the bottom-left corner with coordinates 0, 0 – which of course works perfectly. Then the starting cords would be replaced by the coords of one of the new corners and the coords of the other corner will be added to C. this would loop until all non-standard boxes are loaded. Then the algorithm would add standardized boxes if possible (and count them). The loop would end, if adding more boxes is not possible anymore due to constraints.
The dimension of the non-standard boxes will be entered in a worksheet - one box per row. The dimensions of the container and the standardized boxes will be written there as well.
Constraints would be, that no box can overlap another and all boxes would have to inside the container. Although rotation is practically possible, it is not necessary to implement it in the code as I am trying to orient the packages along the container.
Here is some pseudo code of the BLT-Algorithm I found:
**Procedure BLF(width, height,maxWidth)**
begin
initialize the arrays x and y
initialize the list and add the null point
for all rectangles
initialize choosePoint as impossible
while choosePoint is impossible and j < length of list
if the rectangle could be placed in a specific point
choose the point
endif
endwhile
if choosePoint is possible
update the arrays x and y
remove the point from the position choosePoint
from list
add the points (xi+width,yi),(xi,yi+height) to the points list
else
if (width > maxWidth) the problem has no solution
else xi = 0 and yi = max(heightk + yk)
where k 2 {1, . . . , i − 1}
endif
endif
endfor
solutions: the arrays x and y with (xi, yi)
the coordinates of rectangle i
end
Now, although I know a lot (like really A LOT) more about packing algorithms I am still not very experienced with VBA. Especially not with implementing algorithms.
So again I would be happy for any help you can give me to get started with the implementation.
So I started off with this (I know it’s really nothing, but I find it quite difficult):
Sub BLT1()
Dim Boxes As Variant, i As Integer, j As Integer ‘’Boxes dimensions
Dim Cntnr As Variant, a As Integer, b As Integer ‘’Container dimensions
Dim BLPoints As Variant ‘’Array with coordinates of bottom-left corners
Boxes = Range("B11:C15")
Cntnr = Range("D2:E2")
‘’Now I would like to add the first coordinates (0, 0) to the BLPoints
‘’Then I want to pick the first box and fit it in the container at the (0, 0) coordinates
‘’Then I want to update the BLPoints array with the new coordinates
…
End Sub
I’m looking forward to any constructive feedback and advice!
This is not a very easy problem. Some possible approaches are:
A MIP (Mixed Integer Programming) Model. The most complex part are the no-overlap constraint. For each box in the container we need to make sure it does not occupy space used by another box. The MIP approach has the advantage that we can find optimal solutions, or very good solutions with an indication how much we are away from a possible best solution (i.e. an indication of the quality of the solution).
A constraint programming model. Similar to the MIP model, but some constructs are easier to handle (i.e. the OR construct needed to formulate the no-overlap constraints).
A heuristic or meta-heuristic approach.
I implemented quickly a MIP model and it turns out you can get optimal or near-optimal solutions quite quickly. The solution below was found in less than a minute using a commercial MIP solver:
The yellow boxes are the required non-standard boxes and the blue ones are the optional standard boxes.
See here for more information about these no-overlap constraints. Here are the no-overlap constraints for this problem.

How to create a stars review system in Excel for a table?

I am new in VBA, and I got a task to find a way to implement the 5 stars review like in Amazon/Ebay with graphical stars itself for an excel table. Is it possible that? or even to combine with other technologies.
All I need are some hits, how to do that, and where to start.
Based on your last comment I would like to merely sketch the basic concept of doing such rating manually. Since I couldn't fit it in a comment box I'll put it here as a conceptual draft of what it might look like (this is not an attempt to be a solution):
(1) Getting a star on a sheet is as simple as that
Sheet1.Shapes.AddShape Type:=msoShape5pointStar, _
Left:=100, Top:=100, Width:=7.2, Height:=7.2
(2) Getting four more stars next to it should be simple enough by adding the width of prior star(s) and some extra space to the Left.
(3) Color your star(s) yellow or white with presets (Office 2010+)
.ShapeStyle = msoShapeStylePreset12
(4) In order to locate the stars on the sheet next to the cell containing the value which should be rated you can use the following methods assuming that the value is in cell A1:
.Left = Sheets(1).Range("A1").Left + Sheets(1).Range("A1").Width
Of course, there is a bit more to it than the above. You will have to determine the range of cells for which you want to create stars. Also, if someone adds a column or changes the width of a column then all stars might get changed and will have to be drawn again. Also, you'll have to make sure that the column B is wide enough to hold all the stars. But that's the basic concept (as I would attempt to do it).

How to select multiple lines of code and space them all?

This may be a fairly simple answer, or relatively easy; though I haven't been able to find the correct command/way to do so from anywhere.
As the title states, I'm trying to figure out the way as to space multiple lines of code.
Here's an example of what I'm looking for:
testing = "Testing"
work = "Work"
answer = "Answer"
Now I'd like to add spaces to the first two lines (selected together) at the same time, as in:
testing = "Testing"
work = "Work"
answer = "Answer"
Hold CTRL and click at the beginning of each line.
This will create two cursor points and every command will be iterated at every point.
Another tip:
You can highlight a selection of text, and CTRL + D will highlight all matches for that text and activate a cursor next to it. Very useful for mass editing in things like tables or forms.

Simultaneous paste and copy of selection text

Background
I want to be able to select some text, hit a keystroke that pastes what is on the clipboard over that selection, but at the same time copying that selection to the clipboard. I often find myself doing this operation when switching variables, etc from place to place.
Example
First sentence here, I need to switch it with the second sentence below. (ctrl-c)
...
Second sentence here, I am going to put this where the first one is.
///////
First sentence here, I need to switch it with the second sentence below.
...
First sentence here, I need to switch it with the second sentence below. (ctrl-"vc" after selecting second sentence, first sentence pasted, second sentence copied now)
///////
Second sentence here, I am going to put this where the first one is. (ctrl-v)
First sentence here, I need to switch it with the second sentence below.
My question
Does anyone know if any IDE/software supports such a paste/copy functionality? Has anyone ever run into this?
More specifically, does any one know how to set up a keyboard shortcut to do this in sublime text 2?
You can do it with a plugin. I threw this together rather quickly. I don't do anything special for multiple cursors (though it should take the content of multiple cursors as well as paste to all the proper locations).
import sublime
import sublime_plugin
class PasteAndCopyCommand(sublime_plugin.TextCommand):
def run(self, edit):
view = self.view
current_content = []
cursors = view.sel()
for cursor in cursors:
current_content.append(view.substr(cursor))
for cursor in cursors:
view.replace(edit, cursor, sublime.get_clipboard())
sublime.set_clipboard("\n".join(current_content))
After you save the plugin, use paste_and_copy as the the command for your key binding or command palette entry.