1

Trying to get a program similar to this to work on the android platform. The trouble is I'm not sure on using xml to display text/read user input. I'm fairly new to mobile software development but from the code below, I'm only trying to display the print statements and obviously get user input too.

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

    public class assignmentMSD
    {
        public static void main(String[] args)
        {
            //objects(system)
            Scanner in = new Scanner(System.in);
            Random rand = new Random();

        //Variables used

        String[] enemies = {"Zombie", "Necromorph", "Soldier", "MadMax"};
        int maxEnemyHealth = 75;
        int enemyAttackDamage = 25;

        //Player Variables
        int health = 100;
        int attackDamage = 50;
        int numHealthElixers = 3;
        int healthPotionHeal = 30;
        int healthPotionDropChance = 50;

        boolean running = true;

        System.out.println("Welcome to the Thunderdome");

        GAME:
        while(running)
        {
            System.out.println("--------------------------------------------------");

            int enemyHealth = rand.nextInt(maxEnemyHealth);
            String enemy = enemies[rand.nextInt(enemies.length)];
            System.out.println(" \t####" + enemy + " has appeared#### \n");

            while(enemyHealth > 0)
            {
                System.out.println("\t Your Health:" + health);
                System.out.println("\t" + enemy + "'s Health:" + enemyHealth);
                System.out.println("\n \t What would you like to do?");
                System.out.println("\t 1. Attack");
                System.out.println("\t 2. Drink Health Potion");
                System.out.println("\t 3. Run");

                String input = in.nextLine();
                                if(input.equals("1"))
                                {
                                   int damageGiven = rand.nextInt(attackDamage);
                                   int damageTaken = rand.nextInt(enemyAttackDamage);

                                   enemyHealth -= damageGiven;
                                   health -= damageTaken;

                                   System.out.println("\t You strike the " + enemy + " for " + damageGiven + " damage.");
                                   System.out.println("\t You take " + damageTaken + " in combat:");

                                   if(health < 1)
                                   {
                                       System.out.println("\t You have taken too much damage and therefore are too weak to fight!");
                                       break;

                                   }


                                }
                                else if(input.equals("2"))
                                {
                                   if(numHealthElixers > 0)
                                   {
                                       health += healthPotionHeal;
                                       numHealthElixers--;
                                       System.out.println("You drink a heakth potion for " + healthPotionHeal + "."
                                                            + "\n\t You now have" + health + " HP."
                                                            +"\n\t You have " + numHealthElixers + " Health Elixers left. \n");

                                   }
                                   else
                                   {
                                       System.out.println("\t You have no health Elixer left!, You must defeat enemies for a chance to get more \n");

                                   }
                                }
                                else if(input.equals("3"))
                                {
                                    System.out.println("\t You run away from the " + enemy + "!" );
                                    continue GAME;
                                }
                                else
                                {
                                    System.out.println("\t\n Invalid Command");
                                }
            }

                        if(health < 1)
                        {
                            System.out.println("You Flee from the Thunderdome, weak from battle");
                            break;
                        }

                        System.out.println("-------------------------------");
                        System.out.println(" # " + enemy + "was defeated! # ");
                        System.out.println(" # You have" + health + " HP left. #");
                        if(rand.nextInt(100) < healthPotionDropChance)
                        {
                            numHealthElixers++;
                            System.out.println(" # The " + enemy + " dropped a health potion #");
                            System.out.println(" # You now have " + numHealthElixers + " Health Elixer(s). #");

                        }
                        System.out.println("-------------------------------");
                        System.out.println("What would you like to do now?");
                        System.out.println("1. Continue Fighting");
                        System.out.println("2. Exit Dungeon");

                        String input = in.nextLine();

                        while(!input.equals("1") && !input.equals("2"))
                        {
                            System.out.println("invalid command");
                            input = in.nextLine();

                        }

                        if(input.equals("1"))
                        {
                            System.out.println("You continue through the thunderdome");
                        }
                        else if(input.equals("2"))
                        {
                            System.out.println("You exit the thunderdome a wiser man, sucessful on your quest");
                            break;
                        }
        }

                System.out.println(" -------------------------- ");
                System.out.println(" ----Thanks for playing----");
                System.out.println(" -------------------------- ");


    }
}
Rami
  • 7,879
  • 12
  • 36
  • 66
Bryan
  • 51
  • 1
  • 1
  • 7
  • 2
    Simply use TextView for displaying text and an EditText for user input, check this out: http://developer.android.com/guide/topics/ui/controls/text.html. I also recommend some video tutorials on Android basics: http://www.newthinktank.com/2014/06/android-beginners/ – Frank D. Nov 24 '15 at 10:40
  • You are doing a complete mesh above there. And I know you won't even understand a single word from the docs. I suggest you to have a look at these https://www.youtube.com/watch?v=gcPgdra10Oc&index=5&list=PLonJJ3BVjZW6hYgvtkaWvwAVvOFB7fkLa – Aawaz Gyawali Nov 24 '15 at 10:44

2 Answers2

2

Yes, you generally use TextView for displaying text, and edit text for text for the user to input.

This is a good link to look at: http://developer.android.com/training/basics/firstapp/building-ui.html

The developer.android training articles are very good, even for a beginner. Use the xml file in the res/layout folder for your UI stuff. Android Studio even let's you see how your UI will look if you click on the Design tab at the bottom.

Side Note: You'll find it's a lot more fun than System.out.println()

Ramonster
  • 444
  • 4
  • 15
1

First in android you need to define a layout. Use:

  • EditText to replace inputs of the user (Scanner)
  • TextView to replace outputs to the user (System.out.println)

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/some_label_text"/>
<EditText
    android:id="@+id/example_input"
    android:hint="@string/some_hint_text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

NOTE: Labels, if text is predefined, can have the text in the definition.

Second reference your objects into your java app by id like this.

EditText exampleInput = (EditText )dialogCarro.findViewById(R.id.example_input);

Third you will need to add Listeners to Buttons or EditTexts.

Community
  • 1
  • 1
Jordi Castilla
  • 26,609
  • 8
  • 70
  • 109
  • Would I have to replace each system.out.print with a new text view or can it all be done from one – Bryan Nov 24 '15 at 13:44
  • Depending your requeriments, but in a standart flow with a single one must be enough – Jordi Castilla Nov 24 '15 at 14:12
  • At the moment its just to get it working on android. I got advice on converting the text to a string and putting it into the text view, but that would require multiple text views. If there's a way to display everything in one text view for the moment I suppose that would be a better option. – Bryan Nov 24 '15 at 14:23
  • Why multiple? Just get `TextView` object like `exampleInput` in my answer and assign it with `setText()` each time you want to put a new text – Jordi Castilla Nov 24 '15 at 14:26
  • each time the user makes a selection, the text just repeats itself(the options 1,2,3 that the user chooses) so if i assign setText() will it display again after the user makes a choice? – Bryan Nov 24 '15 at 14:40
  • Text will be replaced, but... Whydont you try it and see? – Jordi Castilla Nov 24 '15 at 14:42