5

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 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.

bhu1st
  • 1,282
  • 11
  • 23
  • 1
    Why would it be any different from implementing it in any other language? – kprevas Feb 01 '11 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 Feb 01 '11 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 Feb 01 '11 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 Feb 01 '11 at 20:10
  • @Lirik it's a board game. http://en.wikipedia.org/wiki/Bagh-Chal – bhu1st Feb 01 '11 at 20:15
  • Thinking about all the "Which language for AI?" questions, I'm tempted to answer: **Forget it! You cannot implement AI with Javascript.** – ziggystar Jun 04 '14 at 18:55

5 Answers5

8

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.

Kiril
  • 39,672
  • 31
  • 167
  • 226
2

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.

Eric Mickelsen
  • 10,309
  • 2
  • 30
  • 41
  • i have added a link to the game i am working on in comment above, can you please check once. which board game AI algorithm would be best to start with for such game? – bhu1st Feb 01 '11 at 20:21
  • @bhu1st: Seems like an interesting game. You will either need to find a good approach to Bagh-Chal specifically (or dream one up) or use the sort of machine learning that has been applied to checkers. JavaScript doesn't really present any barriers to any approach you might select, so you should probably delete and restate your question to be specifically about Bagh-Chal. – Eric Mickelsen Feb 01 '11 at 20:27
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)

Davy8
  • 30,868
  • 25
  • 115
  • 173
1

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.

Saggio
  • 2,212
  • 6
  • 33
  • 50
  • -1 for not mentioning JavaScript... just kidding. But seriously, a little randomization can do wonders and is far easier than machine learning algorithms. – Eric Mickelsen Feb 01 '11 at 20:15
1

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.

Gergely Fehérvári
  • 7,811
  • 6
  • 47
  • 74
  • I think you mean there is no http://en.wikipedia.org/wiki/Artificial_life yet, but there is certainly intelligent systems (AI). The difference being that artificial intelligence means that given a non - standard set of inputs, the system will make a heuristic judgement on what to do and react accordingly. With artificial life, as you said, the machine is 'thinking' of the solution, and is self-conscious/aware etc. I don't its something that will be covered in JavaScript... – hiddensunset4 Feb 02 '11 at 04:17
  • @Daniel, I'm not sure if the term artificial life can be interchanged with synthetic life, but as of May 2010 we do have [artificial life courtesy of Dr Craig Venter](http://news.bbc.co.uk/2/hi/science/nature/8695992.stm). It seems that people do interchange those terms, although I don't necessarily agree they should. – Kiril Feb 02 '11 at 20:29
  • Its all one big mess, but they should be termed as synthetic life. I guess its whether you use the words for their literal meaning or their comparative meaning (against AI). – hiddensunset4 Feb 02 '11 at 21:28