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.
Related
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?
I've a bit problem with a short circuit condition, what I need to do is check if an array have almost 2 indexes or a string is equal to a certain value, to recreate the problem here, suppose this:
string[] favItem = new string[] { "hello", "world", "test", "foo" };
string temp = "hello";
var itemToRemove = temp.Split(',');
foreach(var fav in favItem)
{
if(fav == "foo" || (itemToRemove.Length > 1 & fav == itemToRemove[0] || fav == itemToRemove[1]))
{
//do something
}
}
so essentially I need to compare fav with a default value, if this fail, I need to check if the itemToRemove array have almost 2 indexes, if yes I need to compare the fav value in iteration, with the two indexes of itemToRemove.
Now suppose that in itemToRemove there is only one index, I'm not able to exit from this:
(itemToRemove.Length > 1 & fav == itemToRemove[0] || fav == itemToRemove[1])
in particular with & I'm trying to exit from the condition if the indexes aren't two.
In the debug mode I can see the false value with the breakpoint, but I don't understand why the code fall to:
OutOfRangeException
with itemToRemove[1] when should be leave the condition.
What am I doing wrong?
Your code has two problems.
First, the short-circuiting boolean "and" operator is &&. & is the bitwise and operator, and does not short circuit.
Secondly, && has a higher precedence than ||, so you need to group the second || together like this (itemToRemove.Length > 1 && (fav == itemToRemove[0] || fav == itemToRemove[1])). The rule of thumb to remember precedence is that and is like multiplication 0 && 1 = 0, while or is like addition 0 || 1 = 1.
Short circuit is && not &.
I have this Or condition in an IF statement (in a foreach loop) in a Windows Form C# program:
if ((splittedFile.Count() != 3) || (splittedFile.Count() != 4))
continue;
and it always does continue, even if splittedFile.Count() is 3 or 4.
The thing is that if I remove the Or condition:
if ((splittedFile.Count() != 4))
continue;
it works properly!! Any ideas why?
This is the correct behavior, you need to use &&.
The reason is that the count is fixed number, let's say n. Now the condition reads:
n is not 3 or n is not 4.
Given n is 4, this means it is not 3, thus the test succeeds and vice versa.
A compiler can't detect this is trivially true, because between the two if statements, the Count() might change (for instance in a multithreading setting where the second thread would add/remove something to/from the collection). I agree however some analysis tools could be capable in some conditions to detect such trivial behavior. In general however such analysis can't be implemented because of Rice's theorem.
If you use &&, the expression reads:
n is not 3 and n is not 4.
Thus both conditions should be true. In other words only if n is less than three and greater than 4, the condition holds.
Try:
if ((splittedFile.Count() != 3) && (splittedFile.Count() != 4))
continue;
I know || sound logical because : if splittedFile.count is not 3 OR it is not 4 then continue; But because there are 2 NOT ! operators in the if expression an AND && is needed.
put a real number into your expression:
if( 3 != 3 || 3 != 4)
which is
if( false || true )
your expression will always be true, as splittedFile.Count() is always (not 3) or (not 4)
you want to && your results together, which looks like
(x != 3) || (x != 4)
or
!( x == 3 || x == 4)
Because one of your expressions will be always true. That's why true || something always returns true.
Let's analyze your splittedFile.Count() is 3, 4 and other than these values.
For 3, your expression will be false || true and this returns true.
For 4, your expression will be true || false and this returns true.
For other than 3 or 4, your expression will be true || true and this returns true.
Strongly suspect you are looking for && operator which provides logical-AND.
Relevant misc info:
Project = Tic Tac Toe (console app)
My issue is the following:
I have an array that initially is filled with 0's which MUST stay as 0's unless the user takes a move (then it'll become a 1) or the computer makes a move then it becomes a 2.
Example code:
else if (posStatus[2] != 0 && posStatus[5] != 0 && posStatus[8] != 0 && (posStatus[2] + posStatus[5] + posStatus[8] % 2 == 0))
{
if (posStatus[2] == 0)
{
posStatus[2] = 2;
return;
}
else if (posStatus[5] == 0)
{
posStatus[5] = 2;
return;
}
else if (posStatus[8] == 0)
{
posStatus[8] = 2;
return;
}
issue:
There is a grid that is 9 long which three rows, 3 columns and two diagonals. Initially all values are set to 0 and this is used to determine if the space is free so changing to another type would cause issues. If the value 1 is held player 1 has played there. If the value 2 is held the computer has played there.
So originally I had put in
posStatus[0] + posStatus[1] + posStatus[2] % 2 == 0)
Which decides where the computer will play in this case it will be the first row if the enemy player has put 2 values there which logically should work perfectly and it does apart from my issue which is as follows:
When nothing is played on a row the values are: 0, 0 ,0 if you do 0 + 0 + 0 % 2 you'll discover that equals 0 which is the parameter for the above condition. This means it will attempt to make sure there is a value in each row/column/diagonal which of course is not good as it means the player can win in 3 turns..
So to combat this I added in (I'm aware I can refactor it) I added:
posStatus[0] != 0 && posStatus[1] != 0 && posStatus[2] != 0
Which means if a row/column/diagonal is empty do not play there which means it never plays in empty rows/columns/diagonals which is an issue.
Suggestions?
You have to check few conditions for each time, need not to consider the sequence. Possible cases of win for any player would be:
0,1,2
0,3,6
0,4,8
1,4,7
2,4,6
2,5,8
3,4,5
AND 6,7,8
I am assuming that numbering is row wise and started from 0.
Now you have to tell computer based on the input of user.
For example:
User has selected 2, so you have posStatus[2] = 1 Now the only cells you have to check are 0,1,4,5 and 6 as these are the only possible cells for win.
To handle the beginning of game situation, you have two options:
Let the user start always.
Start anyone from 0,2,6 and 8 as these are part of the maximum solutions.
Another Approach:
There are 3 possibilities of success:
A. Row wise : Add or substitute number by 1 and check status ,
B. Column wise : Add or substitute number by 3 and check status ,
C. Diagonally : Add or substitute number by 2 AND 4 then check status
For example if user has selected i cell. Your check should be:
A. See the status of (i-2),(i-1),(i+1) AND (i+2) - Obviously this should be in range of 0-8
B. See the status of (i-6),(i-3),(i+3) AND (i+6)
C. See the status of (i-8),(i-4),(i-2),(i+2),(i+4) AND (i+8)
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