/* * Written By : Naqibullah Danishjo * Date : 20/12/2009 * Website : danishjo.webs.com * Description : this program reads four-character words from a file called dictionary.txt and * places them into a 4x4 board according to their highest score. the board configuration is read from another file called input.txt * notice that the words inside dictionary.txt file should not exceed 4. and should exactly have 4 characters. * Example Files : * dictionary.txt file contents are as bellow : * ASKE 50 * BSKE 79 * GOLD 1000 * TEST 5 * * input.txt file contents are as bellow : * * A--- * ---- * B--- * -T-- * * After Execution : * * ATGB * -EOS * BSLK * -TDE */ import java.util.*; // for the Scanner import java.io.*; // for file public class wordPuzzle // defining the main class { public static void main(String[] args) // main method. { char board[][] = new char[4][4]; // defining a 2 dimensional character array. int count=0; // initializing a counter. String tmp[] = new String[4]; // defining a temporary String array. try // checking for exceptions. { Scanner input = new Scanner(new File("input.txt"));// reading file using Scanner class while (input.hasNextLine())// loops until the last line in the file. { tmp[count] = input.nextLine(); // taking the lines in the file. count++;// increment counter by 1 } file2("dictionary.txt");// calling file2 method. } catch (Exception b) // catch the error { System.out.println("Erro ! File Not Found !"); // prints the error } for (int i=0; i<4; i++) // loops until the number of words { for (int j=0; j<4; j++) // // loops until the number of words { if (tmp[i].charAt(j) >= 'A' && tmp[i].charAt(j) <= 'Z') // check for a alphabetic character { board[i][j] = tmp[i].charAt(j); // assings the character to the main board } else { board[i][j] = '-'; // fills the remaining cells with a '*' character } System.out.print(board[i][j]); // prints the board row by row } System.out.println(); } int indexOfVertical = 0; // initializing int indexOfHorizontal=0; // initializing for (int i=0; i<4; i++) // loops until the number of words // { if (putVertically(words[i], board) > 0) // check if the given word could be placed vertically// { indexOfVertical = putVertically(words[i], board); // gets the vertical index to place the word for (int k=0; k<4; k++) // because the words are a four character words // { board[k][indexOfVertical] = words[i].charAt(k); // places the word in appropriate column } } else if ((indexOfHorizontal = putHorizontally(words[i], board)) >= 0) // check if the given word could be placed horizontally// { for (int j=0; j<4; j++)// because the words are a four character words // { board[indexOfHorizontal][j] = words[i].charAt(j);// places the word in appropriate row } } } System.out.println("\n___________after run__________\n"); for (int i=0; i<4; i++) // prints the board after placement of the words // { for (int j=0; j<4; j++) { System.out.print(board[i][j]); // prints row by row } System.out.println(); } } public static String words[] = new String[4]; // a string array which holds words from dicionrary.txt file public static int score[] = new int[4]; // a integer array which holds scores from dicionrary.txt file public static int putHorizontally(String word, char board[][]) // this method will place the words horizontally { int j=0, i=0; // initializing variables for (i=0; i<4; i++) // loops through the each row { for (j=0; j<4; j++) // loops through the each column { if (word.charAt(j) != board[i][j] && board[i][j] != '-') // checks for conflicts { j=4; // this will make the second loop to break } if (j == 3) // it means no conflicts are encountered (the word could be placed here) { return i; // return the horizontal index to place the word } } } return -1; // means the word could not be placed horizontally } public static int putVertically(String word, char board[][])// places the word vertically { int count = 0; //initializing for (int i=0; i<4; i++) // loops through each row { for (int j=0; j<4; j++) // loops through each column { if (word.charAt(j) != board[count+j][i] && board[count+j][i] != '-') // checks for conflicts { j=4; // makes the second loop to break } if (j == 3) // mean the word can be placed vertically { return i; // returns the appropriate column index } } } return -1; // it means the word could not be placed vertically } public static void file2(String inpu2) // this method will read words from dictionary.txt { try { // controls the exceptions Scanner dic = new Scanner(new File(inpu2)); // defining a Scanner object int i=0; while(dic.hasNextLine()) // loops until the last line in the file { String line1 = dic.nextLine(); // gets the next line from the file Scanner scan = new Scanner(line1); // gets the line for tokens words[i] = scan.next(); // puts the first token into the words array score[i] = scan.nextInt(); // puts the second token into the scores i++; // increments } } catch (Exception e) // checks the exceptions { } sort(); // calls the sort() method which sorts the words read from dictionary.txt } public static void sort() // method sort() { for(int i=0;i=score[j]) // compares the scores of the words { // swaps // String tmp = words[j]; // words[j] = words[i]; // words[i] = tmp; // // swaps // int tmp1 = score[j]; score[j] = score[i]; score[i] = tmp1; } } } } }