Question: I need help with my code. someone help me. I am having a hard time reading from a dictionary file and a board text file.
I need help with my code. someone help me. I am having a hard time reading from a dictionary file and a board text file. It is for the boggle game.
EXECUTE LIKE THIS: C:\> java Boggle dictionary.txt 4x4.txt (or any boggle input file) this is how I will test it and will expect you use args[0] as the dictionary and args[1] as the input file.
ONLY WORDS OF LENGTH 3 or MORE ARE TO BE COUNTED/OUTPUTTED
Solving the game of Boggle can be done elegantly with recursion and backtracking. Backtracking is a technique whereby an algorithm recognizes it is impossible or unnecessary to search any deeper into the search space from a given point. An example of this is finding your way through a maze. When you hit a dead end you retreat from that point and never take the same path to that point again. A fast and efficient solution to Boggle requires a heuristic that helps you recognize a dead end and save vast amounts of wasted words formed and wasted searches (and thus vast amounts of time).
heres the dictionary file.
http://people.cs.pitt.edu/~hoffmant/S18-401/project-10/dictionary.txt
heres the 4x4.txt file
4 qu e a i n e r e t w s t o r k y
this is the output solution of 4x4 file
http://people.cs.pitt.edu/~hoffmant/S18-401/project-10/4x4-board-output.txt
im suppose to do it for a 5x5 and 10x10 file.
Heres my code below. Someone help me please
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner; import java.io.*; import java.util.*;
public class GFG {
static int row[] = {0,0,1,-1,-1,1,-1,1};
static int col[] = {1,-1,0,0,-1,1,1,-1};
public static void find(String d[],int mat[][])
{
boolean flag = false;
boolean visited[][] = new boolean[3][3];
for(int i=0;i { for(int k=0;k<3;k++) { for(int j=0;j<3;j++) { visited[k][j] = false; } } for(int p=0;p<3;p++) { for(int q=0;q<3;q++) { if(mat[p][q]==d[i].charAt(0)) { //System.out.println(p+" "+q+" "); if(findutil(d[i],mat,p,q,0,visited)) { System.out.println(); flag = true; break; } } } if(flag == true) { flag = false; break; } } } } public static boolean findutil(String str,int mat[][],int i,int j,int m,boolean visited[][]) { if(m==str.length()-1) { System.out.println(str); return true; } if(m>str.length()-1) return false; for(int p=i+0;p<8;p++) { if(i+row[p]>=0 && i+row[p]<3 && j+col[p]<3 j+col[p]>=0 && visited[i+row[p]][j+col[p]] == false && mat[i+row[p]][j+col[p]]==str.charAt(m+1)) { //System.out.print(str.charAt(m+1)+" "+(m+1)); visited[i+row[p]][j+col[p]] = true; findutil(str, mat, i+row[p], j+col[p], m+1,visited); } } return false; } public static void main (String[] args) throws FileNotFoundException { // I am not sure of how the words are placed in the dictionary.txt. // Is it comma seperated words OR one word per line. // below I assume one word per line and store the same in the dictionary[] array. //creating File instance to reference text file in Java // provide the correct path to the dictionary.txt file. File text = new File("C:/temp/dictionary.txt"); //Creating Scanner instnace to read File in Java Scanner scnr = new Scanner(text); String dictionary[]; // assuming the first line will give the number of words in the dictionary.txt dictionary = new String[scnr.nextInt()]; while(scnr.hasNextLine()){ int i =0 ; String line = scnr.nextLine(); dictionary[i] = line; i++; } // lets read the 4x4 Board.txt file. text = new File("C:/temp/Board.txt"); scnr = new Scanner(text); int boggle[][]; int leng = scnr.nextInt(); boggle = new int[leng][leng]; int i = 0, j=0; while(scnr.hasNextLine()){ String line[] = scnr.nextLine().split(" ");//here each line in file contains some number as per your logic and they might be seperated by space so here I am splitting line read here by space here I have changed the type of line to array int k = 0; for( j=0;j boggle[i][j] = Integer.parseInt(line[k]);//and boggle is int array here converting string content into integer k = k + 2; } i++; } } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
