Tic tac toe BASIC bot?
Hi all. I'm a bit of a newbi and i just designed a basic tic tac toe game which gets player 1 and player 2 moves, not much error checking going on. I am using the basics only being arrays, 2 funtions main and display grid() and i'm kinda stuck on how to create basic AI just using the basics without going into classes and stuff. Can any one help? thx
PS i read Sijmen's post and i got lost :(
again thx
An easy bot to make would be a completely random bot. Everytime it's a player's turn, see if that player is AI, and have them randomly pick a spot.
Assuming turn logic like this:
(I'm assuming iPlayerTurn is an integer which keeps track of which player's turn it is.)
Simple enough right? Just keep's looping until the player picks a spot that is valid (to prevent them from picking a spot that already has an X or an O on it). Just modify the logic to see if the player is AI, and if so, randomly grab a spot.
Maybe not the most sophisticated AI, but fun none-the-less. If you want to add some more logic to the AI (ie, picking a corner if it's free, or getting the middle), instead of choosing a random spot, evaluate the board and set iSpot to the appropriate location.
Play around and have some fun.
Assuming turn logic like this:
(I'm assuming iPlayerTurn is an integer which keeps track of which player's turn it is.)
do{ int iSpot = 0; std::cout << "Pick your spot: " << std::endl; std::cin >> iSpot;} while(AddSpot(iSpot, iPlayerTurn) == false)
Simple enough right? Just keep's looping until the player picks a spot that is valid (to prevent them from picking a spot that already has an X or an O on it). Just modify the logic to see if the player is AI, and if so, randomly grab a spot.
do{ int iSpot = 0; if((PlayerTurn == 1 && bPlayerOneIsAI == true) || (iPlayerTurn == 2 && bPlayerTwoIsAI == true)) { // Computer Turn iSpot = rand() % 9; // Choose a random spot. } else { // Human Turn std::cout << "Pick your spot: " << std::endl; std::cin >> iSpot; }} while(AddSpot(iSpot, iPlayerTurn) == false)
Maybe not the most sophisticated AI, but fun none-the-less. If you want to add some more logic to the AI (ie, picking a corner if it's free, or getting the middle), instead of choosing a random spot, evaluate the board and set iSpot to the appropriate location.
Play around and have some fun.
[google] for the minimax algorithm. It is an ideal candidate for TTT.
"In order to understand recursion, you must first understand recursion."
My website dedicated to sorting algorithms
My website dedicated to sorting algorithms
January 08, 2005 03:21 PM
oh yeah i get it - thats quite an effective way of doing it thx GroZZleR. I'm gonna try it out, once i got it working i'll post up what i done :D - thanks.
iMalc: I'll do just that - thanks.
iMalc: I'll do just that - thanks.
I've had a little think like this, as I developed a little method when I was a kid to make myself unbeatable at noughts and crosses. I think if you computerised it, the algorithm to detect the computers next best move would look something like this: (define the two players as player for player1 and computer for player2)
(i) If the computer can win next turn, place the victory piece.
(ii) if a line is found which assures victory for the player next turn, place a piece to block off this line. This should be done on the first line found, although if this algorithm is correct, there should only be one each turn.
(iii) if the centre is available take it.
(iv) if the player's last move was to one of the corners, play a piece to one of the side squares.
if the player's last move was to one of the side squares, play a piece to one of the corners.
if the player's last move was to the centre, do either of the above
when picking a side square or a corner, always pick the location closest to the player's last played piece.
I've only developed this algorithm conceptually, so please feel free to point out any faults.
(i) If the computer can win next turn, place the victory piece.
(ii) if a line is found which assures victory for the player next turn, place a piece to block off this line. This should be done on the first line found, although if this algorithm is correct, there should only be one each turn.
(iii) if the centre is available take it.
(iv) if the player's last move was to one of the corners, play a piece to one of the side squares.
if the player's last move was to one of the side squares, play a piece to one of the corners.
if the player's last move was to the centre, do either of the above
when picking a side square or a corner, always pick the location closest to the player's last played piece.
I've only developed this algorithm conceptually, so please feel free to point out any faults.
OklyDokly: I don't think you can pick a side when the opponent chose the center (at least on the first move), but other than that I think you have it correct.
"Walk not the trodden path, for it has borne it's burden." -John, Flying Monk
Yep, Extrarius is right. If the person who goes first chooses the center then you choose a side, you've already lost (assuming the other player is playing optimally).
ok thanks, guess you'd modify iv to be go for a corner (if available), if the players last move was to the centre.
So, this is pretty simple.
If you've got an array of chars(2 bit, To make the checking easier, the entire thing is only 18 bits! so you can store the entire board on an INT. Its not hard to get info in & out either.), and number 1 is the comp, number 2 is the user, and 0 is nothing. (a useful system, i might add).
What you'd be doing, is checking winning against a few numbers (which is insanely easy, once your used to it). You can do draws against one number (11111111111.....). You can also do the ai, with a few bitwise operations!
Want to check if the center square is occupied?
One &. (magic number, of cource.)
One if.
Done.
Want to find an open courner?
One &.
And now you've got the location for the corner
Another & (or a bs, depending on magic number)
Another | and you've made your move.
It is thriftyness to the extreem.
Its good practice, it helps you practice bitwise operations, how to use hex.
Its fun, once you get the hang of it.
From,
Nice coder
If you've got an array of chars(2 bit, To make the checking easier, the entire thing is only 18 bits! so you can store the entire board on an INT. Its not hard to get info in & out either.), and number 1 is the comp, number 2 is the user, and 0 is nothing. (a useful system, i might add).
What you'd be doing, is checking winning against a few numbers (which is insanely easy, once your used to it). You can do draws against one number (11111111111.....). You can also do the ai, with a few bitwise operations!
Want to check if the center square is occupied?
One &. (magic number, of cource.)
One if.
Done.
Want to find an open courner?
One &.
And now you've got the location for the corner
Another & (or a bs, depending on magic number)
Another | and you've made your move.
It is thriftyness to the extreem.
Its good practice, it helps you practice bitwise operations, how to use hex.
Its fun, once you get the hang of it.
From,
Nice coder
Click here to patch the mozilla IDN exploit, or click Here then type in Network.enableidn and set its value to false. Restart the browser for the patches to work.
Wow what a response. Thanks guys and girls – very much appreciatated. Oh and guess what I done it yey (Thx to you). I was gonna post it up, but I read a post some where, in which some poor guy is put down just for showing his work. Any way thanks a lot guys.
PS only one more question. At University we use Visual Studio.Net enterprise edition and when I’m following book tutorials I see they have numbers on each line to let you know the line on which to look at.
IE
1.if (grid [0][0] == player && grid [0][1] == player && grid [0][2] == player)
2.move_valid = true; etc etc
I read somewhere you can do it in C++, cant find it under edit, or view menu -can any one help.
Again thanks (GroZZleR,Nice Coder,OklyDokly,j-locke,Extrarius and Anonymous Poster) a millions for all your help. Got some new ideas on how to Implement basic AI.
PS only one more question. At University we use Visual Studio.Net enterprise edition and when I’m following book tutorials I see they have numbers on each line to let you know the line on which to look at.
IE
1.if (grid [0][0] == player && grid [0][1] == player && grid [0][2] == player)
2.move_valid = true; etc etc
I read somewhere you can do it in C++, cant find it under edit, or view menu -can any one help.
Again thanks (GroZZleR,Nice Coder,OklyDokly,j-locke,Extrarius and Anonymous Poster) a millions for all your help. Got some new ideas on how to Implement basic AI.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement