Advertisement
Guest User

WordSearch

a guest
Apr 21st, 2015
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.28 KB | None | 0 0
  1. import java.util.*;
  2. import java.io.*;
  3.  
  4. /**
  5.  * Handles the WordSearch itself, as a buffer between the generator and the GUI.
  6.  *
  7.  */
  8. public class WordSearch
  9. {
  10.  
  11.     //
  12.     // Fields
  13.     //
  14.  
  15.     /** The game board for this word search. */
  16.     private char[][] gameBoard;
  17.  
  18.     /** The words in the Word Search. */
  19.     private ArrayList< String > words;
  20.  
  21.     //
  22.     // Constructors
  23.     //
  24.    
  25.     /**
  26.      * Constructs a new WordSearch from the words in the given file.
  27.      * @param input
  28.      *          The file to read words from.
  29.      */
  30.     public WordSearch( File input )
  31.     {
  32.         generateWordSearch( readWords( input ) );
  33.     }
  34.    
  35.     //
  36.     // Actions
  37.     //
  38.    
  39.     /**
  40.      * Creates the word search from the list of words to hide.
  41.      */
  42.     private void generateWordSearch( ArrayList< String > words )
  43.     {
  44.         // determine the size of the game board
  45.         int size = 0;
  46.         for ( String s : words )
  47.         {
  48.             int tempSize = ( int ) ( s.length() * 1.5 );
  49.             if ( tempSize > size ) size = tempSize;
  50.         }
  51.         gameBoard = new char[ size ][ size ];
  52.        
  53.         WordSearchGenerator.create( gameBoard, words );
  54.     }
  55.    
  56.     /**
  57.      * Reads the words in the file.
  58.      *
  59.      * @param input
  60.      *          The file to read.
  61.      */
  62.     private ArrayList< String > readWords( File input )
  63.     {
  64.         words = new ArrayList<>();
  65.        
  66.         try ( Scanner scanner = new Scanner( input ) )
  67.         {
  68.             while ( scanner.hasNext() )
  69.             {
  70.                 //alter the words to all upper case and remove every space in the word
  71.                 words.add( scanner.nextLine().toUpperCase().replace( " ", "" ) );
  72.             }
  73.         }
  74.         catch ( FileNotFoundException e )
  75.         {
  76.             // cleverly disguise the error message
  77.             words.add( "ERROR" );
  78.             words.add( "WHILE" );
  79.             words.add( "READING" );
  80.             words.add( "FILE" );
  81.         }
  82.         // print out the words
  83.         System.out.println( "Words added:\n" + words.toString() );
  84.        
  85.         return words;
  86.     }
  87.    
  88.     //
  89.     // Getters
  90.     //
  91.    
  92.     public ArrayList< String > getWords()
  93.     {
  94.         return words;
  95.     }
  96.    
  97.     /**
  98.      * @return The game board of characters.
  99.      */
  100.     public char[][] getGameBoard()
  101.     {
  102.         return gameBoard;
  103.     }
  104.    
  105.     public int getWidth()
  106.     {
  107.         return gameBoard.length;
  108.     }
  109.    
  110.     public int getHeight()
  111.     {
  112.         return gameBoard[ 0 ].length;
  113.     }
  114.  
  115. }
  116.  
  117. //
  118. // This class generates the word searches
  119. //
  120.  
  121. import java.util.*;
  122.  
  123. /**
  124.  * Responsible for generating word searches.
  125.  */
  126. public class WordSearchGenerator
  127. {
  128.    
  129.     //
  130.     // Fields
  131.     //
  132.    
  133.     /** The word search game board we're creating */
  134.     private static char[][] matrix;
  135.  
  136.     /** The width of the board */
  137.     private static int boardWidth;
  138.  
  139.     /** The height of the board */
  140.     private static int boardHeight;
  141.  
  142.     //
  143.     // Actions
  144.     //
  145.  
  146.     /**
  147.      * Creates a wordsearch, by means of populating the char[][] passed, with the
  148.      * passed list of words.
  149.      *
  150.      * @param matrix
  151.      *          The word search board.
  152.      * @param words
  153.      *          The words to hide in the board.
  154.      */
  155.     public static void create( char[][] matrix, ArrayList< String > words )
  156.     {
  157.         WordSearchGenerator.matrix = matrix;
  158.         boardWidth = matrix.length;
  159.         boardHeight = matrix[ 0 ].length;
  160.        
  161.         fillWithSpaces();
  162.         hideWords( words );
  163.         fillSpaces();  
  164.     }
  165.    
  166.     //
  167.     // Helper Methods
  168.     //
  169.    
  170.     /**
  171.      * Fills the entire matrix with empty spaces.
  172.      */
  173.     private static void fillWithSpaces()
  174.     {
  175.         for ( int i = 0; i < boardWidth; i++ )
  176.         {
  177.             for ( int j = 0; j < boardHeight; j++ )
  178.             {
  179.                 matrix[ i ][ j ] = ' ';
  180.             }
  181.         }
  182.     }
  183.    
  184.     /**
  185.      * Hides the words into the matrix.
  186.      *
  187.      * @param words
  188.      *          The words to hide in the matrix.
  189.      */
  190.     private static void hideWords( ArrayList< String > words )
  191.     {
  192.         int boardWidth = matrix.length;
  193.         int boardHeight = matrix[ 0 ].length;
  194.         for ( String s : words )
  195.         {
  196.             // keep repeating indefinitely until an accurate place for the word is found
  197.             while ( true )
  198.             {
  199.                 int[] possibleLocation = generatePossibility( s );
  200.                 int x = possibleLocation[ 0 ];
  201.                 int y = possibleLocation[ 1 ];
  202.                 boolean vertical = possibleLocation[ 2 ] == 1;
  203.                
  204.                 if ( vertical && isVerticallyPossible( s, x, y ) )
  205.                 {
  206.                     // insert the word into the matrix
  207.                     for ( int row = y; ( row - y ) < s.length(); row++ )
  208.                     {
  209.                         matrix[ x ][ row ] = s.charAt( row - y );
  210.                     }
  211.                    
  212.                     break; // the placemen is successful, break out of the while loop
  213.                 }
  214.                 else if ( !vertical && isHorizontallyPossible( s, x, y ) )
  215.                 {
  216.                     for ( int col = x; ( col - x ) < s.length(); col++ )
  217.                     {
  218.                         matrix[ col ][ y ] = s.charAt( col - x );
  219.                     }
  220.                    
  221.                     break; // the placement is successful, break out of the while loop
  222.                 }
  223.             }
  224.         }
  225.     }
  226.    
  227.     /**
  228.      * Checks to see if the vertical positioning for the word is possible.
  229.      *
  230.      * @param word
  231.      *          The current word.
  232.      * @param x
  233.      *          The x position of the word.
  234.      * @param y
  235.      *          The y position of the word.
  236.      */
  237.     private static boolean isVerticallyPossible( String word, int x, int y )
  238.     {
  239.         // check to see if there are any conflicts going down
  240.         for ( int row = y; row - y < word.length(); row++ )
  241.         {
  242.             // if the space isn't empty
  243.             // AND
  244.             // if the character in the space doesn't match the character the word would have at this space
  245.             // then this position isn't possible, return false immediately
  246.             if ( matrix[ x ][ row ] != ' ' && matrix[ x ][ row ] != word.charAt( row - y ) )
  247.             {
  248.                 return false;
  249.             }
  250.         }
  251.         return true; // if there were no conflicts, then it can be placed here
  252.     }
  253.    
  254.     /**
  255.      * Checks to see if the horizontal positioning for the word is possible.
  256.      *
  257.      * @param word
  258.      *          The current word.
  259.      * @param x
  260.      *          The x position of the word.
  261.      * @param y
  262.      *          The y position of the word.
  263.      */
  264.     private static boolean isHorizontallyPossible( String word, int x, int y )
  265.     {
  266.         // check to see if there are any conflicts going down
  267.         for ( int col = x; col - x < word.length(); col++ )
  268.         {
  269.             // if the space isn't empty
  270.             // AND
  271.             // if the character in the space doesn't match the character the word would have at this space
  272.             // then this position isn't possible, return false immediately
  273.             if ( matrix[ col ][ y ] != ' ' && matrix[ col ][ y ] != word.charAt( col - x ) )
  274.             {
  275.                 return false;
  276.             }
  277.         }
  278.         return true; // if there were no conflicts, then it can be placed here
  279.     }
  280.    
  281.     /**
  282.      * Generates a possibility for a place for the word to be placed on the board.
  283.      *
  284.      * @param word
  285.      *          The word to generate a possible location for.
  286.      */
  287.     private static int[] generatePossibility( String word )
  288.     {
  289.         int height = boardHeight;
  290.         int width = boardWidth;
  291.        
  292.         boolean direction = Math.random() > 0.5; // if true it will be vertical, false is horizontal
  293.        
  294.         // change the boundaries based off of the direction the word goes in
  295.         if ( direction )
  296.         {
  297.             height -= word.length();
  298.         }
  299.         else
  300.         {
  301.             width -= word.length();
  302.         }
  303.        
  304.         // randomly select points and return them
  305.         int x = ( int ) ( Math.random() * width );
  306.         int y = ( int ) ( Math.random() * height );
  307.         int dir = ( direction ? 1 : 0 );
  308.        
  309.         return new int[] { x, y, dir };
  310.     }
  311.    
  312.     /**
  313.      * Fills up the rest of the spaces with random character.
  314.      */
  315.     private static void fillSpaces()
  316.     {
  317.         for ( int i = 0; i < boardWidth; i++ )
  318.         {
  319.             for ( int j = 0; j < boardHeight; j++ )
  320.             {
  321.                 // if there are any empty spaces, just fill it wih a random letter
  322.                 if ( matrix[ i ][ j ] == ' ' )
  323.                 {
  324.                     matrix[ i ][ j ] = ( char ) ( ( Math.random() * 26 ) + 'A' );
  325.                 }
  326.             }
  327.         }
  328.     }
  329.  
  330. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement