I'm working on a project for a tic tac toe game. I'm working on this with a partner (assigned) but he has kind of fallen by the wayside.
Here is the assignment:

Filenames:
ttt.h, ttt.cpp, tttmain.cpp
Your second project will be completed with a partner. For this project, you will
complete a two-player game of Tic-Tac-Toe using classes and structures.
You may design your program however you want, but you must include the following
minimum requirements:
Create a structure called Player with at least the following members:
o name
o symbol (X or 0)
o currentScore

Create a class called TicTacToe with at least the following members and
functions:
private data members:
-two Players
-int currentWinner (Eg. 0=tie, 1 = player1, 2 = player2)
-char board[3][3] to represent the playing board
Note: You may decide the return types and parameters of the following
functions and add any other functions that you need to complete your
program
public member functions:
constructor to initialize data
Reset() should reset the board and currentWinner
DisplayBoard() should display the board
Eg.

1|2|3

4|5|6

7|8|9

ChoosePosition() should prompt the user for their spot on the
board and update the board accordingly
Note: Include validation for spots already chosen in this
function.
CheckforWinner() should check for a winner and return the current
winner
DisplayScore() should display the current number of wins and ties
for each player.
This function should be called at the end of
each game.
Eg:
Player 1 Wins
Player 2 Wins

Tie Games

2
3
2
Use the following menu to drive your program in main():
1)
Start a new match (two new players)
2)
Continue the current match (the same two players)
3)
Exit
Create a UML diagram to represent your TicTacToe class.

Here is my code:

//ttt.h
// 
// June 9, 2015

#include<string>
using namespace std;


#ifndef TTT_H
#define TTT_H

//struct
struct Player
    {
        string name;
        char symbol;
        int currentScore, tie;

        Player()
        {
            name = "Player";
            symbol = 'Z';
            currentScore = 0;
            tie = 0;
        }

    };

class TicTacToe
{
private:

    Player user[2];
    int currentWin;
    char board[3][3];
    char plays[9];
public:
    TicTacToe()
    {
        //player1
        user[0].name = "Player 1";
        user[0].symbol = 'X';

        //player2
        user[1].name = "Player 2";
        user[1].symbol = 'O';

        currentWin = 4;

        int temp = 49;

        for(int i=0;i<3;i++)
        {
            for(int j=0;j<3;j++)
            {
                board[i][j] = temp;
                plays[i*3+j] = board[i][j];
                temp++;
            }
        }
    }


    //setter
    void Reset(bool,char, char);
    void SetSymbol(char);
    void SetPlayerName();
    void SetPlayerSymbol();

    //getter
    void ChoosePosition();
    int CheckForWinner();
;


    //display
    void DisplayBoard();
    void DisplayScore(int);

};

#endif

Then:

//ttt.cpp
// 
//June 8, 2015

#include"ttt.h"
#include<iostream>
#include<string>
#include<iomanip>
#include<cctype>
using namespace std;

//Globals
// identifies current player
int z =1;
// tracks turns
int turn = 0;

// resets board
void Reset (bool resetWin, char board, char plays)//We cannot use a preexisting data type with a struct or class, therefore we call the function as it states in the header file we made.
    {
        //reset globals to ensure proper play order
        z=1;
        turn = 0;

        // reset board to 1-9
        for(int i=0;i<3;i++)
        {
            for(int j=0;j<3;j++)
            {
                board[i][j] = plays[i*3+j];

            }
        }
        // clears scores when Start new match choosen
        if(resetWin)
        {
            for(int i=0; i<2; i++)
            {
                user[i].currentScore = 0;
                user[i].tie = 0;
                user[0].name = "Player 1";
                user[1].name = "Player 2";

            }
        }
    }

void TicTacToe::SetPlayerName()
    {
        cin.ignore();
        for(int i=0; i<2;i++)
        {
        cout<<"Enter name for "<<user[i].name<<" : ";
        getline(cin, user[i].name);
        cout<<endl;
        }
    }

void TicTacToe::SetPlayerSymbol()
    {
        for(int i=0; i<2;i++)
        {
        cout<<"Enter Symbol for "<<user[i].name<<" : ";
        cin>>user[i].symbol;
        user[i].symbol = (toupper(user[i].symbol));
        cout<<endl;

        while(!isalpha(user[i].symbol))
            {
            cout<<"Letters only.\nTry again : ";
            cin>>user[i].symbol;
            user[i].symbol = (toupper(user[i].symbol));
            cout<<endl;
            }
        }
    }

void TicTacToe::DisplayScore(int found)
    {

        if(found == 1 || found == 2)
        {
            user[found-1].currentScore++;
            cout<<user[found-1].name<<" Wins!\n\n";
        }
        else if(found == 3)
        {
            user[0].tie++;
            cout<<"\nIt's a Tie\n\n";
        }


        cout<<user[0].name<<" Wins\t"<<user[1].name<<" Wins\t Tie Games\n";
        cout<<"--------------------------------------------\n";
        cout<<setw(7)<<user[0].currentScore<<setw(17)<<user[1].currentScore<<setw(14)<<user[0].tie<<endl;
        cout<<endl<<endl;
    }

void TicTacToe::ChoosePosition()
{
    // to hold player selected move
    int move = 0;
    // used for move validation
    bool play = true;

        //manipulates z to change player
        if(z == 1)
        {
            z--;
        }
        else if(z == 0)
        {
            z++;
        }

        //repeates player turn till valid move
        do
        {
            // gets player move
            cout<<user[z].name<<" it's your turn."<<endl;
            cout<<"Select your move: ";
            cin>>move;
            cout<<endl;
            play = true;

        for(int i=0;i<3;i++)
            {int p = 0;
                for(int j=0;j<3;j++)
                {
                    int o = 0;

                    int equal = i*3+j+1;

                    if(move == equal && board[i][j] == (char)equal)
                    {   
                        board[i][j] = user[z].symbol;
                        //break;
                    }
                    else if(cin.fail() || move<1 || move>9)
                    {
                        if(p == 2 && o == 2)
                        {
                        cin.clear();
                        cin.ignore();
                        cout<<"Invalid selection.\n\n";
                        play= false;
                        //break;
                        }
                    }
                    else
                    {
                        if(p == 2 && o == 2)
                        {
                        cout<<"Position " <<move<<" already taken.\n\n";
                        play = false;
                        //break;
                        }
                    }

                }p++;
            }

                    //check for move and if space is already taken
                    if((move == 1) && (board[0][0] != user[0].symbol && board[0][0] != user[1].symbol))
                    {
                    board[0][0] = user[z].symbol;
                    }
                    else if((move == 2) && (board[0][1] != user[0].symbol && board[0][1] != user[1].symbol))                
                    {
                    board[0][1] = user[z].symbol;
                    }
                    else if((move == 3) && (board[0][2] != user[0].symbol && board[0][2] != user[1].symbol))
                    {
                    board[0][2] = user[z].symbol;
                    }           
                    else if((move == 4) && (board[1][0] != user[0].symbol && board[1][0] != user[1].symbol))    
                    {
                    board[1][0] = user[z].symbol;
                    }           
                    else if((move == 5) && (board[1][1] != user[0].symbol && board[1][1] != user[1].symbol))            
                    {
                    board[1][1] = user[z].symbol;
                    }
                    else if((move == 6) && (board[1][2] != user[0].symbol && board[1][2] != user[1].symbol))            
                    {
                    board[1][2] = user[z].symbol;
                    }
                    else if((move == 7) && (board[2][0] != user[0].symbol && board[2][0] != user[1].symbol))                
                    {
                    board[2][0] = user[z].symbol;
                    }
                    else if((move == 8) && (board[2][1] != user[0].symbol && board[2][1] != user[1].symbol))                
                    {
                    board[2][1] = user[z].symbol;
                    }
                    else if((move == 9) && (board[2][2] != user[0].symbol && board[2][2] != user[1].symbol))                
                    {
                    board[2][2] = user[z].symbol;
                    }
                    //Invalidates anything but 1-9
                    else if(cin.fail() || move<1 || move>9)
                    {
                        cin.clear();
                        cin.ignore();
                        cout<<"Invalid selection.\n\n";
                        play= false;
                    }
                    //if position already taken
                    else
                    {
                    cout<<"Position " <<move<<" already taken.\n\n";
                    play = false;
                    }
        }while(!play);

    }     

int TicTacToe::CheckForWinner()
    {
    int winner = 0;
    turn++;
    cout<<turn<<endl;

    if(board[0][0] == board[1][1] && board[0][0] == board[2][2])
        {
            if(board[0][0] == user[z].symbol)
            {
            winner = z+1;
            }

            else if(board[0][0] == user[z].symbol)
            {
            winner = z+1;
            }
        }

    else if(board[2][0] == board[1][1] && board[2][0] == board[0][2])
        {
            if(board[2][0] == user[z].symbol)
            {
            winner = z+1;
            }

            else if(board[2][0] == user[z].symbol)
            {
            winner = z+1;
            }
        }

    else if(board[0][0] == board[0][1] && board[0][0] == board[0][2])
        {
            if(board[0][0] == user[z].symbol)
            {
            winner = z+1;
            }

            else if(board[0][0] == user[z].symbol)
            {
            winner = z+1;
            }
        }

    else if(board[1][0] == board[1][1] && board[1][0] == board[1][2])
        {   
            if(board[1][0] == user[z].symbol)
            {
            winner = z+1;
            }

            else if(board[1][0] == user[z].symbol)
            {
            winner = z+1;
            }
        }

    else if(board[2][0] == board[2][1] && board[2][0] == board[2][2])
        {
            if(board[2][0] == user[z].symbol)
            {
            winner = z+1;
            }

            else if(board[2][0] == user[z].symbol)
            {
            winner = z+1;
            }
        }

    else if(board[0][0] == board[1][0] && board[0][0] == board[2][0])
        {
            if(board[1][0] == user[z].symbol)
            {
            winner = z+1;
            }

            else if(board[1][0] == user[z].symbol)
            {
            winner = z+1;
            }
        }

    else if(board[0][1] == board[1][1] && board[0][1] == board[2][1])
        {
            if(board[0][1] == user[z].symbol)
            {
            winner = z+1;
            }

            else if(board[0][1] == user[z].symbol)
            {
            winner = z+1;
            }
        }

    else if(board[0][2] == board[1][2] && board[0][2] == board[2][2])
        {
            if(board[0][2] == user[z].symbol)
            {
            winner = z+1;
            }

            else if(board[0][2] == user[z].symbol)
            {
            winner = z+1;
            }
        }
        else if(turn == 9)  
        {
            winner = 3;
        }
        else
        {
            winner = 0;
        }
    return winner;
    }



void TicTacToe::DisplayBoard()
    {
        for(int i=0;i<3;i++)
        {
            cout<<"\t";
            for(int j=0;j<3;j++)
            {
                if(j == 2)
                cout<<board[i][j];
                else
                cout<<board[i][j]<<" | ";
            }
                if(i<2)
                {
                    cout<<"\n\t---------\n";
                }
        }
        cout<<endl<<endl;
    }

...and the driver:

 //ttt.h
// 
// June 9 2015

#include"ttt.cpp"
#include<iostream>
#include<string>
#include<iomanip>
#include<cctype>
using namespace std;


// prototypes

// type: int
// paramaters: none
// purpose: Display Main Menu
int MainMenu();


int main ()
{
    // to hold selection from main menu
    int menuSelect = 0;
    int findWin = 0;

    TicTacToe ttt1;

    cout<<"\n\t\t  Welcome to TicTacToe 2.0\n";
    cout<<"\t\t   (Now with more class)\n";
    do
    {
        bool resetWin = false;

        menuSelect = MainMenu();

        if(menuSelect == 3)
        {
            break;
        }
        else if(menuSelect == 1)
        {
            resetWin = true;
        }

        ttt1.Reset(resetWin);

        if(menuSelect == 1)
        {
            ttt1.SetPlayerName();

            ttt1.SetPlayerSymbol();
        }

    for(int i = 0; i<=9;i++)
        {   
        ttt1.DisplayBoard();

        ttt1.ChoosePosition();

        findWin = ttt1.CheckForWinner();

        if(findWin == 1 || findWin == 2 || findWin == 3)
            {
            break;
            }
        }

        ttt1.DisplayBoard();

        ttt1.DisplayScore(findWin);

    }while( menuSelect != 3);

return 0;
}





int MainMenu()
{
    int select = 0;

    cout<<"Main Menu\n";
    cout<<"\n";
    cout<<" 1) Start a new match (two new players).\n";
    cout<<" 2) Continue the current match (the same two players).\n";
    cout<<" 3) Exit.\n\n";

    cout<<"Enter your choice:";
    cin>>select;
    cout<<endl;

    while(cin.fail() || select <1 || select >3)
    {
        cin.clear();
        cin.ignore();
        cout<<"Please enter a valid choice : ";
        cin>>select;
    }

    return select;
}

The number of errors on the implementation file are numerous. I need some direction in getting started on fixing it.
Thanks Daniweb community

Not exactly what I was looking for, but I appreciate it and I will try that one also. I'm looking for simplistic display in the terminal. I'm not setting window sizes and attaching real graphics yet.

Hi,

i con't understand your code. if you tell me about the code detail i will be help.

One cause for errors I see is:

void Reset (bool resetWin, char board, char plays)

This is declared ouside the class and instead of passing character arrays you're passing characters and trying to access them like arrays.

Once you declare the function inside the class you don't need to pass board and play as you'll now be inside their scope and can access them directly:

void TicTacToe::Reset (bool resetWin)

Remember to change the declaration in the header file.

Edited 1 Year Ago by tinstaafl

This article has been dead for over six months. Start a new discussion instead.