Project Rosalynde for KAIST student
import java.io.*;
import java.util.Random;
class Node
{
public String data;
public Node next;
public Node()
{
data=null;
next=null;
}
public Node(String str)
{
data=str;
next=null;
}
}
class Dict
{
public Node head;
private int size;
public Dict()
{
head=new Node();
size=0;
}
public void Add(String add)
{
Node temp=head;
while(temp.next!=null)
{
if(temp.next.data.compareToIgnoreCase(add)>0)
{
Node newNode=new Node(add);
newNode.next=temp.next;
temp.next=newNode;
size++;
break;
}
temp=temp.next;
}
if(temp.next==null)
{
Node newNode=new Node(add);
temp.next=newNode;
size++;
}
}
public void PrintAll()
{
Node temp=head.next;
while(temp!=null)
{
System.out.println(temp.data);
temp=temp.next;
}
System.out.println("PrintAll Finished");
}
public String FindWord(char ch)
{
int start=0, end=size+1, n=0, check=0;
Node temp=head.next;
while(temp!=null)
{
n++;
if(temp.data.toLowerCase().charAt(0)==ch && check==0)
{
start=n;
check=1;
}
if(temp.data.toLowerCase().charAt(0)!=ch && check==1)
{
end=n;
check=2;
break;
}
temp=temp.next;
}
if(check==0) return null;
Random rand=new Random();
int index=start+rand.nextInt(end-start)-1;
temp=head;
String str;
for(int i=0;i<index;i++)
{
temp=temp.next;
}
str=temp.next.data;
return str;
}
public boolean Me(String mestr)
{
Node temp=head;
while(temp.next!=null)
{
if(temp.next.data.compareToIgnoreCase(mestr)==0) return true;
temp=temp.next;
}
return false;
}
public boolean findDup(String str)
{
Node temp=head;
while(temp.next!=null)
{
if(temp.next.data.compareToIgnoreCase(str)==0) return true;
temp=temp.next;
}
return false;
}
public char startWith()
{
Random rand=new Random();
int a=rand.nextInt(size);
Node temp=head.next;
for(int i=0; i<a; i++) temp=temp.next;
return temp.data.charAt(0);
}
}
public class Project {
public static void main(String arg[])
{
BufferedReader br=null, fr=null;
try {
br=new BufferedReader(new InputStreamReader(System.in));
fr=new BufferedReader(new InputStreamReader(new FileInputStream("dict.txt")));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.exit(0);
}
Dict dict=new Dict();
Dict used;
while(true)
{
String str=null;
try {
str=fr.readLine();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(str==null) break;
dict.Add(str);
}
while(true)
{
System.out.println("Computer : 새 게임을 시작한다.");
used=new Dict();
char ch=dict.startWith();
while(true)
{
String comstr=dict.FindWord(ch);
if(comstr==null)
{
System.out.println("Computer : 할 단어 없다. 내가 졌다. y/n>");
break;
}
used.Add(comstr);
System.out.println("Computer : "+comstr);
System.out.print("Me : ");
ch=comstr.toLowerCase().charAt(comstr.length()-1);
String mestr=null;
try {
mestr=br.readLine();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.exit(0);
}
if(mestr.length()==0)
{
System.out.println("Computer : 말을 못하다니, 너는 졌다.");
break;
}
else if(mestr.toLowerCase().charAt(0)!=ch)
{
System.out.println("Computer : " + ch +"로 시작해야 한다. 너는 졌다.");
break;
}
else if(used.findDup(mestr))
{
System.out.println("Computer : 했던 단어다. 너는 졌다.");
break;
}
else if(dict.Me(mestr))
{
ch=mestr.toLowerCase().charAt(mestr.length()-1);
used.Add(mestr);
System.out.println("Computer : "+ch+"이라...");
}
else
{
System.out.println("Computer : 그런 단어는 없다. 너는 졌다.");
break;
}
}
System.out.println("Computer : 또 할래? y/n>");
System.out.print("Me : ");
String check="n";
try {
check=br.readLine();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(check.compareToIgnoreCase("n")==0 || check.compareToIgnoreCase("no")==0) break;
}
}
}