C
C#2y ago
Mr. Cres

❔ Requesting Assistance for some programs

🙏
42 Replies
Mr. Cres
Mr. CresOP2y ago
Working on a zodiac compatibility thing left side: when the button is clicked, it checks if the zodiac sign for the years are compatible. If so it says it's a match. Else, it's not how would I go about doing that?
Pobiega
Pobiega2y ago
Well, I'd write a method to calculate the zodiac sign for a given date then another method that takes two zodiac signs and returns a match or a non-match then you just wire these two methods up together
Mr. Cres
Mr. CresOP2y ago
hmm could I put those years in like an int or something. If int1 matches with int2 it'd say if it's a match or not?
Pobiega
Pobiega2y ago
? I have no idea what the rules for zodiac matching or even calculating the sign for a given date are
Mr. Cres
Mr. CresOP2y ago
wait uhh hold on
Pobiega
Pobiega2y ago
are we talking chinese zodiacs that (if I understand things correctly) are year by year? ie, "year of the tiger" etc or are we talking western(greek?) zodiacs, like the twins, the fish, the lion etc?
Mr. Cres
Mr. CresOP2y ago
Mr. Cres
Mr. CresOP2y ago
chinese zodiacs I should've clarified sorry
Pobiega
Pobiega2y ago
Right, so we only care about the year
Mr. Cres
Mr. CresOP2y ago
yeah
Pobiega
Pobiega2y ago
Well, a full list of every year and what sign it corresponds to sounds like a bad idea better to define a fixed point in time and say "this is the year of the rat" then you can calculate a difference from the given year and that fixed point, then modulo that to find what "cycle" the year is in okay, so I think I got something
[Theory]
[InlineData(1948, ChineseZodiacSign.Rat)]
[InlineData(1949, ChineseZodiacSign.Ox)]
[InlineData(1971, ChineseZodiacSign.Pig)]
[InlineData(2020, ChineseZodiacSign.Rat)]
public void YearLookup(int year, ChineseZodiacSign expected)
{
var value = new DateOnly(year, 1, 1).ToChineseZodiacSign();

value.ShouldBe(expected);
}
[Theory]
[InlineData(1948, ChineseZodiacSign.Rat)]
[InlineData(1949, ChineseZodiacSign.Ox)]
[InlineData(1971, ChineseZodiacSign.Pig)]
[InlineData(2020, ChineseZodiacSign.Rat)]
public void YearLookup(int year, ChineseZodiacSign expected)
{
var value = new DateOnly(year, 1, 1).ToChineseZodiacSign();

value.ShouldBe(expected);
}
this test works out - but its using january first as the switch point, aka it goes by the gregorian calendar im not sure if thats correct for your usecase here is the code for it, btw
public enum ChineseZodiacSign
{
Rat,
Ox,
Tiger,
Rabbit,
Dragon,
Snake,
Horse,
Goat,
Monkey,
Rooster,
Dog,
Pig
}

public static class ZodiacExtensions
{
private const int FixedRatPoint = 1948;

public static ChineseZodiacSign ToChineseZodiacSign(this DateOnly date)
{
var mod = Math.Abs(date.Year - FixedRatPoint) % 12;
return Enum.GetValues<ChineseZodiacSign>()[mod];
}
}
public enum ChineseZodiacSign
{
Rat,
Ox,
Tiger,
Rabbit,
Dragon,
Snake,
Horse,
Goat,
Monkey,
Rooster,
Dog,
Pig
}

public static class ZodiacExtensions
{
private const int FixedRatPoint = 1948;

public static ChineseZodiacSign ToChineseZodiacSign(this DateOnly date)
{
var mod = Math.Abs(date.Year - FixedRatPoint) % 12;
return Enum.GetValues<ChineseZodiacSign>()[mod];
}
}
Mr. Cres
Mr. CresOP2y ago
I think I just ended up working on some code spaghetti
Pobiega
Pobiega2y ago
then unravel your spaghetti 🙂 but you see how simple this is? assuming we can look at only the year, that is Im thinking chinese zodiac signs probably should respect chinese new year, not gregorian new year
ero
ero2y ago
just (ChineseZodiacSign)(date.Year % 12), right ah, you need to find a different starting point
Pobiega
Pobiega2y ago
year 0 was year of the dragon so if you re-order the enum, that works
Mr. Cres
Mr. CresOP2y ago
I think I just zoned out I don't know where I was going with this
Pobiega
Pobiega2y ago
yeah thats the naive approach and its not a good idea 🙂 You'll want to make a method takes in the year and calculates the correct zodiac sign that way its re-usable
Mr. Cres
Mr. CresOP2y ago
ah Okay I'll be honest I'm even more lost now with your method
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Mr. Cres
Mr. CresOP2y ago
yeah each sign is just +12 until it happens again
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Mr. Cres
Mr. CresOP2y ago
all of this is new to me
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Pobiega
Pobiega2y ago
check here. @Mr. Cres If you're not comfortable with methods, I'd suggest you spend some time focusing on that - its a very important concept If you want more hands-on help, I suggest you share your code and describe where you are stuck
Mr. Cres
Mr. CresOP2y ago
I got my thing here
Mr. Cres
Mr. CresOP2y ago
idk if there's a way to shorten it
circles.png
circles.png2y ago
you should put all this logic in a dictionary or a json file
Pobiega
Pobiega2y ago
No. Just make an enum and a single one-line method as shown above
Mr. Cres
Mr. CresOP2y ago
i don't know how to do that
ero
ero2y ago
Like this
Pobiega
Pobiega2y ago
You can create your own methods from scratch, you are not limited to only making methods via winforms events so an easy solution to this would to make a method to convert a year into a zodiac sign
public static ChineseZodiacSign ToChineseZodiacSign(int year)
{
const int fixedRatPoint = 1948;

var mod = Math.Abs(year - fixedRatPoint) % 12;
return Enum.GetValues<ChineseZodiacSign>()[mod];
}
public static ChineseZodiacSign ToChineseZodiacSign(int year)
{
const int fixedRatPoint = 1948;

var mod = Math.Abs(year - fixedRatPoint) % 12;
return Enum.GetValues<ChineseZodiacSign>()[mod];
}
is the simplest version of this now, this method returns a ChineseZodiacSign, so you can create that too. Its not a method, thats its own type, an enum type.
public enum ChineseZodiacSign
{
Rat,
Ox,
Tiger,
Rabbit,
Dragon,
Snake,
Horse,
Goat,
Monkey,
Rooster,
Dog,
Pig
}
public enum ChineseZodiacSign
{
Rat,
Ox,
Tiger,
Rabbit,
Dragon,
Snake,
Horse,
Goat,
Monkey,
Rooster,
Dog,
Pig
}
you would NOT place this inside Form1, you would make a new file along side it
ero
ero2y ago
two new files, probably and a new folder and then just do .ToString() on the result of that ToChineseZodiacSign method and then you have your "Rat" and whatever
Pobiega
Pobiega2y ago
@Mr. Cres do you understand?
Mr. Cres
Mr. CresOP2y ago
Could you run me through it?
MODiX
MODiX2y ago
Ero#1111
REPL Result: Success
var now = DateTime.Now;
Console.WriteLine(ToChineseZodiacSign(now.Year).ToString());

static ChineseZodiacSign ToChineseZodiacSign(int year)
{
const int fixedRatPoint = 1948;

var mod = Math.Abs(year - fixedRatPoint) % 12;
return Enum.GetValues<ChineseZodiacSign>()[mod];
}

enum ChineseZodiacSign
{
Rat,
Ox,
Tiger,
Rabbit,
Dragon,
Snake,
Horse,
Goat,
Monkey,
Rooster,
Dog,
Pig
}
var now = DateTime.Now;
Console.WriteLine(ToChineseZodiacSign(now.Year).ToString());

static ChineseZodiacSign ToChineseZodiacSign(int year)
{
const int fixedRatPoint = 1948;

var mod = Math.Abs(year - fixedRatPoint) % 12;
return Enum.GetValues<ChineseZodiacSign>()[mod];
}

enum ChineseZodiacSign
{
Rat,
Ox,
Tiger,
Rabbit,
Dragon,
Snake,
Horse,
Goat,
Monkey,
Rooster,
Dog,
Pig
}
Console Output
Rabbit
Rabbit
Compile: 708.585ms | Execution: 44.913ms | React with ❌ to remove this embed.
ero
ero2y ago
what's there to walk through? that's literally it
MODiX
MODiX2y ago
Ero#1111
REPL Result: Success
Console.WriteLine(DateTime.Now.ToChineseZodiacSign().ToString());

static ChineseZodiacSign ToChineseZodiacSign(this DateTime date)
{
return (ChineseZodiacSign)(date.Year % 12);
}

enum ChineseZodiacSign
{
Monkey, Rooster, Dog, Pig,
Rat, Ox, Tiger, Rabbit,
Dragon, Snake, Horse, Goat
}
Console.WriteLine(DateTime.Now.ToChineseZodiacSign().ToString());

static ChineseZodiacSign ToChineseZodiacSign(this DateTime date)
{
return (ChineseZodiacSign)(date.Year % 12);
}

enum ChineseZodiacSign
{
Monkey, Rooster, Dog, Pig,
Rat, Ox, Tiger, Rabbit,
Dragon, Snake, Horse, Goat
}
Console Output
Rabbit
Rabbit
Compile: 644.645ms | Execution: 87.373ms | React with ❌ to remove this embed.
Mr. Cres
Mr. CresOP2y ago
english is not my first language I'm having trouble understanding some parts
ero
ero2y ago
so ask about the parts
Pobiega
Pobiega2y ago
@Mr. Cres I honestly don't know how to tell you simpler than I already have.
Mr. Cres
Mr. CresOP2y ago
I think I misread the prompt
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server