writing a program to calculate losses and victory in a war game for social media
i'm using c# since thats what i know alongside JS, and im attempting to convert string values to an integer for arithmetic to work. it aint doing it, and what ive tried from my documentation hasnt worked.
im working on it based off this set of rules:
1 tank does 2000
1 troop does 50
1 artillery does 5000
if a tank or troop is on the offensive, they lose 25% of their hit points,
if a tank or troop is on the defensive, they gain 25% more hit points.
artillery cannot be on the offensive, and is locked to defensive.
1 tank has 4000 hit points, 5000 when on defense, 3000 when on offense
1 troop has 100 hit points, 125 when on defense, 75 when on offense
1 artillery has 250, as it is locked to defensive
thank you!
46 Replies
(whoops.. i think i randomized the filenames!) heres another one
Please use $paste
If your code is too long, you can post to https://paste.mod.gg/ and copy the link into chat for others to see your shared code!
Mobile devices need to download the files to see the content.
And to parse as an integer, use $tryparse
When you don't know if a string is actually a number when handling user input, use
int.TryParse
(or variants, e.g. double.TryParse
)
TryParse
returns a bool
, where true
indicates successful parsing.
Remarks:
- Avoid int.Parse
if you do not know if the value parsed is definitely a number.
- Avoid Convert.ToInt32
entirely, this is an older method and Parse
should be preferred where you know the string can be parsed.
Read more hereAh, i see
BlazeBin - bvlzjlyptrlh
A tool for sharing your source code with the world!
Thank you!
Convert.ToInt32(artillery1)
These are pointless too
It doesn't do anythingReplace it with parse?
you are not saving down the result into a variable
TryParse, yes.
You declare Troops1 as a string
It will always be a string
Unless you declare another variable of integer type, parse the value and then set the newly declared variable to the result of the parse
Alrighty, i'll get to replacing it.
I'll be using these values:
I'm unsure how I would implement it. Do I use an if statement, as shown above, or keep it individual?
https://paste.mod.gg/xpfjnkpypuqt/0
BlazeBin - xpfjnkpypuqt
A tool for sharing your source code with the world!
Did you read the text above?
TryParse
returns abool
, wheretrue
indicates successful parsing.
So its only trying it to see if it can do it, and not doing it?
I need to use int.Parse then for the actual conversion, thats what im thinking
Parse just throws an exception if the format is invalid
Which means your app will crash if unhandled
TryParse will give you a boolean on whether it was successful or not
Allowing you to gracefully quit or ask again
Something like this to check?
Read the code out loud for yourself and try to understand what it does
Documentation scrolling time...
The first part is correct, yes.
The code inside the code block is not.
I can see that
number
isnt declared.
And it.. doesnt really do anything otherwise
Hold on.
If you want to give an error or something, you can invert the if-statement like this
It is just now that i figured out i can just.. make the code block CS.
But the error block is also useful, i'll see where I can implement it
Output.
Is that correct logic of what the code does?
Just want you to confirm or deny
Issue was in your previous code that you tried to multiply a string by an amount, of which in C# you cannot do.
The finished product is meant to take in values from two sides, troops, tanks and artillery on one side, and the same on the other. Based off which side is on the offensive or the defensive, it calculates the losses, and should use a randomizer to decide which side wins.
TL;DR, calculates losses, randomizes the victory
We are not writing the code for you. That is your task to do.
However I am helping you get started.
I know this.
I'm telling you the objective.
Thank you for what you've told me so far.
troops1 * 0.75;
this also does not do anything. For one, it's an error because troops1 is a string and secondly, it is not modifying the value.
You can use the shorthand method.
conTroops1 *= 0.75
Which is the equivalent to conTroops1 = conTroops1 * 0.75
This annoying keybind keeps happening and again and again I keep forgetting how to disable it, my typing indicator is now a replacement indicator.
Press insert
Wow, that was an easy fix.
Thanks!
Also, based on your code. You assume the user always write what you want. What if they don't?
What happens if they write
Side505
Rule of programming. Never trust the user.Good to know.
It is good to inform the users that it is an invalid prompt
Spit an error at them.
Value "
Side505
" is invalid.Which is exactly why TryParse is preferred, that and it doesn't throw an exception which is good for performance
Avoiding exceptions is a great thing
Exceptions are annoying, so I can see how its a great thing. Especially if you dont understand what is immediately wrong with the code.
Great! Then do that. If neither matches, then write something to console, informing the user what they did was incorrect.
if/else statement time.
I updated the paste code: https://paste.mod.gg/gczqzmpbdcuh/0
BlazeBin - gczqzmpbdcuh
A tool for sharing your source code with the world!
What its trying to do here is say "if the converted troops value is not an integer, spit this error." However, the "int" is invalid.
conTroops1 will always be an integer as I mentioned before
It is a statically typed language
Use the error given in TryParse, that validates if it is an integer or not
This:
!int.TryParse(troops1, out conTroops1
or this:
Well, the good news is that the primary issue is solved, and I am able to move on with the program. Thank you.https://paste.mod.gg/ibvjrtpjsjua/0
I have completed the victory randomizer part and the hit points calculation, now I need to calculate the losses using those hit points.
i'm going off the below text for it:
"it then calculates the losses of each side using a double randomizer, and it chooses a percentage in the form of a float ranging from 0.01 to 1. this calculation is done twice for both sides, and after multiplying the hit points of each side by the other sides chosen loss float the numbers are rounded to the nearest 1 and the final loss results are the total losses for each side, both losses kept seperate."
my current issue is how i would start to calculate it, as the calculations are done inside of 2 if/else statements for all 4 possible offensive/defensive scenarios.
BlazeBin - ibvjrtpjsjua
A tool for sharing your source code with the world!