# Thread: making tic tac toe, need some help

1. ## making tic tac toe, need some help

Code:
```/*
WE NEED A CLASS NAMED PLAYER WITH METHOD TAKESQUARE
WE NEED A CLASS NAMED BOARD WITH METHOD CLEAR AND SET SQUARE
WE NEED AN ARRAY NAMED BOARDVECTOR [3][3]
*/

#include <iostream>
#include <string>

using namespace std;

//=========================================================

class PlayingArea
{

public:

PlayingArea();
~PlayingArea();
int ClearBoard(); //this will clear the board and start a new game
int SetSquare(int SquareTaken, string Symbol); //this will place an X or and O in the appropriate space

private:

string BoardVector[3][3]; //this is the tic tac toe board

};

class Player
{

public:
Player();
~Player();
int TakeSquare(); //this will return which square the user wishes to take.
void Symbol();
int Wins;
int Losses;
int Ties;
int SquareTaken;
private:
string PlayerSymbol;

};

//==========================================================

int PlayingArea::ClearBoard()
{
int z=1;

for (int x=1; x < 4; x++){

int y=1;

BoardVector[x][y] = z;
z++;
y++;
BoardVector[x][y] = z;
z++;
y++;
BoardVector[x][y] = z;
z++;
y++;
}

return(0);

}

//===========================================================

int PlayingArea::SetSquare(int SquareTaken, string Symbol)
{

int z=1;

for (int x=1; x < 4; x++){

int y=1;

if (z == SquareTaken){
BoardVector[x][y] = Symbol;
}
z++;
y++;

if (z == SquareTaken){
BoardVector[x][y] = Symbol;
}
z++;
y++;

if (z == SquareTaken){
BoardVector[x][y] = Symbol;
}
z++;
y++;
}

return(0);
}

//===========================================================

PlayingArea()
{
return NULL;
}

//===========================================================

~PlayingArea()
{
return NULL;
}

//===========================================================

Player()
{
return NULL;
}

//===========================================================

~Player()
{
return NULL;
}

//===========================================================

int main()
{

PlayingArea Board;
Player PlayerA;
Player PlayerB;

return(0);
}```
--------------------Configuration: TicTacToe - Win32 Debug--------------------
Compiling...
Source.cpp
C:\Documents and Settings\Owner\Desktop\Comp Prog\C++\Sams C++\Lesson 10\TicTacToe\Source.cpp(118) : error C2588: '::~PlayingArea' : illegal global destructor
C:\Documents and Settings\Owner\Desktop\Comp Prog\C++\Sams C++\Lesson 10\TicTacToe\Source.cpp(119) : error C2084: function 'int __cdecl PlayingArea(void)' already has a body
C:\Documents and Settings\Owner\Desktop\Comp Prog\C++\Sams C++\Lesson 10\TicTacToe\Source.cpp(132) : error C2588: '::~Player' : illegal global destructor
C:\Documents and Settings\Owner\Desktop\Comp Prog\C++\Sams C++\Lesson 10\TicTacToe\Source.cpp(133) : error C2084: function 'int __cdecl Player(void)' already has a body
C:\Documents and Settings\Owner\Desktop\Comp Prog\C++\Sams C++\Lesson 10\TicTacToe\Source.cpp(142) : error C2146: syntax error : missing ';' before identifier 'Board'
C:\Documents and Settings\Owner\Desktop\Comp Prog\C++\Sams C++\Lesson 10\TicTacToe\Source.cpp(142) : error C2065: 'Board' : undeclared identifier
C:\Documents and Settings\Owner\Desktop\Comp Prog\C++\Sams C++\Lesson 10\TicTacToe\Source.cpp(143) : error C2146: syntax error : missing ';' before identifier 'PlayerA'
C:\Documents and Settings\Owner\Desktop\Comp Prog\C++\Sams C++\Lesson 10\TicTacToe\Source.cpp(143) : error C2065: 'PlayerA' : undeclared identifier
C:\Documents and Settings\Owner\Desktop\Comp Prog\C++\Sams C++\Lesson 10\TicTacToe\Source.cpp(144) : error C2146: syntax error : missing ';' before identifier 'PlayerB'
C:\Documents and Settings\Owner\Desktop\Comp Prog\C++\Sams C++\Lesson 10\TicTacToe\Source.cpp(144) : error C2065: 'PlayerB' : undeclared identifier
Error executing cl.exe.

TicTacToe.exe - 10 error(s), 0 warning(s)

2. Did you miss the part where you ask a question?

By the way, your function definations could use some work. Take a look at this:
Code:
`int PlayingArea::SetSquare(int SquareTaken, string Symbol)`
Compare that with this:
Code:
`PlayingArea()`
You need to put the methods of your class into the scope of the class.

3. how do i correct those errors.

4. I made a tic tac toe game the other day... 2 hours of coding.. 1 hour to debug/tweak. I'll post up the code of what I came up with after you've demonstrated that you have a working program. So far, the compiler is giving you good advice on what needs to be fixed.

5. Originally Posted by Frantic-
how do i correct those errors.
all your compilation errors come from invalid declarations of constructors and destructors.

Code:
```PlayingArea()
{
return NULL;
}

//==================================================  =========

~PlayingArea()
{
return NULL;
}

//==================================================  =========

Player()
{
return NULL;
}

//==================================================  =========

~Player()
{
return NULL;
}```
with:

Code:
```PlayingArea::PlayingArea()
{
}

//=========================

PlayingArea::~PlayingArea()
{
}

//=========================

Player::Player()
{
}

//=========================

Player::~Player()
{
}```
(remember that constructors and destructors cannot return any value)

Best regards, bilbo

6. ahh thanks

ill have to remember both of those 2 things.

7. when i call this function

Code:
```int PlayingArea::ClearBoard()
{
int z=1;

for (int x=1; x < 4; x++){

int y=1;

BoardVector[x][y] = z;
z++;
y++;
BoardVector[x][y] = z;
z++;
y++;
BoardVector[x][y] = z;
z++;
y++;
}

return(0);
}```
i get a send error report screen, but when i click dont send, the program finished running just fine and waits for you to press enter. Does this have anything to do with this function, or is it something with my computer?

8. Oh, it's the function. You're overstepping your array bounds. Look at your declaration.
Code:
`	string BoardVector[3][3]; //this is the tic tac toe board`
That declares a two-dimensional array, with each dimension having a range of 0 - 2. Now take a look at your function:
Code:
```for (int x=1; x < 4; x++) //start x at 1, loop until x > 4
//that's bad, since x = 3 is invalid
{
int y=1; //start y at 1

BoardVector[x][y] = z;
z++;
y++; //y is now 2
BoardVector[x][y] = z;
z++;
y++; //y is now 3
BoardVector[x][y] = z; //accessing [x][3]...outside of array
}```

9. Here is my version of tic tac toe

Code:
```#include<iostream>
#include<cstdlib>
#include<iomanip>
#include<cctype>
using namespace std;

class TicTac
{

public:

TicTac();
void display_grid();
void prompt_player();
void update_grid();
void set_turn();
bool check_winner();
void reset();

private:

char grid[5][6];
char square;
int  turn;
int	 turn_counter;
bool tie;
};

char again();

int main()
{
TicTac Toe;

do{
Toe.reset();

Toe.display_grid();

do{
Toe.prompt_player();

Toe.update_grid();

Toe.set_turn();

Toe.display_grid();

}while(!Toe.check_winner());

}while(again()!='N');

return 0;
}

//FUNCTION DEFINITIONS

TicTac::TicTac()
{
for(int row=0; row<5; row++)
for(int column=0; column<5; column++)

grid[row][column]='0';

turn	= 1;
tie     = 0;
}

{

for(int row=0;row<5;row+=2)

grid[row][1]=grid[row][3]=(char)179;

for(row=1;row<4;row+=2)

grid[row][0]=grid[row][1]=grid[row][2]=grid[row][3]=grid[row][4]=(char)196;

for(row=0;row<6;row++)

grid[row][5]='\0';
}

void TicTac::display_grid()
{
int spacer;

if(turn==1)
spacer = 20;
else
spacer = 39;

system("cls");

if(tie)

cout << "\a\n\n\t   *** Tie!!  No Winners!!  ***\n";

else if(check_winner())

cout << "\a\n\n\t   *** Player " << turn << " weeens! ***\n";

else

cout << "\n\n\tPlayer 1 = X       Player 2 = O\n" << setw(spacer) << "------------";

cout << endl << endl << endl << endl;

for(int row=0;row<5;row++)

cout << setw(25) << grid[row] << endl;
}

void TicTac::prompt_player()
{
cout << endl << endl << endl;

cout << "\tPlayer " << turn << ", Enter a Square: ";
cin  >> square;
cin.ignore(1000,'\n');

turn_counter++;
}

void TicTac::update_grid()
{
bool found = false;

for(int row=0;!found && row<5; row+=2)
for(int column=0;!found && column<5;column+=2)
{
if(grid[row][column]==square)
{
if(turn==1)
grid[row][column]='X';

else
grid[row][column]='O';

found=true;
}
}

}

void TicTac::set_turn()
{
if(!check_winner())
{
if(turn==1)
turn= 2;
else
turn= 1;
}
}

bool TicTac::check_winner()
{

//Row Check
for(int row=0,column=0; row<5; row+=2)
{
if(grid[row][column]==grid[row][column+2]&&grid[row][column+2]==grid[row][column+4])

return true;
}

//Column Check
for(row=0,column=0; column<5; column+=2)
{
if(grid[row][column]==grid[row+2][column]&&grid[row+2][column]==grid[row+4][column])

return true;
}

//Diag Check
if(  (grid[0][0]==grid[2][2]&&grid[2][2]==grid[4][4]) ||
(grid[0][4]==grid[2][2]&&grid[2][2]==grid[4][0])   )

return true;

//Tie Check
if(turn_counter==9)
{
tie	 = true;
return true;
}

return false;
}

char again()
{
char choice;
cout << "\n\n\n\tWould ye' like to try again? (Y/N): ";
cin  >> choice;
cout << endl << endl;

}

void TicTac::reset()
{
int square_number = '1';

for(int row=0;row<5;row+=2)
for(int column=0;column<5;column+=2)

grid[row][column]=square_number++;

turn   = 1;
tie    = false;
turn_counter = 0;
}```