Solving Robozzle puzzle [migrated] - algorithm

This question was migrated from Stack Overflow because it can be answered on Puzzling Stack Exchange.
Migrated 17 days ago.
I have a problem with solving the "Dispersion V2" puzzle on robozzle.com:
The puzzle has a robot that can be moved and turned with a program. The goal of the puzzle is to have the robot visit all cells that have a star -- in this case just one.
The robot can perform actions unconditionally or conditional on a certain cell color.
This particular level looks like a rectangular spiral, the robot's initial position at bottom-left.
Every side of the spiral has 4 blocks of red;
Every edge(turn) is red block
Every side of the spiral is shorter than prev.side by 1 block
I need to write an algorithm using 4 functions only, named F1, F2, F3, and F4. Each function can only perform 2 commands, except F2 which can do 3 commands. Here you see the empty template for the program (the greyed numbers are just placeholders where the actual commands must be placed):
The available commands that these functions can use are:
↑: move forward
↰: turn left
↱: turn right
Fx: call function (either F1, F2, F3, F4). Recursion is fine.
These commands can be accompanied by a color or not. When they have a color, they only have effect when the current cell (having the robot) has that same color. When it is different, the command is ignored, and execution continues with the next command.
Once the puzzle is solved the program will halt (no matter how deep the call stack is) with success. If the robot is steered off the grid, the program will halt with a failure.
What I have now:
How I can solve this puzzle?

Your attempt has no main loop: it has a loop over blue cells, but then calls F3 and F2 will never be run again.
What you did in F2 is a good start: moving along as long as squares are blue. But I would do that in one of the other functions that only has 2 entries. More, I would not make the move conditional; only the repetition of it should be conditional on blue. That way you can also call it when you're on a red spot:
F4: ↑ F4(blue)
So the action F4 will bring you to a non-blue spot, but if you start on a non-blue spot, it will bring you to the next one.
We need to have this happen four times. Well, 4 is 2x2, so let's first repeat this twice in the function F3:
F3: F4 F4
... and repeat F3 twice so that we will have moved to the fourth non-blue spot. That's where we need to make a turn:
F2: F3 F3 ↰
So, F2 will bring us to the fourth red spot and turn. This is should be repeated endlessly:
F1: F2 F1
And that's it!

Related

Game Maker - Touch Event

I´m making my first game in Game Maker.
In the game i need to the user to draw a figure, for example a rectangle, and the game has to recognize the figure. How can i do this?
Thanks!
Well, that is a pretty complex task. To simplify it, you could ask him to place a succession of points, using the mouse coordinates in the click event, and automatically connect them with lines. If you store every point in the same ds_list structure, you will be able to check conditions of angle, distance, etc. This way, you can determine the shape. May I ask why you want to do this ?
The way I would solve this problem is pretty simple. I would create a few variables for each point when someone clicked on one of the points it would equal true. and wait for the player to click on the next point. If the player clicked on the next point i would call in a sprite as a line using image_angle to line both points up and wait for the player to click the next point.
Next I would have a step event waiting to see if all points were clicked and when they were then to either draw a triangle at those coordinates or place an sprite at the correct coordinates to fill in the triangle.
Another way you could do it would be to decide what those points would be and check against mouse_x, and mouse_y to see if that was a point and if it was then do as above. There are many ways to solve this problem. Just keep trying you will find one that works for your skill level and what you want to do.
You need to use draw_rectangle(x1, y1, x2, y2, outline) function. As for recognition of the figure, use point_in_rectangle(px, py, x1, y1, x2, y2).
I'm just wondering around with ideas cause i can't code right now. But listen to this, i think this could work.
We suppose that the user must keep his finger on touchscreen or an event is triggered and all data from the touch event is cleaned.
I assume that in future you could need to recognize other simple geometrical figures too.
1 : Set a fixed amount of pixels of movement defined dependent on the viewport dimension (i'll call this constant MOV from now on), for every MOV you store in a buffer (pointsBuf) the coordinates of the point where the finger is.
2 : Everytime a point is stored you calculate the average of either X and Y coordinates for every point. (Hold the previous average and a counter to reduce time complexity). Comparing them we now can know the direction and versus of the line. Store them in a 2D buffer (dirVerBuf).
3 : If a point is "drastically" different from the most plain average between the X and Y coordinates we can assume that the finger changed direction. This is where the test part of MOV comes critical, we must assure to calculate an angle now. Since only a Parkinsoned user would make really distorted lines we can assume at 95% that we're safe to take the 2nd point that didn't changed the average of the coordinate as vertex and let's say the last and the 2nd point before vertex to calculate the angle. You have now one angle. Test the best error margin of the user to find if the angle is about to be a 90, 60, 45, ecc.. degrees angle. Store in a new buffer (angBuf)
4 : Delete the values from pointsBuf and repeat step 2 and 3 until the user's finger leaves the screen.
5 : if four of the angles are of 90 degrees, the 4 versus and two of the directions are different, the last point is somewhat near (depending from MOV) the first angle stored and the two X lines and the Y lines are somewhat equal, but of different length between them, then you can connect the 4 angles using the four best values next to the 4 coordinates to make perfect rectangular shape.
It's late and i could have forgotten something, but with this method i think you could even figure out a triangle, a circle, ecc..
With just some edit and confronting.
EDIT: If you are really lazy you could instead use a much more space complexity heavy strategy. Just create a grid of rectangles or even triangles of a fixed dimension and check which one the finger has touched, connect their centers after you'have figured out the shape, obviously ignoring the "touched for mistake" ones. This would be extremely easy to draw even circles using the native functions. Gg.

Finding an algorithm that can find the shortest way to solve a one dimensional variant of the "lights out" game

I'm solving a task from an old programming competition. The task is to make a program that can find if there exists a possible solution, and what the shortest solution is, for a version of the well known game "lights out". In short, we have several lights connected. By clicking on of the lights you change the status of it, and the two adjacent lights. The goal is to activate all the lights.
In the classic version of "lights out" we are working with two dimensions, but in this version the lights are connected in a "one dimensional" string, where the "edges" are connected. Basically a circle of lights.
The number of lights can go up to 10000, so the bruteforce method I tried was obviously not good enough. It only manages to solve the versions that have a solution, and where there are under ~10 lights. Here is an example of a solveable setup. The 1's mark lights that are activated, and the 0's mark lights that are deactivated. The first line includes the number of lights in the string. If a solution doesn't excist, the program will output that it isn't possible. Remember that the edges are connected.
5
10101
Click one of the "edges" (doesn't matter which one, I clicked the left one).
01100
Click the opposite edge
11111
If a solution doesn't excist the program outputs a message. If not, it outputs the shortest solution, in this case: 2.
Could anyone help me find an algorithm?
Thanks for the help.
Suppose you knew whether in the solution (if one exists) you need to click on the first and second light.
Once we have this information, we can immediately deduce whether we need to click on the third light as this is the last choice that can affect the second light (clicking on the first light changes the last/first/second, clicking on the second light changes the first/second/third, clicking on the third light changes the second/third/fourth - but no other clicks can change the second light).
Similarly, we can then immediately deduce whether to click the fourth light, as this is the last choice that can affect the third light.
You can then work all the way round to the end to find out whether you have a consistent solution (with all lights out).
Simply try all 4 options for the first 2 switches, and pick the best scoring one.
Complexity O(4n)

object as a children of multiple objects

Can an object be a child of multiple objects?
A.add( C );
B.add( C ); is valid right?
what happens if I translate B but not A ? Will C move?
Perspective: I have 27 cubes in a 3 X 3 matrix. All the cubes are part of the main cube_group.
I now want to move just the top 9. Can I put them in a separate group and move the group? Will that work?
It's going to be a while, before I try this out, so thought I'd ask in the meantime.
Rubik's cube, hm?
You question is actually similar to this: can a ball be in two different boxes at the same time? What if I would to move one box?
It's not possible (unless it's a ball in a box in a box), neither you have real need to do something like that.
Don't group them at all, just apply centered transformation on the (logical) group of nine pieces.
So, the answer are: "no, it can't" and it's not valid".

In Processing 2, How would I make this happen? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
In Processing 2, How would I make this happen?
First left click of the mouse should display ball centered where mouse was when clicked.
Second left click of the mouse should display another ball centered where the mouse was when clicked.
Once both balls are displayed, a left click will launch 1st ball at the second ball.
When the 1st ball touches the edge of the second ball, the 1st ball should stop and the second ball should move in the same direction at the same speed and move the same amount of distance that the 1st ball moved
I won't give you code for this, as that would be too involved (and isn't the purpose of Stack Overflow anyway). However, I'll outline some of the principles you'll need, and hopefully you can go from there.
The first thing to do is keep track of the state. It sounds like your states will be:
Waiting for first click.
Waiting for second click.
Moving 1st ball.
Moving 2nd ball.
This approach is quite common, and is sometimes referred to as a Finite State Machine. Typically you'd define a constant integer for each state, and store a "current state" integer somewhere. It will be updated when you want to change/advance state.
In the main drawing loop, you'd execute different code depending on which state you're in. For example, in the third state, it will draw both balls, and keep on moving the 1st ball closer to the second 2nd, calculating the distance between them. When they touch, it moves on to the fourth state.
You'll obviously need a mouse handler to detect and handle clicks. That will store the ball positions and advance the state appropriately.
For the mathematical side of it, you'll need two things. First, you'll need to get comfortable using vector maths (specifically, normalising a vector to calculate direction, and multiplying it up to get a desired speed). Secondly, you'll need to use the Euclidean distance formula (basically just Pythagoras' theorem) to calculate the distance between the balls, determining when they're close enough to touch each other. There are loads of tutorials online for all this stuff which you may find useful.
If you get stuck on a particular issue in your coding, feel free to post a more specific question (although always bear in mind it may have been asked/answered already).

identifying custom shapes in a 2D grid

Dear stackoverflowers.
So, let's say there's a grid, whose values at certain x and y represent whether there is a tile there (1) or it's missing (0).
For example,
100110100010
100100111110
111110000000
000010000000
And there are some already known shapes A, B and C, for example,
(A) (B) (C)
1 1 1
111, 111, 11
So what I am trying to achieve is to identify which 1's on a grid belong to which shape.
All of 1's should be used up, exact number of shapes is known, but rotation (no mirroring) is allowed, so I guess it's better to add rotated versions and think, that some shapes won't be found on grid.
So, the expected result would be (it's known that it should be exactly 1xA, 2xB, 2xC):
A00CC0B000C0
A00C00BBBCC0
AABBB0000000
0000B0000000
If there are several possible matches, any would suit, as long as every tile gets allocated to it's own shape.
Moreover, finding out whether a tile is present or not ("uncovering") is an expensive operation (but results are cached, tiles don't appear out of nowhere), so I am actually seeking for a way to identify them with as minimum number of "uncoverings" as possible.
(It's okay if it's not optimum, just identifying shapes would be great).
Obviously, set of known shapes might change (but it will be known by the time of implementing and it will stay constant, so it's possible to tune up code for a particular set of tiles or develop some search strategies), but it won't be large (~5-6) and grid is quite small too (~15x15).
Thanks!
Using the ideas here and/or here (I guess using this one, the object types would be 0 and 1), one way to do it might be to try and match your own patterns against the catalog of collected objects. To take you own example,
100110100010
100100111110
111110000000
000010000000
Shapes A, B and C:
(A) (B) (C)
1 1 1 111 1 11
111 or 1 111 or 1 11 or 1
11
The first collected object might be,
1 11
1 1
11111
1
=> represented as a set of numbers: [(0,0),(0,1),(0,2),(1,2)..etc]
(the objects need not start or include (0,0) but object
bounds seem needed to calibrate the pattern matching)
Testing object A against the top left of the object would match [(0,0),(0,1),(0,2),(1,2)]. After A is matched, the program must find a way to calibrate the remaining points - the bottom right corner will effectively be measured as (2,3) rather than (4,3) - testing the bottom right of the remaining points in the object would match object B. Continue in a similar vein to match all, trying different combinations if a total match is not found.

Resources