12 Replies - 1569 Views - Last Post: 13 December 2012 - 07:43 PM Rate Topic: -----

#1 maklo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 11-December 12

Help with text based game

Posted 11 December 2012 - 01:49 AM

Hello i'm new here and i have started learning java recently by reading online tutorials and I am in the middle of making my own text based game and i have a few questions in mind:
I am writing it in eclipse and i was wondering weather i could simplify it by not having to repeat each line after each line. Also if it is possible to step back a level after you die, or have it take you further forward into the game by skipping some steps.
System.out.println("You have woken up on an island and see a river to your right and a jungle to you left."+"\n"+"Type right to go to the river, or type left to go to the jungle,");
		choice =user_in.nextLine();
		
		
		if(choice.equals("right")){
			System.out.println("You come to the river, but the edge is unstable and you fall in and get washed away."+"\n"+"THE END.");
			} else if(choice.equals("left")){
			System.out.println("You have come to the jungle. You do see an old path going throught the jungle, a path going around to the left to a town and one to the right heading for the mountain."+"\n"+"Type jungle to go through the jungle, or town to go to the left, or right to go to the mountain.");}
			else {System.out.println("That was not valid");
			}
		choice =user_in.nextLine();
		
		if(choice.equals("right")){
			System.out.println("You walk towards the mountain and see an old tunnel to the left there is also a path to the right heading up over the mountain."+"\n"+"If you wish to go over the pass and brave the storms type right but if you choose to go into the tunnel type left");
			} else if(choice.equals("left")){
			System.out.println("As you walk towards the town you see it has strong defences and lots of guards, you call out to them but all they answer with is a cloud of arrows."+"\n"+"THE END");}
			else if(choice.equals("jungle")){
			System.out.println("You walk along this path for some time until you spot a small camp off in the distance."+"\n"+"If you wish to enter the camp type camp, but if you wish to keep on walking on the path pass the camp, type path.");}
			else {System.out.println("That was not valid");
			}
		choice =user_in.nextLine();


I would also like to have items that i could have in an inventory as such and weapons, not sure if this is possible or not.

Also i would like to know how to export this, i'm not sure what file you could export this and what is involved.

thankyou

Is This A Good Question/Topic? 0
  • +

Replies To: Help with text based game

#2 LittleGyros  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 14
  • Joined: 05-January 10

Re: Help with text based game

Posted 11 December 2012 - 04:18 AM

Hi Maklo,

you could create a class "Quest", which defines a question and several answers. It also could have an function which wants one of the answers and gives you a number.
Maybe something like: -1, 0 or +1
Depending on this number you could go one level forward or one level backward or even don't move.
You would have to initialize every quest ones and save them into an array or list, which would simplify your access to them. And it would be another challenge to load the quests out of a text file.

i can imagine that your code could look like this:
int currentLevel = 0;
String choice;
while(true)
{
   quests[currentLevel].printQuest();  //question gets printed here

   choice = user_in.nextLine();

   if(choice.equals("exit"))
      break;

   int levelProgress = quests[currentLevel].answerQuest(choice); //question gets answered here

   currentLevel += levelProgress;
   if(currentLevel < 0)  //game over
      break;

   if(currentLevel >= maxLevel)  //user won
      //do something...
}



This way you can skip several levels forward and backward and move your user where ever you want.

The inventory is actually pretty easy, too. Just create a class "Item" and fill it with some attributes you need. Afterwards you need something to store those items and i would suggest you just use a list. You add or remove the items to the list and that's it. ;)/>

hope i could help
Was This Post Helpful? 1
  • +
  • -

#3 LittleGyros  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 14
  • Joined: 05-January 10

Re: Help with text based game

Posted 11 December 2012 - 04:30 AM

Oh sorry,

i didn't mention that you would have to create something like a question map

0 = start // the quest of the very first quest could change your level to 1 OR 6 depending on the answer...
1 = left way
now those are "subQuests" of left way(1)
2 = left way
3 = right way
.
.
5 = tunnel // because of no "subQuests" maybe dead OR => the tunnel brings you to level 10 directly
6 = right way
(subQuests)
7 = camp
8 = some old women selling strange things
.
10 = the gold chest


I know it looks a bit complicated and maybe too much work, but this is how games are created. You have to think about every single small little tiny step in your game and think of the story and how the user interacts and whatever you can think of.
Was This Post Helpful? 0
  • +
  • -

#4 maklo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 11-December 12

Re: Help with text based game

Posted 13 December 2012 - 01:17 AM

Ok i understand most of it so far, just when you mean put it into the number section for each one like:
1 start
2 left from start
3 right from start
4 left from 2
5 right from 2
left from 3
ect..

how would i build this in i have had a few goes and a search around but i am having trouble setting it up, cheers for helping so much!
Was This Post Helpful? 0
  • +
  • -

#5 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 8922
  • View blog
  • Posts: 33,445
  • Joined: 12-June 08

Re: Help with text based game

Posted 13 December 2012 - 08:11 AM

Typically I would (and have) use a 2-d integer array. The number at coordinate X,Y would indicate something specific.. like say an immoveable tree, free space to walk, a door, etc.

After a while I then revamped everything and made a more complex custom node object and had my array be 2d of that custom node.
Was This Post Helpful? 0
  • +
  • -

#6 LittleGyros  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 14
  • Joined: 05-January 10

Re: Help with text based game

Posted 13 December 2012 - 04:07 PM

Well,

You would have to write them down first, so you have them fixed somewhere.
After that you know how many "quests" you got and then create an array which is exactly that size.

I'll try to format it better this time:

  • [0] StartQuest
  • [1] Left way
  • [2] Right way
    • [3] Tunnel
    • [4] Dark forest

  • [5] Well
  • [6] Abandoned village
    • [7] Abandoned house

  • [8] Another path
    • [9] Graveyard
    • [10] Old woman

  • [11] End of game


Well if this would be your layout, you would create an array which has a size of 11 Quests.

What is happening:
You start at level 0, hence your quest will be StartQuest.
You ask the question and wait for the user's input. He/She could choose between the Left or the Right Way.
The Left way has no "SubQuests" so it can only bring you to the next "MainQuest" which would be the well.
The Right way has 2 "SubQuests", which both also lead the well, but it would be a longer way to get there (The user doesn't know this of course).
As soon you reached the 5th level(Well), you ask the question of the 5th quest. (Hint: quests[5].printQuest(); where quests is your array with your quests). There he/she can decide between 2 different ways, again.
Level 6 leads you to an abandoned house and level 8 leads you to a graveyard or to an old woman, but BOTH finally lead you to the end of game(level 11).

How is it happening:
Well, the answer of this question is actually up to you, but I'm going to show you how it would work in this demonstration.
Every progress in level is depending on the answers you give. If you answer Left way in the starter quest, your progress will be 1 (Total: 1).
But if you answer Right way your progress will be 2(Total: 2).

For example you would be at level 5.
If you choose the abandoned village, your level progress will be 1(Total: 6) and the "Another path" would progress the level by 3(Total: 8).

IMPORTANT:
The variable level doesn't symbolize anything in your game. It would be just a variable which indicates at which position you are (It is just a variable with which you can access your array of quests pretty easy). You have to think about the level-progress of each quest, because this is the part where you have control over the gameflow. You would have to test every single path, to see if everything is working the way it was supposed to.

I hope this will help :D

ps: me not speaking english nativly. please no hurts me.
Was This Post Helpful? 2
  • +
  • -

#7 LittleGyros  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 14
  • Joined: 05-January 10

Re: Help with text based game

Posted 13 December 2012 - 04:21 PM

View Postmodi123_1, on 13 December 2012 - 08:11 AM, said:

Typically I would (and have) use a 2-d integer array. The number at coordinate X,Y would indicate something specific.. like say an immoveable tree, free space to walk, a door, etc.

After a while I then revamped everything and made a more complex custom node object and had my array be 2d of that custom node.


This would be a perfect start for a 2D game. But a text based game wouldn't work with that :sweatdrop:
Was This Post Helpful? 0
  • +
  • -

#8 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7564
  • View blog
  • Posts: 12,685
  • Joined: 19-March 11

Re: Help with text based game

Posted 13 December 2012 - 04:39 PM

For a zorkalike, I would use a graph structure for each location. A location is a node which ties into some other nodes. Easiest way to do this is probably a map of "exits" which each location owns. What you map to is an interesting question. You can use strings, "North", "South", and so forth, but if you're representing the language a little more deeply you might have Direction objects, which could also be keys in your map.

Some fun design decisions to make here.
Was This Post Helpful? 0
  • +
  • -

#9 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 8922
  • View blog
  • Posts: 33,445
  • Joined: 12-June 08

Re: Help with text based game

Posted 13 December 2012 - 06:48 PM

Quote

This would be a perfect start for a 2D game. But a text based game wouldn't work with that

I would disagree. A textbased game is just one that isn't shown pictures ... that doesn't preclude it from using useful array maps when making levels and verifying your map is spatially correct.
Was This Post Helpful? 0
  • +
  • -

#10 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7564
  • View blog
  • Posts: 12,685
  • Joined: 19-March 11

Re: Help with text based game

Posted 13 December 2012 - 07:07 PM

I'm not sure I see the sense in an array-based mapping for a zorkalike. It seems to me to present more complications than it resolves. What are the advantages?
Was This Post Helpful? 0
  • +
  • -

#11 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 8922
  • View blog
  • Posts: 33,445
  • Joined: 12-June 08

Re: Help with text based game

Posted 13 December 2012 - 07:12 PM

Typically - for me - I can see everything laid out in say a level maker... it helps check against spatially distorted movement.. I don't know - probably just an idiosyncratic hold over from d&d and graph paper.
Was This Post Helpful? 0
  • +
  • -

#12 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7564
  • View blog
  • Posts: 12,685
  • Joined: 19-March 11

Re: Help with text based game

Posted 13 December 2012 - 07:38 PM

See, my first thought was, now you're stuck in ordinary flat planar graphs, which makes your mazes no fun at all... :)
Was This Post Helpful? 0
  • +
  • -

#13 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 8922
  • View blog
  • Posts: 33,445
  • Joined: 12-June 08

Re: Help with text based game

Posted 13 December 2012 - 07:43 PM

Pffsst.. like I can't have multiple arrays with depth..
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1