mardi 11 juillet 2017

How to validate user input against string elements that have been randomly generated in an ArrayList?

I'm fairly new to java and am experimenting building my own text based game, at the moment im just trying to work on individual concepts and then hopefully be able to pull it all together in a final project, my problem i have come across at the moment and cant seem to find an answer is exactly what the title question states:

How can i validate user input against a String ArrayList of randomly generated elements?

To clarify, i have a working program at the moment that when run generates a random amount and random types of enemy and then populates them into the dungeon each time the dungeon object is created, the user is then presented with a question allowing them to pick which enemy they should confront first, this is where i require the validation in order that the game would continue, im trying to use a while loop which if the condition returns false would just skip and move on to if statements and their validations, i hope that makes sense and ive posted my code below, apologies in advance if there are many things wrong with my code either syntactically or structurally as stated earlier im still very new to java and at the moment if i can just learn to code then i can worry about how professional or proper something should be.

Here is my code:

package com.davegreen;

import java.util.List;
import java.util.Random;
import java.util.Scanner;

public class Main
{

    public static void main(String[] args)
    {
        // Declaring some variables and creating some objects of other classes that i need.

        Scanner scanner = new Scanner(System.in);
        Dungeon dungeon = new Dungeon();
        List<String> enemyType = dungeon.getEnemy();        // Gets all the enemy from my enemy ArrayList in the Dungeon class.
        int enemyNumberTotal = dungeon.getEnemy().size();   // Gets the amount of enemy in my enemy ArrayList.

        runGame();
        int randomAmountOfEnemy = generateRandomNumber(enemyNumberTotal);   // Generates a random amount of enemy into the dungeon from the total number in the ArrayList.

        System.out.println("\n The enemies in this dungeon are: \n");

        // Generates the random enemies that will populate the dungeon based on a method in my Dungeon class and then passes a random amount of said enemy each time also.

        List<String> randomlyPickedEnemy = dungeon.populateWithRandomEnemy(enemyType, randomAmountOfEnemy);

        System.out.println("\n\t * " + randomlyPickedEnemy);

        System.out.println("\n> Which enemy would you like to confront first?");
        String userChoice = scanner.nextLine();


        // I want to validate the user input from line 31 of the code to reflect that if the user has not selected an enemy that was in the randomly populated list
        // then we stay in the while loop and continue asking until of course the user types in a enemy that was in the randomly populated list, at which
        // point we would of course skip the while loop moving onto the if statements validation, it would be nice also to have ignore case somewhere in there.

        while(!userChoice.equals(randomlyPickedEnemy))
        {
            System.out.println("\n\t That enemy does not live in this dungeon!");
            System.out.println("\n> Which enemy would you like to confront first?");
            userChoice = scanner.nextLine();
        }

        // For the validation here i realise of course that just using the variable randomlyPickedEnemy in the sout would more than likely return ALL the enemy that
        // were randomly populated and not specifically the enemy that the user had chosen, so at this point i need a way to be able to access exactly what enemy it was
        // that the user had chosen to battle so that the sout statement makes sense but more importantly so i can then direct the code where it needs to go based on any given
        // particular enemy.

        if(userChoice.equals(randomlyPickedEnemy))
        {
            System.out.println("You have chose to battle " + randomlyPickedEnemy);
        }
    }

    public static void runGame()
    {
        System.out.println("##################################################");
        System.out.println("#####            DUNGEON CRAWLER             #####");
        System.out.println("##################################################");
    }

    // This is my method to generate a random amount of enemy for the total amount of enemy in my ArrayList.

    public static int generateRandomNumber(int howManyEnemies)
    {
        Random random = new Random();
        int rng = random.nextInt(howManyEnemies) + 1;
        System.out.println("\n There are " + rng + " enemies in this dungeon!");
        return rng;

    }
}




Aucun commentaire:

Enregistrer un commentaire