/* package whatever; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
package Exercise6;
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Scanner;
import java.io.*;
import java.awt.*;
public class LadderGame
{
public static void ladder
(String startingword,
String endingword, ArrayList
<String
> dictionary, ArrayList
<String
> history,
int count
) {
String tempword
= dictionary.
get(count
); count++;
int count2 = 0;
for(int i = 0; i < tempword.length(); i++)
if(startingword.charAt(i) != tempword.charAt(i))
count2++;
if(count2 == 1)
{
startingword = tempword;
history.add(startingword);
}
// else
// if(tempword.equals(endingword))
// round++;
if(startingword.equals(endingword))
System.
out.
println(endingword
); else
{
if(count < dictionary.size())
ladder(startingword, endingword, dictionary, history, count);
// else if(count == dictionary.size() && round > 0)
// {
// ladder(startingword, endingword, dictionary, history, 0, round - 1);
// }
else
System.
out.
println("No ladder found."); }
}
public static void main
(String[] args
) {
Scanner sc
= new Scanner
(System.
in);
ArrayList<String> dictionary = new ArrayList<String>();
ArrayList<String> history = new ArrayList<String>();
System.
out.
println("Enter starting word."); String startingword
= sc.
nextLine().
toUpperCase(); System.
out.
println("Enter ending word."); String endingword
= sc.
nextLine().
toUpperCase();
try
{
fr
= new FileReader("/Users/Huangyonghuan/Downloads/words.txt"); int lines = countLines();
for(int i = 0; i < lines; i++)
{
String readword
= br.
readLine().
toUpperCase(); if(readword.length() == endingword.length())
dictionary.add(readword);
}
}
{
System.
err.
println("Exception!"); }
{
e.printStackTrace();
}
ladder(startingword, endingword, dictionary, history, 0);
}
public static int countLines()
{
int lines = 0;
try
{
fr
= new FileReader("/Users/Huangyonghuan/Downloads/words.txt"); while (br.readLine() != null) lines++;
}
{
System.
err.
println("Exception!"); }
{
e.printStackTrace();
}
return lines;
}
/* if(endingword.equals(readword))
for(int i = 0; i < endingword.length(); i++)
if(startingword.charAt(i) != readword.charAt(i))
tempword.charAt(i) = readword.charAt(i);*/
/* fr = new FileReader("/Users/Huangyonghuan/Downloads/words.txt");
br = new BufferedReader(fr);
String readword = br.readLine().toUpperCase();
if(endingword.length() == readword.length())
{
for(int i = 0; i < endingword.length(); i++)
if(endingword.charAt(i) != readword.charAt(i))
{
count++;
location = i;
}
if(count == 0)
System.out.println(endingword);
else if(count == 1)
{
for(int i = 0; i < location; i++)
tempword += endingword.charAt(i);
tempword += readword.charAt(location);
for(int i = location + 1; i < endingword.length(); i++)
tempword += endingword.charAt(i);
System.out.println(tempword);
ladder(tempword, endingword);
}
}
else
return ladder(startingword, endingword);*/
}
/* if(tempword.equals(endingword))
{
if(count2 == 1)
System.out.println(endingword);
else if(count2 > 1)
round++;
}
else
if(count2 == 1 && !(history.contains(tempword)))
{
startingword = tempword;
history.add(startingword);
}*/
/* do
{
String readword = br.readLine().toUpperCase();
if(readword.length() == endingword.length())
dictionary.add(readword);
if(readword.equals(""))
t++;
}while(t == 0);*/
/* String tempword = dictionary.get(count);
count++;
int count2 = 0;
for(int i = 0; i < tempword.length(); i++)
if(startingword.charAt(i) != tempword.charAt(i))
count2++;
if(count2 == 1)
{
startingword = tempword;
history.add(startingword);
}
// else
// if(tempword.equals(endingword))
// round++;
if(startingword.equals(endingword))
System.out.println(endingword);
else
{
if(count < dictionary.size())
ladder(startingword, endingword, dictionary, history, count, round);
// else if(count == dictionary.size() && round > 0)
// {
// ladder(startingword, endingword, dictionary, history, 0, round - 1);
// }
else
System.out.println("No ladder found.");
}*/