• Create Account

## AI is harder than I thought

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

39 replies to this topic

### #21SerialKicked  Members

583
Like
2Likes
Like

Posted 13 April 2014 - 09:28 AM

If fuzzy logic wasn't already an established term, and used in many fields of engineering and programming systems, then I might sound more like a fangirl, but it is established, and used in more applications than I can yet expound upon. Again, the term is new, but the idea is old.

No one told you that it wasn't used for a lof of things. But like every AI system it's a tool. And as with all tools they have specific uses.I wouldn't use Fuzzy logic to solve tic tac toe and i wouldn't use a screwdriver to fell a tree.

second post addressed to me[...]

No they do not assume that. With the rules we explained regarding tic tac toe, what the opponent play doesn't matter. If he play more or less correctly he'll get a draw, if not he'll loose period.

The chess analogy isn't relevant and it's off topic, and you may want to read a bit more about the logic systemS used in chess before jumping to conclusion.

Seriously...

### #22Tutorial Doctor  Members

2565
Like
-3Likes
Like

Posted 13 April 2014 - 09:35 AM

. A guard would have heard you, but due to distance and other factors he isn't sure sure if it's an error or your location so it continues his patrol but with a wider cone of vision / improved hearing / etc depending on how sure he is to have heard an intruder

That is the type of reasoning I am talking about. I come to this type of reasoning in pretty much all games. Because there is always a little uncertainty in real reasoning. And fuzzy logic considers this. It is not always a matter of true or false, because the condition is uncertain. When I am playing against a human opponent, I am not sure what move they will make, because they might not play the "best move." Bobby Fischer once played a move that was not the "best move" by the book, but because he understood the condition by another reasoning, he actually was playing the game against his opponent (using his pride) and not against the book.

That is all I will say on this subject though. The response was to how AI could be done, using chess as an example (more complex game). It is relevant.

Edited by Tutorial Doctor, 13 April 2014 - 09:38 AM.

They call me the Tutorial Doctor.

### #23LAURENT*  Members

656
Like
0Likes
Like

Posted 13 April 2014 - 09:45 AM

Um.......................I had this idea to use the rand operator. Not working like I hoped. Anyone thinks this will work?

### #24Aardvajk  Members

12284
Like
0Likes
Like

Posted 13 April 2014 - 09:51 AM

Um.......................I had this idea to use the rand operator. Not working like I hoped. Anyone thinks this will work?

Depends how you plan to use it really

Normal use of rand is with modulus like

int i = rand() % 10;

That will give you a random number between 0 and 9 inclusive. If you are trying to pick a random square, that would be the way to use it. Replace 10 with 9 to get an index into the (zero based) array of squares.

### #25Waterlimon  Members

4373
Like
0Likes
Like

Posted 13 April 2014 - 11:20 AM

Um.......................I had this idea to use the rand operator. Not working like I hoped. Anyone thinks this will work?

Depends how you plan to use it really

Normal use of rand is with modulus like

int i = rand() % 10;

That will give you a random number between 0 and 9 inclusive. If you are trying to pick a random square, that would be the way to use it. Replace 10 with 9 to get an index into the (zero based) array of squares.

[insert rant about this method not actually producing uniformly distributed random integers]

You need to generate the random integer in a loop, such that if the random integer is at the very end of the range (specifically, larger than MAX_RANGE - MAX_RANGE%10) you throw it away and generate a new one, until you get one that is not a part of that "remainder" at the end of the range (thus the loop)

int range=10;
int rand=randInt();
//while (rand > MAX_INT - MAX_INT%range) <-dis wrong. dont do dis.
while (rand >= MAX_INT - MAX_INT%range) //if result part of remainder at end of possible random values, regen
{
rand=randInt();
}
return rand % range;

Though, better read an article written by someone who loves PRNGs to be sure this is correct. ;)

Edited by Waterlimon, 14 April 2014 - 04:55 AM.

o3o

### #26LAURENT*  Members

656
Like
0Likes
Like

Posted 13 April 2014 - 01:33 PM

Don't worry waterlimon the rand operator is in my while loop, Also I got success with the computer randomly picking spots. Unfortunately I now have a logic problem where it only circles's turn once and x turn for the rest of the game.

### #27Bacterius  Members

13102
Like
1Likes
Like

Posted 13 April 2014 - 02:47 PM

Um.......................I had this idea to use the rand operator. Not working like I hoped. Anyone thinks this will work?

Depends how you plan to use it really

Normal use of rand is with modulus like

int i = rand() % 10;

That will give you a random number between 0 and 9 inclusive. If you are trying to pick a random square, that would be the way to use it. Replace 10 with 9 to get an index into the (zero based) array of squares.

[insert rant about this method not actually producing uniformly distributed random integers]

You need to generate the random integer in a loop, such that if the random integer is at the very end of the range (specifically, larger than MAX_RANGE - MAX_RANGE%10) you throw it away and generate a new one, until you get one that is not a part of that "remainder" at the end of the range (thus the loop)

int range=10;
int rand=randInt();
while (rand > MAX_INT - MAX_INT%range) //if result part of remainder at end of possible random values, regen
{
rand=randInt();
}
return rand % range;

Though, better read an article written by someone who loves PRNGs to be sure this is correct. ;)

Indeed, because it is not correct (see how easy it is to get it wrong?). It has a bias towards zero. Test of your code on 8-bit variables, with range 3, over 20 billion trials (there is a bias for every power of two variable size, but it's easier to detect with smaller variables, obviously):

0	6718852175
1	6640484379
2	6640663446


Oops  the fix is to change the comparison to >=, so that the extra element causing the bias is excluded:

/* ... */
while (rand >= MAX_INT - MAX_INT%range)
/* ... */

Which gives (note there are cleaner ways to implement the above that are easier to prove correct):

0	6666558968
1	6666714886
2	6666726146


But this is seriously off-topic for the For Beginners section. I didn't want to reply but I had to, just so someone coming across this post in the future doesn't copy-paste your code and end up with a severe and hard-to-track bug in one of his core functions. Not to mention that trying to robustly generate pseudorandom numbers using a standard library function with an implementation-defined distribution operating on signed integers is just asking for trouble either way

“If I understand the standard right it is legal and safe to do this but the resulting value could be anything.”

### #28ram64  Members

862
Like
1Likes
Like

Posted 13 April 2014 - 03:45 PM

Not sure if anyone already suggested it but I find this Tic-Tac-Toe AI very helpful.

Edited by ram64, 13 April 2014 - 03:45 PM.

### #29Mouser9169  Members

401
Like
1Likes
Like

Posted 13 April 2014 - 07:57 PM

1) Don't know the name of it - 'game board' is lines of dots. You take away dots on your turn. You win if you opponent picks up the last dot.

It's called Nim, and it has been solved for over a hundred years.

That would be the one.

I know it's been solved (as all simple games of open knowledge have been), but it gives a bit more 'wiggle room' to work with some AI.

You can slice it, dice it , analyze it six ways from Sunday but there are only nine squares on a Tic-Tac-Toe board. Rotate the matrix and you have exactly three potential opening moves. You have a few more possibilities for moves 2 and 3 (though many will be 'blunders' leading to an instant win by the opponent), but the number is still small. I was substitute teaching a class for a week and we did a brute force solve of tic-tac-toe on the blackboard one day when I was bored (it was a photography class, and I couldn't give them the free rein with equipment they'd normally have - so there was a lot of 'down time'). It's possible we missed a few cases, and we stopped as soon as it was apparent a move was a blunder (no need to keep checking down that path), but I think we got them all.

Something like Nim isn't quite so obviously finite (I know, in reality it is) so I think it would be more fertile ground for developing your own AI algorithm. Especially since not everyone knows the solution - and if you want practice developing an AI, do it for something you don't know the solution for. See how well your AI works, where it falls short, why it loses.

It's the 'pong' thing. Pong games have been written for ever OS there is (probably even for some 'smart toasters'). If you want to start to program games, it's still a good place to start. That's how we learn. When there's a known solution we can check our work against, that's even better (usually), or maybe you make your pong game, then look at others and find three different approaches to solving the same problem - again, a good learning experience.

Tic-Tac-Toe doesn't have that kind of 'room' though: a move is either right or wrong, and clearly so. I'm hesitant to even call it AI: If you can win, play the winning move. If you need to block, block. If there's only one or two squares left, play it (or pick one) - game's a tie at that point. After that you load the matrix, compare it to the matrices in the computers list, iterating over the four rotations, and when you find a match, make the move. Done. The computer will never lose a game.

"The multitudes see death as tragic. If this were true, so then would be birth"

- Pisha, Vampire the Maquerade: Bloodlines

### #30Boffy  Members

132
Like
4Likes
Like

Posted 14 April 2014 - 04:50 AM

Having just completed a Unity3D book that creates the AI in Tic-Tac-Toe, this is the way the state order should work:

1. Win - Can I get 3 in a row this turn?

2. Block - So I can't win, but can my opponent this turn? If so, block.

3. Create a trap

4. Prevent a trap:

5. Take centre point

6. Take a corner

7. Take a side

8. Random Square

Now this will make the most perfect AI. To resolve this  the author suggests making the AI roll the dice, if it fails it goes down the list to the next option. This way it causes the AI to make mistakes. (ie: blocking instead of taking a winning square)

Maybe not the most advanced AI, but I felt great as a beginner with AI.

### #31Tutorial Doctor  Members

2565
Like
0Likes
Like

Posted 14 April 2014 - 09:41 AM

Snap crackle and pop! I was so thinking of checkers this whole time. I guess I'll still keep at the checkers AI I started on. Haha.

I've started on a tic tac toe AI too. It looks less fuzzy, although for opponent strategy, the offensiveness and defensiveness of the opponent can be fuzzy. However, the fuzziness is overwritten by the exact values. So it is layered.

If a player has two in a row, there is no fuzziness about it, you must block. And if you have two in a row, there is no doubt you must play 3 in a row to win. It is best move to take center on your first move, but one could take a corner.

These are my notes so far before I try to implement it. I am missing a few strategies I know. I still don't think a random move is necessary.

Offense:
-if you have two in a row play 3 in a row. This checks if adjacent squares are not taken first.
-if  not center taken then play center.
Else if not diagonal from center taken then play diagonally from center(check to see if they are taken and play the free one.

Defense:
-if opponent  has two in a row, play to block.
-in other words, check which squares they have occupied, and play the unoccupied square adjacent to them.

note to self: play to block could be a dominant strategy.

Victory condition:
If you have three in a row, you win
If they have three in a row, they win

Ai Notes:
What does you win, versus they win mean?
They is opponent (no matter how many)

The logic changes when attitude is a factor.

Defensive player plays to block more often.
Offensive player plays two in a row more often.
Balanced player a balance of defense and offense.

Fuzzy values:
Offensiveness
Defensiveness

Conditions override fuzzy tendencies.


They call me the Tutorial Doctor.

### #32Herwin P  Members

851
Like
0Likes
Like

Posted 14 April 2014 - 11:03 PM

Speaking of randomness and AI, like Lactose said in his first post in this thread, don't make a truly random AI, because it's cheating the players. In most games, players tend to learn their enemies as they play, and take actions according to what they've learned. For example, one learns a monster's behaviors in Monster Hunter and tries to take advantage from them, like by using traps the monster is weak against. Another example is how people learn attack patterns in action games and use that knowledge to defeat the enemy effectively. Players will feel better because they know they've learned something and get better. They won't feel cheated because a monster who appears weak to fire in the beginning turns to be fire-immune on the next stage.

Other than that, it also makes it easier to make, because it's easier to make a pre-determined AI than a random one. There's no need to make your AI much more complicated than it needs to be.

### #33Satharis  Members

2082
Like
0Likes
Like

Posted 15 April 2014 - 05:21 AM

There's a lot of ways to actually implement AI but just keep in mind even without all the guides and tutorials and such the basic rule of AI in(well most games, not every case of course) games is usually, attempt to seem like a human.

So what does that really mean? Well think about tic-tac-toe, think about if you were playing with a friend and the decisions they might make while playing you, they probably will look at your moves and attempt to block you from winning, most of the time they would succeed at that unless perhaps you used an unusual shape that they might miss easily at one point. Another thing you could do that might be a nice quality of life point is to simulate fatigue of a sort on the computer, i.e. if they keep winning it might slightly increase their chance for a mistake or something.

Basically look at how a competitor or even you might come to decisions and try and emulate that in code, perhaps with some randomization and tweaking. One of the first times I made a rudimentary AI was for a Pong game, I tried a few different things and one of the best ways to make the AI seem more realistic was to make it act like a player. For instance a pong player will generally hover their paddle around the middle when the ball is traveling away from them as they watch for where it is going to bounce to, they also might be slow to respond or overshoot when the ball comes towards them quickly, that sort of thing.

Just some basic AI advice, think what you would do and how you could make the same thing appear with an AI player, sometimes subtle touches a player might not even ever see can make a big difference.

### #34Mouser9169  Members

401
Like
0Likes
Like

Posted 15 April 2014 - 01:47 PM

Bobby Fischer once played a move that was not the "best move" by the book, but because he understood the condition by another reasoning, he actually was playing the game against his opponent (using his pride) and not against the book.

Garry Kasparov made a 'finger fumble' in the opening of one of his games against Deep Blue and found the game lost before it ever left the opening stage.

Counting on your opponent's lack of knowledge is seldom a good strategy.

"The multitudes see death as tragic. If this were true, so then would be birth"

- Pisha, Vampire the Maquerade: Bloodlines

### #35Tutorial Doctor  Members

2565
Like
0Likes
Like

Posted 18 April 2014 - 02:30 PM

Well, it seems someone beat me to it. But in this presentation was the exact thing I was talking about. It is a powerpoint on AI (which you might find useful) and later on in the paper it talks about a tic tac toe AI it also mentions fuzzy logic, but I think it is for another example. The steps looks very identical to my steps I listed earlier.

You can use layers, but for tic tac toe, it might not be as necessary, although it would add a nice layer for realism.

http://www.cs.kent.edu/%7Emscherge/AI/Notes/chapter01.ppt

They call me the Tutorial Doctor.

### #36walsh06  Members

1406
Like
0Likes
Like

Posted 18 April 2014 - 04:11 PM

Min-max is what everyone else was talking about. And that was just an intro to AI lecture. There are thousands of them online.

### #37Aardvajk  Members

12284
Like
1Likes
Like

Posted 19 April 2014 - 04:00 AM

it talks about a tic tac toe AI it also mentions fuzzy logic

It doesn't, however, mention fuzzy logic in the context of tic tac toe. Its just on the following slide. Give this up.

### #38Tutorial Doctor  Members

2565
Like
0Likes
Like

Posted 19 April 2014 - 11:26 AM

Yeah, I know. A few posts ago I acknowledged that I was thinking of checkers and adjusted my reasoning. The adjustment I posted also, and then I found this document, and the adjustment I made for tic tac toe was the same here.

The link was given as a reference to AI, and also to how Fuzzy sets can be used, as well as how Tic tac toe can be done (independent in this case of fuzzy sets).

And I am not going to give up fuzzy logic altogether however, but in the case of tic tac toe, I already noted that it would not be necessary.

They call me the Tutorial Doctor.

### #39 Nathan2222_old   Members

-396
Like
0Likes
Like

Posted 20 April 2014 - 02:46 PM

This is the point in a thread that Nathan222 mentions JARVIS normally.

Derailed = true. Sorry if I was any part of that. Just like newcomers to be given decent advice on here.

What are you talking about?

UNREAL ENGINE 4:
Total LOC: ~3M Lines
Total Languages: ~32

--
GREAT QUOTES:
I can do ALL things through Christ - Jesus Christ
--
Logic will get you from A-Z, imagination gets you everywhere - Albert Einstein
--
The problems of the world cannot be solved by skeptics or cynics whose horizons are limited by the obvious realities. - John F. Kennedy

### #40LAURENT*  Members

656
Like
0Likes
Like

Posted 20 April 2014 - 09:16 PM

Listen there is no need to continue.  This thread just needs to go now. 89% of this stuff wasn't really helpful. I'm still a beginner, I've been fighting just to make the computer take a turn because I had no idea how to make the computer press a button in SDL 1.5. You guys are sharp but thread is now pointless.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.