让我的游戏循环?

时间:2013-04-07 10:42:11

标签: java

我已经做了一个刽子手的人,它基本上是完整的但是我似乎无法得到一个循环工作,所以当一个用户赢了他们被问到他们是否想要另一个游戏,如果他们说是,那么它再次开始?谁能帮助我?

Hangman课程:

/*
 * 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));

                }

什么都没发生。

1 个答案:

答案 0 :(得分:1)

最简单的方法是这样的:

if("Yes".equals(loop) || "yes".equals(loop)) {
    new Game(words, scanner);
}

或者如果玩家再次选择等级更好,你应该在Hangman类中放置一个真正的循环(你必须改变它:

if(hangman == true) {    <- why there was while ??
    System.out.println("You lose!");
    //System.exit(0);    <- remove this
}

并允许Game类完成,返回Hangman类,所有代码都应该在循环中(仅在用户想要退出时结束)。试试这个并询问是否有任何问题。