函数调用无缘无故重复..战舰AI故障
本文关键字:AI 故障 战舰 无缘无故 函数调用 | 更新日期: 2023-09-27 18:33:44
我有点机智...任何帮助将不胜感激:此代码适用于在此论坛上举办的战舰AI竞赛,然后将其指定为AI项目.class
我的问题(除了在 C# 上相当无能(出在 Get Shot 函数中,它似乎反复调用射击策略函数,从而增加了我的stratlist
的大小。我不想只是解决我的问题,而是想从概念上理解为什么会发生这种情况,以及如何避免它。任何其他风格建议或提示也将非常受欢迎。对于任何违反礼仪的行为,请提前道歉...这是我发布的第一个问题。
在第二次阅读时,这有点模糊,所以我会尝试清除它:
get shot 函数声明一个名为 shot 的点;然后,它通过从保留列表中获取随机值(共 50 个值(为镜头分配一个值。 它继续以几种方式评估这个镜头。下次我们来到 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() { }
}
}
}
你确定shotstrategy
没有在其他地方被调用吗?因为GetShot
似乎没有问题.代码中唯一可以增加列表大小的部分是shotstrategy
函数。你需要仔细看看它的电话。
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();
}