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.
Related
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'm currently working on making a 2d tilebased game in java.
The way my game works right now is: Finding out which tiles are inside the screen area, then rendering these. The tiles are stored in an int array, where each tile is represented by a number.
For example 1 is grasstile and 2 is dirt tile.
It uses a getTile method to decide what numbers is at the specific coordinates, then returns a tiletype based on what number is at that coordinate, example:
int[] tiles=new int[width*height];
if(tiles[x+y*width]==1) return grassTile;
else if(tiles[x+y*width==2]) return dirtTile;
else if..... and so on
But i'm wondering if it would be more efficient to make an array of tile objects?
Tile[] tiles=new Tile[mapWidth*mapHeight];
return tiles[x+y*width];
I'm wondering what is best performance wise?
Sorry if this is a bad question. I'm new to programming and I can't seem to find this anywhere else.
Thanks you!
The performance cost of either of those methods is so positively tiny compared to the cost of doing graphical rendering that you really shouldn't care. Instead of worrying about performance there, you should focus on writing clear, robust, extensible code. With that in mind, I'd recommend:
1) Use a Tile class. It will allow you to add much more than tile_types to them later on.
2) Use new Tile[mapWidth][mapHeight], and address tiles as tiles[x][y]. This approach costs more memory, and you will have to create all of the sub-arrays yourself, but it guards you against a lot of easy-to-make off-by-one errors, and other mathematical mistakes.
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.
So I'm doing the project of an introduction to Java course and it seems that I chose something that goes way beyond what I'm able to do. :P
Any help would be greatly appreciated. This is what I'm having problems with:
You have a cursor that is controlled by a player (goes forward or
turns 90°) which leaves a colored line as it goes. If you manage to go
over your own line and close a polygon of any shape (only right angles
though), its surface changes color into the color of your line.
I can detect when this situation arises but I am kind of lost as how to actually fill the correct polygon just closed. I can't seem to imagine an algorithm that would cover any case possible.
I looked at the Scanline fill algorithm but I think it would start having problems by the time there are already some polygons already filled in the map.
The Floodfill algorithm would be perfect if I had a way of finding a point inside the polygon, but, as there are many different possibilities, I can't think of a general rule for this.
I'm using an array 2x2 of integers where each color is represented by a number.
Does anyone have an idea on how to approach this problem?
If you can detect the situation then this can be solved in very simple manner. The question is which point to choose as start point for floodfill. The simple answer is: try all of them. Of course it makes a sense to start only with points adjacent to the one where your cursor is located. In this case you will have at most 8 points to check. Even better - at least 2 of them are definitely painted already if current point forms a polygon.
So you have 8 points to check. Launch floodfill 8 times starting from each of those points.
Two things which you probably should keep in mind:
You should try filling the area in cloned version of your field in order to be able to get back if floodfill will not find a polygon.
Launching floodfill second time and later you should reuse this cloned version of your field to see whether it was filled there. This will allow you to check every point at most once and this will make your 8 floodfills almost as fast as 1 floodfill.
Check this question, using Graphics2 and Polygon to fill an arbitrary polygon: java swing : Polygon fill color problem
Finding out whether a point is inside or outside a polygon: http://en.wikipedia.org/wiki/Point_in_polygon
Make sure you use double buffering. If you set individual pixels and don't use double buffering the component may redraw after every pixel was set.
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];
}
}