• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Logic of placeDotCom->Gamehelper

 
Steve Issacs
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please UseCodeTags and IsolateTheProblem. What don't you get?
 
Janeice DelVecchio
Saloon Keeper
Posts: 1809
12
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Janeice DelVecchio
Saloon Keeper
Posts: 1809
12
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1809
12
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks! yes, i understand the invocation of this method.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic