Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 无明显原因重复函数调用…战舰AI故障_C# - Fatal编程技术网

C# 无明显原因重复函数调用…战舰AI故障

C# 无明显原因重复函数调用…战舰AI故障,c#,C#,我有点不知所措。。。任何帮助都将不胜感激:此代码用于本论坛上主办的战列舰AI竞赛,该竞赛随后被指定为a.I.课程的一个项目 我的问题(除了在C#方面相当笨拙之外)在于Get Shot函数,它似乎反复调用Shot strategy函数,从而增加了我的stratlist的大小。 我不想仅仅解决我的问题,而是想从概念上理解为什么会发生这种情况,以及如何避免它。任何其他风格的建议或提示也将非常受欢迎。提前为任何违反礼仪的行为道歉……这是我第一个发帖的问题 在第二次阅读时,这有点模糊,因此我将尝试澄清:

我有点不知所措。。。任何帮助都将不胜感激:此代码用于本论坛上主办的战列舰AI竞赛,该竞赛随后被指定为a.I.课程的一个项目

我的问题(除了在C#方面相当笨拙之外)在于Get Shot函数,它似乎反复调用Shot strategy函数,从而增加了我的
stratlist
的大小。 我不想仅仅解决我的问题,而是想从概念上理解为什么会发生这种情况,以及如何避免它。任何其他风格的建议或提示也将非常受欢迎。提前为任何违反礼仪的行为道歉……这是我第一个发帖的问题

在第二次阅读时,这有点模糊,因此我将尝试澄清:

get shot函数声明一个名为shot的点;然后,它为通过从保留列表中获取随机值(50个值中的一个)而派生的快照指定一个值。接下来,我们将从几个方面对这张照片进行评估。下一次,当我们进入“获取快照”功能并进入第二行时,我的列表的大小增加了两倍,无论我看起来如何或在哪里,我都不知道为什么

代码如下:

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() { }
    }
}


}
名称空间战舰
{
使用制度;
使用System.Collections.ObjectModel;
使用系统图;
使用System.Collections.Generic;
使用System.Linq;
公共级波将金:Ibattleship对手
{
公共字符串名称{get{返回“Potemkin”;}
公共版本版本{get{返回this.Version;}}
Random rand=新的Random();
版本=新版本(1,1);
游戏大小;
bool shotstat=false;
List stratlist=新列表();
List aimlist=新列表();
public void NewGame(大小、时间跨度、时间跨度)
{
这个。游戏大小=大小;
战略();
}
公共作废配售船舶(只读收款船舶)
{
foreach(船舶中的船舶)
{
s、 放置(
新点(
rand.Next(这个.gameSize.Width),
rand.Next(this.gameSize.Height)),
(船舶定位)兰德Next(2));
}
}
私人投资策略()
{ 
对于(intx=0;x0)
{
shot=aimlist[0];
aimlist.RemoveAt(0);
回击;
}
}
回击;
}
public void NewMatch(字符串对手){}
公开空位对手射门{}
公共空白填充列表(定点拍摄)
{
aimlist.Add(新点(shot.X,shot.Y+1));
aimlist.Add(新点(shot.X,shot.Y-1));
aimlist.Add(新点(shot.X+1,shot.Y));
aimlist.Add(新点(shot.X-1,shot.Y));
}
公共虚空射击命中率(定点射击,击沉)
{   
如果(!沉没)
{
shotstat=true;
}
其他的
{
shotstat=false;
}  
}
公共空射失误(定点射击){}
公共void GameWon(){}
public void GameLost(){}
public void MatchOver(){}
}
}
}

您确定没有在其他地方调用
shotstrategy
?因为
GetShot
似乎没有问题。代码中唯一可以增加列表大小的部分是
shotstrategy
函数。您需要仔细查看它的调用。

函数
GetShot()
似乎没有增加
stratlist
。只有
shotstrategy()

你的问题可能是这条线

stratlist.Add(new Point(x, y));
每次新游戏开始时,你都会增加更多的分数。例如,在8乘8的栅格中,将有32个正方形作为目标。在下一场比赛中;这将再增加32个,以此类推

您可以通过重置
NewGame
函数中的
stratlist
值来解决此问题

public void NewGame(Size size, TimeSpan timeSpan)
{
    stratlist = new List<Point>
    this.gameSize = size;
    shotstrategy();
}
public void NewGame(大小、时间跨度、时间跨度)
{
stratlist=新列表
这个。游戏大小=大小;
战略();
}

你的意思是
stratlist
正在增长吗?您在哪里调用
shotstrategy()
?也许它应该先清除列表。非常感谢您的时间……问题仍然没有解决……我知道只有shotstrategy()可以增加列表的价值。问题的核心在于,理论上,我的AI应该在每次比赛中调用该函数一次,在比赛开始时,当调用newgame()时。实际情况是,一旦代码进入getshot(),它只通过函数一次,然后返回newgame()。这让我意识到我的问题在于我对这门课所处的框架的理解。我想我只是想走一条谦虚的道路,让我的教授/同学看一看,并指出我希望的是一个明显的小问题。再次感谢您的帮助和努力,非常感谢,希望我能在将来继续。@user2837918如果新游戏方法的目的是初始化或重置游戏;您应该将当前游戏中的所有设置重置为开始设置