Round Counter using Get and Set C# - c#

Im using Get; and Set; im trying to make a round counter when user inputs Rounds, Minutes, and seconds. Ive managed to create the buttons and input using Get; Set; ... the problem is when the time reaches 0 for minutes and seconds it doesnt reset the minutes and seconds to the original value the user entered the first time, it just keeps it at 0.... Please help.. I didnt add the button or timer start code that all works just the get set problem
class User
{
public static int Rounds { get; set; }
public static int InputRounds { get; set; }
public static int Sec { get; set; }
public static int Min { get; set; }
}
private void Timer_Elapsed(object sender, ElapsedEventArgs e){
User.sec--;
if(User.sec <= 0)
{
User.Min--;
}
if(User.Min <= 0)
{
User.Rounds++;
}
if(User.Rounds >= User.InputRounds) // User.Input is the first value the user input
{
User.Min = ????; // This is my problem with the question marks
User.Sec = ????; // Im trying to set the value back to the users value entered
User.Rounds++; // then add 1 round
}
}

Maybe you just need to refactor your User class, and please don't use static properties unless necessary.
public class User
{
private int oMin;
private int oSec;
public User(int min, int sec, int maxRounds)
{
Min = min;
oMin = min; // Saving Original Values
Sec = sec;
oSec = sec; // Saving Original Values
MaxRounds = maxRounds;
}
public int Rounds { get; private set; }
public int MaxRounds { get; }
public int Sec { get; private set; }
public int Min { get; private set; }
public void ReduceSec()
{
Sec--;
if (Sec <= 0)
{
Min--;
}
if (Min <= 0)
{
Rounds++;
}
if (Rounds >= MaxRounds)
{
Min = oMin;
Sec = oSec;
Rounds++;
}
}
}

Related

How to use variable values from different classes WPF C#

I need some help with using variables that belong to one class inside another class.
I have Semester class that contains Weeks and StartDate properties.
In another class CalcDisplay I have method CalculateStudyHours that should calculate study hours based on Weeks and StartDate properties from Semester class.
I created a Semester object in my WPF class and I am passing through constructor weeks and startDate values that are provided by user.
private void btnEnter_Click(object sender, RoutedEventArgs e)
{
int weeks = int.Parse(txtbWeeks.Text);
DateTime startDate = (DateTime)dteDate.SelectedDate;
Semester s = new Semester(weeks, startDate);
ModuleListHandler.semesterDetails.Add(s);
MessageBox.Show("Semester details added successfully");
btnNext.Visibility = Visibility.Hidden;
}
Semester class
namespace TimeManagementClassLibrary
{
public class Semester
{
public int Weeks { get; set; }
public DateTime StartDate { get; set; }
//default constructor
public Semester()
{
}
public Semester(int weeks, DateTime startdate)
{
Weeks = weeks;
StartDate = startdate;
}
}
}
CalcDisplay class
public class CalcDisplay
{
//Method calculating study hours
public double CalculateStudyHours()
{
return (credits * 10 / weeks) - classHours;
}
}
What would be the most effective way to pass credits and weeks parameters to CalcDisplay class?
Additionally to my comment, let's imagine you have 3 classes:
public class Semester
{
public int Weeks { get; set; }
public Semester() { }
public Semester(int weeks)
{
Weeks = weeks;
}
}
public class Credit
{
public int Credits { get; set; }
public Credit() { }
public Credit(int credits)
{
Credits = credits;
}
}
public class SomeClass
{
public int ClassHours { get; set; }
public SomeClass() { }
public SomeClass(int classHours)
{
ClassHours = classHours;
}
}
You obviously would create their instances and set/calculate it's Weeks, Credits, ClassHours values in some way.
Then you use that values to pass as arguments to some method of another class:
public class CalcDisplay
{
// May be static as uses passed in arguments values, non class members
public static double CalculateStudyHours(int credits, int weeks, int classHours)
{
return (credits * 10 / weeks) - classHours;
}
}
And passing them from somewhere:
private void btnEnter_Click(object sender, RoutedEventArgs e)
{
// Get values in some way
int weeks = int.Parse(txtbWeeks.Text);
int credits = int.Parse(txtbCredits.Text);
int classHours = int.Parse(txtbClassHours.Text);
// Create your instances
Semester s = new Semester(weeks);
Credit c = new Credit(credits);
SomeClass sc = new SomeClass(classHours);
// Then pass it's properties values
double result = CalcDisplay.CalculateStudyHours(c.Credits, s.Weeks, sc.ClassHours);
}
Or CalcDisplayclass may has own fields/properties, which you set through its Constructor:
public class CalcDisplay
{
private int credits;
private int weeks;
private int classHours;
// Constructor
public CalcDisplay() {}
public CalcDisplay(int credits, int weeks, int classHours)
{
this.credits = credits;
this.weeks = weeks;
this.classHours = classHours;
}
// Non-static as we use class members
public double CalculateStudyHours()
{
return (credits * 10 / weeks) - classHours;
}
}
private void btnEnter_Click(object sender, RoutedEventArgs e)
{
// ... same as upper
CalcDisplay = new CalcDisplay(c.Credits, s.Weeks, sc.ClassHours);
double result = c.CalculateStudyHours();
}

i have a problem with my adaptive moving average trading system

I created a trading system with an adaptive moving average on the average true range but the program reports this error to me
the modifier public is not valid for this item
at line 21 of the code
public int avgTrueRange.value1 { get; set; }
I tried to change public but it always reports this error.
this is the code :
public class MediaMobileAdattiva : SignalObject
{
public MediaMobileAdattiva(object _ctx): base(_ctx)
{
Range = 14;
FirstLength = 10;
AvgTrueRange.value1 = 1;
}
private IOrderMarket buy_order;
public int Range { get; set; }
public double FirstLength { get; set; }
public int AvgTrueRange.value1 { get; set; }
private double FirstAverage()
{
if (AverageTrueRange < AvgTrueRange.value1)
return FirstLength;
}
protected override void Create()
{
// create variable objects, function objects, order objects
buy_order = OrderCreator.MarketNextBar(new SOrderParameters(Contracts.Default, EOrderAction.Buy));
}
protected override void StartCalc()
{
// assign inputs
}
protected override void CalcBar()
{
// strategy logic
if (Bars.CurrentBar > 1)
{
switch (FirstAverage)
{
case FirstLength:
return 1;
}
}
if (Bars.CurrentBar > 1 && Bars.Close.CrossesOver(FirstAverage, ExecInfo.MaxBarsBack)
{
switch (FirstLength)
{
case 1:
buy_order.Send(Bars.Close[0]);
}
}
}
}
What you need is to make a struct for AvgTrueRange:
public struct Range
{
public int value1 {get; set;}
}
and change:
public int AvgTrueRange.value1 { get; set; }
to
public Range AvgTrueRange { get; set; }
Your code still won't compile btw but I don't really understand what you are trying to do in this line:
if (AverageTrueRange < AvgTrueRange.value1)
Also, change:
switch (FirstAverage)
{
case FirstLength:
return 1;
}
to
var avg = FirstAverage();
int? result = avg switch
{
var avg when avg == FirstLength => 1,
_ => null
};
if (result.HasValue) return result.Value;
as cases can only be constant values.

C# Change a created objects value from another class

I have a Main.class which runs a timer at 5 seconds (when it resets i want to do a bunch of methods and then repeat the timer). I have a Water.class with a currentReserve-property and a pop.class with a CurrentThirst and MaximumThirst-propertys. In the Pop class i have a method thats get called when the timer reset. Basically i want the popclass to consume the currentReserve-int in the object which i have created in the Mainclass. I provide the essential code and mark with comments where the debugger thinks iam wrong:
public AITest()
{
public Water _water;
public Pop _pop;
Timer dayTimer = new Timer();
int timeLeft = 5;
public AITest()
{
InitializeComponent();
_water = new Water(8000);
lblWater.Text = _water.CurrentReserve.ToString();
_pop = new Pop(100, 100);
dayTimer.Interval = 1000;
dayTimer.Enabled = true;
dayTimer.Tick += dayTimer_Tick;
dayTimer.Start();
lblCountDown.Text = timeLeft.ToString();
}
private void dayTimer_Tick(object sender, EventArgs e)
{
lblCountDown.Text = timeLeft.ToString();
timeLeft -= 1;
if (timeLeft < 0)
{
timeLeft = 5;
_water.CurrentReserve += 100;
_pop.HaveToDrink();
lblWater.Text = _water.CurrentReserve.ToString();
}
}
}
}
public class Pop
{
public int CurrentThirst { get; set; }
public int MaximumThirst { get; set; }
public Water _water;
public Pop(int currentThirst, int maximumThirst)
{
CurrentThirst = currentThirst;
MaximumThirst = maximumThirst;
}
public void HaveToDrink()
{
CurrentThirst -= 30;
_water.CurrentReserve -= 30; //Here i get an error
}
}
public class Water
{
public int CurrentReserve { get; set; }
public Water(int currentReserve)
{
CurrentReserve = currentReserve;
}
}
You've got a _water member twice: once in AITest and once in the Pop class. Use only one, and pass it around. The exception you get will be a NullReferenceException, because nothing is ever assigned to the Pop._water member.

Get a list of dates responding to rules

I have a list of dates like:
21/12/2019
22/12/2019
22/12/2019
24/12/2019
26/12/2019
26/12/2019
27/12/2019
I have the rule model defined as:
public class Timing
{
public int PeriodNbMinutes { get; set; }
public int NbTimes { get; set; }
public bool IsConsecutive { get; set; }
public int Multiplicity { get; set; }
}
Now I have the rule like: I want to find all dates where:
Dates which appears 3 times in 2 days and for twice times
So I should get two groups (I don't want to reuse dates already in a group):
First
1st: 21/12/2019 - 22/12/2019 - 22/12/2019
Second
2nd: 26/12/2019 - 26/12/2019 - 27/12/2019
I then have my rule like this:
this.PeriodMinutes = 2680;
this.NbTimes = 2;
this.Multiplicity = 2;
this.IsConsecutive=true;
Edited
public int PeriodNbMinutes { get; set; }
public int NbTimes { get; set; }
public bool IsConsecutive { get; set; }
public int Multiplicity { get; set; }
internal bool Test(List<DateTime> metrics)
{
bool res = false;
int times = 0;
int multiple=0;
TimeSpan ts = new TimeSpan(0, PeriodNbMinutes, 0);
for (int i = 0; i < metrics.Count; i++)
{
var dates = metrics.Skip(i);
var selectedDts = metrics.Zip(dates, (dtFirst, dtNext) => (dtNext - dtFirst).Ticks < ts.Ticks);
if (selectedDts.Count() > 0)
{
res = true;
times++;
if (times == Multiplicity)
{
multiple++;
if (multiple == Multiplicity)
return true;
// Consecutive???
}
else res = false;
}
else
{
res = false;
times = 0;
}
}
return res;
}
How can I find these groups with Linq? I tried with TimeSpan but can't get it to work.
Additionally, In the two groups selected I need to know if they are consecutive or not.
I tried a lot of different solutions but nothing good. How can I do it simply?
Thanks,

How to store average score and high score in class?

I don't know how to store a player's average score, high score and average time to complete a game in my user.cs class. Whenever the player finishes a round of my game, the average and high scores and average time have to update every time in their labels.
I've tried using arrays and arraylists but I am still unsure because neither seem to work.
Here is my user.cs class:
public class User
{
public string fname { get; set; } = "";
public string lname { get; set; } = "";
public string username { get; set; } = "";
public string password { get; set; } = "";
public User() { }
public User (string fname, string lname, string username, string password)
{
this.fname = fname;
this.lname = lname;
this.username = username;
this.password = password;
}
}
I also need to show the users name, username, high score, average score and timing in labels.
The format should be double/float.
You cannot store average score because of the way averages work. While you can count games played by a user by simply increasing the counter by one every time a game finishes, there is no analytical form to advance the average.
However, if you stored total number of games and total score, then you would be able to advance all the metrics you need.
class User
{
public int HighScore { get; private set; } = 0;
public double AverageScore =>
this.GamesPlayed > 0 ? this.TotalScore / (double)this.GamesPlayed : 0;
private int GamesPlayed { get; set; } = 0;
private int TotalScore { get; set; } = 0;
public void GameOver(int score)
{
this.HighScore = Math.Max(this.HighScore, score);
this.GamesPlayed += 1;
this.TotalScore += score;
}
}
You can store the average and then recalculate after a game finishes. This way you don't need to store a value (totalscore) that will cause overflow issues (sooner or later).
class User
{
public int HighScore { get; private set; } = 0;
public double AverageScore { get; private set; } = 0;
private int GamesPlayed { get; set; } = 0;
public void GameOver(int score)
{
this.HighScore = Math.Max(this.HighScore, score);
// get the prev total score then increase with the current score and get the new average in the end (also increase the GamesPlayed)
this.AverageScore = ((this.AverageScore * this.GamesPlayed) + score) / ++this.GamesPlayed;
}
}

Categories

Resources