絞首刑執行人を作成しましたが、基本的には完了していますが、ループが機能していないように見えるため、ユーザーが勝ったときに、「はい」と言った場合に別のゲームが欲しいかどうか尋ねられ、再び開始されますか? 誰か助けてくれませんか?
ハングマンクラス:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package hangman;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;
import java.util.Scanner;
/**
*
* @author Adam2_000
*/
public class Hangman {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
String selection;
Scanner scan = new Scanner(System.in);
/*
* I moved the variables found here to the Game class
*/
//New instances of class and arrays
Words words = new Words();
System.out.println("Welcome to Hangman version 1");
System.out.println("Please choose a difficulty");
System.out.println("A: Easy");
System.out.println("B: Medium");
System.out.println("C: Hard");
System.out.println("X: Exit"); // Just for user friendlyness
System.out.println(" _________ ");
System.out.println("| | ");
System.out.println("| 0 ");
System.out.println("| /|\\ ");
System.out.println("| / \\ ");
System.out.println("| ");
System.out.println("| ");
char iChoice;
do {
selection = scan.nextLine().toUpperCase();
} while (selection.isEmpty());
iChoice = selection.charAt(0);
if (iChoice != 'X') {
switch (iChoice) {
case 'A':
System.out.println("You have choosen easy:");
new Game(words.easyWords, scan); //All three levels can be called from the same class with the same code.
break; //This will help reduce amount of code, and help when you need to change all three at once.
case 'B':
System.out.println("You have choosen Medium");
new Game(words.mediumWords, scan); //Called with the String[] of words you want to use and the scanner to save memory.
break;
case 'C':
System.out.println("You have choosen Hard");
new Game(words.hardWords, scan);
break;
}
}
}
}
ゲームクラス:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package hangman;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Scanner;
/**
*
* @author Adam2_000
*/
public class Game {
String player = "";
int turn = 12;
List<String> wordBox = new ArrayList<String>();
boolean hangman = false;
boolean win = false;
String loop;
Scanner scan;
Random random = new Random();
String letters; // For displaying letters
String mask; // For character Mask
public Game(String[] words, Scanner scanner) {
scan = scanner;
int selectA = random.nextInt(words.length);
letters = words[selectA]; //Load word into String for display. This just makes the code more readable.
mask = letters.replaceAll("\\S", "*"); // Mask the words
System.out.println("Random String selected: " + "\n" + mask);
System.out.println("This word contains " + letters.length() + " letters");
while (hangman == false) {
System.out.println("Turns remaining: " + turn);
System.out.println("Please choose a letter A-Z :");
String ChosenLetter = scan.next();
if (wordBox.contains(ChosenLetter)) {
System.out.println("Letter alreay choosen please choose another letter");
turn++;
} else {
wordBox.add(ChosenLetter);
}
if (letters.contains(ChosenLetter)) {
char[] cLetters = letters.toCharArray(); //Load letters and mask to char array for editing
char[] cMask = mask.toCharArray();
for (int i = 0; i < cMask.length; i++) {
if (cLetters[i] == ChosenLetter.charAt(0)) {
cMask[i] = cLetters[i];
}
}
mask = new String(cMask); //Load new mask into String
System.out.println("Wordbox letters are: " + wordBox);
System.out.println("Yes!" + "\n" + mask); // Prints mask after Yes!
turn--;
if (mask.contains(words[selectA])) {
System.out.println("You win!");
System.out.println("would you like another game?");
loop = scan.next();
while ("Yes".equals(loop) ||"yes".equals(loop));
}
} else {
System.out.println("Wordbox letters are: " + wordBox);
System.out.println("No!" + "\n" + mask); //Prints mask after No!
turn--;
}
if (turn == 0) {
hangman = true;
}
while (hangman == true) {
System.out.println("You lose!");
System.exit(0);
}
}
}
}
単語クラス:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package hangman;
import java.lang.reflect.Array;
import java.util.Random;
/**
*
* @author Adam2_000
*/
public class Words extends Hangman {
String[] easyWords = {"bee", "car", "fish", "shed"};
String[] mediumWords = {"house", "sheep", "castle", "phone"};
String[] hardWords = {"octagon", "crocodile", "chocolate", "motorbike"};
public String[] getEasyWords() {
return easyWords;
}
public void setEasyWords(String[] easyWords) {
this.easyWords = easyWords;
}
public String[] getMediumWords() {
return mediumWords;
}
public void setMediumWords(String[] mediumWords) {
this.mediumWords = mediumWords;
}
public String[] getHardWords() {
return hardWords;
}
public void setHardWords(String[] hardWords) {
this.hardWords = hardWords;
}
@Override
public String toString() {
return "words{" + "easyWords=" + easyWords + ", mediumWords=" + mediumWords + ", hardWords=" + hardWords + '}';
}
}
私はこれを試しましたか?
if (mask.contains(words[selectA])) {
System.out.println("You win!");
System.out.println("would you like another game?");
loop = scan.next();
while ("Yes".equals(loop) ||"yes".equals(loop));
}
何も起こらなかった。