Beginner C++ programmer, Tic Tac Toe.

I'm not sure if i'm on the right track of getting this program to work. I've been writing out different scenarios every possibility. It seems right to me, but it's not debugging for some reason.


#include <iostream>

using namespace std;

void CheckPlay(char board[3][3]);
void PrintBoard(char board[3][3]);

int main()
{
PrintBoard(char board[3][3]);

char name1;
char name2;

cout << "Please enter your name" << endl;
cin >> name1;

cout << "Please enter your opponents name" << endl;
cin >> name2;

cout << "Alright let the games begin. " << name1 << "v.s. " << name2 << "." << endl;

while (CheckPlay == false)
{
char board[3][3];
cout << name1 << ", it's your move." << endl;
cin >> board[][];
cout << "It's your move now, " << name2 << endl;
cin >> board[][];
}

return 0;

}

void PrintBoard (char board[3][3])
{

cout << "_" << board[1][1] << "_|_" << board[1][2] << "_|_" << board[1][3] << "_" << endl;
cout << "_" << board[2][1] << "_|_" << board[2][2] << "_|_" << board[2][3] << "_" << endl;
cout << " " << board[3][1] << " | " << board[3][2] << " | " << board[3][3] << endl;

}

void CheckPlay (char board[3][3])
{
char name1 = 'X';
char name2 = 'O';

if (board[1][1] == name1, board[1][2] == name1, board[1][3] == name1)
{
cout << name1 << " has won!" << endl;
}
else if (board[2][1] == name1, board[2][2] == name1, board[2][3] == name1)
{
cout << name1 << " has won!" << endl;
}
else if (board[3][1] == name1, board[3][2] == name1, board[3][3] == name1)
{
cout << name1 << " has won!" << endl;
}
else if (board[1][1] == name1, board[2][2] == name1, board[3][1] == name1)
{
cout << name1 << " has won!" << endl;
}
else if (board[1][2] == name1, board[2][2] == name1, board[3][2] == name1)
{
cout << name1 << " has won!" << endl;
}
else if (board[1][3] == name1, board[2][3] == name1, board[3][3] == name1)
{
cout << name1 << " has won!" << endl;
}
else if (board[1][1] == name1, board[2][2] == name1, board[3][3] == name1)
{
cout << name1 << " has won!" << endl;
}
else if (board[1][1] == name1, board[1][2] == name1, board[1][3] == name1)
{
cout << name1 << " has won!" << endl;
}
else if (board[1][3] == name1, board[2][2] == name1, board[3][1] == name1)
{
cout << name1 << " has won!" << endl;
}
else
{
cout << name2 << " has won!" << endl;
}
}
Valid indices for array are 0 to size-1 where size is the number of elements in the array.

For an array char board[3][3] the allowable indices are board[0][0] to board[2][2]. You are using indices that are outside the allowable range, resulting in undefined behavior.

i have made those following changes and no luck still. :(
All those if and else/if conditions are wrong.

For the top row it should be:

if ( board[0][0] == name1 && board[0][1] == name1 && board[0][2] == name1 )

The way the comma operator works is: Evaluate the left hand argument, discard the result and evaluate the right hand argument -- the expression becomes the value of the right hand argument.

if ( board[0][0] == name1, board[0][1] == name1, boar[0][2] == name1 )
is equivalent to
if ( board[0][2] == name1 )
since the first arguments are discarded after being evaluated.

In addition, it would probably make more sense to do those comparisons like so:

1
2
3
    if ( board[0][0] == board[0][1] && board[0][1] == board[0][2] )
        cout << board[0][0] << " has won!\n" ;
    else if ...





Topic archived. No new replies allowed.