【发布时间】:2014-03-10 02:23:59
【问题描述】:
已编辑
我在 Main 中有这个,我在其中设置对象并调用 Player 类。很抱歉代码中缺少 cmets。
Player MyPlayer;
MyPlayer = new Player();
Hiders[] MyHiders = new Hiders[4];
for (i = 0; i < 4; i++)
{
MyHiders[i] = new Hiders();
}
while (FoundAll == false)
{
MyPlayer.Move();
for (i = 0; i < 4; i++)
{
MyHiders[i].DisplayDistance(MyPlayer.x, MyPlayer.y);
}
for (i = 0; i < 4; i++)
{
MyHiders[i].CheckCapture(MyPlayer.x, MyPlayer.y);
}
FoundAll = true;
for (i = 0; i < 4; i++)
{
if (MyHiders[i].Found == false)
{
FoundAll = false;
}
}
}
Console.WriteLine("You Win!");
还有这些类
class Player
{
public int x;
public int y;
public void Move()
{
string buffer;
Console.WriteLine("Where would you like to move?");
buffer = Console.ReadLine();
if (buffer == "u")
{
x++;
}
if (buffer == "d")
{
x--;
}
if (buffer == "l")
{
y--;
}
if (buffer == "r")
{
y++;
}
}
}
class Hiders
{
private int x;
private int y;
public bool Found;
int[,] map = new int[10, 10];
Random MyRandom = new Random();
int Randomhx;
int Randomhy;
public Hiders(int hx, int hy)
{
MyRandom = new Random();
Randomhx = MyRandom.Next(1, 10);
Randomhy = MyRandom.Next(1, 10);
}
public Hiders()
{
}
public void DisplayDistance(int px, int py)
{
double distance;
distance = Math.Sqrt(Math.Pow(x - px, 2) + Math.Pow(y - py, 2));
Console.WriteLine(distance);
}
public void CheckCapture(int px, int py)
{
if (Randomhx == px & Randomhy == py)
{
Found = true;
}
}
}
所有的隐藏者都得到相同的值。我如何使它从1-10改变。这应该可以工作。
【问题讨论】:
-
你是说 Hiders 类的 DisplayDistance 方法吗?如果是这种情况,您已经使用 MyHiders[i].DisplayDistance(MyPlayer.x, MyPlayer.y); 调用它
-
好的,现在我明白了。有一些放错地方的东西使它显示距离之前说左/右上/下
-
我对其进行了编辑,并更改了问题。所有的隐藏者都出现了相同的值。我如何改变他们在网格上的位置?
-
即使你设法使随机工作,你仍然会得到 x 和 y 的零(在 Hiders 类中),因为你从未初始化它们。
-
如何初始化它们 mbm?
标签: c# class oop c#-4.0 object