Thread: C++ Tictactoe

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    6

    C++ Tictactoe

    I just wanted to write a c++ programme to make the computer play a game of tictactoe against a human opponent. I am very much new to this field and this is the first ai program I am going to write. I plan to have three versions of the program just for learning as much as I can. I need small help here and there in all.
    The first version will be just the algorithm part with a very poorly designed interface. I think that using some minimax principles, I can manage to write a code that ensures a win or a draw for the computer. What I am not sure is that should this be called AI? The other option, which feels more like a real AI, is that the computer plays very badly in the beginning, slowly learns from its mistakes and finally becomes an unbeatable opponent. Which one is real AI? If it is the second one, then I have no idea about how even to start the programme.

    In the second version, I plan to work on the interface part. I want to have a 3D graphical interface. Although it sounds odd to have a 3D graphical interface for tictactoe, my purpose to do so is just learning. So mostly I will be using OpenGL or DirectX. Just one thing I don't understand about DirectX is that is it not just another c++ library? If it is, then why is it's SDK for?

    In the third version, I plan to add a to player mode play-able on the internet. Here, I have no clue how to start. Which library can be used for this purpose?

  2. #2
    Registered User fischerandom's Avatar
    Join Date
    Aug 2005
    Location
    Stockholm
    Posts
    71
    An algorithm based on A.I. is an algorithm that is trying to make a choice (the best) based on the criteria given, in a given situation from a number of choices. It acts intelligent.
    Bobby Fischer Live Radio Interviews http://home.att.ne.jp/moon/fischer/

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    52
    1. Your first idea (using minimax) would qualify as AI in my book - it's just a fixed algorithm AI. The second idea is a learning system, which is a branch of AI.

    2. DirectX is an SDK/API for 2D/3D graphics in Windows. It's not 'just another c++ library', but in effect it can be treated that way.

    3. SDL ( http://www.libsdl.org ) can handle networking as well as 2D/3D interfaces, and is very portable.

  4. #4
    Registered User
    Join Date
    Aug 2002
    Location
    Hermosa Beach, CA
    Posts
    446
    Somewhere in the AI threads I have a fully functioning tic-tac-toe game, that uses minmax. It only uses a console interface. Might be useful if you get stuck.
    The crows maintain that a single crow could destroy the heavens. Doubtless this is so. But it proves nothing against the heavens, for the heavens signify simply: the impossibility of crows.

  5. #5
    Cat Lover
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    109

  6. #6
    Registered User
    Join Date
    Apr 2007
    Posts
    26

    Question Help with tic tac toe......

    I am writing a simple tic tac toe problem for my first C++ programming class that needs four functions, one to display the board, one for player1 to enter its postion on the board one for player 2 to enter its position on the board, and a function that decides when a winner has emerged. I also need the program to loop until there is a winner and to ask if the players want to play another round. Right now I am just trying to get the basics down seeing if I can get an X to actually show up when I call the displayBoard function for the second time after player 1 goes. If anyone can help please let me know.

    Here is my code so far:
    Code:
    #include <iostream>
    #include <iomanip>
    using namespace std;
    
    //Global constants
    const int COLS = 3;             //Number of columns on the board
    const int ROWS = 3;             //Number of rows on the board
    
    void displayBoard(int [][COLS], int);   
    void player1 (int); 
    void player2 (int);
    
    int main ()
    {
        int space = 0 ;
        int space2 = 0 ;
        int B[ROWS][COLS] = {1, 2, 3, 4, 5, 6, 7, 8, 9 };
        cout << "The playing board looks like:\n";
        displayBoard(B, ROWS);    
        player1 (space);
        displayBoard(B, ROWS);
        system("pause");
        return 0;
    }
    
    
    void displayBoard ( int array[][COLS], int rows, int space, int space2 )
    {
         for (int x = 0; x < rows;  x++)
         {
             for (int y = 0; y < COLS; y++)
             { 
                 cout << array[x][y] << " ";
             }
             cout << endl;
         }
    }    
    
    void player1 (int space)
    {
         
         cout << "Please select a space on the board to place your 'x' by selecting space 1 through 9\n";
         cin >> space ;
         int B[ROWS][COLS] = {1, 2, 3, 4, 5, 6, 7, 8, 9 };
         if (space = 1) B[0][0] = 'x' ;
         if (space = 2) B[0][1] = 'x' ;
         if (space = 3) B[0][2] = 'x' ;
         if (space = 4) B[1][0] = 'x' ;
         if (space = 5) B[1][1] = 'x' ;
         if (space = 6) B[1][2] = 'x' ;
         if (space = 7) B[2][0] = 'x' ;
         if (space = 8) B[2][1] = 'x' ;
         else if (space =9) B[2][2] = 'x' ;
         
         
    }
    
    void player2 (int space2)
    
    {
         cout << "Please select a space on the board to place your 'x' by selecting space 1 through 9\n";
         cin >> space2 ;
         int B[ROWS][COLS] = {1, 2, 3, 4, 5, 6, 7, 8, 9 };
         if (space2 = 1) B[0][0] = 'x' ;
         if (space2 = 2) B[0][1] = 'x' ;
         if (space2 = 3) B[0][2] = 'x' ;
         if (space2 = 4) B[1][0] = 'x' ;
         if (space2 = 5) B[1][1] = 'x' ;
         if (space2 = 6) B[1][2] = 'x' ;
         if (space2 = 7) B[2][0] = 'x' ;
         if (space2 = 8) B[2][1] = 'x' ;
         else if (space2 =9) B[2][2] = 'x' ;
    }
    
    char winner (char winner)
    {
         int B[ROWS][COLS] = {1, 2, 3, 4, 5, 6, 7, 8, 9 };
         if (B[0][0] == B[0][1] && B[0][1] == B[0][2])
            winner = B[0][0];
         if (B[1][0] == B[1][1] && B[1][1] == B[1][2])
            winner = B[1][0];
         if (B[2][0] == B[2][1] && B[2][1] == B[2][2])
            winner = B[2][0];
         if (B[0][0] == B[1][0] && B[1][0] == B[2][0])
            winner = B[1][0];
         if (B[0][1] == B[1][1] && B[1][1] == B[2][1])
            winner = B[1][1];
         if (B[0][2] == B[1][2] && B[1][2] == B[2][2])
            winner = B[2][2];
         if (B[0][0] == B[1][1] && B[1][1] == B[2][2])
            winner = B[2][2];
         if (B[0][2] == B[1][1] && B[1][1] == B[2][0])
            winner = B[1][1];
         return winner ;
    }

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Glad to see you searched a little, but please post new questions in new threads. Include a link to the old thread if there is something in it that is pertinent to your problem. Maybe a moderator will move this into its own thread for you.

    Can you be more specific about what is happening that you don't expect, or why you think you need help? One thing I see now is:
    Code:
    if (space = 1)
    That should be ==, not =. Another thing is that you are not passing the actual board to the player1 function, so it is changing a temporary local board instead of the one that gets displayed later. You want to pass in the board as a function parameter like you do for displayBoard.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. TicTacToe AI
    By Loic in forum C++ Programming
    Replies: 9
    Last Post: 05-28-2007, 01:37 PM
  2. tictactoe symbol entering problem
    By ademkiv in forum C Programming
    Replies: 1
    Last Post: 03-21-2006, 11:53 AM
  3. TicTacToe game doesn't work
    By Overlord in forum C++ Programming
    Replies: 2
    Last Post: 01-06-2006, 07:46 AM
  4. Tictactoe problem
    By endo in forum Windows Programming
    Replies: 3
    Last Post: 04-10-2005, 09:36 AM
  5. TicTacToe OOP
    By NickESP in forum C++ Programming
    Replies: 4
    Last Post: 02-07-2003, 11:47 AM