• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

does 200 mean something?

 
Lee EunA
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello
I am a Korean.
I can not speak English...
sorry..
but i have a question ..
chapter 6. GameHelper Source.
=Head First Java=

------------

// Tiger version

import java.io.*;
import java.util.*;

class GameHelper {

private static final String alphabet = "abcdefg";
private int gridLength = 7;
private int gridSize = 49;
private int [] grid = new int[gridSize];
private int comCount = 0;


public String getUserInput(String prompt) {
String inputLine = null;
System.out.print(prompt + " ");
try {
BufferedReader is = new BufferedReader(
new InputStreamReader(System.in));
inputLine = is.readLine();
if (inputLine.length() == 0 ) return null;
} catch (IOException e) {
System.out.println("IOException: " + e);
}
return inputLine.toLowerCase();
}



public ArrayList<String> placeDotCom(int comSize) { // line 19
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
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~here!!!
// Number 200!!!
//Why 200???
while ( !success & attempts++ < 200 ) { // main searchloop (32)
//////////////////////////////////


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 { // found already used location
System.out.print(" used " + location);
success = false; // failure
}
}
System.out.println("\n");
System.out.println(attempts);
} // end while


int x = 0; // turn good 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;
}
}
-------------
I wondered

The meaning of 200............

It's just a big number of our actions?





[ April 29, 2008: Message edited by: Lee EunA ]

[edited to make subject meaningful - was "important question !!!please~~~"]
[ April 29, 2008: Message edited by: Jeanne Boyarsky ]
 
Alaa Nassef
Ranch Hand
Posts: 471
Hibernate Mac OS X Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Lee,

Which book is this from? How is this struts related? Administrators are going to refuse the title of the post since it's not descriptive.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Welcome to JavaRanch!

Yes, it's just a large number. After 200 attempts, the loop will exit.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's often better to give "magic numbers" like this a descriptive name. For example...

static final int MAX_ALLOWED_ATTEMPTS = 200;

Then the code would be more clear...

while(!success && (attempts++ < MAX_ALLOWED_ATTEMPTS)) {...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic