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.
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 I'm working on a agent based simulation and I'm going to use a 2D array which could be 500x5000 or 1000x1000. The actual simulation will consist of walls which make up rooms. The problem I don't want to manually insert each wall because this could take very long.
Is there a way where I can create a simple 2D image of a map and have the program read this image and insert the walls into the 2D array based on the image.
Regards
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'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'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];
}
}