triplemocha
triplemocha
CC#
Created by triplemocha on 7/16/2023 in #help
❔ How to get a random enum value?
I have a function here where I would like to get a random value from an enum type. The goal is to give NextValue any enum type, and it will calculate its length and return a random value. The cast on return isn't working. Is this possible to do?
public static T NextValue<T>() where T : Enum
{
return (T)Rand.Next(0, Enum.GetNames(typeof(T)).Length - 1);
}
public static T NextValue<T>() where T : Enum
{
return (T)Rand.Next(0, Enum.GetNames(typeof(T)).Length - 1);
}
30 replies
CC#
Created by triplemocha on 7/16/2023 in #help
❔ What is causing my new thread to run slow?
Here's my code. I'm playing a song with System.Beep while throwing random 'character-sized pixels' to the screen with a font size of 1 and full screen. This is pure C# with a console utility library I'm building with some P/Invoking of the Windows API. But I'm always curious why when I'm setting the font size really small the beep song plays slower. It's its own thread, so I don't see why it matters how tiny the "pixels" are. When at font size of 24, the song plays beautifully. When at 1, it starts to crawl. It's like the thread isn't fully independent. Note I'm just trying to learn from why the created thread is behaving as such for academic reasons. https://www.paste.org/125720
4 replies
CC#
Created by triplemocha on 7/7/2023 in #help
❔ Should procedural 2d maps be with structs or instantiated with 'new'?
I am creating a game that can sometimes have a 1000x1000 interior grids in width and height. Exteriors will likely be larger, maybe a 5000x5000 2d array. Each grid square is called a Cell. A Cell class includes an enumerated type, two Lists (can hold up to 20 ints), and a float and boolean variable. My assumption is only a few maps will be loaded at a time, but I'm open to the idea of saving procedurally generated maps in a Dictionary key/value later. Should each Cell object be of a struct or class type?
6 replies
CC#
Created by triplemocha on 6/29/2023 in #help
❔ Any issue with this for a game?
I'm making a game. I have a Array2D object that holds "Cells". So, each grid cell has data. Each cell can be a wall, path, etc. Every cell has two List<int> collections. If I have a 1000x1000 map that holds a cell in each, will I have any issues? Seems like a lot having 1000x1000x2 lists. Each list is estimated to hold at most 10 items.
14 replies
CC#
Created by triplemocha on 6/29/2023 in #help
❔ Is this good practice with public accessors?
I come from C++ which used get/sets to access private members. In C#, there seems to be several ways of doing this. I learned the latest one. It seems like less time accessing these probbaly due to less function call overhead. I'm creating a CustomDateTime class that's specific toward a game I'm working on. Is below good practice of using these public accessors? I assume the rule is if it fits in one line (member variable or expression), it's good practice?
// Accessors
//
// Hour
public int Hour => (m_hour24 > 12) ? m_hour24 - 12 : m_hour24;
public int Hour24 => m_hour24;
public string AMPM => (m_hour24 >= 12) ? "PM" : "AM";
// Minute
public int Minute => m_minute;
// Second
public int Second => m_second;
// Year
public int Year => m_year;
public string ShortYear => m_year.ToString().Substring(m_year.ToString().Length - 2, 2);
// Month
public int Month => m_month;
public int MonthCount => m_monthNames.Count();
public string MonthName => m_monthNames[m_month - 1];
public string MonthNameAbbr => m_monthNamesAbbr[m_month - 1];
// Day
public int Day => m_dayOfMonth;
public int DaysPerMonth => m_daysPerMonth;
public int DayOfYear => ((m_month - 1) * m_daysPerMonth) + m_dayOfMonth;
public string WeekdayName => m_weekdayNames[(m_dayOfMonth - 1) % 7];
// Format
public string LetterDate => String.Format("{0} {1}, {2}", m_weekdayNames[(m_dayOfMonth - 1) % 7], m_dayOfMonth, Year);
public string ShortDate => String.Format("{0}/{1}/{2}", m_month, m_dayOfMonth, ShortYear);
public string Time => String.Format("{0}:{1:00} {2}", Hour, m_minute, AMPM);
public string Time24 => String.Format("{0}:{1:00}", m_hour24, m_minute);
// Accessors
//
// Hour
public int Hour => (m_hour24 > 12) ? m_hour24 - 12 : m_hour24;
public int Hour24 => m_hour24;
public string AMPM => (m_hour24 >= 12) ? "PM" : "AM";
// Minute
public int Minute => m_minute;
// Second
public int Second => m_second;
// Year
public int Year => m_year;
public string ShortYear => m_year.ToString().Substring(m_year.ToString().Length - 2, 2);
// Month
public int Month => m_month;
public int MonthCount => m_monthNames.Count();
public string MonthName => m_monthNames[m_month - 1];
public string MonthNameAbbr => m_monthNamesAbbr[m_month - 1];
// Day
public int Day => m_dayOfMonth;
public int DaysPerMonth => m_daysPerMonth;
public int DayOfYear => ((m_month - 1) * m_daysPerMonth) + m_dayOfMonth;
public string WeekdayName => m_weekdayNames[(m_dayOfMonth - 1) % 7];
// Format
public string LetterDate => String.Format("{0} {1}, {2}", m_weekdayNames[(m_dayOfMonth - 1) % 7], m_dayOfMonth, Year);
public string ShortDate => String.Format("{0}/{1}/{2}", m_month, m_dayOfMonth, ShortYear);
public string Time => String.Format("{0}:{1:00} {2}", Hour, m_minute, AMPM);
public string Time24 => String.Format("{0}:{1:00}", m_hour24, m_minute);
36 replies
CC#
Created by triplemocha on 6/8/2023 in #help
❔ Adding list in single line
In C++, I could add a list in a single line using a vector. For example:
std::vector<std::string> months = { "January", "February", "March" };

// another thing I could do:
setMonths({ "January", "February", "March" });
std::vector<std::string> months = { "January", "February", "March" };

// another thing I could do:
setMonths({ "January", "February", "March" });
Is there a similar way with C# and lists? That way I'm not calling "Add" 12 times, but keeping them like an array on initialization. With C#, I think it's just
List<string> months = new List<string>();
months.Add("January");
months.Add("February");
months.Add("March");
List<string> months = new List<string>();
months.Add("January");
months.Add("February");
months.Add("March");
Or AddRange(): -- Which isn't too bad, but still feels bigger than it should be.
List<string> vMonths = new List<string>();
string[] months = new string[] { "January", "February", "March" };
vMonths.AddRange(months);
dt.SetMonths(vMonths);
List<string> vMonths = new List<string>();
string[] months = new string[] { "January", "February", "March" };
vMonths.AddRange(months);
dt.SetMonths(vMonths);
Ideal:
dt.SetMonths({ "January", "February", "March" }); // Param: SetMonths(List<string> list)
dt.SetMonths({ "January", "February", "March" }); // Param: SetMonths(List<string> list)
28 replies
CC#
Created by triplemocha on 6/7/2023 in #help
❔ operator * cannot be applied to operands of type T and T
I'm getting this error: operator * cannot be applied to operands of type T and T I understand why, but I'm wondering if there's a way to add a constraint only to ints , floats and doubles. Sounds like it's a no. What else can be done?
struct Vector2D<T>
{
public T x, y;

public Vector2D()
{
x = y = default(T);
}

public Vector2D(T xPos, T yPos)
{
x = xPos;
y = yPos;
}

public T GetMagnitude()
{
return System.Math.Sqrt(x * x + y * y); // error
}
}
struct Vector2D<T>
{
public T x, y;

public Vector2D()
{
x = y = default(T);
}

public Vector2D(T xPos, T yPos)
{
x = xPos;
y = yPos;
}

public T GetMagnitude()
{
return System.Math.Sqrt(x * x + y * y); // error
}
}
15 replies
CC#
Created by triplemocha on 6/7/2023 in #help
❔ How do I get the direction between two points?
Say that you are at position (10, 10) on the map, and your destination is (2, 2). I would like to calculate this and output this: "You need to go northwest." There will be all other directions, too. In my program, my Array2D class is being used to store the 2d array, and (0,0) is at the top-left corner. My function is currently this, assuming I need vectors for it. What do I need to do to achieve this?
public EDirection GetDirection(int xPos, int yPos, int xDest, int yDest)
{
Vector2D pos = new Vector2D(xPos, yPos);
Vector2D dest = new Vector2D(xDest, yDest);


}
public EDirection GetDirection(int xPos, int yPos, int xDest, int yDest)
{
Vector2D pos = new Vector2D(xPos, yPos);
Vector2D dest = new Vector2D(xDest, yDest);


}
12 replies
CC#
Created by triplemocha on 6/6/2023 in #help
❔ How can I create a fill method that creates unique elements?
I have the following 2D array method that is for basic types and references.
/// <summary>
/// Fills the array with the given value.
/// </summary>
/// <param name="value">The value to fill with.</param>
public void Fill(T value)
{
Array.Fill(m_array, value);
}
/// <summary>
/// Fills the array with the given value.
/// </summary>
/// <param name="value">The value to fill with.</param>
public void Fill(T value)
{
Array.Fill(m_array, value);
}
This works when I want basic types and every reference value pointing to the same object. I want to keep this method for those, which is the most common. However, I need a similar one that makes every element unique using new. For example, every Wall object in the array is unique. Each wall has its own properties. I do want the method generic so it allows any object of a class. Here's the idea:
/// <summary>
/// Fills the array with unique values.
/// </summary>
public void FillNew(T value)
{
for (int i = 0; i < Size; i++)
m_array[i] = new T(value);
}
/// <summary>
/// Fills the array with unique values.
/// </summary>
public void FillNew(T value)
{
for (int i = 0; i < Size; i++)
m_array[i] = new T(value);
}
Again, this is just example code of what I want to see happen, but it has a compile error that T does not have a new() constraint. Is there a good solution to this? For reference, my class is written as this:
class Array2D<T>
{
private T[] m_array;
private int m_width, m_height;
class Array2D<T>
{
private T[] m_array;
private int m_width, m_height;
24 replies
CC#
Created by triplemocha on 5/29/2023 in #help
How to replace elements in an array of type T?
I have an Array2D<T> class that can take integer, reference types, etc. They are stored in this:
private T[] m_array = new T[width * height];
private T[] m_array = new T[width * height];
I have a method here that should replace elements.
void Replace(T from, T to)
{
}
void Replace(T from, T to)
{
}
I can't find any existing replace method. Any suggestions?
11 replies