• 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
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

Are for/doWhile loop suitable for this?

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone, I'm new here and I'm trying to practice java by doing some kind of terminal text game. My problem is that I want to establish a turn-combat system with the following scheme:

---->method of the enemy is called
*Show player the option to fight or run away
*Picks fight with "f"

From now I want this in a loop until player or enemy hp is <=0:
--------------------------------------------------
*Shows to options (Attack/Inventory)
*Let's say we pick Attack
*A random number controls the turn
*You take a portion of the enemy's life
*Then enemy attacks you
---------------------------------------------------

I will show you a code example



I tried several ways but I found two major problems:
1. I can't include a switch inside another so I should call a method inside the case like:
?
2. I tried for and do while to loop and achieve to substract the damage from the players life, but once, after the first substraction it shows the same number over and over. The damage will change depending on another random number, substracting a different number from players life until 0.

Thanks in advance hope I was clear!
 
Sheriff
Posts: 6033
157
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would, in general, answer yes to question one.  Calling a method from a switch case is a good strategy.

For question 2, I would have to see an example of what you did to comment on it.  Except that a for/next loop when you don't know how many iterations the loop is going to make, is not recommended. A while or do/while is better.
 
Carlos Roma
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Knute Snortum wrote:I would, in general, answer yes to question one.  Calling a method from a switch case is a good strategy.

For question 2, I would have to see an example of what you did to comment on it.  Except that a for/next loop when you don't know how many iterations the loop is going to make, is not recommended. A while or do/while is better.



Edit: I think this suits my needs, I found the answer!



Thanks!!
 
Bartender
Posts: 5906
57
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is an infinite loop. 'i' is set to '10' and 'i' is compared to '0'. Nowhere in the loop do you change the value of 'i'.
 
Bartender
Posts: 2294
95
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Adding to what Carrey pointed out, why do you want to create a new Random Object every time ?
Should not be a part of the loop.
 
Carlos Roma
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

salvin francis wrote:Adding to what Carrey pointed out, why do you want to create a new Random Object every time ?
Should not be a part of the loop.



I didn't noticed it, I picked it from another forum. It is certainly not necessary, thanks!

Carey Brown wrote:This is an infinite loop. 'i' is set to '10' and 'i' is compared to '0'. Nowhere in the loop do you change the value of 'i'.  



Yes, I know, that example tried to show that, but I found useful how it managed the variables inside of the loop. That gave me a clue on how to achieve what I was asking! Thanks
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!