- Home /
Question by
R-K918 · Feb 03 at 05:46 PM ·
c#arraysif-statementsif statement
if statement only ever triggering the first condition even if it's false and the second one is true
public void checkWin(int posXh, int posY, int boardPosPos)
{
int posX = posXh;
for (int i = 0; i < 3; i++)
{
if (posX > i)
{
Debug.Log(i + 1);
boardPos[posX - (1 + i), posY] = 1;
}
if (i == 3)
{
boardPos[posX, posY] = 1;
}
}
for (int i = 0; i < 3; i++)
{
if (posY > i)
{
Debug.Log(i + 1);
boardPos[posX, posY - (1 + i)] = 1;
}
if (i == 3)
{
boardPos[posX, posY] = 1;
}
}
Debug.Log("test");
if (boardPos[posX, posY] == 1)
{
if ((boardPos[posX, posY - 1] == 1 && boardPos[posX, posY - 2] == 1 && boardPos[posX, posY - 3] == 1) ||
(boardPos[posX - 1, posY] == 1 && boardPos[posX - 2, posY] == 1 && boardPos[posX - 3, posY] == 1))
{
Debug.Log("tr");
rPoints += 5;
Debug.Log("red");
if (moves <= 10)
{
rPoints += 2;
StreamWriter writer = new StreamWriter("Assets/scoreDoc.txt", true);
writer.WriteLine(Name1 + rPoints);
writer.Close();
AssetDatabase.ImportAsset("Assets/scoreDoc.txt");
if (rPoints > hS1)
{
PlayerPrefs.SetInt("highscore1", rPoints);
PlayerPrefs.SetString("highscore1Text", Name1);
}
else if (rPoints > hS2)
{
PlayerPrefs.SetInt("highscore2", rPoints);
PlayerPrefs.SetString("highscore2Text", Name1);
}
else if (rPoints > hS3)
{
PlayerPrefs.SetInt("highscore3", rPoints);
PlayerPrefs.SetString("highscore3Text", Name1);
}
else if (rPoints > hS4)
{
PlayerPrefs.SetInt("highscore3", rPoints);
PlayerPrefs.SetString("highscore3Text", Name1);
}
else if (rPoints > hS5)
{
PlayerPrefs.SetInt("highscore3", rPoints);
PlayerPrefs.SetString("highscore3Text", Name1);
}
}
if (moves > 10)
{
StreamWriter writer = new StreamWriter("Assets/scoreDoc.txt", true);
writer.WriteLine(Name1 + rPoints);
writer.Close();
AssetDatabase.ImportAsset("Assets/scoreDoc.txt");
if (rPoints > hS1)
{
PlayerPrefs.SetInt("highscore1", rPoints);
PlayerPrefs.SetString("highscore1Text", Name1);
}
else if (rPoints > hS2)
{
PlayerPrefs.SetInt("highscore2", rPoints);
PlayerPrefs.SetString("highscore2Text", Name1);
}
else if (rPoints > hS3)
{
PlayerPrefs.SetInt("highscore3", rPoints);
PlayerPrefs.SetString("highscore3Text", Name1);
}
else if (rPoints > hS4)
{
PlayerPrefs.SetInt("highscore3", rPoints);
PlayerPrefs.SetString("highscore3Text", Name1);
}
else if (rPoints > hS5)
{
PlayerPrefs.SetInt("highscore3", rPoints);
PlayerPrefs.SetString("highscore3Text", Name1);
}
}
winnerText.text = "Winner: RED";
}
}
The first if statement is the only one that is ever triggered even if its false and the other one is true. Also, sorry for the sloppy code.
Comment
No one is going to want to read 100+ lines of code to try and figure out what issue you're having.
My suggestion is place a breakpoint and step through the code to find your issue, this is a easy problem to solve with debugging.
You have a lot of if statements in there, you should probably trim down your snippet.
Assu$$anonymous$$g this is the one you're talking about:
if (posX > i)
{
Debug.Log(i + 1);
boardPos[posX - (1 + i), posY] = 1;
}
Are you absolutely sure that (posX > i) is false? Try adding a debug statement to be sure:
Debug.Log("posX= " + posX + ", i= " + i);
if (posX > i)
{
Debug.Log(i + 1);
boardPos[posX - (1 + i), posY] = 1;
}
in addition to the comments above: The check for if (i==3)
will never be true when your loop only goes to i<3
for (int i = 0; i < 3; i++)