简体   繁体   中英

I am making a turn based combat thing in Java just to see if I can figure it out myself, But I have a problem

So, I am making a turn based combat class in java for when I try to make an rpg. I will provide the code below. I have a public static String called state and a Scanner called sc, and a switch statement for state. Before the switch statement, state = "0". case "0" of the switch statement prints out "A Slime appears\n(1)Attack\n(2)Defend" and makes state = "1". case "1" has another switch statement in it for sc.nextLine. in this switch statement, case "1" generates damage for me and the slime, subtracts them from our hp, and prints out a message for the attacks, and makes state = "2". case "2" generates the half damage of the slime and prints out a message for the slimes damage, being half what it would be, with you defending inside of attacking and makes state = "2". So, either attack or defend, it makes state = "2". Then for the first switch statement, case "2" prints out the options of attack and defend again and makes state = "1". When I run the code, it prints the first message sayinjg the slime appears and either attack or defend, but I cant type anything in to console and the code is terminated. I am using Eclipse.

package dfguy;

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

public class Main {
    
    public static String state;
    
    public static int cmhp = 40;
    
    public static int cchp = 40;
    
    public static int smhp = 20;
    
    public static int schp;
    
    public static Random dmg = new Random();
    
    public static Scanner sc = new Scanner(System.in);

    public static void main(String[] args) {
        
        state = "0";
        
        switch(state) {
        
        case "0":
            
            System.out.println("A Slime appears!\n(1)Attack\n(2)Defend");
            
            schp = smhp;
            
            state = "1";
            
            break;
            
        case "1":
            
            switch(sc.nextLine()) {
            
            case "1":
                
                int cdmg = (dmg.nextInt(6) + 5);
                
                int sdmg = (dmg.nextInt(5) + 4);
                
                System.out.println("You attack the Slime for " + cdmg + "damage!\nThe Slime attacks "
                        + "you for " + sdmg + "damage!");
                
                schp = schp - cdmg;
                
                cchp = cchp - sdmg;
                
                state = "2";
                
                break;
                
            case "2":
                
                int sbdmg = (dmg.nextInt(3) + 2);
                
                cchp = cchp - sbdmg;
                
                System.out.println("You defend against attack! The Slme attacks for " + sbdmg + "damage!");
                
                state = "2";
                
                break;
            
            }
            
            break;
            
        case "2":
            
            System.out.println("(1)Attack\n(2)Defend");
            
            state = "1";
            
            break;
        
        }

    }

}

There is no loop around the switch to run it more than once - so you go through with state=0, flip it to state=1 and then you're done.

For now you can get away with while(true) { /*your switch*/ } but eventually you probably want while(.dead) {... } . Also perhaps put your whole switch into a different method rather than doing it all in main .

You are writing a proper state machine, but one key thing about a state machine is that it needs to be called over and over repeatedly. The first time through the state is one thing and later it changes to something else.

The thing is, each state may run over and over. So you have to ask your question in one state, and wait for the response in the next. If you leave it in that state it's just going to keep spamming the question. So state 0 should ask the question, then state 1 just loops waiting for a response and the response sends you to state 2 or 3 for more text.

Or you can even have state machines within state machines. The point is that looping code keeps reaching the same point until it's time to do something else.

The whole thing has to be inside a while loop. You can use while(true) or while(1), or even for(;;) but I like to have a boolean value that I can use so that if I ever want to end the program I can just set it to false and the program ends.

Pseudo-code:

boolean running = true;

while(running){
   switch(state){
      // etc. etc. for the state machine.  
      // if you want to end the program somewhere then set running to false
   }
}

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