• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Game Knight vs Monster Using Java

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello guys,

i am a newbie programmer and want to tryng develop a Knight vs Monster Game using java and i am having trouble as to how to make it so fighting between monster and knight. Which classes and method should i be using in order to approach this problem ?

 
 
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

I shall add this question to our games forum. Games programming is a science in its own right, which I know little about. Before you write any code, you should work out the logic of the application (here, you call that the rules of the game). Please show us what the rules are. That way you know you are programming code to match the rules.
A few comments about your code:-
  • 1: It is good to see somebody who knows how to format code well
  • 2: I can see similarities between the two classes. Consider whether to make them subclasses of an abstract superclass.
  • 2: Your comments look good; they tell readers something they don't know. But I think you should add documentation comments so users know what the methods all do.
  • 3: Why have you got those setXXX() method?
  • 4: Be careful about parameter names. Prefer this:-
  • to this:-It has the advantage that you are “exposing” the good variable name which you have spent so much time choosing carefully. The same applies to setXXX() methods.
  • 5: What does the predicate in line 113 mean? It looks as if you had meant to write if(...) and forgotten the if.
  • 6: Move all that code out of the main() method into a Game#play() (or similar) method. The ideal length of a main() method is one statement.
  • 7: I prefer printf() to using the + operator and println. I don't use \n, but printf() and %n. I like to join multiple print instcutions into one larger instruction.

  • Please use the code button for all code. Since you are new, I applied the code button to your code, and doesn't it look better now
     
    Campbell Ritchie
    Marshal
    Posts: 79179
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    More comments:-
  • 8: Why have you got a Knight field (line 63)? You don't appear to use it. Why did you declare it as type Object? The name of a field should be spelt knight.
  • 9: Why are you using random.nextInt() in some places and Math.random() elsewhere? I would prefer random.nextInt() everywhere. Use the same Random object throughout; you usually don't need multiple Random objects.
  •  
    Rancher
    Posts: 5008
    38
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Also posted at: https://www.dreamincode.net/forums/topic/418974-hero-vs-monster-program-with-java/
     
    Campbell Ritchie
    Marshal
    Posts: 79179
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    OP: Please read this.
     
    Something about .... going for a swim. With this tiny ad ...
    a bit of art, as a gift, that will fit in a stocking
    https://gardener-gift.com
    reply
      Bookmark Topic Watch Topic
    • New Topic