我在Main中有这个,我设置对象并调用Player类。很抱歉代码中缺少评论。
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变化。这应该有效。
答案 0 :(得分:3)
Hiders的每个实例都在实例化其自己的Random实例。由于它们几乎同时被实例化,并且Random使用系统时钟作为默认种子,因此Random的所有实例将产生相同的值序列。我的建议是让Hiders实例共享一个Random的实例。
答案 1 :(得分:-1)
随机的默认构造函数使用系统时间作为种子。尝试使用随机种子,例如Guid.NewGuid()。GetHashCode()
var rnd = new Random(Guid.NewGuid().GetHashCode());
答案 2 :(得分:-1)
根据@DeveloperGuo的回答,您应该使用不同的种子,以便获得不同的随机值。这种改变应该有效。
public Hiders()
{
MyRandom = new Random( Guid.NewGuid().GetHashCode() );
x = MyRandom.Next( 1, 10 );
y = MyRandom.Next( 1, 10 );
}
编辑:
由于我上面的例子实际上并不是一个好习惯,所以另一个镜头是:
public Start_New_Game()
{
bool FoundAll = false;
int i;
Player MyPlayer;
MyPlayer = new Player();
Hiders[] MyHiders = new Hiders[ 4 ];
HiderGenerator hiderGenerator = new HiderGenerator();
for ( i = 0 ; i < 4 ; i++ )
{
MyHiders[ i ] = hiderGenerator.Get_New_Hider();
}
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 HiderGenerator
{
Random MyRandom = new Random();
public Hiders Get_New_Hider()
{
return new Hiders( MyRandom.Next(1,10), MyRandom.Next(1,10) );
}
}
class Hiders
{
private int x;
private int y;
public bool Found;
int[,] map = new int[ 10, 10 ];
public Hiders( int x, int y )
{
this.x = x;
this.y = y;
}
public void DisplayDistance( int px, int py )
{
if ( Found )
return;
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 ( Found )
return;
if ( x == px && y == py )
{
Found = true;
}
}
}
这不是一个完美的答案,但至少它可能是一个起点。