Scrabble, puting chars into a 2d array
OK im making a game of scrabble in java for Computer Science.
The thing I can't work out, is how when given a string for a word and its direction (horizontal, vertical) can I check whether it goes there ( 1 char per cell-thingo without leaving the 15x15 array, and whether it will be allowed, must have a common letter with a word already placed (unless first word).
This is important so I can implement the scoring method.
Thanks in advance
int grid = new grid;
I assume the user is putting the pieces in the grid 1 letter at a time.
using the piece's coordinates as index numbers, plug it in the array. The array could hold "0" for an empty spot and "1" for an already occupied spot. Then you can check for combinations of letters around it. This would probably be a recursive method.
For instance, lets say the word "dog" takes up spots (1,1), (1,2),(1,3). Now the user puts in an "s" at (x,y), where x=1 and y=4. Now check surrounding tiles. Eventually, it'll see there's a letter at (1,y-1), the "g". Have it continue searching for letters in that direction, the Y-direction. Eventually, it'll either find a blank spot, or in this case hit the edge of the board. So now you have letters from (1,1) to (1,4). Combine the 4 letters into a String and check the word against known words in your dictionary. The dictionary is needed to make sure the words exists in the language. Using a binary search, you can make this check perform relatively quickly.
thanks, never thought about it that way
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL