Win a copy of Node.js Design Patterns: Design and implement production-grade Node.js applications using proven patterns and techniques this week in the Server-Side JavaScript and NodeJS 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Game Knight vs Monster Using Java

 
Greenhorn
Posts: 2
  • 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: 74085
332
  • 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: 74085
    332
    • 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.
  •  
    Master Rancher
    Posts: 4465
    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: 74085
    332
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    OP: Please read this.
     
    Oh, sure, you could do that. Or you could eat some pie. While reading this tiny ad:
    Thread Boost feature
    https://coderanch.com/t/674455/Thread-Boost-feature
    reply
      Bookmark Topic Watch Topic
    • New Topic