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??