I'm making a project for a course at university. It's some kind of rpg which consists out of 3 dimensional dungeons. every dungeon is a cube holding squares. Now what I need to do is "collect" all squares from the sides of the cube.
I could easily do this by making 6 for loops but I don't think thats a very elegant way of doing this.
Does anyone know a better way of solving this problem then by using a ton of for loops?
If you have data in 3 dimensions, the most obvious way to store that information is in a 3 dimensional structure like a 3-dimensional array. That would allow you easily select all items at the 6 sides of the cube by getting all blocks where either a dimension is 0 or maximum. Where maximum is the size of the array.
However, this is all highly speculative without a bit of code. Maybe if you post what you have now it will help get you some more concrete answers.
You can iterate over two dimensions and do all 6 faces at once.
for (u...) {
for (v...) {
add(cube[0][u][v];
add(cube[M][u][v];
add(cube[u][0][v];
add(cube[u][M][v];
add(cube[u][v][0];
add(cube[u][v][M];
}
}
Related
I want to make a program that will randomly generate a sudoku board to play. For those not familiar with the game, you are given a 9x9 gameboard with mostly empty spaces and some numbers pre-filled in. To complete it you must fill in the empty squares so there is 1-9 in every row, column, and 3x3 square, but only once.
The way I am currently imagining it is to use a 2d array. I use a for loop within a for loop to populate it, making sure that the number going into the square is never one that has already been used in the same row or column.
The problem I am having is how to check if the number is already used in the 3x3 part of the grid. I was thinking about using a 3d array and the third dimension is where the 3x3 data is stored, but I don't know a simple way to decide which array to check in for the current square.
I also don't know for sure if randomly generating the tiles the way I am will always produce a complete board. I am worried it might get to one of the last few tiles and find that no number between 1 and 9 will fit in the square because it is already used. I don't know how to check if this is a possibility.
I did do a minimal amount of looking at other questions on the topic but I don't want to accidentally come across the answer, I just need a pointer in the right direction. Also none of them seemed to be directly related to what I am asking.
Hopefully what I am asking makes sense, it is a little difficult to describe in text. I would appreciate it if you could give me a pointer in the right direction without giving me the answer. Also if you don't know much about sudoku (why did you click on this question) you can play it here:
http://www.websudoku.com/
If you need clarification I will respond to comments as quickly as I can.
I'll try to just give you some hints rather than giving you the answers outright.
First, its a great idea to use a 2d array -- that is exactly what a sudoku board is. As for your 3d array idea, it is a bit overcomplicated. Think about using mathematical functions to find the top corner of each 3x3 box (i.e. [0, 0], [0, 2], [2, 0], etc.) and use a for loop to traverse that 3x3 box (still in the 2d array). As for generating the board by putting numbers randomly in, it might not work, and board generation is maybe not as trivial as you might think provided you want each board to have only one correct solution. Make sure you can check board validity first, then take a look at the link posted by kenshinji.
I want to know what I should use for 2-dimensional positions for a Sprite.
I see Dimension objects referring to size, mainly around GUI components.
I was thinking about doing int[2], but it could easily be sabotaged by inserting in an empty array or one with the incorrect number of elements.
I only see Point objects around Mouse positions, so I don't know if it has the same purpose as what I'm looking for.
I have no idea what to choose for my purpose.
I have a 2D array for a game of a 9x9 board for Gomoku. I want to scan the board as quickly and efficiently as possible to find pieces. I need to check if their is a row/col/diagonal of X amount of whites/blacks.
Is the best way just to use a for-loop or is there a better way?
Thanks
This usually depends on the game, how many pieces there are vs cells on the board. If your game had a 1000x1000 board and only 10 pieces it would be terrible to use a nested loop to find them (sparse array). It would be better to track the board pieces using a dedicated structure like a list or hashmap.
But in your case a nested loop + 2d array is a good solution. The 2d array also has the benefit of quick random access.
So in this game I'm programming the "board" is an Array of tiles. I have to get which type of tile is at certain positions and move them around. Not every position that could have a tile has a tile.
I see two ways of doing this.
1) Have a 2D Array of tiles. The dimensions of the board in tiles would be equal to the dimensions of the Array. Places where there is no tile would be represented by null. These would be organized so that myArray[1][1] would refer to the tile at (1, 1).
Pros: Easy to find a tile at a specific coordinate
Cons: Seems bulky, many of the Tiles in the array will be null.
2) Have a regular unordered ArrayList of tiles. Since tiles have an x and a y component, I do not need to sort this.
Pros: Lightweight, takes less memory
Cons: When I need to find a tile at a specific coordinate I will need to use a for loop and search through every tile
Thanks :)
The question in my mind is: is it better to use a concrete array or a sparse array, and the solution (again in my mind) is "it depends". If your universe is potentially very large, then definitely use the sparse array, else you'll be wasting resources on cells that have no current relevance. If on the other hand the universe will be well limited in size, then the convenience of a concrete array (or some type of collection) will come to the fore.
You can try hashmap. Where keep the coordinate as key. And tile value at value position.
Ex pos(2,3)->2.3 so on ..
This way search and memory both can be optimized.
This question already has answers here:
Java Program using 4D array [closed]
(8 answers)
Closed 9 years ago.
I have been studying Java and C++ this year, and I am wondering; what practical uses are there for a 4 or even 5 dimensional array? My teacher basically says that you are using the wrong datatype if you want to use a 4 dimensional array.
Have you ever used a 4 of 5 (or more) dimensional array? What did you use it for? What practical uses can you think of for them?
Thanks!
A practical use of a 4D array is to keep track of a 3d object, could keep track of [x-cord][y-cord][z-cord][time]. This 4D array would be a useful use of a 4D array. This could keep track of a range of cords and time, and the value in the array could say the speed of of the object.
Speed = arr[5][9][6][1500];
That would record the speed of the object when it was at x cord 5, y cord 9, z cord 6, and say been alive for 1500 seconds.
As for your 5D array:
int left; //#of cars left in world
left = allCars[year][brand][color][condition][horsepower];
brand count be an int(have a table of values aka Ferrari = 1, same with color. Condition could be on a scale of 0 being destroyed, to 1 being brand new).
Then you could find the # of 1947 Ferrari's that are red in brand new condition with 200 horsepower.
Aka in general you could use many dimensional arrays to keep track of large databases of things.
You may create a Sodoku hypercube with 4 dimensions and then you can store the number which the user enters into a 4dimensional int array.
Rotating 4D hypercube (GIF movie). Imagine a person who lives in 4
spatial dimensions, watching a hypercube rotate. The 4-dimensional
person has a 3-dimensional retina in each of its 2 eyes (two eyes
suffice for depth perception, even in 4D). Each retina records a
3-dimensional image.
This image shows what the 4-dimensional person sees, with the extra
dimension shown as a variation in colour. That is, the horizontal,
vertical, and `colour' directions correspond to the 3 directions on
the 3-dimensional retina of the 4-dimensional person. The vertical bar
to the right of the picture shows the correspondence between colour
and position in the extra dimension.
Also you may check the Tesseract
Take almost anything from physics, where tensors are common, for example general relativity, computational chemistry, quantum physics.
http://en.wikipedia.org/wiki/Tensor#Applications
Tensor with rank 4 is common for example.
http://www.oonumerics.org/FTensor/FTensor.pdf
http://mpqc.svn.sourceforge.net/viewvc/mpqc/trunk/mpqc/src/lib/chemistry/qc/lmp2/lmp2.cc?revision=9342&view=markup&pathrev=9492
The example was a stock-keeping system where jeans could be indexed by
inventory[size][length][color][fit] = number_received
which is only slightly contrived. You'd have no problem with a database structured in such a way but it does look funny as code.
You might be running a geophysics simulation, where you have a 3D region such as an oil field, and you are measuring the relative quantities of different substances found at different points in the region (oil, water, sand, porous rock and so on). You might have three dimensions for the position, and one dimension for which substance you're looking at.