This week's book giveaway is in the Java in General forum.
We're giving away four copies of Event Streams in Action and have Alexander Dean & Valentin Crettaz on-line!
See this thread for details.
Win a copy of Event Streams in Action this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

Redirecting from a default switch

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I have this code from a java text adventure I'm making and I would like to know how to redirect the player from de default case to the beginning of the question after showing him the warning msg.



I know this can be made turning every decision into a method but that sounds crazy to me, I would have to make a lot of methods. The other thing I'm thinking about is making a do while loop in every decision with the following condition:



What do you think? Take in count that I only want to nest 3 or 4 switches, one into another.

Thanks in advance!
 
Marshal
Posts: 65039
247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It s better to have a simple method call after each case xyz: than to write all the code inside the switch. You can wrap the switch in a loop
But method names like queenclaw and chapter1frontier are ot at all clear to read.
 
Campbell Ritchie
Marshal
Posts: 65039
247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'll let you find my spelllling mistake. It may be better to use nextInt() for keyboard input, and make the switch take an int as its variable. Also, I think the variable in line 1 must be initialised immediately if you use var.
 
Carlos Roma
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:It s better to have a simple method call after each case xyz: than to write all the code inside the switch. You can wrap the switch in a loop
But method names like queenclaw and chapter1frontier are ot at all clear to read.




For this kind of projects, having a method for every decision case isn't madness? It's a hell of redirection isn't it? Thanks a lot for the solution, it works like charm!
 
Campbell Ritchie
Marshal
Posts: 65039
247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carlos Roma wrote:. . . a method for every decision case isn't madness?

No. That would be the right thing to do. Having a large block of code makes the code difficult to maintain.

It's a hell of redirection isn't it?

No. You are only redirecting one method for each option.

Thanks a lot for the solution, it works like charm!

That's a pleasure
 
Carlos Roma
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Well, then 207 lines of code for enemy seems like too much hahaha, I tried to do it with methods but I wanted every combat to be personal so... is going to be a ride. Thanks again
 
Campbell Ritchie
Marshal
Posts: 65039
247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carlos Roma wrote:. . . 207 lines of code for enemy seems like too much hahaha, . . .

That makes me suspect there is a problem with your design of methods or of inheritance hierarchies.
 
Carlos Roma
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But, then, let's say I have 3 enemies made. And your probability to run from them is different in every case (if you pick run in the combat options), it's preferable to have 3 methods called runEnemy1()... And then adress them to every eneme main code?
 
Rancher
Posts: 4177
47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In that case you define an Enemy class that has whatever attributes are needed.
If it's just a probability then that strikes me as fairly simple.

Since this is an adventure game I would, to be honest, expect most of this to be defined in other files that the game loads up into classes.
 
Carlos Roma
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Dave Tolls wrote:In that case you define an Enemy class that has whatever attributes are needed.
If it's just a probability then that strikes me as fairly simple.

Since this is an adventure game I would, to be honest, expect most of this to be defined in other files that the game loads up into classes.



It's a probablity calculated with this formula: (throw of a random number from 0 to 2)+(player strenght/enemy strenght)
If the sum is >= 2, you ran, otherwise not. The higher your strenght and the lower the enemy, more sure you can be of running-. The random number is set to give a chance even at lower levels.

Also, I do have an "Enemies" class with his attributes, and an "Objects" class.
 
Campbell Ritchie
Marshal
Posts: 65039
247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please explain that formula a little more. How can you have a probability > 2?
Did you say Enemy class or Enemies? The usual convention is to use singular words (Enemy/Object) and reserve the plurals (Enemies/Objects) for utility classes. But don't call anything Object or Objects, becuase classes with those names already exist. You might have an Enemy class with subclasses like Kilngon, Dalek and Skrane, in which case you can use those as parameters to your attack() method.
 
Carlos Roma
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Please explain that formula a little more. How can you have a probability > 2?
Did you say Enemy class or Enemies? The usual convention is to use singular words (Enemy/Object) and reserve the plurals (Enemies/Objects) for utility classes. But don't call anything Object or Objects, becuase classes with those names already exist. You might have an Enemy class with subclasses like Kilngon, Dalek and Skrane, in which case you can use those as parameters to your attack() method.



It's not a probability is just the way I tried to express it because I'm not native and I didn't find the word, sorry. Is just that, when you enter in combat with a certain enemy a division between your strenght and his is made and then a number between 0 and two is generated.
Lets say player have 10 of strenght and enemy have 50, that's 0,2... So, these are the things that can happen:
-Random number is 0, 0 + 0,2 < 2, you can't run
-Random number is 1, 1 + 0,2 < 1,2, same
-Random number is 2, then you can run.

If enemy have more strenght than you, you have a little chance to run. 1 of 3. Is probably too primitive but i'm more concerned about other things right now.

Btw, thanks to you I ordered my code and reduce it a lot, like you said, I had a lot of misconceptions about inheritance and method desgining. I was making an individual combat for every enemy (object) and that was chaos, now I have my enemies and I call a combat() method.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!