This site requires JavaScript, please enable it in your browser!
Greenfoot back
IsVarious
IsVarious wrote ...

2012/4/16

How to use an 2d array?

IsVarious IsVarious

2012/4/16

#
I'm having some issues, with asigning values to a 2d array, but first here's the purpose. I'm re working a reversi game in Greenfoot, and the issue I'm having now is that I'm trying to hold the locations for where I want the pieces to be displayed on the screen. I already have all the "x & y" cords for each item, but I'm not sure how to use a 2d array, or what would be the best construct to deal with in this matter. Any ideas? If it makes sense to use a 2d array, then what I'm trying to do is store both the x & y cords in one array, then when it comes time for the computer to place a piece, it can refer to the given location in the array. Again, any ideas?
danpost danpost

2012/4/16

#
Instead of storing the x & y cords in the array(s), use the position in the array as the coordinate. I like to keep things managable, using a single string to track to position would be my choice. Using a string: the position can be a space for empty, one letter for one player and another for the other. You will have a string length of 64 (for your 8 x 8 squares), which can be turned to the coordinates. The following list of methods would probably suit your needs using a string.
String layout = "                           wb      bw                           ";
// b: black    w: white    (starting position)

private int getStringPosition(int x, int y)
{
    return (y * 8 + x);
}

private int getIntX(int stringPosition)
{
    return (stringPosition % 8);
}

private int getIntY(int stringPosition)
{
    return ((stringPosition - getIntX(stringPosition)) / 8);
}

private String getPieceAt(int x, int y)
{
    return ("" + layout.charAt(getStringPosition(x, y));
}

private void setPieceAt(int x, int y, String piece) 
{ // piece is either "w" or  "b"
    int position = getStringPosition(x, y);
    String aft = layout.substring(position + 1);
    layout = layout.substring(0, position) + piece + aft;
}

private void flipPieceAt(int x, int y)
{
    String piece = "" + " bw".charAt("wb".indexOf(getPieceAt(x, y)) + 1);
    setPieceAt(x, y, piece);
}
IsVarious IsVarious

2012/4/16

#
I'm a bit confused, is this piece of code for the logic of figuring out how to move and flip pieces, or is it setting up the x & y cords? I'm doing something very different from any reversi game I've saw here. I've changed the world portion so it has a lot of space around the board, so the main world/board class isn't just setup for pieces and the game logic. I've actually setup a series of menus, which setup the game, and allow the user to pick which color it wants to play. It also keeps track of the current score, will know when a side needs to pass, (this may be automatic) and in the end declare a winner. So the piece of code above, I'm not really sure what it's doing. Could you comment it out, or does this post describe something a bit different than what you were responding to? in any event, thanks for such a fast reply.
danpost danpost

2012/4/17

#
As long as the board on your screen does not move around, all you need are offsets. Figure out the distance from the left edge of the window to the center of the first column of squares and call it 'xOffset'. Do the same from the top edge to the first row and call it 'yOffset. The only other thing you will need is the width (or height) of a square, and call it 'squareSize'. Then, with addObject or setLocation, the x-coordinate will be 'xOffset + squareSize * x' and the y-coordinate will be 'yOffset + squareSize * y', where 'x' and 'y' are values between zero and seven (0 to 7).
danpost danpost

2012/4/17

#
I would suggest creating an array of 64 Piece objects, and populate the array as you create and add the Pieces to the world (all 64 from the start). One advantage is you have something to click on to make the moves. Another is, once the pieces are in place, you are done with them. You can create a Piece constructor to recieve two parameters; one for the square number (0 to 63) and the other for the String representation of the piece it should be (" ", "b", or "w"; to be used to set the proper image). The act method can continuously check to make sure the world's 'layout' String has the same character for its location, and if not, update its representation by copying the layout character for it and updating the image. This way, any change to the 'layout' string in the world will be automatically reflected on the screen.
2patrickMurphy 2patrickMurphy

2012/4/17

#
If you can run a program without a 2D array it is much more reccomended. But if your interested in how a 2D array functions here is a simple way to look at it.
How do you access all elements in a 1D array.

for( you should know what goes here)
{
      stuff you want done;
}

now a 2d array

for ( use i ; i<size; i ++)
{
     for ( use k; k<othersize;k++)
        {
             what you want done to each element
         }
}
Think of a 2 dimensional array as an array of arrays. So for each index you have a reference to another array which has a set of index's. These index's can be references by i and k. If you wanted a 3 dimensional array and greater you just add one more for loop and use a different letter / name for the size of that particular array. The thing about 2 dimensinoal arrays is if you needed it each array can be a different length and size. How to do this im not too sure yet i havnt had a program that needed it yet. Now back to 2D arrays. If you storing two values there is another way to do this. You can make accessor methods (this is less efficient though) and make a class that holds your x,y coordinates (if i read appropriately) then create a 1 dimensinoal array of that class. This way instead of dealing with a 2D array your only dealing with a 1D array. Problem with that is this, 2D array depending on your functions and the things you need to program will be more difficult and the datastructure will be more challenging then using a class of a one dimensional array. The benefit of using a 2D array is your program will run faster and work better then it would with the 1D array of a class. Now back to what i said earlier, listen to danpost it seems he knows greenfoot much better then me. Im VERY NEW to greenfoot only reason why im using it is because i have a class assignment in it. Also remember the less loops you use and less dimensions you use and if statements and method calls and times you allocate space + do computations (math) the more efficient your program becomes. If you are having performance issues, go back and reduce the things i just mentioned. Also there are times where the way you design your program itself has a role in how well it runs. These things can be learned through ready programming books. I dont know them myself it is an expertise and study in computer science to make things efficient so there are LOTS of methods and ways to do this. (depending on your program and how complicated it is of course). I hope this helped. and good luck! i read your profile and what you want to do is going to need the use of databases. I reccomend learning programming on greenfoot then create your game on a system engine (reversi that is). Also read into artificial intellegence and research the bets AI for chess and reversi and checkers. If you like i can help you with these things when im not in class and help develop the game to your liking i kinda like the idea of learning artificial intellegence myself :). Anyway Best of luck to you ill follow you.
RedPhoneBooth RedPhoneBooth

2012/4/17

#
Why is it not recommended to not use a 2-D array? I love 2-D arrays, there great for tile based stuff and rooms and stuff like that. do they take up a lot of memory? on the adventure game im working on the whole room system is tracked by a 2-D array so that I can "load" rooms as I pass through them.
IsVarious IsVarious

2012/4/17

#
Thanks to everyone who's responding, it means a lot and these are very helpful ideas. So to clear the fog a bit more, I'll explain what I have setup already, and from here maybe some ideas will pop out. Remember, I'm not wanting people to give me the code so much that someone else is writing this for me. I'm mainly needing to understand how to implement a few things for this game, but I do love code samples which help illustrate ways to implement these ideas. Here's where I'm at, while my profile reads about wanting to design a game that plays with my own play-style in mind, this current project is for my Final in a course with game programming in Greenfoot. I picked a reversi game that's already designed here, but I don't like it's AI, and the look/feel of it. So for my final I've chosen to redo it all, starting with the graphics of it, the look, the board, (which now has a boarder, and a larger area where there is a menu on the side, places for the score, and indicators for who's turn it is currently at any given time) and then the AI. One issue I came to right out of the gate, is wondering how I'm going to deal with the piece's x & y cords in the calculations of the game's AI. I figured out all the locations for where each piece would look good, I created a new board in Photoshop, so from this I stored all the x & y cords in a method as a temporary holding location. I wrote one method which places the four starting pieces, and on the remaining pieces I have of the board, (since the color of the squares are dark green) it places green pieces on all the empty spaces. I did this for a few reasons, one, is that I wanted to see how all the pieces would look, then it was easy for me to just change the current images. two, it gives me a spot for click events, which I wasn't sure if it was a good idea to implement the logic in this way. Now I'm at the part where I'm curious if it's faster/ easier to just use a 2d array to store the x & y locations, and then connect that to the computers calculations, or if I should just focus on implementing the logic into the click event of the green/empty spaces already. Any ideas? NOTE: I would of just build the game off the current game's AI but it's pretty bad, and I want to vamp it up a bit so it's a little stronger. They didn't really comment much of their code so it's hard to follow the logic of what's currently in place, also it's not setup to use the same world size I'm using, it basically has an 8,8 world, which is fine if you don't care about any extra bells and whistles, but for my final I'd like to make it look classy. So here's my question, is it better to use an array with the method above, where I store all the x&y locations in an array, or would it be better to just work with the click event of every piece? (e.g is it easier, simpler and faster) Any feedback to this will rock the socks off. and again thanks to everyone who's contributed thus far.
2patrickMurphy 2patrickMurphy

2012/4/17

#
@RedPhoneBooth 2D arrays are great for what you use it for and are needed. But if your using a two-dimensional array in a situation where you dont NEED one you are increasing the memory usage exponentially. Refer a 2D array to n^2 and a 1D array to n^1. N will be much smaller with a 1D array, but if your calling a bunch of methods and doing alot of excess calculuations in your 1D array (and creating classes) this is where a 2D array is needed (only if its more efficient). @isVarious OOOOHHHHHHHHH i thought u wer just casually making programs :). In that case i have some feedback and ideas, i still say research what AI they use to chess i know they have some very nice well developed AI in chess you can take that and develop it for reversi. Take databases into account when doing an AI (atleast i think). Everytime you do an action the AI reads the actions decides if its a good one or not then develops its play style based on professional feedback on current situations and rules then as the program is ran through time it develops its own playstyle that constantly changes and hopefully gets better. How you do the modulus for all the databses (idk) how you set up the databases (idk) i suggest researching that too. As for your question about whether its better to use a 2D array or not. I say do this, test the duration or speed it takes your program to execute with the 2d array, then test it without a 2d array. Then pick the faster one. A question like this is hard to answer unless you yourself have programmed the exact program your talking about (in the same way) since there are many ways to program. But like i said before research is the best idea. Im certain you can find tons of info on AI they use in games like chess they might not be open-source but they will reference you to algorithmns and styles of how to do it. But you have to remember you gotta teach your AI how to play reversi first then decide how he reacts to situations, then save all this in a database that continues to grow and adapt according to how games are played. The adapting part might be alittle difficult and require lots of work or it might not. So i suggest doing research again to see how long something like that might take to implement. Also, good luck on your project and i hope you get a good score! Im doing a game too but mine isnt as fun as yours. Im making capture the flag with worms -.-
Builderboy2005 Builderboy2005

2012/4/17

#
An array (2D or 1D) is a type of data structure, and as such the amount of memory it uses is only dependent on the amount of memory you are trying to represent. If I had a tilemap or something, I could represent it using either a 1D array or a 2D array, but both methods would take the same amount of space because they would be representing the same data. Data structures are used to represent data and give you different ways to access it, it doesn't really change the size of the data you are trying to represent.
IsVarious IsVarious

2012/4/18

#
2patrickMurphy wrote...
@RedPhoneBooth Im doing a game too but mine isnt as fun as yours. Im making capture the flag with worms -.-
Thanks for the advise everyone, and I'll take this and see what sticks. If I have another question I'll be sure to ask. Also, I'd be interested in seeing your progress sometime Pat.
You need to login to post a reply.