home *** CD-ROM | disk | FTP | other *** search
Java Source | 1997-11-01 | 12.0 KB | 409 lines |
- import aspcomp.*;
- import java.util.*;
-
- public class HiddenWords
- {
- // Size of the generated square
- private final static int X_SIZE = 20;
- private final static int Y_SIZE = 20;
-
- // Number of words in the random square
- private final static int NUM_WORDS = 10;
-
- // Random number object.
- // Saves us having to instantiate one each time we want a random number
- private Random m_rnd;
-
- // List of cities
- private String m_strWords[]=
- {
- "ExAir", "Auckland", "Seattle", "Sydney",
- "London", "Lusaka", "Paris", "Moscow",
- "Tokyo", "Brasilia", "Bogota", "Caracas",
- "Ottowa", "Toronto", "Dallas", "Houston",
- "Manchester", "Cairo", "Dublin", "Khartoum",
- "Riyadh", "Kuwait", "Durban", "Lisbon",
- "Madrid", "Rome", "Berlin", "Warsaw",
- "Stockholm", "Oslo", "Helsinki", "Wellington",
- "Canberra", "Reykjavik", "Copenhagen", "Delhi",
- "Singapore", "Jakarta", "Manila", "Bangkok",
- "Perth", "Quito", "Lima", "Montevideo",
- "Santiago", "Dunedin", "Suva", "Papeete",
- "Apia", "Adelaide", "Brisbane", "Melbourne",
- "Rotorua", "Napier", "Liverpool", "Ndola"
- };
-
- // Do we want to cheat? true means the square has bold words!
- private boolean m_fWannaCheat = false;
-
- // ASP response object
- private Response m_Response;
-
- // Array of letters
- private SquareElement[][] m_seArray = new SquareElement[X_SIZE][Y_SIZE];
-
- // All the valid words
- private Vector m_vectWords;
-
- ////////////////////////////////////////////////////////////////////////////////
- // ctor
- HiddenWords()
- {
- m_vectWords = new Vector();
- m_rnd = new Random();
- }
-
- ////////////////////////////////////////////////////////////////////////////////
- // buildWordSquare (COM Exposed)
- // Builds and displays a random word square populated with random cities.
- // If fWannaCheat is true then the letters of the cities are bold
- public void buildWordSquare(boolean fWannaCheat)
- {
- m_fWannaCheat = fWannaCheat;
-
- getIntrinics();
- generateRandomSquare();
- generateRandomWords();
- displaySquare();
- }
-
- ////////////////////////////////////////////////////////////////////////////////
- // getWordList (COM Exposed)
- // Brings back a list of words to the client formatted in a <TABLE>
- public void getWordList()
- {
- // Write the start of the table
- m_Response.write("<TABLE ID=TableCityList>");
- m_Response.write("<TR>\n");
-
- // Count through each word
- int iWhichWord = 0;
- for (Enumeration e = m_vectWords.elements(); e.hasMoreElements(); )
- {
- ChosenWord cw = (ChosenWord)e.nextElement();
- String strWord = cw.m_strWord;
-
- // Split the table into two rows
- if (iWhichWord == m_vectWords.size() / 2)
- {
- m_Response.write("</TR>");
- m_Response.write("<TR>");
- }
-
- // Write the word to the table and include the relevant OnClick() code
- // when the user clicks on this word.
- // For example, if the city is Auckland then the following is outputted:
- // <A onclick=WordClick("Auckland") ID=Auckland>Auckland</A>
- // WordClick() is VBScript code in the calling ASP page
- m_Response.write("<TD ALIGN=CENTER>");
- m_Response.write("<A onclick=WordClick(\"" + strWord + "\") ID=" + strWord + ">");
- m_Response.write(strWord + "  ");
- m_Response.write("</A>");
- m_Response.write("</TD>\n");
-
- iWhichWord++;
- }
-
- // Close the table
- m_Response.write("</TR>");
- m_Response.write("</TABLE>\n\n");
- }
-
- ////////////////////////////////////////////////////////////////////////////////
- // getIntrinics
- // Get any ASP intrinsics we need, in this case we are only writing data
- // back to the client so we only need the Response interface.
- private void getIntrinics()
- {
- m_Response = AspContext.getResponse();
- }
-
- ////////////////////////////////////////////////////////////////////////////////
- // displaySquare
- // Displays the fully populated word square in an HTML <TABLE>
- private void displaySquare()
- {
- // Write the start of the table and give it an ID
- m_Response.write("<TABLE ID=TableHiddenWords>");
- m_Response.write("<TR>");
-
- // This is the top/left blank square where there is no x/y coord.
- m_Response.write("<TD> </TD>");
-
- // Write the x-coords
- for (int i=0; i < X_SIZE; i++)
- m_Response.write("<TD ALIGN=CENTER BGCOLOR=#0000FF><FONT COLOR=#FFFFFF><B>" + (i < 10 ? "0" : "") + i + "</B></FONT></TD>");
-
- m_Response.write("</TR>\n");
-
- // Write the y-coord and each letter out in the square
- for (int i=0; i < Y_SIZE; i++)
- {
- // start a new line
- m_Response.write("<TR>");
-
- // write the y coord
- m_Response.write("<TD ALIGN=CENTER BGCOLOR=#0000FF><FONT COLOR=#FFFFFF><B>" + i + "</B></FONT></TD>");
-
- for (int j=0; j < X_SIZE; j++)
- {
- // get the letter
- String strLetter = new String();
- strLetter = strLetter + m_seArray[j][i].m_cLetter;
-
- // get other details about the letter
- boolean fPartOfWord = !m_seArray[j][i].m_fJunkLetter; // is it junk or part of a word?
-
- m_Response.write("<TD>");
-
- // if the letter is part of a valid word then write an <A ID=xx> out with the letter
- // For example, if the city is Auckland then following is written for the letter 'K'
- // if it is located at X=19, Y=8 and is written down the square:
- // <A ID=AUCKLANDD0819-3>AUCKLAND</A>
- // This allows the DHTML code to find the data in the square easily
- if (fPartOfWord)
- {
- if (m_fWannaCheat) m_Response.write("<STRONG>");
-
- int iIndex = m_seArray[j][i].m_iIndexIntoWord; // what is the index into the word?
- int iDirection = m_seArray[j][i].m_cw.m_direction; // get the direction of the word
- String strID = m_seArray[j][i].m_cw.m_strWord.toUpperCase() +
- (iDirection == ChosenWord.ACROSS ? "A" : "D") +
- (j < 10 ? "0" : "") + j +
- (i < 10 ? "0" : "") + i +
- "-" +
- iIndex;
- m_Response.write("<A ID=" + strID + ">");
- }
-
- m_Response.write(strLetter.toUpperCase());
-
- if (fPartOfWord)
- {
- m_Response.write("</A>");
- if (m_fWannaCheat) m_Response.write("</STRONG>");
- }
-
- m_Response.write("</TD>");
- }
-
- m_Response.write("</TR>\n");
- }
-
- m_Response.write("</TABLE>\n\n");
- }
-
- ////////////////////////////////////////////////////////////////////////////////
- // generateRandomSquare
- // Generates a random square of letters
- private void generateRandomSquare()
- {
- for (int i=0; i<X_SIZE; i++)
- {
- for (int j=0; j<Y_SIZE; j++)
- {
- m_seArray[i][j] = new SquareElement();
- m_seArray[i][j].m_cLetter = getRandomLetter();
- }
- }
- }
-
- ////////////////////////////////////////////////////////////////////////////////
- // getRandomLetter
- // Generates one random, uppercase letter
- private char getRandomLetter()
- {
- int iRand = Math.abs(m_rnd.nextInt()) % 26;
- return (char)((int)'A' + iRand);
- }
-
- ////////////////////////////////////////////////////////////////////////////////
- // generateRandomWords
- // Builds all the random words and populates the random square
- private void generateRandomWords()
- {
- for (int i=0; i < NUM_WORDS; i++)
- {
- // Get the word details
- ChosenWord cw = getRandomWordAndLocation();
- m_vectWords.addElement(cw);
-
- // Add the word to the square
- if (cw.m_direction == ChosenWord.ACROSS)
- {
- for (int j=0; j < cw.m_strWord.length(); j++)
- {
- m_seArray[cw.m_x + j][cw.m_y].m_cw = cw;
- m_seArray[cw.m_x + j][cw.m_y].m_cLetter = cw.m_strWord.charAt(j);
- m_seArray[cw.m_x + j][cw.m_y].m_fJunkLetter = false;
- m_seArray[cw.m_x + j][cw.m_y].m_iIndexIntoWord = j;
- }
- }
- else
- {
- for (int j=0; j < cw.m_strWord.length(); j++)
- {
- m_seArray[cw.m_x][cw.m_y + j].m_cw = cw;
- m_seArray[cw.m_x][cw.m_y + j].m_cLetter = cw.m_strWord.charAt(j);
- m_seArray[cw.m_x][cw.m_y + j].m_fJunkLetter = false;
- m_seArray[cw.m_x][cw.m_y + j].m_iIndexIntoWord = j;
- }
- }
- }
- }
-
- ////////////////////////////////////////////////////////////////////////////////
- // getRandomWordLocation
- // Generate a random word (from the list) and make sure it's unique
- private ChosenWord getRandomWordAndLocation()
- {
- String strWord;
-
- // Create a random, unique word
- do
- {
- strWord = getRandomWord();
-
- } while (isWordAlreadyUsed(strWord));
-
- // Create a new ChosenWord object and set the word
- ChosenWord cw = new ChosenWord();
- cw.m_strWord = strWord;
- getRandomLocationAndDirection(cw);
-
- return cw;
- }
-
- ////////////////////////////////////////////////////////////////////////////////
- // getRandomWord
- // Chose a word at random from the lit
- private String getRandomWord()
- {
- int iRand = Math.abs(m_rnd.nextInt()) % m_strWords.length;
- return m_strWords[iRand];
- }
-
- ////////////////////////////////////////////////////////////////////////////////
- // isWordAlreadyUsed
- // Is this word already in the vector of chosen words?
- private boolean isWordAlreadyUsed(String strWord)
- {
- boolean fUsed = false;
-
- for (Enumeration e = m_vectWords.elements(); e.hasMoreElements(); )
- {
- ChosenWord cw = (ChosenWord)e.nextElement();
- if (cw.m_strWord.equals((String)strWord))
- {
- fUsed = true;
- break;
- }
- }
-
- return fUsed;
- }
-
- ////////////////////////////////////////////////////////////////////////////////
- // getRandomLocationAndDirection
- // Chose a random location and direction,
- // making sure we don't cross the path of another word!
- private void getRandomLocationAndDirection(ChosenWord cw)
- {
- do
- {
- int iMaxX=X_SIZE, iMaxY=Y_SIZE;
-
- // 50/50 chance of ACROSS word or DOWN word
- if (m_rnd.nextInt() > 0)
- {
- iMaxX = X_SIZE - cw.m_strWord.length();
- cw.m_direction = ChosenWord.ACROSS;
- }
- else
- {
- iMaxY = Y_SIZE - cw.m_strWord.length();
- cw.m_direction = ChosenWord.DOWN;
- }
-
- cw.m_x = Math.abs(m_rnd.nextInt()) % iMaxX;
- cw.m_y = Math.abs(m_rnd.nextInt()) % iMaxY;
-
- } while (isLocationAlreadyUsed(cw));
- }
-
- ////////////////////////////////////////////////////////////////////////////////
- // isWordAlreadyUsed
- // Check if this word crosses the path of another
- private boolean isLocationAlreadyUsed(ChosenWord cw)
- {
- boolean fUsed = false;
- if (cw.m_direction == ChosenWord.ACROSS)
- {
- for (int i=0; i < cw.m_strWord.length(); i++)
- {
- // if we bump into a non-junk letter then it
- // must be a valid word so we're outta here
- if (!m_seArray[cw.m_x + i][cw.m_y].m_fJunkLetter)
- {
- fUsed = true;
- break;
- }
- }
- }
- else
- {
- for (int i=0; i < cw.m_strWord.length(); i++)
- {
- // if we bump into a non-junk letter then it
- // must be a valid word so we're outta here
- if (!m_seArray[cw.m_x][cw.m_y + i].m_fJunkLetter)
- {
- fUsed = true;
- break;
- }
- }
- }
-
- return fUsed;
- }
- }
-
- ////////////////////////////////////////////////////////////////////////////////
- // SquareElement class
- // Each element in the random letter square is actually one of these
- class SquareElement
- {
- public ChosenWord m_cw; // reference to the chosen word (if applicable)
- public char m_cLetter; // the letter in this square
- public boolean m_fJunkLetter; // is it a junk letter or part of a word?
- public int m_iIndexIntoWord; // if it's part of word then what index into the word is it?
-
- SquareElement()
- {
- m_cw = new ChosenWord();
- m_cLetter = '0';
- m_fJunkLetter = true;
- m_iIndexIntoWord = 0;
- }
- }
-
- ////////////////////////////////////////////////////////////////////////////////
- // ChosenWord class
- // A Vector of ChosenWord's is maintained
- class ChosenWord
- {
- public final static int ACROSS=0; // Word goes left -> right
- public final static int DOWN =1; // Word goes top -> down
-
- public String m_strWord; // The word
- public int m_x, m_y; // It's starting location
- public int m_direction; // It's direction
-
- ChosenWord()
- {
- m_strWord = new String("");
- m_x = m_y = 0;
- m_direction = ACROSS;
- }
- }
-