Can someone explain what the use/meaning is of the following return statement: t > 1; (see last if statement last method)
the code is from a game called "Reversi" http://en.wikipedia.org/wiki/Reversi
This method checks if you enclose a stone of another player.
public bool allowed(int x, int y, bool player)
{
int color;
if(player == true) // true = player is blue.
color= 1;
else color= -1;
if(stone[x,y] != 0)
return false;
else
{
for (int dx = -1; dx <= 1; dx ++)
{
for (int dy = -1; dy <= 1; dy ++)
{
if (dx != 0 || dy != 0) // 0, 0 must be skipped, own stone.
{
if (close(color, x, y, dx, dy))
return true;
}
}
}
}
return false;
}
public bool close(int color, int x, int y, int dx, int dy)
{
int s;
int testx;
int testy;
for(int t=1; t<stone.Length;t++)
{
testx = x + t * dx;
testy = y + t * dy;
if(testx >= board.Width || testx <= 0 || testy >= board.Heigth|| testy <= 0)
return false;
s = stone[x + t * dx, y + t * dy];
if (s == 0) return false;
if (s == color) return t>1;
}
return true;
}
This code:
return t > 1;
Is equivalent to:
if (t > 1)
{
return true;
}
else
{
return false;
}
Except the latter approach is unnecessarily verbose and unfortunately quite common to see in real code.