简体   繁体   中英

Maze game using enums to decide which text is printed

I am making a maze game. What I am trying to do is print different messages when a user bumps into a wall depending on a random number. The number is generated by the random number generator. For this, I am using enums because I thought it would be easier, but I am having a problem. The class GameManager takes all the inputs from the user and decides what to do with them. When I call the print statement for textcard.cardtype.WALL, it returns null and I am not sure why. I am pretty sure my problem has to do with my getCard method. I am trying to return that text card in that class but I am not sure if I did it right anyway. Below are my enums and wall method.

import java.util.Random;
public class TextCard
{
    // instance variables - replace the example below with your own
    private String text;//text in the card
    private int number;
    private TextCard text1;
    public enum CardType{
        WALL, TRAP
    }
    /**
     * Constructor for objects of class TextCard
     */
    //messages that will appear throughout the game
    public TextCard(CardType cardType)
    {
       if(cardType == (CardType.WALL)){
           wallCard();
        }

    }
    public String wallCard(){
        Random rnd = new Random();
        number = rnd.nextInt(100) + 1;
        if(number < 20){
            return ("wall1");
    }
    if(20 < number && number > 40){
        return ("wall2");
    }
    if(40 < number && number > 60){
        return ("wall3");
    }
    if(60 < number && number > 80){
        return ("wall4");
    }
    else{
        return ("wall5");
    }
  }
  public TextCard getCard(){
      return text1;
    }

}

METHOD CALL IN GAMEMANAGER

System.out.println(new TextCard(TextCard.CardType.WALL).getCard());

Best to do:

TextCard newcard = new TextCard();
newcard.getString();

And in TextCard:

public String getString() {
  return taco; // where taco is a string at the class level
}

Your .getCard() method returns a TextCard object within your TextCard class that you never assigned to anything.

Instead of returning text1, forget the text1 variable and just return this; in your getCard() method.

This will get you the object itself which will look like garbage: TextCard@5c647e05

To get any values of that object, you'll need to do getCard().getThing() and include a getThing method in your card class that returns the "thing" (be it an int, String, etc.)

System.out.println(new TextCard(TextCard.CardType.WALL).wallCard());

I don't know why you want a method to return the object itself, you would have that already. I assume you want to call your method to get the random string.

Also, you could make the wallCard method static so you don't need a new instance of TextCard that would be just deleted right after the print

public static String wallCard(){

This would then be used like so

System.out.println(TextCard.wallCard());

To explain what is wrong with the code you posted, you call wallCard() in the constructor which returns the random string but you don't store it. The getCard() method isn't clear, but it looks like with how you call it in the print, you want it to return the random string. If you want just 1 random string to reuse, you need to use the text String variable. So in the constructor, do this

text = wallCard(); 

then in getCard(), return text. It would need to return a String.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM