开发者

How can we implement Artificial Intelligence in Javascript games?

i am working on a two player board game in html5/JavaScript. the two player version is almost complete. i want to add single player mode, where computer would be oppon开发者_高级运维ent. 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.


For 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 complete 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 computation 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 computer 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 computer 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 computer 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜