can someone pls tell me if u found bugs with my tic tac toe! thnx
Code:
#include <iostream.h>
#include <stdlib.h>
#include <ctype.h>
#include <time.h>
class XOXboard
{
public:
XOXboard();
~XOXboard();
void draw(void);
void PosXOX(void);
void update(int x);
int check(void);
private:
static int xwins;
static int owins;
char *xox;
};
int XOXboard::owins = 0;
int XOXboard::xwins = 0;
XOXboard::XOXboard()
{
xox = new char[10];
for(int i = 0; i < 9; ++i)
xox[i] = ' ';
xox[i] = '\0';
}
XOXboard::~XOXboard()
{
delete [] xox;
}
void XOXboard::draw()
{
int i = 0;
system("cls");
cout << "\n\n\t\t SAMPLE TIC TAC TOE MADE BY EON-SLASH"
<< "\n\n\n\t\t\t " << ' ' << xox[0] << " | " << ' ' << xox[1] << " | " << ' '
<< xox[2] << endl
<< "\t\t\t ----------------" << endl
<< "\t\t\t " << ' ' << xox[3] << " | " << ' ' << xox[4] << " | " << ' '
<< xox[5] << endl
<< "\t\t\t ----------------" << endl
<< "\t\t\t " << ' ' << xox[6] << " | " << ' ' << xox[7] << " | " << ' '
<< xox[8] << endl << endl << endl
<< "\t\t\t\t\t\t CURRENT SCORES X: " << xwins << " O: " << owins;
}
void XOXboard::PosXOX()
{
char offset;
while(1)
{
cout << "\nPOSITION ( 1 - 9 ) : ";
cin >> offset;
cin.ignore(255,'\n');
if(int(offset) < 49 || int(offset) > 57)
{
cout << "\nwrong choice\n";
continue;
}
else
if(xox[offset - 49] == 'X' || xox[offset - 49] == 'O')
{
cout << "\nposition " << offset << " is already " << xox[offset - 49];
continue;
}
else
break;
}
xox[int(offset - 49)] = 'X';
}
int XOXboard::check()
{
char *x = new char[10];
for(int i = 0; i < 10; ++i)
x[i] = xox[i];
if((x[0] == x[1] && x[0] == x[2]) || (x[0] == x[4] && x[0] == x[8]) || \
(x[0] == x[3] && x[0] == x[6]))
{
if(x[0] != ' ')
{
cout << "\n\n\t\t\t\t" << x[0] << " wins\n\n";
if(x[0] == 'X')
xwins++;
else
owins++;
return 1;
}
}
if((x[1] == x[4] && x[1] == x[7]))
{
if(x[1] != ' ')
{
cout << "\n\n\t\t\t\t" << x[1] << " wins\n\n";
if(x[1] == 'X')
xwins++;
else
owins++;
return 1;
}
}
if((x[2] == x[5] && x[2] == x[8]) || (x[2] == x[4] && x[2] == x[6]))
{
if(x[2] != ' ')
{
cout << "\n\n\t\t\t\t" << x[2] << " wins\n\n";
if(x[2] == 'X')
xwins++;
else
owins++;
return 1;
}
}
if((x[3] == x[4] && x[3] == x[5]))
{
if(x[3] != ' ')
{
cout << "\n\n\t\t\t\t" << x[3] << " wins\n\n";
if(x[3] == 'X')
xwins++;
else
owins++;
return 1;
}
}
if((x[6] == x[7] && x[6] == x[8]))
{
if(x[6] != ' ')
{
cout << "\n\n\t\t\t\t" << x[6] << " wins\n\n";
if(x[6] == 'X')
xwins++;
else
owins++;
return 1;
}
}
return 0;
}
void XOXboard::update(int x)
{
srand(time(NULL));
int ai = rand() % 9;
if(x >= 5)
return;
while(1)
{
if(xox[ai] == 'X' || xox[ai] == 'O')
{
ai = rand() % 9;
continue;
}
else
break;
}
xox[ai] = 'O';
}
int check(XOXboard *eon, int shot);
int main()
{
int shot = 0;
int checker;
XOXboard *eon = new XOXboard;
while(shot++ < 5)
{
eon->draw();
eon->PosXOX();
checker = check(eon, shot);
if(checker == 1)
{
shot = 0;
eon = new XOXboard;
continue;
}
else
if(checker == 2)
break;
eon->update(shot);
checker = check(eon, shot);
if(checker == 1)
{
shot = 0;
eon = new XOXboard;
continue;
}
else
if(checker == 2)
break;
}
eon->draw();
cout << "\nTHNX FOR PLAYING!\n\n";
system("pause");
return 0;
}
int check(XOXboard *eon, int shot)
{
char choice;
if((eon->check()) || (!eon->check() && shot >= 5))
{
eon->draw();
if(!eon->check() && shot >= 5)
cout << "\nITS A DRAW!!!!";
cout << "\nDO YOU WANT TO PLAY AGAIN? (Y/N): ";
cin >> choice;
cin.ignore(255, '\n');
if(tolower(choice) == 'y')
return 1;
else
return 2;
}
return 0;
}
pls tell me about bugs if you found some!!!! THNX!!!
hoy mark try mo toh ASTIG hehehehe! wala lang!!!