I'm trying to create Word Search type of game where words are hidden in a bunch of letters. I'd like to know whats the best way to go about it. I mean do I create a char grid. How do I draw them on the screen (I mean like using drawString(.. or something). I know how to create a line grid using drawLine but I am kind of stuck in this part, creating the grid. Any kind of resources or guidance would help a lot.
Yes, you could use either drawString() or pack all of the characters into a single char and use drawChars(); it's up to you. You set the font on the Graphics before drawing the first letter, and go from there. If you can calculate the correct points for the gird lines, doing the same with a slight adjustment should work for the characters.
I recommend experimenting with it a little and see how far you can get. Come back if you get stuck and post what you have with some specific questions.
Heres what I have gone to so far... This is just the Grid panel which will be added to the main frame.
It just manages to draw a grid and insert letters. Now I'm looking for a way to select words so that the point where I press the mouse button and releases it is matched with the starting and ending columns and rows of the word respectively. It then marks the word as found. Any ideas would help.
You basically need to turn mouse events into word selection events, as it appears you've started above. As a first design I would do the following. Note that a grid cell is denoted by its row and column.
mousePressed(): Save starting grid cell, change background of starting cell to some highlight color. Set tracking cell to starting cell.
mouseMoved(): If new cell is the same as the tracking cell -- user is in the same cell as the last update, exit method without doing anything more. Otherwise, set tracking cell to new cell and set the background colors of all cells between the starting and tracking cell to the highlight color. If the line of cells from starting cell to tracking cell don't make up a valid line (aren't perfectly horizontal, vertical, or diagonal), highlight only the starting cell. Make sure you clear the backgrounds of the previously highlighted cells if they shouldn't still be highlighted.
mouseReleased(): Clear all highlighting. If the line of cells from starting cell to tracking cell is valid, the user has selected a word. Perform any other validation you need to do and exit.
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop