admin管理员组文章数量:1236733
i am working on a two player board game in html5/JavaScript. the two player version is almost plete. i want to add single player mode, where puter would be opponent. this game will be played in single browser (no server side integration).
i am new to AI. i want some guidelines on AI implementation in JavaScript games, where should i begin?
please help.
Edited: The game is Bagh-Chal
Thanks for the answers: I've managed to implement Minimax on the baghchal game. Here.
i am working on a two player board game in html5/JavaScript. the two player version is almost plete. i want to add single player mode, where puter would be opponent. this game will be played in single browser (no server side integration).
i am new to AI. i want some guidelines on AI implementation in JavaScript games, where should i begin?
please help.
Edited: The game is Bagh-Chal
Thanks for the answers: I've managed to implement Minimax on the baghchal game. Here.
Share Improve this question edited Jun 5, 2018 at 17:55 bhu1st asked Feb 1, 2011 at 20:05 bhu1stbhu1st 1,31813 silver badges24 bronze badges 6- 1 Why would it be any different from implementing it in any other language? – kprevas Commented Feb 1, 2011 at 20:08
- 5 This question makes no sense. All I can see is "teach me AI". That's not what this site is for. – Noldorin Commented Feb 1, 2011 at 20:08
- 3 this question is a bit broad. you should probably provide more details about the type of game, what players do and what sort of problems the "AI" would need to solve. e.g. path finding? targeting? collaboration? – Assaf Lavie Commented Feb 1, 2011 at 20:09
- bhu1st, what kind of game is it? Depending on the type of game, we may be able to provide you with an appropriate suggestion for an AI implementation: is it a strategy, first person shooter, board game, card game, etc? – Kiril Commented Feb 1, 2011 at 20:10
- @Lirik it's a board game. en.wikipedia/wiki/Bagh-Chal – bhu1st Commented Feb 1, 2011 at 20:15
5 Answers
Reset to default 8For Bagh-Chal you might want to take the Minimax approach with Alpha-beta pruning.
There are a lot of good resources on the algorithm, but here is a CS Recitation for Minimax with Alpha-beta Pruning. I, personally, wouldn't call this an AI algorithm, but it is often discussed in introductions to AI.
Alternately, you can train up an actual AI algorithm to play the game (neural net, genetic algorithm, etc.), but this approach seems to be somewhat unpractical for a game like Bagh-Chal.
Step 1: Learn (A) JavaScript.
Step 2: Learn (B) an AI algorithm for the board game.
Step 3: Implement B in A.
Optional Step 4: Choose another board game; then go to Step 2.
Minimax with Alpha-beta pruning that Lirik mentioned is a good place to start, but it takes some time to wrap your mind around if you're not familiar with it.
Alternatively you can think about how you would play the game if you had a perfect memory and could do fast calculations and try to implement that. The upside is that's usually easier to understand.
Minimax would probably result in shorter but more difficult to understand (for those unfamiliar with it) code that depending on the game, could result in playing a perfect game if the game is simple enough (however it also has the disadvantage of favoring not losing to winning because it assumes the opponent will be playing perfectly as well)
Since it sounds like it's a game of plete information (the whole board is visible to all players at all times) a properly implemented Minimax with infinite look-ahead could give an AI that would never lose (assuming infinite putation time). In games using Minimax the difficulty level is often determined by how many moves ahead the algorithm looks at. It gets exponentially slower the more steps there are, so you will run into a hardware limitation if the game isn't super simple (which is why there isn't a perfect Chess playing AI yet, I think last I checked it would take a couple thousand years on the fastest puter at the time of the article I read, sorry no citations)
I think you're best bet would be to start with a rigid A.I. algorithm i.e. an opponent that always does the same thing in a given situation.
To have true "A.I." You would need implement a machine learning algorithm that keeps track of previous inputs and if it was the right decision, so that it can get better. This is done with something along the lines of a Neural Network.
there is no AI. yet. you can simulate the way of thinking human person, but you cant force the game think instead of you. and in the javasccript all you should use : functions, loops, variables, arrays strings. the puter shoudl check the game in one point of wiev, and calculate the best step. for example sort each solution descending by one propertyand add increase the rate of the first ten item. and then sort by other and rate again, and couple of these momentss the higest rated step will be the best.
other way to create a win strategy but this is even hard to a human not event to implement into js.
it will be better if i tell you an example everybody know the XOX game
there is a 3x3 table and you should put 3 X or O in a row to win
_|_|_
_|_|_
| |
this is the map
and this is one way of win
x|o|_
_|x|o
|o|x
i think you remember now.
so what the AI at the server stands for.
when an user puts an X or O (now the user is X the server is O) the server has to calculate how desperathe his situation
is there 2 X in a row? if yes the machine must PUT an O into the midle or at the end of the 2 X.
if there is no 2 X in a row, the machine must calculate there is a trick somewhere?
for example
_|x|_
_|_|x
o| |
this is a trick because the puter must take the O to the top left corner or the machine will loose.
all of these are questions (if-else statements) what humans ask during the play. if you want to implement you should realy force yourself to record your thinking.
what i first search for? hmmm.first i put the X to the middle because this has got a lot of possibility.
the most important to create an AI you should simulate your way of thinking. good luck.
本文标签: algorithmHow can we implement Artificial Intelligence in Javascript gamesStack Overflow
版权声明:本文标题:algorithm - How can we implement Artificial Intelligence in Javascript games? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1739926413a2211021.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论