public void Console(List<Keys> keys)
{
clickNo ++;
start = DateTime.Now;
progressBar1.Maximum = 1;
progressBar1.Step = 1;
progressBar1.Value = 0;
switch (clickNo)
{
case 1:
DoRequest(ScreenshotRequest.DannysCommands.NormalOperation); break;
case 2:
DoRequest(ScreenshotRequest.DannysCommands.Displayoverlays); break;
case 3:
DoRequest(ScreenshotRequest.DannysCommands.Dontdisplayoverlays); break;
}
}
clickNo is global int variable.
I make the three cases in this case it's by using the keys
CTRL + G
But I want that after three times if I make again
CTRL + G it will start over again case 1 case 2 case3 ...
Now after the third case it's not doing anything.
You could reset the counter after the last case:
case 3:
DoRequest();
clickNo = 0;
break;
public void Console(List<Keys> keys)
{
clickNo ++;
start = DateTime.Now;
progressBar1.Maximum = 1;
progressBar1.Step = 1;
progressBar1.Value = 0;
switch (clickNo)
{
case 1:
DoRequest(ScreenshotRequest.DannysCommands.NormalOperation);
break;
case 2:
DoRequest(ScreenshotRequest.DannysCommands.Displayoverlays);
break;
case 3:
DoRequest(ScreenshotRequest.DannysCommands.Dontdisplayoverlays);
clickNo = 0;
break;
}
}
Just set to 0 in the last case.
Related
Trying to make snake game. I am using Console.SetCursorPosition(int x, int y) to draw body parts("O"), 2D array to store "O" previous locations and Console.KeyAvailable to detect direction changes. When a key is pressed that is not in the switch statement, drawing stops for one iteration. When a key is pressed that is in the switch statement, Console erases oldest body part.
Debugger doesn't show the reason. Can anyone help me to understand why is that happening?
static void Main(string[] args)
{
Console.CursorVisible = false;
while (true)
{
ConsoleKey key = Console.ReadKey().Key;
switch (key)
{
case ConsoleKey.RightArrow:
direction = "RIGHT";
break;
case ConsoleKey.LeftArrow:
direction = "LEFT";
break;
case ConsoleKey.DownArrow:
direction = "DOWN";
break;
case ConsoleKey.UpArrow:
direction = "UP";
break;
}
breakOut = false;
switch (direction)
{
case "RIGHT":
for (int i = x; i <= 100; i++)
{
Iterator(i, y);
x = i;
if (breakOut)
break;
};
break;
case "LEFT":
break;
case "DOWN":
for (int i = y; i <= 100; i++)
{
Iterator(x, i);
y = i;
if (breakOut)
break;
}
break;
case "UP":
break;
}
}
}
static void Iterator(int x, int y)
{
Draw(x, y);
xSlot = x;
ySlot = y;
histPoints[slotCounter, (int)place.x] = xSlot;
histPoints[slotCounter, (int)place.y] = ySlot;
if (slotCounter >= length)
{
Erase(histPoints[slotCounter - length, (int)place.x], histPoints[slotCounter - length, (int)place.y]);
}
slotCounter += 1;
if (Console.KeyAvailable)
{
breakOut = true;
return;
}
Sleep();
}
Hi everyone I have a question about how to make this more clean, reusable and readable. I have some data models (relics) and this has rarity and level. Depending on the level and the rarity it has to be grouped to later apply this in some math calculation. For this reason, I need to parse all my relics and check for the level and rarity and store in a var counter.
public double TotalGlobalBonus
{
get
{
float commonRarityMultiplier = 20;
float rareRarityMultiplier = 120;
float epicRarityMultiplier = 320;
float legendaryRarityMultiplier = 540;
int rarityCommonLevel1 = 0;
int rarityCommonLevel2 = 0;
int rarityCommonLevel3 = 0;
int rarityCommonLevel4 = 0;
int rarityCommonLevel5 = 0;
int rarityRareLevel1 = 0;
int rarityRareLevel2 = 0;
int rarityRareLevel3 = 0;
int rarityRareLevel4 = 0;
int rarityRareLevel5 = 0;
int rarityEpicLevel1 = 0;
int rarityEpicLevel2 = 0;
int rarityEpicLevel3 = 0;
int rarityEpicLevel4 = 0;
int rarityEpicLevel5 = 0;
int rarityLegendaryLevel1 = 0;
int rarityLegendaryLevel2 = 0;
int rarityLegendaryLevel3 = 0;
int rarityLegendaryLevel4 = 0;
int rarityLegendaryLevel5 = 0;
foreach (RelicModel relic in this.equipedRelics)
{
switch (relic.rarity)
{
case RarityType.COMMON:
switch (relic.Level)
{
case 1:
rarityCommonLevel1++;
break;
case 2:
rarityCommonLevel2++;
break;
case 3:
rarityCommonLevel3++;
break;
case 4:
rarityCommonLevel4++;
break;
case 5:
rarityCommonLevel5++;
break;
}
break;
case RarityType.RARE:
switch (relic.Level)
{
case 1:
rarityRareLevel1++;
break;
case 2:
rarityRareLevel2++;
break;
case 3:
rarityRareLevel3++;
break;
case 4:
rarityRareLevel4++;
break;
case 5:
rarityRareLevel5++;
break;
}
break;
case RarityType.EPIC:
switch (relic.Level)
{
case 1:
rarityEpicLevel1++;
break;
case 2:
rarityEpicLevel2++;
break;
case 3:
rarityEpicLevel3++;
break;
case 4:
rarityEpicLevel4++;
break;
case 5:
rarityEpicLevel5++;
break;
}
break;
case RarityType.LEGENDARY:
{
switch (relic.Level)
{
case 1:
rarityLegendaryLevel1++;
break;
case 2:
rarityLegendaryLevel2++;
break;
case 3:
rarityLegendaryLevel3++;
break;
case 4:
rarityLegendaryLevel4++;
break;
case 5:
rarityLegendaryLevel5++;
break;
}
break;
}
}
}
double common = (commonRarityMultiplier / 100) * (rarityCommonLevel1 * 1 + rarityCommonLevel2 * 5 +
rarityCommonLevel3 * 10 + rarityCommonLevel4 * 20 +
rarityCommonLevel5 * 40);
double rare = (rareRarityMultiplier / 100) * (rarityRareLevel1 * 1 + rarityRareLevel2 * 5 +
rarityRareLevel3 * 10 + rarityRareLevel4 * 20 +
rarityRareLevel5 * 40);
double epic = (epicRarityMultiplier / 100) * (rarityEpicLevel1 * 1 + rarityEpicLevel2 * 5 +
rarityEpicLevel3 * 10 + rarityEpicLevel4 * 20 +
rarityEpicLevel5 * 40);
double legendary = (legendaryRarityMultiplier / 100) * (rarityLegendaryLevel1 * 1 + rarityLegendaryLevel2 * 5 +
rarityLegendaryLevel3 * 10 + rarityLegendaryLevel4 * 20 +
rarityLegendaryLevel5 * 40);
double final = common + rare + epic + legendary;
return final;
}
}
It is a really longe property and will grow if the number of levels grows, so this is not precisely scalable
You need to put all these values in a Dictionary with keys of type RarityType and values of type int[].
var rarity = new Dictionary<RarityType, int[]>();
rarity[RarityType.COMMON] = new int[6];
rarity[RarityType.RARE] = new int[6];
rarity[RarityType.EPIC] = new int[6];
rarity[RarityType.LEGENDARY] = new int[6];
foreach (RelicModel relic in this.equipedRelics)
{
rarity[relic.rarity][relic.Level]++;
}
The first element of each array, the one having index = 0, is intended to be unused.
I have some months read into an array from an external text file and I need to convert the months to an array that holds the value equivalent of the months e.g. January = 1, February = 2 etc. So that they can then be put through Quicksort.
public static void Main(string[] args)
{
//Read external files into arrays.
string[] Month = File.ReadLines(#"Month.txt").ToArray();
string[] Year = File.ReadLines(#"Year.txt").ToArray();
//Convert arrays from string to double to be used in sort.
double[] YearSort = Array.ConvertAll(Year, double.Parse);
int UserInput1;
//Create new array that will hold selected array to be used in sort.
double[] data = new double[1022];
//Prompt user to select action.
Console.WriteLine("Press 1 to Sort by month or 2 to sort by year.");
UserInput1 = Convert.ToInt32(Console.ReadLine());
if(UserInput1 == 1)
{
Array.Copy(Month,data,1022);
QuickSort(data);
for (int i = 0; i < 1022; i++)
Console.WriteLine(data[i]);
Console.WriteLine();
}
else if (UserInput1 == 2)
{
Array.Copy(YearSort,data,1022);
QuickSort(data);
for (int i = 0; i < 1022; i++)
Console.WriteLine(data[i]);
Console.WriteLine();
}
else
{
Console.WriteLine("Please try again and select a valid option");
}
}
static int MonthToDouble( string Month )
{
int NewMonth = 0;
switch(Month)
{
case "January":
case "january":
NewMonth = 1;
break;
case "February":
case "february":
NewMonth = 2;
break;
case "March":
case "march":
NewMonth = 3;
break;
case "April":
case "april":
NewMonth = 4;
break;
case "May":
case "may":
NewMonth = 5;
break;
case "June":
case "june":
NewMonth = 6;
break;
case "July":
case "july":
NewMonth = 7;
break;
case "August":
case "august":
NewMonth = 8;
break;
case "September":
case "september":
NewMonth = 9;
break;
case "October":
case "october":
NewMonth = 10;
break;
case "November":
case "november":
NewMonth = 11;
break;
case "December":
case "december":
NewMonth = 12;
break;
}
return NewMonth;
}
static string DoubleToMonth(double Month)
{
string NewMonth = "";
switch ((int)Month)
{
case 1:
NewMonth = "January";
break;
case 2:
NewMonth = "February";
break;
case 3:
NewMonth = "March";
break;
case 4:
NewMonth = "April";
break;
case 5:
NewMonth = "May";
break;
case 6:
NewMonth = "June";
break;
case 7:
NewMonth = "July";
break;
case 8:
NewMonth = "August";
break;
case 9:
NewMonth = "September";
break;
case 10:
NewMonth = "October";
break;
case 11:
NewMonth = "November";
break;
case 12:
NewMonth = "December";
break;
}
return NewMonth;
}
//QuickSort for double data values are in ascending order.
public static void QuickSort(double[] data)
{
Quick_Sort(data, 0, data.Length - 1);
}
public static void Quick_Sort(double[] data, int left, int right)
{
int i, j;
double pivot, temp;
i = left;
j = right;
pivot = data[(left + right) / 2];
do
{
while ((data[i] < pivot) && (i < right)) i++;
while ((pivot < data[j]) && (j > left)) j--;
if (i <= j)
{
temp = data[i];
data[i] = data[j];
data[j] = temp;
i++;
j--;
}
} while (i <= j);
if (left < j) Quick_Sort(data, left, j);
if (i < right) Quick_Sort(data, i, right);
}
}
A DateTime object's Month property gives you the integer value of the month starting at 1 like you need. So you can use DateTime.ParseExact() to parse the string into a full DateTime object then grab the Month property:
int monthNumber = DateTime.ParseExact("January", "MMMM", CultureInfo.CurrentCulture).Month;
You just need to replace "January" with your month strings, and leave "MMMM"which is the Custom Format String for "The full name of the month".
All the above code does is simplify your MonthToDouble() method, which you are not even using for some reason (also it should return a double, not an int). Contrary to your title, you already have a method to "Convert months to number equivalent", you just aren't using it.
So, I assume the only thing you are missing is to replace this:
Array.Copy(Month,data,1022);
QuickSort(data);
With this:
double[] monthsAsDoubles = new double[Month.Length];
for (int i = 0; i < monthsAsDoubles.Length; i++)
{
monthsAsDoubles[i] = MonthToDouble(Month[i]);
}
QuickSort(monthsAsDoubles);
Also change the return value of MonthToDouble() from int to double (cast if you need to).
Edit: On second thought, Quantic's answer is simpler. I'll just leave this here as an alternative.
Your code can be simplified a lot by using the DateTimeFormatInfo.MonthNames property, along with a case-insensitive string comparison. This also has the advantage of being much easier to port to use different languages for the month names.
Here is a snippet:
using System;
using System.Globalization;
using System.Linq;
using System.Collections.Generic;
public class Test
{
public static void Main()
{
var InputMonths = new List<string> { "January","march","sepTEmber","smarch" };
var MonthNames = new DateTimeFormatInfo().MonthNames.ToList();
var InputMonthNumbers = new List<int>();
foreach (var m in InputMonths)
{
//Find index of the month name, ignoring case
//Note if the input month name is invalid, FindIndex will return 0
int month_num = 1 + MonthNames.FindIndex(name => name.Equals(m, StringComparison.OrdinalIgnoreCase));
if (month_num > 0)
{
InputMonthNumbers.Add(month_num);
}
}
foreach (var n in InputMonthNumbers)
{
Console.WriteLine(n.ToString());
}
}
}
output:
1
3
9
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I'm developing a text-based RPG and I'm implementing a stat system. My code for the classes that interact with it is:
switch (charClass)
{
case "a":
Player.charC(0);
break;
case "b":
Player.charC(1);
break;
case "c":
Player.charC(2);
break;
}
public static void charC(int charPath)
{
switch (charPath)
{
case 0:
Player.stats(0);
break;
case 1:
Player.stats(1);
break;
case 2:
Player.stats(2);
break;
}
}
public static void stats(int stat)
{
Player p = new Player();
switch (stat)
{
case 0:
p.m_health = 200;
p.m_mana = 75;
p.fast = 7;
p.strng = 20;
p.smrt = 7;
p.move = 7;
p.level = 1;
break;
case 1:
p.m_health = 100;
p.m_mana = 200;
p.fast = 10;
p.strng = 7;
p.smrt = 15;
p.move = 7;
p.level = 1;
break;
case 2:
p.m_health = 100;
p.m_mana = 100;
p.fast = 10;
p.strng = 10;
p.smrt = 10;
p.move = 10;
p.level = 1;
break;
}
}
When I try to run it, it returns all the stats as 0.
Example: MAX HEALTH 0. MAX MANA 0. FAST 0. STRONG 0. SMART 0.
In the stats method you're not returning the player object. You create a new player, set its properties, but discard the object at the end of the method.
In short: You're not working on the object you think you're working on.
Why is all this static?
You need to remove the static-ness of stats and call it with the instance of the player
public void stats(int stat)
{
switch (stat)
{
case 0:
m_health = 200;
m_mana = 75;
fast = 7;
strng = 20;
smrt = 7;
move = 7;
break;
case 1:
m_health = 100;
m_mana = 200;
fast = 10;
strng = 7;
smrt = 15;
move = 7;
break;
case 2:
m_health = 100;
m_mana = 100;
fast = 10;
strng = 10;
smrt = 10;
move = 10;
break;
}
level = 1;
}
use the instance of player in both switches, i.e
switch (charClass)
{
case "a":
playerInstance.charC(0);
break;
case "b":
playerInstance.charC(1);
break;
case "c":
playerInstance.charC(2);
break;
}
public void charC(int charPath)
{
switch (charPath)
{
case 0:
this.stats(0);
break;
case 1:
this.stats(1);
break;
case 2:
this.stats(2);
break;
}
}
I have code that iterates trough all cells how can I make each 4x4 cell to be in different color?
Here is my code:
int c = ran.Next(1, 5);
for (int i = 0; i < box_width; i++)
{
for (int j = 0; j < box_height; j++)
{
switch (c)
{
case 1:
MyClass.grid.Rows[j].Cells[i].Style.BackColor = Color.Yellow;
break;
case 2:
MyClass.grid.Rows[j].Cells[i].Style.BackColor = Color.LightGray;
break;
case 3:
MyClass.grid.Rows[j].Cells[i].Style.BackColor = Color.LightBlue;
break;
case 4:
MyClass.grid.Rows[j].Cells[i].Style.BackColor = Color.Blue;
break;
}
}
}
This code fills all grid with color that is randomly picked. I want it to split it into some X x X dimension with different color.
Thank you
int c = ran.Next(1, 5);
for (int i = 0; i < box_width; i += 2)
{
for (int j = 0; j < box_height; j += 2)
{
Color cellColor;
switch (c)
{
case 1:
cellColor = Color.Yellow;
break;
case 2:
cellColor = Color.LightGray;
break;
case 3:
cellColor = Color.LightBlue;
break;
case 4:
cellColor = Color.Blue;
break;
}
MyClass.grid.Rows[j].Cells[i].Style.BackColor = cellColor;
MyClass.grid.Rows[j].Cells[i+1].Style.BackColor = cellColor;
MyClass.grid.Rows[j+1].Cells[i].Style.BackColor = cellColor;
MyClass.grid.Rows[j+1].Cells[i+1].Style.BackColor = cellColor;
}
}
This assumes that box_width and box_height are the same as the number of rows and cells in your DataGridView.
Let me know if that works for you, I haven't tested it but it seemed right in my head.
However, this will throw an exception if the cells are not in an even 4x4 multiple. Are you sure they will always be multiples of 4x4? If not you have to add error checking.