We are suppose to create a program that will let us play tic tac toe. We are required to have 4 functions including main and use arrays as parameters to functions. Is my code correct?? Also there's something wrong with my code and can't figure it out.

#include<iostream>
using namespace std;

bool player1Won();
 bool player1WonInARow();
 bool player1WonInAColumn();
 bool player1WonInDia();

bool player2Won();
 bool player2WonInARow();
 bool player2WonInAColumn();
 bool player2WonInDia();

char tic_tac_toe[3][3]={'*','*','*','*','*','*','*','*','*'};
int player1R[5];
 int player1C[5];
 int player2R[4];
 int player2C[4];
 int countForP1R=0;
 int countForP2R=0;

int main()

{

    int i=0;
    for(;i<9;i++)
    {
    int x,y,z,k;

    //display the contents of the board
    for(int a=0;a<3;a++)
{
    for(int b=0;b<3;b++)
{
    cout<<tic_tac_toe[a][b]<<endl;
}

//ask player one to select a location
    cout<<"Player 1 select a location by entering two number while the first number, in the range of 0 to 2, stands for the row of the loation and the second number in the range of 0 to 2, stands for the columns of the location. Sperate two number by space. ONLY LOCATIONS OCCUPIED BY * ARE AVALIABLE"<<endl;
    cin>>x>>y;

    tic_tac_toe[x][y]='x';
    player1R[i]=x;
    player1C[i]=y;

 //display the contents of the board
    for(int a=0;a<3;a++)
{
    for(int b=0;b<3;b++)
{
    cout<<tic_tac_toe[a][b]<<endl;
}
 //determinate if player1 wins
    if(player1Won()==true)
{
    cout<<"Player 1 wins the game!";

return 0;
}

//ask player two to select a location
    cout<<"PLAYER2 Select a location by entering two number while"
    <<"\nthe first number, in the range of 0 to 2, stands"
    <<"\nfor the row of the loation and the second num, "
    <<"\n in the range of 0 to 2, stands for the columns"
    <<"\nof the location. Sperate two number by space."
    <<"\n ONLY LOCATIONS OCCUPIED BY * ARE AVALIABLE"<<endl;
    cin>>z>>k;

    tic_tac_toe[z][k]='O';
    player2R[i]=z;
    player2C[i]=k;

//determinate if player2 wins
    if(player2Won()==true)
{
    cout<<"Player 2 wins the game!";

    return 0;
}
}

    return 0;

}

//Determinate if player1 wins

bool player1Won();
{
    bool player1Won=false;
    if(player1WonInARow()==true)
{
    cout<<"player1WonInARow"<<endl;
    player1Won=true;
}
    if(player1WonInAColumn()==true)
{
    cout<<"player1WonInAColumn";
    player1Won=true;
}

    if(player1WonInDia()==true)
{
    cout<<"player1WonInDia";
    player1Won=true;
}

    return player1Won;
 }
 bool player1WonInARow();
 {
 bool player1WonInARow=false;
 if(tic_tac_toe[0][0]=='X'&&tic_tac_toe[1][0]=='X'&&tic_tac_toe[2][0]=='X') player1WonInARow=true;
 else if(tic_tac_toe[0][1]=='X'&&tic_tac_toe[1][1]=='X'&&tic_tac_toe[2][1]=='X') player1WonInARow=true;
 else if(tic_tac_toe[0][2]=='X'&&tic_tac_toe[1][2]=='X'&&tic_tac_toe[2][2]=='X') player1WonInARow=true;

return player1WonInARow;
 }
 bool player1WonInAColumn();
{
 bool player1WonInAColumn=false;

if(tic_tac_toe[0][0]=='X'&&tic_tac_toe[0][1]=='X'&&tic_tac_toe[0][2]=='X') player1WonInAColumn=true;
else if(tic_tac_toe[1][0]=='X'&&tic_tac_toe[1][1]=='X'&&tic_tac_toe[1][2]=='X') player1WonInAColumn=true;
 else if(tic_tac_toe[2][0]=='X'&&tic_tac_toe[2][1]=='X'&&tic_tac_toe[2][2]=='X') player1WonInAColumn=true;
 return player1WonInAColumn;

}
}

Recommended Answers

All 2 Replies

Your code doesn't follow the description. You got 8 functions besides Main and none of them take any parameters, let alone any arrays. I would suggest using a class to represent a player. This allows you to have one function that accepts a player object and the board array as parameters, instead of 2 separate functions.

Since this needs a major re-write it would be unnecessary to fix any problems in the code.

I would suggest using a class to represent a player.

Possibly two classes: player and board. That'd be my approach probably. However, we don't know the OP's background. I'm thinking that, judging from the code, they have not gotten to classes yet, so that's probably not an option.

To the OP: you need to format your code. I pasted it into an IDE (Code::Blocks). The spacing and the blank line placement seem to have no rhyme or reason. Compiling the code, I got this error:

error: expected '}' at end of input

Every opening bracket must have a closing bracket. Somewhere you are missing one or it's in the wrong spot. If you indent your blocks of code, the missing bracket(s) will pop out at you. Code::Blocks and the other IDEs have tools to format your code. It is unreadable as it is, so it's not surprising that you missed it. Read the error messages. It is clear what is wrong.

As to your arrays, you have a GLOBAL 3x3 array called tic_tac_toe. I would name it something different like board so it's more descriptive. The general rule is that you don't pass global variables to functions and you don't HAVE global variables unless there isa really good reason for it. So if the board is the array to be passed, then make it a non-global variable. Otherwise what's the point of passing it to the function?

As to what your functions might be, I'm guessing there is supposed to be a function called DisplayBoard that takes the 3x3 array and displays the board. Another two functions might take the same 3x3 array and check for whether player 1 or player 2 won the game. Or you might combine it into one function.

That takes us up to two or three functions. So one or two left I imagine that might be a function (or two) that asks the user to enter his/her move and fills in the board. Again, pass the board as an array, plus any other information that the function needs.

So design first, THEN code. You are coding first, then designing. And again, format your code so it is readable. That's not just so your teacher and Daniweb members can read it, it's so YOU can read it.

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.