#C# Dnd Character
157 messages ยท Page 1 of 1 (latest)
What am I supposed to do in the Ability method?
I am having this problem: DndCharacter.cs(16,9): error CS0200: Property or indexer 'DndCharacter.Strength' cannot be assigned to -- it is read only
I don't know what means a modifier to be 3
I don't know this game
Am i supposed to generate a random number in the ability method?
What I have done so far: ``` public static int Modifier(int score)
{
Console.WriteLine(score);
DndCharacter character = new DndCharacter();
Console.WriteLine(character.Strength);
Console.WriteLine(character.Dexterity);
Console.WriteLine(character.Constitution);
Console.WriteLine(character.Intelligence);
Console.WriteLine(character.Wisdom);
Console.WriteLine(character.Charisma);
return character.Strength + character.Dexterity + character.Constitution + character.Intelligence + character.Wisdom + character.Charisma;
}
public static int Ability()
{
Random rnd = new Random();
int[] roll = new int[4];
for (int i = 0; i < 4; i++)
{
roll[rnd.Next(roll.Length)]++;
}
Console.WriteLine(Minimum(roll));
return 2;
}
public static int Minimum(int[] roll) {
int Min = roll[0];
for (var i = 1; i < roll.Length; i++)
{
Console.WriteLine(roll[i]);
if (roll[i + 1] < Min)
{
Min = roll[i + 1];
}
}
return Min;
}
public static DndCharacter Generate()
{
DndCharacter dndCharacter = new DndCharacter();
return dndCharacter;
}```
You don't need to know the game. The instructions and tests should be plenty.
Yes, this requires random numbers
Are there tests that aren't passing?
only 1 passes
this is my current code: ``` public static int Modifier(int score)
{
DndCharacter character = new DndCharacter();
character.Strength = Ability();
/character.Dexterity = Ability();
character.Constitution = Ability();
character.Intelligence = Ability();
character.Wisdom = Ability();
character.Charisma = Ability();/
return character.Strength + character.Dexterity + character.Constitution + character.Intelligence + character.Wisdom + character.Charisma;
}
public static int Ability()
{
Random rnd = new Random();
int[] roll = new int[4];
for (int i = 0; i < 4; i++)
{
roll[i] = rnd.Next(1,6);
Console.WriteLine(roll[i]);
}
int min = Minimum(roll);
int ability = 0;
for (int i = 0; i < 4; i++) {
if (roll[i] != min)
ability += roll[i];
}
return ability;
}
public static int Minimum(int[] roll) {
int Min = roll[0];
for (var i = 1; i < roll.Length-1; i++)
{
if (roll[i + 1] < Min)
{
Min = roll[i + 1];
}
}
return Min;
}
public static DndCharacter Generate()
{
DndCharacter dndCharacter = new DndCharacter();
return dndCharacter;
}```
what do you think about it? ๐
I updated it now ๐
I think I'd need to see test output to understand what isn't working
what isn't working is my comprehension of the whole task
Assert.Equal(-4, DndCharacter.Modifier(3));
TEST FAILURE
Assert.Equal() Failure
Expected: -4
Actual: 10```
What does the instructions say the modifier function is supposed to return?
well, at the momment that is a bit complex to me
but has to do to 6 abilities
generated randomly
with a sum of three max numbers out of four
No. It's not.
I suppose
Take another look at the instructions. You're confusing parts.
ok
This I do "These six abilities have scores that are determined randomly. You do this by rolling four 6-sided dice and record the sum of the largest three dice. You do this six times, once for each ability."
That has nothing to do with the modifier function
Figured it out?
Passes more tests now?
That's progress
{
DndCharacter character = new DndCharacter();
character.Strength = Ability();
character.Dexterity = Ability();
character.Constitution = Ability();
character.Intelligence = Ability();
character.Wisdom = Ability();
character.Charisma = Ability();
character.Hitpoints = (int) Math.Floor((10 + (character.Constitution - 10))/2m);
return character;
}```
character.Hitpoints doesn't always passes
so sometimes I still get 1 test passed
You know the drill ๐ If you want help with a test, share test output!
for (var i = 0; i < 10; i++)
{
Assert.InRange(DndCharacter.Ability(), 3, 18);
}
TEST FAILURE
Assert.InRange() Failure
Range: (3 - 18)
Actual: 2```
- Do you understand what the test is doing?
- Do you understand what the test is expecting and why?
- Do you understand what your code returns and how it differs?
- Do you understand why your code is returning what it returns?
@little wasp got it working?
not yet
Do you want help?
Let me see the instructions better
According to my expression character.Hitpoints = (int) (10 + (character.Constitution - 10)/2);
That doesn't seem related to the test you shared
If you need help with something, please share the test output and code you want help with. Then answer the four questions.
Assert.Equal(-4, DndCharacter.Modifier(3));
TEST FAILURE
Assert.Equal() Failure
Expected: -4
Actual: -1```
Actually I passed 4 and failed 16
My Actual value is always different
- Do you understand what the test is doing?
- Do you understand what the test is expecting and why?
- Do you understand what your code returns and how it differs?
- Do you understand why your code is returning what it returns?
sometimes is -1, other times is -2, etc.
- isn't the test calling this function?
public static int Modifier(int score) { DndCharacter character = Generate(); int constitutionModifier = (int) Math.Round((character.Constitution - 10)/2m); Console.WriteLine(constitutionModifier); return constitutionModifier; }
"You find your character's constitution modifier by subtracting 10 from your character's constitution, divide by 2 and round down."
this is what I did and returned
Does your function use all the parameters passed in?
what do you mean the agility parameters?
don't know yet the purpose of the score input
The modifier is supposed to be based on the score
Not based on a new character
I'm pretty sure all functions across all of Exercism needs you to use all inputs every time.
yes
so what is the character for?
So the score is related to this "These six abilities have scores"
Don't know yet what do with the score
subtract each ability score
?
The modifier is based only on the score
The character isn't needed for the modifier. Only the score.
The modifier function takes a score number, does math on the score and returns a modifier value
I need to use Ability and Generate functions in some way ๐
Not for the modifier function
does the issue says what math I should do on the score?
The x modifier is modifier(character.x)
if yes, where'
"You find your character's constitution modifier by subtracting 10 from your character's constitution, divide by 2 and round down."
Replace constitution with x
Or score
so it's this: public static int Modifier(int score) { int constitutionModifier = (int) Math.Round((score - 10)/2m); return constitutionModifier; }
15 passed 5 failed
Is that more or less than before?
better
๐
10 more passed than before
Now more challenges: CODE RUN Assert.Equal(-3, DndCharacter.Modifier(5)); TEST FAILURE Assert.Equal() Failure Expected: -3 Actual: -2
But before that, I become a bit lost because I don't know how to use the Generate and Ability that I've implemented ๐
All code is implemented I suppose
- Do you understand what the test is doing?
- Do you understand what the test is expecting and why?
- Do you understand what your code returns and how it differs?
- Do you understand why your code is returning what it returns?
- the test is calling only Modifier method, not Generate nor Ability
- I understand more or less
- I understand all my code, and don't know how to apply Ability and Generate
- I understand code is returning only the Modifier function result
what is missing in my answers? ๐
let's focus on this
score 5 is the score of what ability?
modifier function: by subtracting 10 from your score, divide by 2 and round down.
Code, test output, 4 questions.
The only test I'm failing, related to Ability: CODE RUN var expectedDistribution = new Dictionary<int, int> { [3] = 1, [4] = 4, [5] = 10, [6] = 21, [7] = 38, [8] = 62, [9] = 91, [10] = 122, [11] = 148, [12] = 167, [13] = 172, [14] = 160, [15] = 131, [16] = 94, [17] = 54, [18] = 21 }; var actualDistribution = new Dictionary<int, int>(expectedDistribution); foreach (var key in actualDistribution.Keys) actualDistribution[key] = 0; const int times = 250; const int possibleCombinationsCount = 6 * 6 * 6 * 6; // 4d6 for (var i = 0; i < times * possibleCombinationsCount; i++) actualDistribution[DndCharacter.Ability()]++; const double minTimes = times * 0.8; const double maxTimes = times * 1.2; foreach (var k in expectedDistribution.Keys) Assert.InRange(actualDistribution[k], expectedDistribution[k] * minTimes, expectedDistribution[k] * maxTimes); TEST FAILURE Assert.InRange() Failure Range: (200 - 300) Actual: 510
a bit complex this output ๐
Interesting test...
What's your code?
{
Random rnd = new Random();
int[] roll = new int[4];
for (int i = 0; i < 4; i++)
{
roll[i] = rnd.Next(1,6);
}
int min = Minimum(roll);
int ability = 0;
for (int i = 0; i < 4; i++) {
ability += roll[i];
}
return ability - min;
}
public static int Minimum(int[] roll) {
int Min = roll[0];
for (var i = 1; i < roll.Length-1; i++)
{
if (roll[i + 1] < Min)
{
Min = roll[i + 1];
}
}
return Min;
}```
Try not creating a new Random object every time the function is called
I think your minimum function is broken
What does Minimum (2,1,4,3) return?
4,3,2? let me check
I thought it returned one int?
and returns
What does Minimum(2,1,4,3) return?
4,3,2
with this new code ``` public static int Minimum(int[] roll) {
int min = roll.Min();
return min;
}```
using System.Linq
Minimum (2,1,4,3) returns 3 numbers?
Does your new code pass?
no
- Try not creating a new Random object every time the function is called
- Paste new code and error.
I can't take Random rnd = new Random(); out of the function
I tried to put it near abilities properties but I can't
What about the class initializer?
- done
Assert.InRange() Failure
Range: (200 - 300)
Actual: 507```
maybe ability is badly calculated
don't know what's wrong: ```public static int Ability()
{
int[] roll = new int[4];
for (int i = 0; i < 4; i++)
roll[i] = rnd.Next(1,6);
int min = roll.Min();
int ability = 0;
for (int i = 0; i < 4; i++) {
ability += roll[i];
}
return ability - min;
}```
I deleted Minimum function
I don't understand the code run
it's very complex
I only know it uses Ability here: actualDistribution[DndCharacter.Ability()]++;
Keep reading ๐