aspose file tools*
The moose likes Beginning Java and the fly likes Logic of placeDotCom->Gamehelper Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Logic of placeDotCom->Gamehelper" Watch "Logic of placeDotCom->Gamehelper" New topic
Author

Logic of placeDotCom->Gamehelper

Steve Issacs
Greenhorn

Joined: Feb 15, 2010
Posts: 2
Hi, could someone explain the logic of the above method in GameHelper class on page 152 in a better way than these comments?:

public ArrayList<String> placeDotCom (int comSize) {
ArrayList<String> alphaCells = new ArrayList <String> ();
String [] alphacoords = new String [comSize]; // holds 'f6' type coords
String temp = null; // temporary String for concat
int [] coords = new int [comSize]; //current candidate coords
int attempts = 0; // current attempts counter
boolean success = false; //flag = found a good location?
int location = 0; //current starting location

comCount++; // nth dot com to place
int incr = 1; // set horizontal increment
if ((comCount % 2) == 1) { // if odd dot com (place vertically)
incr = gridLength; // set vertical increment
}

while ( ! success & attempts++ < 200) { // main search loop
location = (int) (Math.random () * gridSize) ; // get random starting point
//System.out.print (" try " + location);
int x = 0; //nth position in dotcom to place
success = true; //assume success
while (success && x < comSize) { // look for adjacent unused spots
if (grid [location] == 0) { // if not already used
coords[x++] = location; // save location
location += incr; // try 'next' adjacent
if (location >= gridSize) { // out of bounds - 'bottom'
success = false; // failure
}
if (x > 0 && (location % gridLength == 0)) { // out of bounds - right edge
success = false;
} // failure
} else {
// System.out.print (" used " + location ;
success = false; // failure
}
}
} // end while

int x = 0; // tour location into alpha coords
int row = 0;
int column = 0;
// System.out.println ("\n");
while ( x< comSize); {
grid[coords[x]] = 1; // mark master grid pts as 'used'
row = (int) (coords[x] / gridLength); // get row value
column = coords[x] % gridLength; // get numeric column value
temp = String.valueOf(alphabet.charAt(column)); //convert to alpha

alphaCells.add(temp.concat (Integer.toString(row))); //
x++;
// System.out.print (" coord "+x+ " = " alphaCells.get (x-1));
}
// System.out.println ("\n");

return alphaCells;
}
}
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

Please UseCodeTags and IsolateTheProblem. What don't you get?


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Janeice DelVecchio
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1665
    
  11

Welcome to javaranch.... I assume you're using the HeadFirst Java?

While I go dust mine off, you should try to edit your post and put all that code into code tags.... it'll make it easier to read


When you do things right, people won't be sure you've done anything at all.
Janeice DelVecchio
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1665
    
  11

At this point in the book, it's not really NECESSARY to understand each part of what's happening here, but we can break this down into pieces. Below is the method method call in code tags..... You'll note the method you pasted is one of the 2 methods in the GameHelper class. The other method handles user input.

The method is called in the DotComBust class, in the setUpGame() method:


Do you understand what's going on in this above code?

It's going to take me a little bit to mark up the other method for you....
Janeice DelVecchio
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1665
    
  11

Here's your method marked up by me.

From here on in, if you don't understand something, copy and paste that part into a new post (using code tags) and we can go over it.


Steve Issacs
Greenhorn

Joined: Feb 15, 2010
Posts: 2
Thanks! yes, i understand the invocation of this method.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Logic of placeDotCom->Gamehelper