C
C#6mo ago
Slots

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
Slots
SlotsOP6mo ago
(whoops.. i think i randomized the filenames!) heres another one
Buddy
Buddy6mo ago
Please use $paste
MODiX
MODiX6mo ago
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!
Buddy
Buddy6mo ago
Mobile devices need to download the files to see the content. And to parse as an integer, use $tryparse
MODiX
MODiX6mo ago
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)
if(int.TryParse("123", out int number))
{
var total = number + 1;
Console.WriteLine(total); // output: 124
}
if(int.TryParse("123", out int number))
{
var total = number + 1;
Console.WriteLine(total); // output: 124
}
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 here
Slots
SlotsOP6mo ago
Ah, i see
Slots
SlotsOP6mo ago
BlazeBin - bvlzjlyptrlh
A tool for sharing your source code with the world!
Slots
SlotsOP6mo ago
Thank you!
Buddy
Buddy6mo ago
Convert.ToInt32(artillery1) These are pointless too It doesn't do anything
Slots
SlotsOP6mo ago
Replace it with parse?
Buddy
Buddy6mo ago
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
Slots
SlotsOP6mo ago
Alrighty, i'll get to replacing it. I'll be using these values:
int conTroops1 = 0;
int conTanks1 = 0;
int conArtillery1 = 0;

int conTroops2 = 0;
int conTanks2 = 0;
int conArtillery2 = 0;
int conTroops1 = 0;
int conTanks1 = 0;
int conArtillery1 = 0;

int conTroops2 = 0;
int conTanks2 = 0;
int conArtillery2 = 0;
Slots
SlotsOP6mo ago
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!
Buddy
Buddy6mo ago
Did you read the text above?
TryParse returns a bool, where true indicates successful parsing.
Slots
SlotsOP6mo ago
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
Buddy
Buddy6mo ago
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
Slots
SlotsOP6mo ago
Something like this to check?
if (int.TryParse(troops1, out conTroops1))
{
var total = number + 1;
Console.WriteLine(total);
}
if (int.TryParse(troops1, out conTroops1))
{
var total = number + 1;
Console.WriteLine(total);
}
Buddy
Buddy6mo ago
Read the code out loud for yourself and try to understand what it does
Slots
SlotsOP6mo ago
Documentation scrolling time...
Buddy
Buddy6mo ago
The first part is correct, yes. The code inside the code block is not.
Slots
SlotsOP6mo ago
I can see that number isnt declared. And it.. doesnt really do anything otherwise Hold on.
if (int.TryParse(troops1, out conTroops1))
{
var total = conTroops1 + 1;
Console.WriteLine(conTroops1);
}
if (int.TryParse(troops1, out conTroops1))
{
var total = conTroops1 + 1;
Console.WriteLine(conTroops1);
}
Buddy
Buddy6mo ago
If you want to give an error or something, you can invert the if-statement like this
if (!int.TryParse(troops1, out conTroops1)
{
Console.WriteLine("Failed to parse troops1!");
return;
}
int myValue = conTroops1 + 100;
Console.WriteLine(myValue);
if (!int.TryParse(troops1, out conTroops1)
{
Console.WriteLine("Failed to parse troops1!");
return;
}
int myValue = conTroops1 + 100;
Console.WriteLine(myValue);
Slots
SlotsOP6mo ago
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
Slots
SlotsOP6mo ago
Output.
No description
Slots
SlotsOP6mo ago
if (!int.TryParse(troops1, out conTroops1))
{
Console.WriteLine("Failed to parse troops1!");
return;
}
int myValue1 = conTroops1 + 100;
Console.WriteLine(myValue1);
if (!int.TryParse(troops1, out conTroops1))
{
Console.WriteLine("Failed to parse troops1!");
return;
}
int myValue1 = conTroops1 + 100;
Console.WriteLine(myValue1);
Buddy
Buddy6mo ago
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.
Slots
SlotsOP6mo ago
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
Buddy
Buddy6mo ago
We are not writing the code for you. That is your task to do. However I am helping you get started.
Slots
SlotsOP6mo ago
I know this. I'm telling you the objective. Thank you for what you've told me so far.
Buddy
Buddy6mo ago
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
Slots
SlotsOP6mo ago
This annoying keybind keeps happening and again and again I keep forgetting how to disable it, my typing indicator is now a replacement indicator.
No description
Buddy
Buddy6mo ago
Press insert
Slots
SlotsOP6mo ago
Wow, that was an easy fix. Thanks!
Buddy
Buddy6mo ago
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.
Slots
SlotsOP6mo ago
Good to know.
Buddy
Buddy6mo ago
It is good to inform the users that it is an invalid prompt
Slots
SlotsOP6mo ago
Spit an error at them. Value "Side505" is invalid.
Buddy
Buddy6mo ago
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
Slots
SlotsOP6mo ago
Exceptions are annoying, so I can see how its a great thing. Especially if you dont understand what is immediately wrong with the code.
Buddy
Buddy6mo ago
Great! Then do that. If neither matches, then write something to console, informing the user what they did was incorrect.
Slots
SlotsOP6mo ago
if/else statement time.
Console.WriteLine("How many troops on Side 1?");
String troops1 = Console.ReadLine();
if (troops1 != /*[a number string]*/])
{
Console.WriteLine("Please enter a number.")
}
Console.WriteLine("How many troops on Side 1?");
String troops1 = Console.ReadLine();
if (troops1 != /*[a number string]*/])
{
Console.WriteLine("Please enter a number.")
}
Slots
SlotsOP6mo ago
I updated the paste code: https://paste.mod.gg/gczqzmpbdcuh/0
BlazeBin - gczqzmpbdcuh
A tool for sharing your source code with the world!
Slots
SlotsOP6mo ago
if (conTroops1 != int)
{
Console.WriteLine("Please enter a number.");
}
if (conTroops1 != int)
{
Console.WriteLine("Please enter a number.");
}
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.
Buddy
Buddy6mo ago
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
Slots
SlotsOP6mo ago
This: !int.TryParse(troops1, out conTroops1 or this:
Console.WriteLine("Failed to parse troops1!"); return;
Console.WriteLine("Failed to parse troops1!"); return;
Well, the good news is that the primary issue is solved, and I am able to move on with the program. Thank you.
Slots
SlotsOP6mo ago
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!
Want results from more Discord servers?
Add your server