Advertisement

TicTacToe AI

Started by August 01, 2003 09:34 PM
4 comments, last by DarkWhoppy 21 years, 1 month ago
There's alot of TicTacToe games out there...mostly 2 player. But im trying to get mine to work and it be a one player game. Programming AI isn't too fun . But could someone help me out here? The "AI()" function is blank..thats where the AI programming goes. The code is messy but it shouldn't be impossible to read BTW, its C++
 #include <iostream>
#include <string>
#include <stdlib.h>
using namespace std;

////////////////////////////////////


char Top[4]= "000";
char Mid[4]= "000";
char Bot[4]= "000";

////////////////////////////////////


void GameInst();
void Board();
void Player();

////////////////////////////////////


int main()
{
    cout<<"\n\t\t\tTicTacToe";
    cout<<"\n\t\tCreated by Tom Williams (DarkWhoppy)";
    GameInst();
    system("Pause");
}

void GameInst()
{
    cout<<"\n\n\n\nHow to play.\n"
        <<"q  w  e\n\n"
        <<"a  s  d\n\n"
        <<"z  x  c";
    cout<<"\n\nThat should look like a TTT board...\n\n";
    system("Pause");
    system("cls");
    Board();
    Player();
}

/////////////////////////////////////////////////////////////////


void Board()
{   
    cout<<"\t\t\t---Board---\n\n";
    
    cout<<"\t\t\t"<<Top<<"\n";
    cout<<"\t\t\t"<<Mid<<"\n";
    cout<<"\t\t\t"<<Bot<<"\n";
}

////////////////////////////////////////////////////////////////


void Player()
{
    char picked;
    
    cout<<"Pick'a spot, you know the keys: ";
    cin>>picked;
    
    if(picked == 'q' && Top[0] == '0')
    { Top[0] = 'X'; }
    
    if(picked == 'w' && Top[1] == '0')
    { Top[1] = 'X'; }
    
    if(picked == 'e' && Top[2] == '0')
    { Top[2] = 'X'; }
    
    if(picked == 'a' && Mid[0] == '0')
    { Mid[0] = 'X'; }
    
    if(picked == 's' && Mid[1] == '0')
    { Mid[1] = 'X'; }
    
    if(picked == 'd' && Mid[2] == '0')
    { Mid[2] = 'X'; }
    
    if(picked == 'z' && Bot[0] == '0')
    { Mid[0] = 'X'; }
    
    if(picked == 'x' && Bot[1] == '0')
    { Bot[1] = 'X'; }
    
    if(picked == 'c' && Bot[2] == '0')
    { Bot[2] = 'X'; }    
    
    system("cls");
    
    Board();
}

//////////////////////////////////////////////////////////


void AI()
{
    
}
[edited by - DarkWhoppy on August 1, 2003 10:35:35 PM]
Visit the 3DHangout
If you want to do something simple, why don''t you try this:

If you can complete three in a row do it
else
If center is available take that
else
if there''s a spot adjacent to any of the computer''s X''s take that

simple yet effective.
Advertisement
Ahhh, a tic-tac-toe, sounds like a good place to try out that ''game tree AI''
For example, assume AI start out. Most likely he would like to take the center piece. Hence, the root of the tree would be ''take center piece''. Next, the player has 8 possible places to put, giving you 8 possible scenarios. Based on each scenario, handle each one respectively. Meaning branching out of root, you get 8 child nodes to handle. Do this recursive, generating a complete tree handling all the game state(you might want to use an editor for it). Dump the content into a file, and load into the game, and just do simple node navigation! Sounds like a pain in the ass, but it''s a way to try this tree concept out. I think it was used for that Deep Blue machine which won the world chess champion, I''m not sure.
Isn''t there a way to insure at least a draw in TicTacToe?

I thinks there''s a strategy to insurance that you can''t loose, check it out.
I teleported home one night; With Ron and Sid and Meg; Ron stole Meggie's heart away; And I got Sydney's leg. <> I'm blogging, emo style
yes there is a way to always insure a draw, so the game isnt that fun if 2 people are smart. The middle spot is not always the best spot when available (considering that it is the first turn, and the person you are playing doesnt know the game very good).
You can use something called a Game Tree to solve a game like Tic-Tac-Toe (or checkers, or chess). The nice thing about it is that you can easily adjust the difficulty level of the computer from a perfect player, to a crappy player.

If you do a search on google for game tree, minimax, and tic-tac-toe you''ll come up with some explanations that are far better than anything I could give.

If you have any questions I''ll try my best to answer them.

Good luck,
Will
------------------http://www.nentari.com

This topic is closed to new replies.

Advertisement