gpt4 book ai didi

c# - 方法返回 boolean 值t> 1是什么意思?

转载 作者:太空宇宙 更新时间:2023-11-03 17:32:43 25 4
gpt4 key购买 nike

有人可以解释以下返回语句的用途/含义吗:t> 1; (请参见last if语句last方法)

该代码来自名为“ Reversi”的游戏http://en.wikipedia.org/wiki/Reversi

此方法检查您是否包围了另一个玩家的石头。

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;
}

最佳答案

这段代码:

return t > 1;


等效于:

if (t > 1)
{
return true;
}
else
{
return false;
}


除了后一种方法不必要地冗长且不幸的是在实际代码中很常见。

关于c# - 方法返回 boolean 值t> 1是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14006940/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com