• Post Reply Bookmark Topic Watch Topic
  • New Topic

Simulating a duel with objects and classes  RSS feed

 
Collin Sampson
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, I have an assignment in beginning java in which 3 guys (Aaron, Bob, Charlie)shoot at each other until 1 is left standing.
Stipulations:
Aaron shoots first and hits with 33% accuracy.
Bob shoots second and hits with 50% accuracy.
Charlie shoots last and hits every time.
Each shooter aims for the best shot (so Aaron and Bob would shoot at Charlie if he was still alive).

We have not yet studied inheritance and I'm a beginner so go easy on me.
Here is what I have. I have not yet included a main method and I've been working at this for too long...please help me out.

import java.util.*;



The last method is giving me an error, which I understand, but I don't see any other way to do it.
Should each shooter have his own class? How can I check to make sure at least two people are alive after each round?

 
William P O'Sullivan
Ranch Hand
Posts: 859
Chrome IBM DB2 Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes.

You shoot create a Shooter Class, with a minimum of 3 attributes.

name
accuracy
health (stillalive)
etc..

WP
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can shorten those methods; look at these conventions for a hint how to do it.
you should never use == true or == false or similar. Miss out == true throughout, and use the bang operator ! instead of == false.
There is something very wrong with a design where a class has details of potential objects. The class should never know that its instances might have names like Charlie. Even less when those names are in other objects. You need to think again about those methods; that is something which ought to be implemented from outside the class.
Accuracy of shooting is something which should be inside the class. As you have been told, accuracy is a field of the class, and the shoot method uses that field. Those fields should not be static, otherwise each duellist would have the same number of lives, and all would die together.

I actually think you need to go back to the drawing board. Create the class, as you have already been told. Create rules for how the Duel class would run the duel. Don’t code anything until those rules are working all right.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!