What is an effective method of moving a 2d array vertically to programmatically find an "empty" set?

Firstly, this is not homework;). I am trying to create a dictionary game from scratch and hit the barrier. I need some guidance.

I am using a 2d character array for a dictionary search grid. I find it very convenient to place words horizontally in these arrays, but I really got stuck on ideas on how to do this vertically.

This is what I have so far, you should just copy / paste it and run it

import java.util.ArrayList; import java.util.List; public class WordGame { private static List<String> words = new ArrayList<String>(); private static int longestWordLength = 0; private static int padSize = 4; private static char[][] grid = null; public static void main(String[] args) { initialiseWords(); workOutLongestWord(); setupGrid(); printIt(); } private static void printIt() { for (int i = 0; i < grid.length; i++) { for (int j = 0; j < grid.length; j++) { System.out.print(grid[i][j]); } System.out.print("\n"); } } private static void setupGrid() { grid = new char[longestWordLength + padSize][longestWordLength + padSize]; for (int i = 0; i < grid.length; i++) { String w = (i >= words.size()) ? "?" : words.get(i); for (int j = 0; j < grid.length; j++) { grid[i][j] = (j >= w.length()) ? '?' : w.charAt(j); } } } private static void workOutLongestWord() { for (String word : words) { if (word.length() > longestWordLength) { longestWordLength = word.length(); } } } private static void initialiseWords() { words.add("monkey"); words.add("cow"); words.add("elephant"); words.add("kangaroo"); } } 

What prints something like ...

 monkey?????? cow????????? elephant???? kangaroo???? ???????????? ???????????? ???????????? ???????????? ???????????? ???????????? ???????????? ???????????? 

I need to randomly lay them on the left / right side, but I can do it myself.

Question What is an efficient way to put words vertically in a 2d array as above? My initial thought was to count down for the required word length, break if something other than a is found ? , and continue to do so until I find a place for the word. However, this does not turn out beautiful, given the coincidence of words.

Any pointers?

+6
java arrays multidimensional-array
source share
2 answers

I made a similar problem in C when implementing Armadillo. Different ships were of different sizes, and you could not cross them.

Once you have vertical words, you will need to check if your horizontal words have hit.

I suggest creating a "Word" class, which is a thin class around String. You just need to keep track of the following.

  • x, y position / index in the world
  • What word is it
  • word length (provided to you by String in Java)
  • Word orientation (top left to right)

Then you create a method that checks the placement of words. EG, the whole word should be on the board, and there is no collision. You can model collisions of words using a series of line segments. This can be done using direct rectangular collision algorithms, where one size is almost 1.

+1
source share

Vertically it should be possible to use the same method as horizontally. As you said, start in one place and move down until you have an empty space, or a space contains a letter from the word that you would like to insert.

+1
source share

All Articles