我需要一个简单的刽子手游戏的帮助。我让游戏运行,我只需要知道在哪里以及如何将猜测的字母添加到数组的正确行中。还有什么代码我可以省略使我的项目更具可读性吗?
public static void main(String[] args)
{
//create scanner to read input
Scanner keyboard = new Scanner(System.in);
//create string name for player
String name;
//get player's name
System.out.println("What is you name?");
name = keyboard.next();
//Print welcome message
System.out.println("Hello " + name + "! Welcome to a game of Hangman!");
System.out.println("Guess letters until you correctly guess the word ");
System.out.println("or you fully assemble a complete hangman.");
System.out.println("You have a 3 letter word!");
Random rand = new Random();
String [] Word = {"hat"};
String secretWord = Word[rand.nextInt(Word.length)];// word from the char array and stores it in a variable
char [] array = secretWord.toCharArray(); //converts the word from the array into a char array
//char [] array = new char [secretWord.length()];
//counter
int i = 0;
char input = 0;
//counts wins
int winCount = 0;
//counts losses
int tries = 20;
int wrongCounter = 0;
ArrayList<Character> list = new ArrayList<Character>();
while(i<tries)
{
//allows player to guess a letter
System.out.println("Guess a letter : ");
input = keyboard.next().charAt(0);
list.add(input);
//displays guessed letter
System.out.println("Guessed Letters are : "+ input);
for(int j = 0; j<notSame(array).length;j++)
{
if(findCorrect(array,input, list)>0)
{
j++;
System.out.println("You got it right");
winCount++;
break;
}
else if(!(input ==notSame(array)[j]))
{
System.out.println("You got it wrong");
wrongCounter++;
break;
}
}
//tests to see if the player won
if(winCount == notSame(array).length)
{
System.out.println("You have Won!");
System.out.println(secretWord + " is the correct word!");
break;
}
else if(hangMan(wrongCounter)){
break;
}
}
System.out.println();
}
public static boolean hangMan(int wrongCounter)
{
boolean isOver= false;
System.out.println("------------|");
switch(wrongCounter)
{
case 1:
System.out.println("| O");
break;
case 2 :
System.out.println("| O");
System.out.println("| |");
break;
case 3 :
System.out.println("| O");
System.out.println("| /|");
break;
case 4:
System.out.println("| O");
System.out.println("| /|\\");
System.out.println();
break;
case 5:
System.out.println("| O");
System.out.println("| /|\\");
System.out.println("| /");
break;
case 6:
System.out.println("| O");
System.out.println("| /|\\");
System.out.println("| / \\");
System.out.println("Sorry, you lost");
System.out.println("hat is the correct word");
isOver = true;
break;
}
return isOver;
}
//removes all same letters from the array
private static char[] notSame(char[] a) {
HashSet<Integer> keys = new HashSet<Integer>();
char[] result = new char[a.length];
int j = 0;
for (int i = 0 ; i < a.length; i++)
{
if (keys.add((int) a[i]))
{
result[j] = a[i];
j++;
}
}
return Arrays.copyOf(result, j);
}
//tests to see if the users input it equal to any of the letters in the array
public static int findCorrect(char [] a,char Input,ArrayList list)
{
int count = 0;
for(int i = 0;i<notSame(a).length;i++)
{
if(notSame(a)[i]==Input)
count++;
}
return count;
}
}
答案 0 :(得分:1)
猜测字母的输出应为System.out.println("Guessed Letters are : "+ list.toString());
这样就可以显示所有猜到的字母而不仅仅是最后一个字母。
要将单词显示为消隐行,直到猜到每个字母,定义一个新的字符数组
String [] Word = {"hat"};
String secretWord = Word[rand.nextInt(Word.length)];/
// modified
char [] displayArray = new char[secretWord.length()];
for(int i=0; i < Word[0].length();i++){
displayArray[i] += '_';
}
然后在正确时(大约65行左右)更新它
if(findCorrect(array,input, list)>0)
{
j++;
//modified
int correctLocation = secretWord.indexOf(""+input);
displayArray[correctLocation] = array[correctLocation];
System.out.println("You got it right");
winCount++;
break;
}
然后在for循环结束时显示它
//modified
System.out.println("Word is : " + new String(displayArray));
//tests to see if the player won
if(winCount == notSame(array).length)
{
System.out.println("You have Won!");
System.out.println(secretWord + " is the correct word!");
break;
}
else if(hangMan(wrongCounter)){
break;
}