Method Name Expected Error- C# - c#

About the code:
I am making a windows form Application for TIC TAC TOE game. This is one of the methods in it. It checks for the winner diagonally,horizontally,vertically.
Error:
In right hand side of the && statement, i get an error saying "Method name expected". I am not able to figure out the error. I hope someone could help.
private void checkForwinner()
{
bool there_is_a_winner= false;
//horizontal check
if((A1.Text==A2.Text)&& (A2.Text==A3.Text)(!A1.Enabled))
there_is_a_winner=true;
else if((B1.Text==B2.Text)&& (B2.Text==B3.Text)(!A2.Enabled))
there_is_a_winner=true;
else if ((C1.Text == C2.Text) && (C2.Text == C3.Text)(!A3.Enabled))
there_is_a_winner = true;
//Vertical Check
else if ((A1.Text == B1.Text) && (B1.Text == C1.Text)(!A1.Enabled))
there_is_a_winner = true;
else if ((A2.Text == B2.Text) && (B2.Text == C2.Text)(!B1.Enabled))
there_is_a_winner = true;
else if ((A3.Text == B3.Text) && (B3.Text == C3.Text)(!C1.Enabled))
there_is_a_winner = true;
//Diagonal Check
else if ((A1.Text == B2.Text) && (B2.Text == C3.Text)(!A1.Enabled))
there_is_a_winner = true;
else if ((A3.Text == B2.Text) && (B2.Text == C1.Text)(!C1.Enabled))
there_is_a_winner = true;
}

You are missing && in every if statement.
Also, always give a space before apllying && operator. Try making your code easier to read.
if((A1.Text==A2.Text) && (A2.Text==A3.Text)(!A1.Enabled))
there_is_a_winner=true;
Use
if((A1.Text==A2.Text) && (A2.Text==A3.Text) && (!A1.Enabled))
there_is_a_winner=true;
Similarly, do in all if-else statements.

Related

Issue with Unity Update Loop

I was playing a little in Unity on a project and I stumbled upon an issue I can't address. Please keep in mind I am a beginner, and my understanding of Unity is fairly limited.
So the issue is this..
I wanted to test some if statement that went like this:
void Update()
{
if (isRow1Good() || isRow2Good() || isRow3Good() || isRow4Good() || isRow5Good() ||
isRow6Good() || isRow7Good() || isRow8Good() || isRow9Good() || isRow10Good())
{
Debug.Log("LOL");
}
}
The content of the functions is this:
Piece p1 = row1[0].ReturnPiece();
Piece p2 = row1[1].ReturnPiece();
Piece p3 = row1[2].ReturnPiece();
Piece p4 = row1[3].ReturnPiece();
if (p1.isTall && p2.isTall && p3.isTall && p4.isTall)
{
return true;
}
else if (p1.isRed && p2.isRed && p3.isRed && p4.isRed)
{
return true;
}
else if (p1.isHollow && p2.isHollow && p3.isHollow && p4.isHollow)
{
return true;
}
else if (p1.isCylinder && p2.isCylinder && p3.isCylinder && p4.isCylinder)
{
return true;
}
else
{
return false;
}
And the others are the same, just instead of row1[] it's row2[].
If the first function is true, the console logs the "LOL" message, but if the second or the third and so on are true, the value is not getting outputted. I tried changing the functions' places, every time it only cares if the first one is true, and the rest are ignored.
What would you say I am doing wrong? :D
else if (p1.isRed && p2.isRed && p3.isRed && p4.isRed)
{
return true;
}
else if (p1.isHollow && p2.isHollow && p3.isHollow && p4.isHollow)
{
return true;
}
else if (p1.isCylinder && p2.isCylinder && p3.isCylinder && p4.isCylinder)
{
return true;
}
if I'm understanding you correct if all four pieces in a row are red its not returning true. You need to double check if the 'isRed' is set properly. if p1.isRed and p2.isRed and p3.isRed and p4.isRed then it will return true no matter what. Also double check if you meant to put || instead of &&.

How to break if-else-if using C#

How do I break if-else-if.....Why its not working? its just checking all the conditions instead of performing the tasks. following is my code. I have checked it through breakpoints its moving to all conditions why it doesn't get stop after meeting the correct condition. even it is not going into the if activity it just read all the conditions and do nothing at the end.
private void ShowHash()
{
inpic = pb_selected.Image;
Bitmap image = new Bitmap(inpic);
byte[] imgBytes = new byte[0];
imgBytes = (byte[])converter.ConvertTo(image, imgBytes.GetType());
string hash = ComputeHashCode(imgBytes);
txt_selectedText.Text = hash;
GetHash();
}
private void GetHash()
{
if (txt_sel1.Text == null && (txt_sel2.Text == null || txt_sel3.Text == null || txt_sel4.Text == null || txt_sel5.Text == null ))
{
txt_sel1.Text = txt_selectedText.Text;
return;
}
else if (txt_sel1.Text != null && (txt_sel2.Text == null || txt_sel3.Text == null || txt_sel4.Text == null || txt_sel5.Text == null))
{
txt_sel2.Text = txt_selectedText.Text;
return;
}
else if (txt_sel2.Text != null && (txt_sel3.Text == null || txt_sel4.Text == null || txt_sel5.Text == null))
{
txt_sel3.Text = txt_selectedText.Text;
return;
}
else if (txt_sel3.Text != null && (txt_sel4.Text == null || txt_sel5.Text == null))
{
txt_sel4.Text = txt_selectedText.Text;
return;
}
else if (txt_sel4.Text != null && (txt_sel5.Text == null))
{
txt_sel5.Text = txt_selectedText.Text;
return;
}
}
I strongly suspect the problem is that the Text property is never null for any of txt_sel*. Assuming these are text boxes in a UI, it's much more likely that if there's no text in the text box, the Text property will return "" instead of null. That's the way most UI frameworks handle empty controls.
I would also suggest extracting the conditions into local variables first:
bool hasSel1 = txt_sel1.Text != "";
bool hasSel2 = txt_sel2.Text != "";
bool hasSel3 = txt_sel3.Text != "";
bool hasSel4 = txt_sel4.Text != "";
bool hasSel5 = txt_sel5.Text != "";
if (!hasSel1 && (!hasSel2 || !hasSel3 || !hasSel4 || !hasSel5)
{
...
}
And ideally, give your controls more meaningful names - a collection of variables with the same prefix but then a numeric suffix is very rarely a good idea, in terms of readability.
Reason:
If there is nothing in those textboxes, textbox.Text will return an empty string ("") not null.
Solution:
Check against "" not null:
private void GetHash()
{
if (txt_sel1.Text == "" && (txt_sel2.Text == "" || txt_sel3.Text == "" || txt_sel4.Text == "" || txt_sel5.Text == ""))
{
txt_sel1.Text = txt_selectedText.Text;
return;
}
else if (txt_sel1.Text != "" && (txt_sel2.Text == "" || txt_sel3.Text == "" || txt_sel4.Text == "" || txt_sel5.Text == ""))
{
txt_sel2.Text = txt_selectedText.Text;
return;
}
....
....
EDIT: You don't have to do ==true for boolean variables. If statement checks it against true by default. Use ! to check against false:
if (hasValue1 && (hasValue2 || hasValue3 || hasValue4 || hasValue5))
{
txt_sel1.Text = txt_selectedText.Text;
return;
}
else if (hasValue2 && (!hasValue1 ||hasValue3 || hasValue4 || hasValue5))
{
txt_sel2.Text = txt_selectedText.Text;
return;
}
else if (hasValue3 && (!hasValue1 || hasValue2 || hasValue4 || hasValue5))
{
txt_sel3.Text = txt_selectedText.Text;
return;
}
....
....
I think for string better to use inbuild null and whitespace check function:
bool hasValue1 = string.IsNullOrWhiteSpace(txt_sel1.Text);
bool hasValue2= string.IsNullOrWhiteSpace(txt_sel2.Text);
bool hasValue3= string.IsNullOrWhiteSpace(txt_sel3.Text);
bool hasValue4= string.IsNullOrWhiteSpace((txt_sel4.Text);
bool hasValue5 = string.IsNullOrWhiteSpace(txt_sel5.Text);
And then define if condition on these bool. This case can handle unexpected null or white space values.
That is bacause it probably doesn't find any of those conditions true.
If it would find a true condition it would execute that if block and won't event check the others.
To add to what others have said, you should really have a final else statement in there to catch issues such as this:
else
{
throw new InvalidOperationException("My If Statement is Broken");
}
Thanks to you all! My problem is solved by changing the conditions and your suggested alteration, following is the code may be helped some beginner like me.
private void GetHash()
{
bool hasValue1 = string.IsNullOrWhiteSpace(txt_sel1.Text);
bool hasValue2 = string.IsNullOrWhiteSpace(txt_sel2.Text);
bool hasValue3 = string.IsNullOrWhiteSpace(txt_sel3.Text);
bool hasValue4 = string.IsNullOrWhiteSpace(txt_sel4.Text);
bool hasValue5 = string.IsNullOrWhiteSpace(txt_sel5.Text);
if (hasValue1 && (hasValue2 || hasValue3 || hasValue4 || hasValue5))
{
txt_sel1.Text = txt_selectedText.Text;
return;
}
else if (hasValue2 && (!hasValue1 ||hasValue3 || hasValue4 || hasValue5 ))
{
txt_sel2.Text = txt_selectedText.Text;
return;
}
else if (hasValue3 && (!hasValue1 || !hasValue2 || hasValue4 || hasValue5 ))
{
txt_sel3.Text = txt_selectedText.Text;
return;
}
else if (hasValue4 && (!hasValue1 || !hasValue2 || !hasValue3 || hasValue5 ))
{
txt_sel4.Text = txt_selectedText.Text;
return;
}
else if (hasValue5 && (!hasValue1 || !hasValue2 || !hasValue3 || !hasValue4 ))
{
txt_sel5.Text = txt_selectedText.Text;
return;
}
else
{
CompareHash();
}
}

C# can't skip the next IF

When "thursdaytime" = Closed <- i want it to skip the next IF
Can't get the "goto" to work.
Sometimes "thursdaytime" is 07:30-16:30 <-- and then the next IF is okay
if (today == DayOfWeek.Thursday && thursdaytime == "Closed")
{
sqlreadclosed();
goto Ended;
}
if (today == DayOfWeek.Thursday && DateTime.Now.TimeOfDay > System.TimeSpan.Parse(thursdaytime.Substring(6, 5)) || DateTime.Now.TimeOfDay < System.TimeSpan.Parse(thursdaytime.Substring(0, 5)))
{
sqlreadclosed();
}
else
{
sqlreadopen();
}
Ended:
Can anyone help me out here.. im a Beginner at C# :)
If its helps, the point is, goto sqlreadclosed() if the clock is under 07:30 or over 16:30 and it works great. but when thursdaytime is "Closed" the under and over IF is crashing.. thats why i want to skip it ,if thursdaytime = Closed
Guess you wanted something like this:
if (today == DayOfWeek.Thursday && thursdaytime == "Closed")
{
sqlreadclosed();
//goto Ended;
}
else if (
today == DayOfWeek.Thursday
&& (
DateTime.Now.TimeOfDay > System.TimeSpan.Parse(thursdaytime.Substring(6, 5))
|| DateTime.Now.TimeOfDay < System.TimeSpan.Parse(thursdaytime.Substring(0, 5))
)
)
{
sqlreadclosed();
}
else
{
sqlreadopen();
}
//Ended:
Generally, using goto in C# is only necessary in very rare cases an it is considered bad practice as it leads to "spaghetti code" (i.e. code that has no clear flow). Therefore, use else if for your second condition.
Furthermore, I believe that you have a problem with your second condition, as the two time checks are mutually exclusive. Therefore, the condition will always evaluate to false. This can be fixed by using parantheses in the correct places. Generally be careful when mixing && and || in the same statement.
Alternatively, you can reformat your code to reduce the complexity and avoid the two calls to sqlreadclosed() (cudos to PJ_pavel):
if (today == DayOfWeek.Thursday)
{
if (
thursdaytime == "Closed"
&& (
DateTime.Now.TimeOfDay > System.TimeSpan.Parse(thursdaytime.Substring(6, 5))
|| DateTime.Now.TimeOfDay < System.TimeSpan.Parse(thursdaytime.Substring(0, 5))
)
)
{
sqlreadclosed();
}
else
{
sqlreadopen();
}
//Ended:
For your specific case:
if (today == DayOfWeek.Thursday) {
if(thursdaytime == "Closed" || (DateTime.Now.TimeOfDay > System.TimeSpan.Parse(thursdaytime.Substring(6, 5)) || DateTime.Now.TimeOfDay < System.TimeSpan.Parse(thursdaytime.Substring(0, 5)))){
sqlreadclosed();
else{
sqlreadopen();
}
}
But as guys said in comments: try to read more about conditional statements usage and don't use GOTO - it's a bad practice.
Try using one boolean variable like 'skipflag'. And avoid using goto statements it is considered bad practice. Goto makes the code complex and harder to read.
if (today == DayOfWeek.Thursday && thursdaytime == "Closed")
{
sqlreadclosed();
skipflag = false;
}
else{
skipflag = true;
}
if(skipflag)
{
if (today == DayOfWeek.Thursday && DateTime.Now.TimeOfDay > System.TimeSpan.Parse(thursdaytime.Substring(6, 5)) || DateTime.Now.TimeOfDay < System.TimeSpan.Parse(thursdaytime.Substring(0, 5)))
{
sqlreadclosed();
}
Else
{
sqlreadopen();
}
}
If it's "thursdaytime == "Closed"" but "today!=DayOfWeek.Thursday" then it skips the first if and goes to the second one which will raise an exception.So your first if should be either:
if (today == DayOfWeek.Thursday || thursdaytime == "Closed")
or:
if (thursdaytime == "Closed")

Check the button state for all mouse buttons

Is there a better way to check the button state for all mouse buttons than to check for any different button extra?
var mouseEventArgs = (System.Windows.Input.MouseEventArgs)e.StagingItem.Input;
if (mouseEventArgs.LeftButton == MouseButtonState.Released &&
mouseEventArgs.MiddleButton == MouseButtonState.Released &&
mouseEventArgs.RightButton == MouseButtonState.Released &&
mouseEventArgs.XButton1 == MouseButtonState.Released &&
mouseEventArgs.XButton2 == MouseButtonState.Released)
{
return;
}
If not, how could I do it more elegant without repeating myself so much?
Thanks in advance!
I don't think there is much you can do except refactoring this into a method, since there is no predefined collection for all buttons. If you want it completely out of sight you can use an extension method like this:
public static class Extensions
{
public static bool CheckUniformButtonState(this MouseButtonEventArgs e, MouseButtonState state)
{
switch (state)
{
case MouseButtonState.Pressed:
return (e.LeftButton == MouseButtonState.Pressed &&
e.RightButton == MouseButtonState.Pressed &&
e.MiddleButton == MouseButtonState.Pressed &&
e.XButton1 == MouseButtonState.Pressed &&
e.XButton2 == MouseButtonState.Pressed);
case MouseButtonState.Released:
return (e.LeftButton == MouseButtonState.Released &&
e.RightButton == MouseButtonState.Released &&
e.MiddleButton == MouseButtonState.Released &&
e.XButton1 == MouseButtonState.Released &&
e.XButton2 == MouseButtonState.Released);
default:
return false;
}
}
}
(Not that anyone would ever check if all 5 buttons are pressed..)
Then you can check like this:
if (mouseEventArgs.CheckUniformButtonState(MouseButtonState.Released))
{
return;
}
var buttonStates = new [] {
mouseEventArgs.LeftButton,
mouseEventArgs.MiddleButton,
mouseEventArgs.RightButton,
mouseEventArgs.XButton1,
mouseEventArgs.XButton2};
if (buttonStates.All(s => s == MouseButtonState.Released))
{
return;
}

Cycle in C# can't get this to end >.<

Right now I'm trying to make it so I've got 1 character and 3 enemies if my character dies it's supposed to give game over or something but I can't make it to work (if enemies die it works tho I don't know why).
Here is what I'm doing:
bool Exit = false;
bool CharDead = false;
Heroe Heroe1 = p.ElementAt(0);
Enemigo Enemigo1 = l.ElementAt(0);
Enemigo Enemigo2 = l.ElementAt(1);
Enemigo Enemigo3 = l.ElementAt(2);
a.Agregar(comienza);
List<Items> item = new List<Items>();
do
{
if (Heroe1.HP > 0)
AccionesHeroe1(l, p);
if (Enemigo1.HP > 0)
AccionesEnemigo1(l, p);
if (Enemigo2.HP > 0)
AccionesEnemigo2(l, p);
if (Heroe1.HP > 0)
AccionesHeroe1(l, p);
else
CharDead = true;
if (Enemigo3.HP > 0)
AccionesEnemigo3(l, p);
if (Heroe1.HP <= 0)
{
CharDead = true;
}
if (Enemigo1.HP <= 0 && Enemigo2.HP <= 0 && Enemigo3.HP <= 0)
{
Exit = true;
}
} while (Exit == false || CharDead == false);
Your ending condition is:
(Exit == false || CharDead == false);
This will only exit when CharDead AND Exit are both true.
You probably want to rework it to be:
(Exit == false && CharDead == false);
This way, as soon as Exit is not false or CharDead is not false, you'll exit.
I think you want to change your while loop expression to
do
{
} while(!Exit && !CharDead)
change
while (Exit == false || CharDead == false);
to
while (Exit == false && CharDead == false);
This would work too, right?
while(!(Exit || CharDead));

Categories