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.
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.
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.
I have built a JPanel which is painted based on a 2D array of data which the user can draw on.
I'm wondering if it is possible to somehow allow the user to upload a background picture, then set the size of the array based on the image to allow them to draw over it by filling in array cells. Then of course, rubbing out the colour in a cell should get back the original segement of the image.
Is this at all possible? I haven't found anything? I have the array at the moment and drawing using a MouseMotionListener but can't seem to find a way to set the 2D array size based on an image and then display it behind the 2d array.
I'm wondering if it is possible to somehow allow the user to upload a background picture, then set the size of the array based on the image to allow them to draw over it by filling in array cells. Then of course, rubbing out the colour in a cell should get back the original segement of the image.
Yes this is definitely possible. The key as in all programming is to break down a big problem into little steps and then try to solve each small step, one at a time.
Is this at all possible? I haven't found anything?
Your question is much too broad to find a complete or even partial solution by searching online. Again you can find solutions for each individual step, but you first have to create your steps. Once this is accomplished then it will be easy to find tutorials and solutions for each step that you may be stuck on.
I have the array at the moment and drawing using a MouseMotionListener but can't seem to find a way to set the 2D array size based on an image and then display it behind the 2d array.
Show us what you have and where you're stuck. We'll need code. And please note, the more specific your question, usually the better and more specific will be our answers. Your current question is in a nut-shell "is this possible", and the answer is of course "yes", but short of that, I'm not sure what else to say without knowing more about where in particular you may be stuck.
For the sake of argument, let's say I want to place a wall object (for simplicity, let's pretend it's just a 1x1 square) on a 2d grid which is 20x20. Let's say I have the object modeled out in coordinates between 0 and 1.
So, my question is, using openGL in the correct manner (I realize there are plenty of ways I could change the coordinates manually, but that doesn't edify me for the future), how do I place this object on the grid in the location (5,5)? Would it be related to the model matrix?
Yes, I think you have the right idea.
If your wall exists in model space from (0,0) to (1,1), and you want to position a particular instance of this wall at (5,5) through (6,6), than an appropriate thing to do would be to draw this wall with a Model Matrix that is translated by 5 units in the x and y direction.
You should not use the transformation matrices to place single primitives. Everytime you change a uniform (aka matrix) it's very likely the rasterizer pipline must be flushed, which is a sure performance killer.
As a general rule, to be efficient, a given transformation matrix should be applied to at least 100 primitives within a scene. So if you have some grid of tiles, it's better to either duplicate-translate them into a larger Vertex Array, or use instancing (if available).
I wanted to create Conway's Game of Life. I read the Java 2d API, but the Graphics class only provides methods to drawRect() and fillRect() on the paintComponent of a JPanel. I mean that the rectangles cannot be handled individually as objects i.e. so that i can check which one is on in relation to the ones in the vicinity.
So I wanted to ask how are squares to be made so that they can be handled individually and the grid be created dynamically?
Create a Sqaure class with all properties required.
Create a list of Square objects representing the board.
In the draw method for the JPanel, iterate over your list of Square objects, drawing each one out, based on its properties.
Keep your display code separate from your logic as much as possible - it's nearly always a good idea.
I'd like to propose a completely different solution. Normally have to treat the generated graphics as output-only, meaning, you don't want to read state from the graphics because that'd be too slow.
You'll have to keep the state of the cells elsewhere, like in a two-dimensional array.
boolean[][] or int[][] for example.
Then you'll need a "render" method of sorts, that takes the values of your cells, and draws it out.
But I'd like to propose an even cooler way of doing this. Instead of keeping a two dimensional array, use the (one dimensional) array that a BufferedImage is made up of.
Normally, each "pixel" is an element in that array. Then you use drawImage to draw that image, and scale that image up. This could perform really well. You might be able to have an entire screen draw in real time pretty much.
There are various methods on BufferedImage, it gets a little confusing at first. In the end you'll find a DataBuffer somewhere. You'll want access to the int[].
Then, to set a cell: data[y * width + x] = -1; (white cell)
to clear a cell: data[y * width + x] = 0; (black cell)
(for example - or vice versa - or any other color).
You can get really fancy with this. You could use various offset variables instead of having to calculate the y*width+x all the time, and optimize it really well.
In fact, I go as far as being able to make it so efficient, that you could actually outperform another guy making the exact same program in C++.