I have a task which is creating an Dungeon Escape game, based on a 4x4 array. I first have to create 3 different postion for E(Exit),K(Key) and P(Player). How can I random those 3 in 3 different position in an array ?
EDIT: I meant I need to find a way to random 3 DIFFERENT POSITION the neatest way. I already know about the rand() and srand thing.
Try to use rand function which is used to generate random number.
First of all you need to initialize random seed with srand(time(NULL)). myRandomNum = rand() % 4; generate number between 0 and 3.
So, you can generate two number which would represent row index and column index and repeat that three times for: E(exit), K(key) and P(Player).
Related
So I have this problem that I want to make a 2d array to a dice format I can compare.
So if the 2d array is for example:
00100
02340
00500
00600
(0 = blank space)
would be:
image
So if you fold it in you get a dice. My problem is I need it in a format so I can compare if it's different from another dice.
For example:
00600
00500
04320
00100
Is the same as the other example, so I need it in a format where I can compare them. The rotation of numbers doesn't matter. Any ideas?
I tried to solve this problem using Dynamic Programming but it seems I am missing some cases that I am unable to find.
Here is the equation that I used for getting values from sub-problem
dp[i][j] = dp[i][j-1] + 3*(dp[i-1][j-1] - dp[i-2][j-2]) + dp[i-3][j-2]
(i = k = no of cells to be colored and j = n = number of columns, note the row is fixed i.e 3)
The terms are as defined below:
dp[i][j-1] : case when I don't color any cell in the nth column.
dp[i-1][j-1] - dp[i-2][j-2] : case when I color one cell in the last column and then have to subtract the case where I color the adjacent cell in the n-1th column and since this can be done for each of the 3 cells in the nth column I multiplied it with 3.
dp[i-3][j-2] : case when I color two cells(top and bottom ones) in the nth column and thus have only one choice for the n-1th column, that is the middle one, hence subtracting 3 from i and since we have already considered the last two columns I reduce 2 from j.
I couldn't find any mistake in the above approach, If you see any mistake please help.
Below is the actual question where an extra condition of P consecutive column not be empty is also mentioned and should be taken care of.
My approach is to first find all the possible ways to color k cells in 3xN matrix such that they are not adjacent and then finding the number of ways where P consecutive columns exist such that there are no cells colored in them and subtracting it with the total count, but in this approach, I'm missing the correct answer by a small margin for smaller inputs and a large margin for larger inputs. I must be missing something here.
I am trying to make a 16x16 LED Snake game using Arduino (C++).
I need to assign a random grid index for the next food tile.
What I have is a list of indices that are occupied by the snake (snakeSquares).
So, my thought is that I need to generate a list of potential foodSquares. Then I can pick a random index from that list, and use the value there for my next food square.
I have some ideas for this but they seem kind of clunky, so I was looking for some feedback. I am using the Arduino LinkedList.h library for my lists in lieu of stdio.h (and random() in place of rand()):
Generate a list (foodSquares) containing the integers [0, 255] so that the indices correspond to the values in the list (I don't know of a quick way to do this, will probably need to use a for loop).
When generating list of snakeSquares, set foodSquares[i] = -1. Afterwards, loop through foodSquares and remove all elements that equal -1.
Then, generate a random number randNum from [0, foodSquares.size()-1] and make the next food square index equal to foodSquares[randNum].
So I guess my question is, will this approach work, and is there a better way to do it?
Thanks.
Potential approach that won't require more lists:
Calculate random integer representing number of steps.
Take head or tail as a starting tile.
For each step move at random free adjacent tile.
I couldn't understand it completely your question as some of those points are quite waste of processor time (i.e. point 1 and 2). But, the first point could be solved quite easily in n proportional complexity as follows:
for (uint8_t i = 0; i < 256; i++) {
// assuming there is a list of food_squares
food_squares[i] = i;
}
Then to the second point you would have to set every food_square to -1, for what? Anyway. A way you could implement this would be as VTT has said and I will describe it further:
Take a random number between [0..255].
Does it is one the snake_squares? If so, back to one, else, go to three.
This is the same as your third point, use this random number to set the position of the food in food_square (food_square[random_number] = some_value).
I'm trying to take an OpenGL mesh that I have and give it a random position within a room. I currently have this line of code:
positions[i] = glm::vec3(rand() % 10 - 1, rand() % 10 - 1, rand() % 10 - 1);
Which just generates a random position (x, y and z). However I'm not entirely sure what the '% 10 - 1' is doing since this code snippet was used online. I can alter the numbers and the mesh moves in the scene but I'm not entirely sure how the numbers are affecting it.
How can I edit the line so that the meshes are not too close together and all fit within the room (I know you don't know the room's dimensions/position, so just an explanation would be very helpful).
This part rand() % 10 - 1 : rand() is random number generator. % is modulo operation. So basically rand() % 10 generates all the numbers in the range [0,9]. Now the rand() % 10 - 1 will change the range to [-1,8].
How can I edit the line so that the meshes are not too close together and all fit within the room (I know you don't know the room's dimensions/position, so just an explanation would be very helpful).
Simply change 10 to your dimensions max value.
Can someone explain the last line of this MatLab expression? I need to convert this to C++ and I do not have any experience in matlab syntax.
LUT = zeros(fix(Max - Min),1);
Bin= 1+LUT(round(Image));
Image is an input image, Min and Max are image minimum and maximum grey levels.
Is Bin going to be an array? What shall it contain? What are the dimensions, same as LUT or Image? What is the '1' stands for (add 1 to each member of array or a shift in array positions? I cannot find any example of this.
Thanks in advance.
LUT is a column vector that has a number of entries that is equal to the difference in maximum and minimum intensities in your image. LUT(round(Image)) retrieves the entries in your vector LUT which are given by the command round(Image). The dimension of Bin will be equal to the size of your matrix Image, and the entries will be equal to the corresponding indices from the LUT vector. So, say you have a 3x3 matrix Image, whose rounded values are as follows:
1 2 3
2 2 4
1 5 1
Then LUT(round(Image)) will return:
LUT(1) LUT(2) LUT(3)
LUT(2) LUT(2) LUT(4)
LUT(1) LUT(5) LUT(1)
And 1+LUT(round(Image)) will return:
1+LUT(1) 1+LUT(2) 1+LUT(3)
1+LUT(2) 1+LUT(2) 1+LUT(4)
1+LUT(1) 1+LUT(5) 1+LUT(1)
Note that this only works if all entries in round(Image) are positive, because you can't use zero/negative indexing in the LUT vector (or any MATLAB matrix/vector, for that matter).