aspose file tools*
The moose likes Beginning Java and the fly likes Wanted help in the war game code Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Wanted help in the war game code" Watch "Wanted help in the war game code" New topic
Author

Wanted help in the war game code

Deepakk Verma
Ranch Hand

Joined: Sep 09, 2009
Posts: 31
I am writing this code for a simple number war game between India and Pakistan.
In this game, i made two class "India" and "Pakistan". after that i declared 3 variables namely
Land,
Army Strength,
and Population
after that i made two constructors for them
1) Default
2) to instantiated the three fields
then, in the WarBegan class i started the main member(where all the real action takes place).
I made two objects indianArmy and pakArmy, and made the pakArmy ask the user(who is considered as the indian) to surrender kashmir....now the real problem arises

What should happen is
if user enters yes:
then pakArmy should say "You are a FOOL"
if user enters no:
then pakArmy should say"Then be prepared for war"

What is happening is
if user enters yes:
then pakArmy says "Then be prepared for war"
if user enters no:
then pakArmy says "Then be prepared for war"


i Know that there can be other possibilities but i am just focusing on these two i.e., YES and NO only as this is my 12th program in java and i need to learn the basics first

here is the code



After this i want the pakArmy to execute an attack() member which should result in the subtraction in indianArmy's armyStrength field, and likewise the game will begin

but for now i am stuck with this code........I know this is a very basic question to ask but it is the basic only that i have to make strong
Your's Thankfully,
Deepak Verma
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39408
    
  28
There is no such thing as a simple war between India and Pakistan

You don't want classes called India and Pakistan. They are not "classes" but "instances." You need a class called Country and create instances called india and pakistan (smal letters usual for instance names).
Get rid of the no-arguments constructors; they do nothing useful.
You are trying to compare Strings with the == operator. You should have been taught that the == operator only gives reliable results on primitives (and then maybe not on doubles and floats). If you look at the String class you will find two, maybe three, methods which check equality. They will probably help.
leroy tsruya
Ranch Hand

Joined: Sep 24, 2009
Posts: 57
when i saw the title of your question i thought this question should be under game programming forum.
when i checked your question i couldn't help but ask myself. "is that a joke"?
then i saw you actually have a code...
so first, it is a bad design to have class for pakistan and india while they are exactly the same.
i suggest do it that way:

and just declare 2 instances:
Army india = new Army(...);
Army pakistan = new Army(...);

now, if there are different methods for each army, declare Army as an abstract class, and subclass India and Pakistan:
Janeice DelVecchio
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1687
    
  12

[EDIT] I didn't read Campbell's reply fully until AFTER I wrote my response.... I posted quickly because I was excited about knowing an answer for once. Oh well.... good practice

I see the problem with your code. It's a pretty typical one I think, cause I made it too.



Look at the API for String..... it has lots of methods for you to use.

Also, remember that the user might be typing in lower or mixed case.... so there might be an easy way in that API to change it all to upper case.

Personally I would change it to something like this:



.... and as a sidenote I would use a class "Country" and create separate instances for india and pakistan like Campbell said.

Have fun, but don't start WWIII please
Janeice


When you do things right, people won't be sure you've done anything at all.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39408
    
  28
Your posts are always helpful, Janeice.

Why haven't you got an enemy field in the Country class? The Pakistan can be rude to India and India can be rude to Pakistan, using the enemy names in the Country class.
John de Michele
Rancher

Joined: Mar 09, 2009
Posts: 600
Deepakk:

Lines 56-65 should probably be changed to something like this:


Of course, giving peace a chance might work better .

John.
Janeice DelVecchio
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1687
    
  12

The problem here is there's no right answer:



I propose the following:


**sigh** if only real life was that easy.

Janeice
Deepakk Verma
Ranch Hand

Joined: Sep 09, 2009
Posts: 31
Thank you gentlemen for all your wonderful suggestions.........here is the program i came up with atlast.....



This is a very interesting coding experience with all you ranchers....lets make a nice game on this forum. It really feels great while coding in a group,i was not expecting that i would be able to overcome this error in today itself......Thanks to You all and I would like to request all of you to give more of your valuable suggestions on this program.
Your's Thankfully,
Deepak Verma
Janeice DelVecchio
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1687
    
  12



... but not all of us are gentlemen

--Janeice
John de Michele
Rancher

Joined: Mar 09, 2009
Posts: 600
Deepakk:

Lines 58-70 don't make sense. Can you ever have a situation where the answer is "YES" and "No"? Why would answering "yes" not be in English?

John.
Deepakk Verma
Ranch Hand

Joined: Sep 09, 2009
Posts: 31
You are right john.....i changed it but still not able to make it work



As soon as i enter NO in place of executing the gotAttacked() member the program stops
Janeice DelVecchio
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1687
    
  12

all right..... I was joking about the Konami Code..... you should google it. The (obviously region specific) reference I was making was that there should be some way to win the game without going to war.

But there should be somewhere that the computer can tell the user to enter a valid respone. What if they have a typo or something?



Janeice
Janeice DelVecchio
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1687
    
  12

..... and I knew all this boy-war-game business would start World War Three
Deepakk Verma
Ranch Hand

Joined: Sep 09, 2009
Posts: 31
Janeice DelVecchio wrote:..... and I knew all this boy-war-game business would start World War Three

ya .....but you only passed this World War III idea to me ...... off course unintentionally.
Deepakk Verma
Ranch Hand

Joined: Sep 09, 2009
Posts: 31

AtLast we did it.......without help of you all guys...i wont have been able to complete this.
All right guys enough coding for tonight.........catch you tomorrow with the same World War III to add some more nations and action
Ba-bye and thanks
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39408
    
  28
No you haven't done it; your inheritance design deserves World War III.

Don't make India extend Country, nor Pakistan. India is an instance of the country class.You will probably need some more fields, so you actually write
Fred Hamilton
Ranch Hand

Joined: May 13, 2009
Posts: 679
You might want add some interfaces here, such as Muslim, Christian, Sikhism, Buddhism etc. These interfaces might help to differentiate the behavior of the warring nations. Also if Country was an abstract superclass, you could have different concrete subclasses such SuperPower, MinorPower, etc. And your different countries could be instances of one or the other of these subclasses. Some instances of class Random for the occasional terrorist act or natural disaster. The United Nations could be a separate class of its own which loops endlessly in a low priority thread, making worthless pronouncements via method calls to an abstract data type which forces the concrete nations to implement pain-in the ass concrete implementations of useless methods that have no code in the body anyways
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39408
    
  28
Do you want me to move this thread to Meaningless Drivel, Fred?

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39408
    
  28
Please don't use such long lines; they are wider than the screen and very difficult to read.
Fred Hamilton
Ranch Hand

Joined: May 13, 2009
Posts: 679
Nothing like a little computer programming humor. It's all to rare. As for the long lines, I'm not sure who you were addressing, but it wouldn't have been me. As I didn't use code tags. . .... hmmm .... My guess it was all Janeice's fault.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39408
    
  28
No, neither you nor Janeice has inflicted long lines on us. Of course, I could try reading the posts in our newly-refurbished lab here; it is full or 1920-px wide screens, in pairs left and right in case anybody wants a realllllllllllllllllllllllly long line!
Janeice DelVecchio
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1687
    
  12

All my lines get word wrapped..... even when in code tags. **shrug**

While I'm here in this thread, I was thinking this game would be better if it was about unicorns and butterflies

Janeice
Fred Hamilton
Ranch Hand

Joined: May 13, 2009
Posts: 679
Janeice DelVecchio wrote:All my lines get word wrapped..... even when in code tags. **shrug**

While I'm here in this thread, I was thinking this game would be better if it was about unicorns and butterflies

Janeice


They may wrap within code tags when you are in the edit window, but when you click "post" they don't wrap. That's the point of code tags. It's kind of like the tags in html, which preserves white space formatting that HTML would normally ignore.

Duly noted about unicorns, though I'd prefer "...Butterflies and zebras, Moonbeams and Fairytales..." (@ jimi Hendrix)
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39408
    
  28
No more digressions, please, however much better they might be than the original thread topic
Janeice DelVecchio
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1687
    
  12

I was actually thinking that this application could go two ways:

1) like a choose your own adventure storybook (with lots of if/thens and switch statements)
or
2) like a role playing dice game (with a rollDice() method)

I think it would be easier to make the first one, but the second one would be more fun to play.

Janeice
Fred Hamilton
Ranch Hand

Joined: May 13, 2009
Posts: 679
Janeice DelVecchio wrote:I was actually thinking that this application could go two ways:

1) like a choose your own adventure storybook (with lots of if/thens and switch statements)
or
2) like a role playing dice game (with a rollDice() method)

I think it would be easier to make the first one, but the second one would be more fun to play.

Janeice


Yeah, I prefer option 1 too. I suppose we might have a Scenario class, where each instance of this class might represent
a particular "state of affairs" for the Indian subcontinent, or the world, or some imaginary adventure land.
Appropriate use of a Random object might introduce variety into the game. Then each country object would use information
from the current scenario object to determine its course of action.

You know, being programmers at heart, if we did this right we wouldn't have to play at all, we could just sit back
and admire our handiwork as our computers duked it out. ( Sorry Campbell, couldn't resist ).
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39408
    
  28
Fred Hamilton wrote: . . . sit back . . . as our computers duked it out. . . .
At least that way no real people get hurt
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Wanted help in the war game code