台球人工智能
我用 Java 实现了一个台球游戏,一切正常。这是一款多人游戏,但尽管如此,也应该可以单独玩。为此,我尝试实现一个简单的 KI。目前,KI 只是随机选择脉冲的方向和随机强度(不知道正确的英文单词)。当然,这个人工智能非常糟糕,不太可能挑战玩家。
于是我就想过改进KI,但是有几个很难解决的问题。首先我想到的是选择最近的球并尝试将其直接放入最近的洞中。这并没有那么糟糕,但如果中间有其他球,它就不再起作用了。此外,这并不能解决计算脉冲强度的问题。
那么有什么一般性的建议吗?或者有什么想法吗?最佳实践?
Im implementing a pool billiard game in Java and it all works fine. It is a multiplayer game, but nevertheless, it should also be possible to play it alone. For this purpose I'm trying to implement a simple KI. At the moment, the KI choose just randomly a direction and a random intensity of the impulse (don't know the correct english word for that). Of course this AI is very poor and unlikely to ever challenge a player.
So i thought about improving the KI, but there are several hard to solve problems. First I thought of just choosing the nearest ball and to try to put it directly into the nearest hole. This isn't that bad, but if there other balls in the line between, it isn't really working anymore. Additionally this dosn't solve te problem of calculating the intensity of the impulse.
So are there any general advice? Or any ideas? Best practices?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
计算游戏的一“步”结果需要多少 CPU 时间和内存?您有能力分析不止一招吗?如果做起来相对便宜,只需选择 N 个随机方向/脉冲,计算结果并选择最好的一个。您可以消除一些“棘手”的情况,即在多次碰撞后球落入袋中。另外,为了简化,您可以限制每次移动的模拟时间(即不要等到所有球都停止,只需计算前 T 秒)。
这样,您就可以拥有不同级别的计算机玩家 - 较高的 N(和 T)对应于较高的游戏级别。
How much CPU time and memory does it take to calculate the results of one "move" of the game? Can you afford to analyze more than one move? If it's relatively cheap to do, just pick N random directions/impulses, calculate the results and pick the best one. You may eliminate some "tricky" cases, when ball goes to pocket after too many collisions. Also, to simplify, you can limit the simulation time for each move (i.e. don't wait until all the balls stop, just calulate the first T seconds).
This way, you can have computer players of different level - the higher N (and T) correspond to higher play level.
根据台球比赛的不同,您通常有两个任务
评估桌子上的情况(获得可能的击球)
在完美的场景(完美的瞄准,完美的击球)中,所有可能的击球都同样困难且如果您只考虑直接击球到一个球,那么您需要分析的最多只有 6 个球 xn 个球的情况(分析简单的标准 - 击中两个球只需要额外的 n^2 个球 x 6 个球的情况)。对于每种情况,确定它们是否可能都需要简单的分析(除非您正在进行非常现实的碰撞模拟)。因此,在非常简单的模拟中,您可能想要尝试构建所有可能的情况并对它们进行排名。要分析离岸击球,您可能需要镜像球和洞。
或者,在列举可能的情况时,您可以简单地对桌子进行线扫描,标记非法投篮的区域,并枚举和构建潜在的投篮,例如......
角度1、球1、口袋2
角度2、球1、口袋3
角度3、球1、球2、口袋1
angle4,cushion2,ball2,pocket1
对于更好的 AI,你想要模拟缺陷,例如,通过在某个点 x 击球(可能定义为远离直接击球的角度)来进行击球,让我们假设将有是 dx 的错误(由于瞄准不良、击球不良或其他原因)——这反过来会导致球的方向错误,该错误会随着距球袋的距离而增加。这提供了一种按难度对射击进行排名的方法 - 射击对瞄准/射击错误的敏感度(某些射击比其他射击更容易)。这取决于从白球到球以及从球到洞的路径长度。
还要考虑的一件事是白球进洞或其他非法击球的风险
< strong>选择击球(不仅基于难度,还基于潜在收益)
Depending on the game of the billiards you usually have two tasks
Assess the situation on the table (get possible shots)
In perfect scenario (perfect aim, perfect shot) all possible shots are equally hard and if you consider only direct shots to one ball there will be only maximum of 6 holes x n balls situations that you need to analyse (analysing simple canons - hitting two balls requires only extra n^2 balls x 6 holes situations). For each of these situations establishing if they are possible requires simple analysis (unless you are doing very realistic collision simulations). So in very simple simulations you might want to try to construct all possible situations and rank them. To analyse shots off the bank you might want to mirror the balls and holes.
Alternatively in enumerating the possible situations you might simply do a line scan of the table, marking the areas which are illegal for shots and enumerating and building potential shots like...
angle1, ball1, pocket2
angle2, ball1, pocket3
angle3, ball1, ball2, pocket1
angle4, cushion2, ball2, pocket1
For nicer AI you want to simulate imperfections, for example the shot is played by hitting a ball at some point x (maybe defined as an angle away from direct hit), let's assume that there will be an error (due to bad aim, or bad hit, or anything) of dx - this in turn will cause the ball to have an error in direction which will increase with the distance to the pocket. This gives one way to rank the shots by difficulty - the sensitivity of the shot in terms of error in aim/shot (some shots are easier then others). This will depend on the length of the path from white to the ball and from the ball to the hole.
One more thing to look at is the risk of white ball going in the hole, or other illegal shots
Choosing the shot (not only based on difficulty, but also on potential gain)
我可以想到两种广泛的方法。
列出母球周围所有可能的球杆位置和力度级别,然后搜索该列表以找到第一个让您击沉球的位置。这是一个相当大的列表,您可以通过使用少量的力量水平并排除任何“明显”不好的击球来修剪它。
逆向工作——观察桌子上的每个球,看看是否可以让母球接触到它。然后计算出正确的球杆位置和力量水平,使其进入洞中。
您可以扩展它来搜索树中的多球击球。
我最喜欢方案1;它可以让你找到能够同时击沉两个或更多球的情况。
I can think of two broad approaches.
Make a list of all possible cue positions surrounding the cue ball and force levels, and then search the list to find the first one that lets you sink a ball. That's a fairly large list, you can trim it by using a small number of force levels and by excluding any "obviously" bad shots.
Work backwards -- look at each ball on the table, and see if the cue ball can be made to contact it. Then work out the correct cue position and force level to make it go in the hole.
You can expand this to search a tree for multiple-ball shots.
I like solution 1 the best; it lets you find situations where you would be able to sink two or more balls at one time.
您可能会考虑将球显示为加权图。您可以将口袋作为特殊节点放入。然后,您根据从母球到特定球再到口袋的路径重量,选择要放入或击打的球。脉冲的强度也可以通过使用该权重的值来设置。然后,您可以使用物理引擎来确定射击是否可行。我从来没有尝试过这样的事情,所以一切都是理论上的,我不知道它是否具有实用性。而且,这种方法不包括让球杆或其他球弹来弹去,所以基本上它只适合直线击球。
You might consider as displaying the balls as a weighted graph maybe. You could put in the pockets as special nodes. You then chose which ball to put, or hit, depending on the weight of the path from the cue ball, to the specific ball and to the pocket. The intensity of the impulse can also be set by using the value of this weight. You can then use a physics engine to determine if the shot is at all possible. I have never tried such thing, so everything is theoretical and I do not know if it is at all practical. Also, such method does not include having the cue or the other balls bouncing around, so basically it will cater only for straight shots.
我认为这不是随机的。您需要一个物理引擎来模拟球杆、球、缓冲器和口袋的相互作用。对我来说,它不太像人工智能,而更像物理。
I would think it's hardly random. You'd want a physics engine to model the interactions of cue, balls, bumpers, and pockets. It feels less like AI and more like physics to me.