Jun 07, 2004 13:19
import cs1.Keyboard;
import java.util.*;
public class NameGameProgram
{
public static void main(String[] args)
{
String strPlayerInput = new String();
String strPrevious = new String();
String first = new String();
int[]usedList = new int[4];
String []wordList = {"TOM HANKS", "JULIA ROBERTS", "ROB SCHNEIDER",
"SEAN CONNERY"};
for (int i = 0; i < wordList.length; i++)
{
usedList[i] = 0;
}
System.out.print("Enter name: ");
strPlayerInput = Keyboard.readString();
strPlayerInput = strPlayerInput.toUpperCase();
strPrevious = Check(strPlayerInput, wordList, usedList);
usedList = getUsed(strPlayerInput, wordList, usedList);
do
{
String computer = new String();
if (strPrevious != "NOT A VALID NAME!"
&& strPrevious != "ALREADY USED, YOU LOSE!")
{
char letter;
char space;
System.out.print("You entered: " + strPrevious);
for (int i = 0; i < strPrevious.length(); i++)
{
space = strPrevious.charAt(i);
if (space == ' ')
{
letter = strPrevious.charAt(i+1);
computer += strPrevious.charAt(i+1);
i = 100;
for (int t = 0; t < wordList.length; t++)
{
computer = wordList[t];
if (computer.charAt(0) == letter &&
usedList[t] != 1)
{
computer = wordList[t];
t = 100;
}
else
{
computer = "Computer loses, you win!";
}
}
System.out.println("\nComputer: " + computer);
}
}// end of for loop
}// end of IF
System.out.print("Enter name: ");
strPlayerInput = Keyboard.readString();
strPlayerInput = strPlayerInput.toUpperCase();
strPrevious = getMatch(strPlayerInput, wordList, usedList, computer, first);
usedList = getUsed(strPlayerInput, wordList, usedList);
}while (strPrevious != "NOT A VALID NAME!"
&& strPrevious != "ALREADY USED, YOU LOSE!");
if (strPrevious != "NOT A VALID NAME!"
|| strPrevious != "ALREADY USED, YOU LOSE!")
{
System.out.print("End of game!");
}
//first = "no";
}
public static String Check(String strPlayerInput, String[]wordList,
int[]usedList)
{
String strPrevious = new String();
for (int i = 0; i < wordList.length; i++)
{
if (strPlayerInput.equals(wordList[i]))
{
strPrevious = strPlayerInput;
i = 100;
}
else //if (strPlayerInput != wordList[i])
{
strPrevious = "NOT A VALID NAME!";
}
}
return strPrevious;
}
public static String getMatch(String strPlayerInput, String[]wordList,
int[]usedList, String computer, String first)
{
char letter = 'x';
char space;
String strPrevious = new String();
for (int p = 0; p < computer.length(); p++)
{
space = computer.charAt(p);
if (space == ' ')
{
letter = computer.charAt(p+1);
p = 100;
}
}
for (int i = 0; i < wordList.length; i++)
{
if (strPlayerInput.equals(wordList[i])
&& (letter == strPlayerInput.charAt(0) || first == "yes"))
{
if (usedList[i] == 1)
{
strPrevious = "ALREADY USED, YOU LOSE!";
}
else
{
strPrevious = strPlayerInput;
usedList[i] = 1;
}
i = 100;
}
else //if (strPlayerInput != wordList[i])
{
strPrevious = "NOT A VALID NAME!";
}
}
return strPrevious;
}
public static int[] getUsed(String strPlayerInput, String[]wordList,
int[]usedList)
{
for (int i = 0; i < wordList.length; i++)
{
if (strPlayerInput.equals(wordList[i]))
{
if (usedList[i] != 1)
{
usedList[i] = 1;
}
}
}
return usedList;
}
}