Botched random choice selection function

This is a discussion on Botched random choice selection function within the C++ Programming forums, part of the General Programming Boards category; Good morning. I was making a tic-tac-toe program to become a little more experienced with C++, but I've come to ...

  1. #1
    Xom
    Xom is offline
    Registered User
    Join Date
    Aug 2011
    Posts
    1

    Botched random choice selection function

    Good morning. I was making a tic-tac-toe program to become a little more experienced with C++, but I've come to an error I can't remove.

    Code:
    #include <iostream>
    #include <ctime>
    #include <cstdlib>
    using namespace std;
    
    int AI(int board[3][3]);
    int wincon(int board[3][3]);
    
    int board()
    {
        int board[3][3]={};
        int j=1;
        int XorO;
        int *pXorO=&XorO;
        pXorO=new int;
        for (int i=0;j==1; i++)
        {
            XorO=i%2+1;
            int choice;
            if (XorO==1)
            {
            cin>>choice;
            }
            else
            {
                choice=AI(board);
                }
        switch(choice)
        {
        case 1:
                      board[0][2]=XorO;
                      break;
        case 2:
                      board[1][2]=XorO;
                      break;
        case 3:
                      board[2][2]=XorO;
                      break;
        case 4:
                      board[0][1]=XorO;
                      break;
        case 5:
                      board[1][1]=XorO;
                      break;
        case 6:
                      board[2][1]=XorO;
                      break;
        case 7:
                      board[0][0]=XorO;
                      break;
        case 8:
                      board[1][0]=XorO;
                      break;
        case 9:
                      board[2][0]=XorO;
                      break;
                      }
                      for(int j=0; j<3; j++)
                              {
                               for (int k=0; k<3; k++)
                                   {
                                          cout<<board[k][j];
                                          }
                                   cout<<"\n";
                                   }
                                   wincon(board);
                                   
        }
        return 0;
    }
                      
    int main()
    {
        cout<<"Would you like to play a game of tic-tac-toe?\n";
        cout<<"If so, please make the first move.\n";
        board();
        return 0;
    }
    
    int AI((int board)[3][3])
    {   
        int reroll;
        do
        {
        int x=rand()%3;
        int y=rand()%3;
        if ((board[x][y]==1)||(board[x][y]==2))
           {
            reroll=1;
            }
        else
            {
                     reroll=0;
                     }
                     }
        While (reroll)
        return (3*x+y+1);
    }
    
    int wincon(int (&board)[3][3])
    {
        for (int i = 0; i < 3; i++){
            if ((board[i][1] == board[i][2] == board[i][3])||(board[1][i] == board[2][i] == board[3][i]))
            {
                    cout<<"Player "<<*pXorO<<" wins!";
                    break;
            }
            else if ((board[1][1] == board[2][2] == board[3][3])||(board[1][3] == board[2][2] == board[3][1]))
            {
                 cout<<"Player "<<XorO<<" wins!";
                 break;
                 }
                 }
        return 0;
    }
    
    
    
    }
    I'm confident that the error isn't in board(), since that was the first function I took care of, and it handled two-player tic-tac-toe well. Whenever I compile, I get the errors "'Int AI' redeclared as different type of symbol" on line 80, with "int AI((int board)[3][3])", and "Previous declaration of 'int AI((*)[3])'", on line 6 with the prototype of AI.

    Can anyone see where I went wrong?

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,554
    Just make your implementation match your prototypes.

    There is no need to add any extra ()& characters.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    2,756
    Look at the function implementation:
    Code:
    int AI((int board)[3][3])
    You don't need the () around the int board.

    There are other errors but this is the one you asked about.

    Jim
    Last edited by jimblumberg; 08-12-2011 at 01:07 PM.

  4. #4
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    I didn't check the logic, but there are several syntax errors:
    1. It is while ..not While.
    2.You need a ; after the while() of a do..while
    3. pXorO and XorO are local to board() , to access them in other functions, pass them by reference.
    4. x and y need to be declared outside the loop first.
    5. What is the last ' } ' for ?
    6. Remove the () around int board in the definition.
    7. Why do you have an array named board inside a function called board ?

    [EDIT: Just saw some of these covered above]
    Last edited by manasij7479; 08-12-2011 at 01:06 PM.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Random selection of a word
    By doobyscoo in forum C Programming
    Replies: 1
    Last Post: 04-14-2003, 11:39 AM
  2. recursive selection sort function
    By Cornelius in forum C Programming
    Replies: 3
    Last Post: 11-08-2002, 03:12 PM
  3. Choice function
    By Pr0gY in forum C Programming
    Replies: 1
    Last Post: 04-18-2002, 08:08 PM
  4. random number selection
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 03-11-2002, 08:02 AM
  5. random selection of words from a text file
    By archie in forum C++ Programming
    Replies: 0
    Last Post: 03-01-2002, 11:59 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21