Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- import java.io.*;
- /**
- * Handles the WordSearch itself, as a buffer between the generator and the GUI.
- *
- */
- public class WordSearch
- {
- //
- // Fields
- //
- /** The game board for this word search. */
- private char[][] gameBoard;
- /** The words in the Word Search. */
- private ArrayList< String > words;
- //
- // Constructors
- //
- /**
- * Constructs a new WordSearch from the words in the given file.
- * @param input
- * The file to read words from.
- */
- public WordSearch( File input )
- {
- generateWordSearch( readWords( input ) );
- }
- //
- // Actions
- //
- /**
- * Creates the word search from the list of words to hide.
- */
- private void generateWordSearch( ArrayList< String > words )
- {
- // determine the size of the game board
- int size = 0;
- for ( String s : words )
- {
- int tempSize = ( int ) ( s.length() * 1.5 );
- if ( tempSize > size ) size = tempSize;
- }
- gameBoard = new char[ size ][ size ];
- WordSearchGenerator.create( gameBoard, words );
- }
- /**
- * Reads the words in the file.
- *
- * @param input
- * The file to read.
- */
- private ArrayList< String > readWords( File input )
- {
- words = new ArrayList<>();
- try ( Scanner scanner = new Scanner( input ) )
- {
- while ( scanner.hasNext() )
- {
- //alter the words to all upper case and remove every space in the word
- words.add( scanner.nextLine().toUpperCase().replace( " ", "" ) );
- }
- }
- catch ( FileNotFoundException e )
- {
- // cleverly disguise the error message
- words.add( "ERROR" );
- words.add( "WHILE" );
- words.add( "READING" );
- words.add( "FILE" );
- }
- // print out the words
- System.out.println( "Words added:\n" + words.toString() );
- return words;
- }
- //
- // Getters
- //
- public ArrayList< String > getWords()
- {
- return words;
- }
- /**
- * @return The game board of characters.
- */
- public char[][] getGameBoard()
- {
- return gameBoard;
- }
- public int getWidth()
- {
- return gameBoard.length;
- }
- public int getHeight()
- {
- return gameBoard[ 0 ].length;
- }
- }
- //
- // This class generates the word searches
- //
- import java.util.*;
- /**
- * Responsible for generating word searches.
- */
- public class WordSearchGenerator
- {
- //
- // Fields
- //
- /** The word search game board we're creating */
- private static char[][] matrix;
- /** The width of the board */
- private static int boardWidth;
- /** The height of the board */
- private static int boardHeight;
- //
- // Actions
- //
- /**
- * Creates a wordsearch, by means of populating the char[][] passed, with the
- * passed list of words.
- *
- * @param matrix
- * The word search board.
- * @param words
- * The words to hide in the board.
- */
- public static void create( char[][] matrix, ArrayList< String > words )
- {
- WordSearchGenerator.matrix = matrix;
- boardWidth = matrix.length;
- boardHeight = matrix[ 0 ].length;
- fillWithSpaces();
- hideWords( words );
- fillSpaces();
- }
- //
- // Helper Methods
- //
- /**
- * Fills the entire matrix with empty spaces.
- */
- private static void fillWithSpaces()
- {
- for ( int i = 0; i < boardWidth; i++ )
- {
- for ( int j = 0; j < boardHeight; j++ )
- {
- matrix[ i ][ j ] = ' ';
- }
- }
- }
- /**
- * Hides the words into the matrix.
- *
- * @param words
- * The words to hide in the matrix.
- */
- private static void hideWords( ArrayList< String > words )
- {
- int boardWidth = matrix.length;
- int boardHeight = matrix[ 0 ].length;
- for ( String s : words )
- {
- // keep repeating indefinitely until an accurate place for the word is found
- while ( true )
- {
- int[] possibleLocation = generatePossibility( s );
- int x = possibleLocation[ 0 ];
- int y = possibleLocation[ 1 ];
- boolean vertical = possibleLocation[ 2 ] == 1;
- if ( vertical && isVerticallyPossible( s, x, y ) )
- {
- // insert the word into the matrix
- for ( int row = y; ( row - y ) < s.length(); row++ )
- {
- matrix[ x ][ row ] = s.charAt( row - y );
- }
- break; // the placemen is successful, break out of the while loop
- }
- else if ( !vertical && isHorizontallyPossible( s, x, y ) )
- {
- for ( int col = x; ( col - x ) < s.length(); col++ )
- {
- matrix[ col ][ y ] = s.charAt( col - x );
- }
- break; // the placement is successful, break out of the while loop
- }
- }
- }
- }
- /**
- * Checks to see if the vertical positioning for the word is possible.
- *
- * @param word
- * The current word.
- * @param x
- * The x position of the word.
- * @param y
- * The y position of the word.
- */
- private static boolean isVerticallyPossible( String word, int x, int y )
- {
- // check to see if there are any conflicts going down
- for ( int row = y; row - y < word.length(); row++ )
- {
- // if the space isn't empty
- // AND
- // if the character in the space doesn't match the character the word would have at this space
- // then this position isn't possible, return false immediately
- if ( matrix[ x ][ row ] != ' ' && matrix[ x ][ row ] != word.charAt( row - y ) )
- {
- return false;
- }
- }
- return true; // if there were no conflicts, then it can be placed here
- }
- /**
- * Checks to see if the horizontal positioning for the word is possible.
- *
- * @param word
- * The current word.
- * @param x
- * The x position of the word.
- * @param y
- * The y position of the word.
- */
- private static boolean isHorizontallyPossible( String word, int x, int y )
- {
- // check to see if there are any conflicts going down
- for ( int col = x; col - x < word.length(); col++ )
- {
- // if the space isn't empty
- // AND
- // if the character in the space doesn't match the character the word would have at this space
- // then this position isn't possible, return false immediately
- if ( matrix[ col ][ y ] != ' ' && matrix[ col ][ y ] != word.charAt( col - x ) )
- {
- return false;
- }
- }
- return true; // if there were no conflicts, then it can be placed here
- }
- /**
- * Generates a possibility for a place for the word to be placed on the board.
- *
- * @param word
- * The word to generate a possible location for.
- */
- private static int[] generatePossibility( String word )
- {
- int height = boardHeight;
- int width = boardWidth;
- boolean direction = Math.random() > 0.5; // if true it will be vertical, false is horizontal
- // change the boundaries based off of the direction the word goes in
- if ( direction )
- {
- height -= word.length();
- }
- else
- {
- width -= word.length();
- }
- // randomly select points and return them
- int x = ( int ) ( Math.random() * width );
- int y = ( int ) ( Math.random() * height );
- int dir = ( direction ? 1 : 0 );
- return new int[] { x, y, dir };
- }
- /**
- * Fills up the rest of the spaces with random character.
- */
- private static void fillSpaces()
- {
- for ( int i = 0; i < boardWidth; i++ )
- {
- for ( int j = 0; j < boardHeight; j++ )
- {
- // if there are any empty spaces, just fill it wih a random letter
- if ( matrix[ i ][ j ] == ' ' )
- {
- matrix[ i ][ j ] = ( char ) ( ( Math.random() * 26 ) + 'A' );
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement