Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (497)
Games in Android Showcase (114)
games submitted by our members
Games in WIP (563)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Text Based Game Progress/Control  (Read 1048 times)
0 Members and 1 Guest are viewing this topic.
Offline flosser

Junior Newbie





« Posted 2012-06-17 01:15:58 »

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
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
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
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
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;
   }  
   
}
Offline actual

JGO Coder


Medals: 23



« Reply #1 - Posted 2012-06-17 03:19:19 »

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

1  
2  
3  
4  
5  
6  
7  
8  
9  
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.
Online UprightPath
« Reply #2 - Posted 2012-06-17 03:51:27 »

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.

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline flosser

Junior Newbie





« Reply #3 - Posted 2012-06-17 13:43:36 »

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

1  
2  
3  
4  
5  
6  
7  
8  
9  
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.


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?

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
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:

1  
RandomEvent.generateEvent();


It throws back:

1  
 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:

1  
 RandomEvent event = New RandomEvent();


and call event.generateEvent(); ?



So after this.

I do:

1  
2  
3  
4  
5  
6  
7  
8  
else {

     gymEvent();
}
goHome();
}

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

Junior Newbie





« Reply #4 - Posted 2012-06-19 21:31:46 »

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:

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:
     ...
}


Then, your event methods like gymEvent() would print whatever is necessary to the command line and read/modify player attributes as necessary.
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

BurntPizza (19 views)
2014-09-19 03:14:18

Dwinin (34 views)
2014-09-12 09:08:26

Norakomi (62 views)
2014-09-10 13:57:51

TehJavaDev (84 views)
2014-09-10 06:39:09

Tekkerue (42 views)
2014-09-09 02:24:56

mitcheeb (64 views)
2014-09-08 06:06:29

BurntPizza (47 views)
2014-09-07 01:13:42

Longarmx (35 views)
2014-09-07 01:12:14

Longarmx (39 views)
2014-09-07 01:11:22

Longarmx (36 views)
2014-09-07 01:10:19
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!