java - What is an effective method of traversing a 2d Array vertically to programatically find an "empty" set? -
first off, isn't homework ;). i'm trying create wordsearch game scratch , have hit barrier need guidance on.
i'm using 2d array of chars grid of wordsearch. i'm quite comfortable placing words in these arrays horizontally, i'm stuck ideas on how vertically.
this have far, should able copy/paste , 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() { (int = 0; < grid.length; i++) { (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]; (int = 0; < grid.length; i++) { string w = (i >= words.size()) ? "?" : words.get(i); (int j = 0; j < grid.length; j++) { grid[i][j] = (j >= w.length()) ? '?' : w.charat(j); } } } private static void workoutlongestword() { (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"); } }
which prints out ...
monkey?????? cow????????? elephant???? kangaroo???? ???????????? ???????????? ???????????? ???????????? ???????????? ???????????? ???????????? ????????????
i need randomly pad them out on left/right hand side, can myself.
question : effective way of attempting place words vertically 2d array above? initial thought count downwards required word length, breaking if other ?
found, , keep doing until can find space word. doesn't pretty once take account word overlapping.
any pointers?
i did similar problem in c when implementing "battleship". different ships different sizes , couldn't have them intersect.
once have vertical words need check if horizontal words hit them well.
i suggest making "word" class thin class around string. need keep track of following.
- x,y position / index in world
- what word is
- the length of word ( given string in java )
- the orientation of word ( down left right )
you make method, validates word placement. e.g, whole word has on board, , there isn't collision. can model word collision series of line segments. done using rect rect collision algorithms, 1 dimension 1.
Comments
Post a Comment