Text Based Game Progress/Control

Hello, I am trying to develop a text based Java game. (my VERY basic main and player class are at the bottom of this post, no more than 20minutes of coding)

Here is what I do not understand:

How does the game flow?

So the game runs and the menu is displayed, but how do I progress the game? I know it’s done through do while loops of if the game is running true/false but I don’t know much further than that! Here is what I am trying to achieve:

Example

Game Loads
Menu Loads
User enters: 1
Welcome Message
Ask user name
Take user name input
Create player object from name input
Code is executed which determines whether a random event happens or not (i.e the player gets 20exp bonus)
If no random event happens:
Player goes to the gym where I will do calculations, give them exp for reps (which are randomly calculated)
After that he goes home
Next day, [random event][gym][home]

How would I program this flow? I’ve seen examples of people shoving everything into the main method but I don’t want to do this.

Any tips? How could I get the program to flow like that?

Hope I’ve explained this well, I am struggling to get my head around these concepts!

Here is my code so far:

Main Class

import java.util.*;


public class Game implements Runnable{
	/*
	 * VARIABLES
	 */
	public boolean running;
	
	
	public void start() {
		running = true;
		new Thread(this).start();
	}
	
	public void stop() {
		running = false;
	}
	
	public void run() {
		menu();
	}

	public static void main(String args[]) {
		Game game = new Game();
		game.start();
	}
	
	public static void menu() {
		System.out.println("<--------------------------------------------------------------->");
		System.out.println("\t\tWelcome to the Adventure");
		System.out.println("\t\t[1] Start Game");
		System.out.println("\t\t[2] Credits");
		System.out.println("\t\t[3] Exit");
		System.out.println("<--------------------------------------------------------------->");
	}
	
}

Player Class



import java.util.*;


public  class player extends Game{

	/*
	 * Player attributes:
	 * Name
	 * Weight
	 * Morale
	 * Money
	 */
	
	
	private String name;
	private int weight;
	private int morale;
	private int money;
	private int exp;
	
	
	public player(String sName, int iWeight, int iMorale, int iMoney, int iExp) {
		
		name = sName;
		weight = iWeight;
		morale = iMorale;
		money = iMoney;
		exp = iExp;
	}
	

	//GET AND SET NAME
	public void setName(String sName) {
		 name = sName;
	}
	public String getName() {
		return name;
	}
	
	//GET AND SET WEIGHT
	public void setWeight(int iWeight) {
		 weight = iWeight;
	}
	public int getWeight() {
		return weight;
	}
	
	//GET AND SET MORALE
	public void setMorale(int iMorale) {
		 morale = iMorale;
	}
	public int getMorale() {
		return morale;
	}
	
	//GET AND SET MONEY
	public void setMoney(int iMoney) {
		 money = iMoney;
	}
	public int getMoney() {
		return money;
	}	
	
	//GET AND SET EXP
	public void setExp(int iExp) {
		 exp = iExp;
	}
	public int getExp() {
		return exp;
	}	
	
}

If you are just looping through (random event) -> (Gym) -> (home) you can do it with something as simple as:


while (continueGame) {
  if (Math.random() < randomEventChance) {
       selectRandomEvent();
  }
  else {
       processGymEvent();
  }
  gotoHome();
}

On an unrelated note, why are you running your code inside a thread. Are you thinking of having other threads running as well? In a text game like this, it doesn’t seem like you would.

The Game Loop is really for games that either need some form of time simulation or some form of rendering. If it’s a purely console based game that is just using a command prompt each time, then it’s like that you’re not doing either.

What you should look into is the idea of a State machine. Basically, you define your set of inputs, then you figure out the different states of your program (Which you’ve already done) and figure out how the inputs move you through these states.

That was just confusion on my part, removed the threads.

So I’ve coded my random events class.

How do I get it to interact with my game?

import java.util.*;


public class Game implements Runnable{
	/*
	 * VARIABLES
	 */
	public boolean running;
	
	
	public void start() {
		
	}
	
	public void stop() {
		running = false;
	}
	
	public void run() {
		menu();
	}

	public static void main(String args[]) {
		Game game = new Game();
		game.start();
		
	}
	
	public static void menu() {
		System.out.println("\t\tWelcome to the Adventure");
		System.out.println("\t\t[1] Start Game");
		System.out.println("\t\t[2] Credits");
		System.out.println("\t\t[3] Exit");
		System.out.println("<--------------------------------------------------------------->");
	}
	
}

And then I have my class: RandomEvent.java which Extends Game, and contains my code to generate random events

But if I do:

RandomEvent.generateEvent();

It throws back:

 Cannot make static reference to a non-static method generateEvent from the type RandomEvent 

I’ve run into this problem in the past, and if I remember correctly do I need to do something along the lines of:

 RandomEvent event = New RandomEvent();

and call event.generateEvent(); ?

So after this.

I do:


else {

     gymEvent();
}
goHome();
}

Then constantly loop this cycle? Thanks a lot for both of your input!

If you are new to Java programming, I would really scale this back. I would get rid of the threads, since you don’t need it for this kind of a game. They are just going to add unnecessary head aches. You’ve got a good foundation here, though.

Since you have a small amount of options that can result from the random event, I would suggestion making a method for each possible event (probably just in the main method, or even an Event() class), and rolling a random number, which then gets filtered into a switch statement to determine which event is called:

[b]int randomEvent = (int)(Math.random() * 5); // this will generate a number between 0 and 4

switch(randomEvent)}

case 0:
gymEvent();
break;

case 1:
goHome();
break;

default:

}[/b]

Then, your event methods like gymEvent() would print whatever is necessary to the command line and read/modify player attributes as necessary.