我在这里有点手足无措...如果有任何帮助,我们将不胜感激:此代码是为在此论坛上举办的战舰AI竞赛编写的,该竞赛随后被指定为A.I.类的一个项目
我的问题(除了在C#上相当笨拙之外)是在Get Shot函数中,它似乎反复调用shot策略函数,从而增加了我的stratlist的大小。我不仅仅想要一个解决我的问题的方案,我更想从概念上理解为什么会发生这种情况,以及如何避免它。任何其他风格的建议或提示也将非常受欢迎。提前为任何违反etiquette...this的行为道歉是我第一个张贴的问题。
在第二次阅读时,这有点模糊,所以我将尝试澄清它:
get shot函数声明一个名为shot的Point;然后,它通过从持有列表中获取一个随机值(共50个值),将一个值分配给shot。它继续用几种方法来评估这个镜头。下一次我们来到get shot函数并到达第二行时,我的列表已经增加了两倍大小,无论我如何查找,我都不知道为什么。
代码如下:
namespace Battleship
{
using System;
using System.Collections.ObjectModel;
using System.Drawing;
using System.Collections.Generic;
using System.Linq;
public class Potemkin : IBattleshipOpponent
{
public string Name { get { return "Potemkin"; } }
public Version Version { get { return this.version; } }
Random rand = new Random();
Version version = new Version(1, 1);
Size gameSize;
bool shotstat = false;
List<Point> stratlist = new List<Point>();
List<Point> aimlist = new List<Point>();
public void NewGame(Size size, TimeSpan timeSpan)
{
this.gameSize = size;
shotstrategy();
}
public void PlaceShips(ReadOnlyCollection<Ship> ships)
{
foreach (Ship s in ships)
{
s.Place(
new Point(
rand.Next(this.gameSize.Width),
rand.Next(this.gameSize.Height)),
(ShipOrientation)rand.Next(2));
}
}
private void shotstrategy()
{
for (int x = 0; x < gameSize.Width; x++)
for(int y = 0; y < gameSize.Height; y++)
if ((x + y) % 2 == 0)
{
stratlist.Add(new Point(x, y));
}
}
public Point GetShot()
{
Point shot;
shot = this.stratlist[rand.Next(stratlist.Count())];
if (shotstat == true)
{
if (aimlist.Count == 0)
{
fillaimlist(shot);
}
while (aimlist.Count > 0)
{
shot = aimlist[0];
aimlist.RemoveAt(0);
return shot;
}
}
return shot;
}
public void NewMatch(string opponent) { }
public void OpponentShot(Point shot) { }
public void fillaimlist(Point shot)
{
aimlist.Add(new Point(shot.X, shot.Y + 1));
aimlist.Add(new Point(shot.X, shot.Y - 1));
aimlist.Add(new Point(shot.X + 1, shot.Y));
aimlist.Add(new Point(shot.X - 1, shot.Y));
}
public void ShotHit(Point shot, bool sunk)
{
if (!sunk)
{
shotstat = true;
}
else
{
shotstat = false;
}
}
public void ShotMiss(Point shot) { }
public void GameWon() { }
public void GameLost() { }
public void MatchOver() { }
}
}
}发布于 2013-10-02 17:45:26
您确定没有在其他地方调用shotstrategy吗?因为GetShot似乎没有问题。代码中唯一可以增加列表大小的部分是shotstrategy函数。你需要仔细看看它的电话。
发布于 2013-10-02 17:58:45
GetShot()函数似乎不会增加stratlist。只有shotstrategy()函数会增加该值。
您的问题可能是这一行
stratlist.Add(new Point(x, y));每次新游戏开始时,你都会添加更多的点数。例如,在8 x 8的栅格中,您将有32个方块作为目标。在下一个游戏中;这将增加另外的32个,以此类推。
您可以通过重置NewGame函数中的stratlist值来解决此问题。
public void NewGame(Size size, TimeSpan timeSpan)
{
stratlist = new List<Point>
this.gameSize = size;
shotstrategy();
}https://stackoverflow.com/questions/19133908
复制相似问题