Recently i saw this code on the net and i have some questions taht i dun understand.
Hope some1 can help to ans.

#include <iostream>
#include <stdlib.h>
#include <time.h>
#include <string.h>

using namespace  std;

struct combos
{
    char key[3];
    char holder[3];
};
void displayBoard(); //display board
void computerMove(); //AI;s Move
bool menu(); 
bool checkWin(); 
int AiBrain(); 
bool isLegal(int); 
void move(bool);
void addToHolder(int,char); 
char board[10] = {'0','1','2','3','4','5','6','7','8','9'};
combos winKey[9];
bool currentPlayer = false; 

void main()
{
    //aLL THE WINNING COMBI
    strcpy(winKey[1].key, "123");
    strcpy(winKey[2].key, "456");
    strcpy(winKey[3].key, "789");
    strcpy(winKey[4].key, "147");
    strcpy(winKey[5].key, "258");
    strcpy(winKey[6].key, "369");
    strcpy(winKey[7].key, "159");
    strcpy(winKey[8].key, "357");
    bool computer = menu(); 
    int tieCount = 0;
    displayBoard();
    while(!checkWin() && tieCount != 9)
{
    if(currentPlayer == true)
            currentPlayer = false;
        else
            currentPlayer = true;
        move(currentPlayer);
        tieCount++;
        displayBoard();

        if(checkWin() && computer)
            computer = false;
        if(computer == true)
        {
            computerMove();
            tieCount++;
            displayBoard();
        }
    }

    if(tieCount == 9 && !checkWin())
        cout << "TIE";
else
    {
        if(currentPlayer == true)
            cout << "Player 1 WINS" << endl;
        else
            cout << "Computer WINS" << endl;
    }
    system("pause");
}
bool menu()
{
    int num;
cout << "Enter 1 to start" << endl;
    cin >> num;
system("cls");
if(num == 1)
    return true;
else
return false;
}
void displayBoard()//DISPLAY
{
    system("cls");
cout << "\n " << board[1] << " | " << board[2] << " | " << board[3] << endl
 << "| |" << endl
 << "---------------" << endl
 << " " << board[4] << " | " << board[5] << " | " << board[6] << endl
 << "| |" << endl
 << "---------------" << endl
 << " " << board[7] << " | " << board[8] << " | " << board[9] << endl
 << "| |" << endl;
}
bool checkWin()
{
    for(int i=1; i<9; i++)
    {
    if(winKey[i].holder[0] == winKey[i].holder[1] && winKey[i].holder[0] == winKey[i].holder[2])
        {
    if(winKey[i].holder[0] == 'X' || winKey[i].holder[0] == 'O')
    return true;
}
}
return false;
}
void move(bool who)
{
    int spot;
if(who == true)
    cout << "\nEnter your move Player 1: ";
    cin >> spot;
if(isLegal(spot))
    {
    if(who == true)
        {
    addToHolder(spot,'X');
board[spot] = 'X';
}
else
        {
    addToHolder(spot,'O');
board[spot] = 'O';
}
}
else
    move(who);
}
bool isLegal(int spot)
{
    if(spot <= 0 || spot >= 10)
    return false;
else if(board[spot] == 'X' || board[spot] == 'O')
return false;
else
return true;
}
void addToHolder(int spot, char player)
{
    for(int i=0; i<9; i++)
    {
    for(int j=0; j<3; j++)
        {
    if(winKey[i].key[j] == board[spot])
    winKey[i].holder[j] = player;
}
}
}
int AiBrain()
{
    int myself[9] = {0,0,0,0,0,0,0,0,0};
int block[9] = {0,0,0,0,0,0,0,0,0};
int i,j;

for(i=0; i<9; i++)
    {
    for(j=0; j<3; j++)
        {
    if(winKey[i].holder[j] == 'X')
    block[i] += 1;
else if(winKey[i].holder[j] == 'O')
myself[i] += 1;
}
}

for(i=0; i<9; i++)//check for win
    {
        if(myself[i] == 2 && block[i] == 0)
    return i;
}
for(i=0; i<9; i++)//check for block
    {
    if(block[i] == 2 && myself[i] == 0)
return i;
}
for(i=0; i<9; i++)//next best move
    {
    if(myself[i] == 1 && block[i] == 0)
    return i;
}
return 0;//no good move 
}
void computerMove()//Random Move
{
    int compPick = evaluation(),
        randPick=100;
    srand((unsigned int)time(0)); 

    if(compPick == 0)
    {
    while(!isLegal(randPick))
        randPick=(rand()%9)+1;

    addToHolder(randPick,'O');
    board[randPick] = 'O';
}
else
    {
    for(int i=0; i<3; i++)
        {
    if(winKey[compPick].holder[i] != 'X' && winKey[compPick].holder[i] != 'O')
            {
        char temp = winKey[compPick].key[i];
        int where = atoi(&temp);//convert char to int
        addToHolder(where,'O');
        board[where] = 'O';
        i=5;
    }
}
}
if(currentPlayer == true)
    currentPlayer = false;
else
currentPlayer = true;
}

Why is he using struct combos for, whats the use and whats the use of strcpy??

Member Avatar for iamthwee

Best NOT to copy someone else's code and do it yourself.

A quick glance at the code indicates whoever wrote it didn't know what he/she was doing. For example lines 28-35 are writing beyond the boundries of the array. Line 25 declares undefined return value for main() -- the only legal value is int, never void. I'm not going to bother reading the rest of that mess.

whats the use of strcpy??

If you don't know what strcpy() does then you shouldn't be attempting to make sense of that code. google for "strcpy" and you will find the answer to your question.

What do u mean by writing beyond the boundries of the array??
and whats wrong with line 25??
could you explain? Thx!

and whats wrong with line 25??

I think @Ancient means it should be int main() rather than void main()

Oh! okay thx.
what about writing beyond the boundries of the array?? what does this means?

It's what happens when you set up an array like int array[10] and use a statement like array[12] = 23;. There is no array[12]. It only goes to array[9].

In your case, "123" is 4 bytes, not 3. it's '1','2','3','\0'. You forgot about the string indicator.

So it shld be strcpy(winKey[1].key, '1','2','3','\0')???

However when it runs i gives error like:
error C2661: 'strcpy' : no overloaded function takes 5 arguments
what does that means?

It means strcpy() does not take 5 arguments as you tried to do with strcpy(winKey[1].key, '1','2','3','\0')

Look up
1) How strcpy() works.
2) Concentrate on it's parameters and how they are defined.
3) Look up how strings are laid out (their definition).

you tried to do with

It's not his code. It's someone else's code ;)

Ya its not my code

Member Avatar for Kwetal

It's someone else's code

Please throw that piece of sh.. away. It exposes a number of plain errors and bad programming habits. Go read a good book about programming in general and C++ programming in particular and start writing your own programs. If you show some effort we may gladly help you with any questions that remain.

It's not his code. It's someone else's code ;)

So? Does that make what I said wrong? :P

Follow Kwetal's advice. What are you trying to learn from this code?

naa!! you're never wrong!! :)

this is wrong!
SNIP

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.