Is there any way to have a int value that is multiple values?
Under here you can see the code, im doing a minigame as a proof of concept.
currently i have x happen at loop number 4 and y happens at loop number 9.
Is there any way to repeat theese actions on other loops wihout maing it a huge mess like this: i == 4||i == 14 || i == 18
what im trying to ask is if its possible to just write i == a
and then have a be multiple values.
If you havent figured already, im quite the beginner at C#, so if you can explain it in a easy to understand way, i would be very thankfull.
If what i wrote is a bit hard to understand, then im sorry for not being a native english speaker, just ask if you're unsure of what i mean.
for (int i = 0; i < 50; i++)
{
string input;
input = Console.ReadKey().Key.ToString();
Console.Clear();
if (input == "A") Animation.frame1();
else Animation.fall();
if (i == 4) Animation.blocklow2();
if (i == 9) Animation.blockhigh2();
input = Console.ReadKey().Key.ToString();
Console.Clear();
if (input == "W" && i == 4) Animation.blockjump();
if (input == "S" && i == 9) Animation.blockduck();
if (input == "D" && i != 4 && i != 9) Animation.frame2();
else if (input != "W" && i == 4) Animation.fall();
else if (input != "S" && i == 9) Animation.smack();
if (i == 3) Animation.blocklow();
if (i == 8) Animation.blockhigh();
}
You could do if (new int[] { 4, 14, 18 }.Contains(i)) to check for multiple values.
Store a list of ints that you want to check against and then use .Contains
List<int> frame2ints = new List<int>{4,9};
if (input == "D" && !frame2ints.Contains(i)) Animation.frame2();
In certain situations you can perform bitwise tests to see whether some bits are set in your number:
if ((x & 4) != 0)
{
// This case covers numbers
// 4, 5, 6, 7, 12, 13, 14, and many others...
}
General answer to your question is NO, and it only makes sense to use additional knowledge about the values you are testing and then use that knowledge.
Another option is to test whether a number belongs to a sequence, like HHLV said in other answer.
So, how did you get to values 4, 14 and 18?
Related
I have a program I'm upgrading from Android 7.1.1 to now 8.1. Most things have gone incredibly smoothly, but I have hit a snag on one thing.
I have a postal code entry using an EditText. This entry begins initially with an InputType of ClassText and as the user enters their characters, I am swapping back and forth from ClassText to ClassNumber to get a resulting format such as: "M1N2J8"
On 7.1.1 this works, no problems. However, the behavior seems to have changed in 8.1. When I enter "M" and then "1", I have no problem, but after I have changed it back to ClassText to enter the "N" it wipes out the "1" and leaves me with "MN"
I would greatly appreciate any insights anyone may be able to offer on how I can get around this. Worst case scenario, I'll simply enable an alphanumeric keyboard and handle the restricting the characters myself, but my client greatly prefers to have the keyboards swap between text and number entry, so that's my goal right now.
Thanks!
if (txt.SelectionStart == 0 || txt.SelectionStart == 2 || txt.SelectionStart == 4)
{
txt.InputType = InputTypes.ClassText | InputTypes.TextFlagCapCharacters;
txt.SetFilters(new IInputFilter[] { new InputFilterLengthFilter(maxLength), new AlphaInputFilter() });
}
else if (txt.SelectionStart == 1 || txt.SelectionStart == 3 || txt.SelectionStart == 5)
{
txt.InputType = InputTypes.ClassNumber;
txt.SetFilters(new IInputFilter[] { new InputFilterLengthFilter(maxLength), new NumberInputFilter() });
}
You don't need to use the second InputFilter, Your final output is alphanumeric and won't fit on any of them
if (txt.SelectionStart == 0 || txt.SelectionStart == 2 || txt.SelectionStart == 4)
{
txt.InputType = InputTypes.ClassText | InputTypes.TextFlagCapCharacters;
txt.SetFilters(new IInputFilter[] { new InputFilterLengthFilter(maxLength) });
}
else if (txt.SelectionStart == 1 || txt.SelectionStart == 3 || txt.SelectionStart == 5)
{
txt.InputType = InputTypes.ClassNumber;
txt.SetFilters(new IInputFilter[] { new InputFilterLengthFilter(maxLength) });
}
I'm making a game in unity, and I have this 'if statement' that by every 5 waves my shop menu will become visible. The code does work, but I am certain I'm doing something wrong or could do something better!
if (waveCount == 5 || waveCount == 10 || waveCount == 15 || waveCount == 20 || waveCount == 25 || waveCount == 30 || waveCount == 35 || waveCount == 40 || waveCount == 45 || waveCount == 50)
{
// yield return new WaitForSeconds(shopWait);
shopPanel.SetActive(true);
}
As you can see the 'if statement' not that good, normally it continues all the way to waveCount == 100 but i cut that out. There must be a simpler or cleaner way to do this :/ but i just can't wrap my head around it :(
Edit 1:
Thanks, I didn't know much about modulo, know I know what I have to read about :)
You can use modulo operation:
if (waveCount % 5 == 0)
Yes, there are indeed simpler ways of doing this. If you use a little bit of maths and logic, you can figure this out.
Since you want to check whether the value of waveCount is a multiple of 5, you can use % to get the reminder of waveCount / 5. If that reminder is 0, waveCount is a multiple of 5.
if (waveCount % 5 == 0 && waveCount <= 100)
I added waveCount <= 100 to replicate your code's behaviour when waveCount is larger than 100 i.e. not get into the if statement.
Alternatively, you can put all the values into a list:
var list = new List<int>();
for (int i = 1 ; i <= 20 ; i++) {
list.Add(i * 5);
}
And then check whether the list contains the number:
if (list.Contains(waveNumber))
The advantage of this is that if you decided to change how the game works and say that the shop menu can be opened at waves 9, 52, and 77, you just add the numbers to the list, without modifying the if statement. This provides a lot of flexibility.
if (waveCount % 5 == 0 && waveCount <= 50) {
//...code
}
If your “if” statement's body just contains shopPanel.SetActive(true); you can do that without even using “if” like that.
shopPanel.SetActive(waveCount % 5 == 0 && waveCount <= 50);
Give it a try
if (waveCount % 5 == 0 && waveCount <= 50)
Use the modulo-operator:
if(waveCount % 5 == 0 && waveCount <= 100) ...
The operator calculates the remainder of an integer-divison. In your case the statement should return zero indicating that your number divided by 5 has no remainder.
Just to generalize: in case the data you have doesn't match a pattern, you can put all the things to match against in a set, then test the set for membership:
var thingsToMatch = Set(2, 5, 8, 14, 23, 80, 274...);
if (someNumber in thingsToMatch) {...}
As long as you know the set isn't being recreated everytime the function is called, this has proven to be fairly fast. If your language doesn't automatically cache the set, you can make it a static variable of the function.
You can use the remainder operator for this:
if (waveCount % 5 == 0 && waveCount > 0 && waveCount <= 50)
{
//yield return new WaitForSeconds(shopWait);
shopPanel.SetActive(true);
}
You can test whether the remainder of the division by 5 is 0, which means that the number is divisible by 5.
if (waveCount % 5 == 0 && waveCount >= 5 && waveCount <= 50)
C# performs integer math on integer number types (int, long, uint, ...).
Example:
13 / 5 = 2
I.e. you never get a decimal fraction part. The complementary operation is the modulo operation. It gives you the remainder of this division:
13 % 5 = 3
I.e., 13 / 5 is 2 plus remainder 3. Together, division and modulo operation allow you to perform the reverse operation.
(5 * (13 / 5)) + (13 % 5) =
(5 * 2 ) + ( 3 ) = 13
If you have irregular figures, quite different approaches are to use a switch statement:
switch (waveCount) {
case 5:
case 10:
case 15:
case 20:
case 25:
case 30:
case 35:
case 40:
case 45:
case 50:
shopPanel.SetActive(true);
break;
}
or an array of allowed values:
private static readonly int[] AllowedValues =
new int[] { 5, 10, 15, 20, 25, 30, 35, 40, 45, 50 };
if(Array.IndexOf(AllowedValues, waveCount) >= 0) { ... }
I'm kinda new to this thread, but in short summary i'm having trouble with a small validation issue. Basically, i'm attempting to make a simple validation program that will collect a user's input and then determine if the input is valid in terms of, input being correctly implemented, the number being positive, and the number having to be either 0, 1, 2, 3, 4 or 5.
The overall program runs, but the issue i'm facing is every form of input is deemed an error, thus displaying my error statement, even if it is a valid input like 5 for example. I feel like there is a small mistake,i have made that is causing this, So is there any suggestions?
int user_input;
int count = 0;
do
{
Console.Write("\n\nUser Input:"
if ((int.TryParse(Console.ReadLine(), out user_input) == false)||(user_input < 0 || user_input != 0 ||user_input != 1 || user_input != 2
|| user_input != 3 || user_input != 4 || user_input != 5))
{
Console.WriteLine("Error : the action entered is not a valid number.");
count = 0;
}
else
count = 1;
Your mistake is because you use OR operator.
For example user print 3. In your statements one condition return false (input != 3), but all others return true.. That's why you always go into if condition..
You can use AND operator, but I can recommend you to simplify your condition. It will be more understandable and readable:
var count = 0;
do
{
Console.Write("\n\nUser Input:");
int user_input;
if ((int.TryParse(Console.ReadLine(), out user_input) == false) || (user_input < 0 || user_input > 5))
{
Console.WriteLine("Error : the action entered is not a valid number.");
count = 0;
}
else
count = 1;
}
while (count != 1);
You already got an answer about the problem with predicate logic. But you can even more simplify with linq like:
var count = (new string[]{"1","2","3","4","5"}).Contains(Console.ReadLine()) ? 1 : 0;
In casino slot games you often have a Wild game piece. What would be a good way of including this mechanic into comparing with 2 other pieces? e.g. given 3 game pieces [Cherry][Cherry][Joker] would be a match.
The code I'm using right now seems really overweight, is there anything that can be done (think bitwise operators?) to make it easier to work with?
if ((box1.BoxRank == box2.BoxRank ||
box1.BoxRank == BoxGameObject.Ranks.Joker ||
box2.BoxRank == BoxGameObject.Ranks.Joker) &&
(box1.BoxRank == box3.BoxRank ||
box1.BoxRank == BoxGameObject.Ranks.Joker ||
box3.BoxRank == BoxGameObject.Ranks.Joker) &&
(box2.BoxRank == box3.BoxRank ||
box2.BoxRank == BoxGameObject.Ranks.Joker ||
box3.BoxRank == BoxGameObject.Ranks.Joker))
{
// has 3 of a kind, or 1 joker and 2 of a kind, or 2 jokers and 1 other
return true;
}
This is easier if you think of the operation in terms of the set of the value of all of the boxes. Just remove all of the jokers from that set and then verify that all of the values are identical:
var allRanks = new[]{box1.BoxRank, box2.BoxRank, box3.BoxRank};
var threeOfAKind = allRanks.Where(rank => rank != BoxGameObject.Ranks.Joker)
.Distinct()
.Count() < 2;
First just remove all of the jokers. If, after doing that, there are two or more distinct values then we do not have a three of a kind.
Yes, represent the Joker as an int with all binary 1's, like 7, 15 or 31.
Represent the cherry and others with an int with only singe binary 1 like 1,2,4,8 smaller than the Joker. Leave zero unused.
Then, using bitwise AND your condition is equivalent to:
(box1.BoxRank & box2.BoxRank & box3.BoxRank) > 0
Note that 3 Jokers will satisfy the condition too.
I have two positions on a 3D system, say [15, 32, 42] and [16, 32, 42]
Is there a easy way to check if they are within a 1 block radius from each other?
This is what I have, but is there a better way of doing it:
if (pos[0] == pos1[0] / 32 || pos[0] == pos1[0] + 1 || pos[0] == pos1[0] - 1)
{
if (pos[1] == pos1[1] || pos[1] == pos1[1] - 1 || pos[1] == pos1[1] + 1)
{
if (pos[2] == pos1[2] || pos[2] == pos1[2] + 1 || pos[2] == pos1[2] - 1)
{
Thanks,
David
You can use Math.abs(pos[0]-pos1[0]) <= 1 to check if two coordinates in the same plane are at most 1 apart.
So all in all, your code could look like this:
if( Math.abs(pos[0]-pos1[0]) <= 1
&& Math.abs(pos[1]-pos1[1]) <= 1
&& Math.abs(pos[2]-pos1[2]) <= 1 )
{
Within a 1 block radius
}
Note that I do not understand why you divided your first equation by 32. I did not include that in this answer.
Note also that this solution makes things a little more readable, but that yours is correct too.
I haven't done this in c# but in Java I use JTS. http://geoapi.codeplex.com/ seems to provice the same functionality in c#. Then you will represent your points as Point objects and have all sorts of useful geospatial functions to use.
But for this case, are you looking for the "as the crow flies" distance, which is just pythagoras, or the "walking distance", which would involve finding the shortest valid route in a directed graph of footpaths?
Julian