Click here to Skip to main content
15,889,034 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I have a small problem but could not find that out.

What I have tried:

#include <iostream>
#include <string>
using namespace std;

const int raws = 3;						// this many raws in the board
const int columns = 3;				// this many columns in the board
string blank = "T";					// set 0 to the whole boards spot
string board [ raws ] [ columns ];		// A 2D array named board contains some number of raws and columns

string Player1Symbol;					// First player in the game
string Player2Symbol;					// Second player in the game

string player1name;
string player2name;

string runningPlayerSpot;				// who is currently running the board

void display_board ( );
void reset_board ( );
string who_first ( );
void TTT_game_is_on ( );
int getXcoordinate ( );
int getYcoordinate ( );
bool check_spot ( int x, int y, string );
bool who_is_winner ( string  );

int main ( )
{
	char input;

	while ( true )
	{
		reset_board ( );

		TTT_game_is_on ( );

		cout << "Play again?  ( y/n ): ";
		cin >> input;

		if ( input == 'N' || input == 'n' )
		{
			exit ( 1 );
		}
		else if ( input == 'Y' || input == 'y' )
		{
			TTT_game_is_on ( );
		}
	}

}

string who_first ( )
{
    string first;
    cout << "Decide who goes first then enter the name as you used before: ";
    cin >> first;

    if ( first == player1name )
    {
        return player1name;
    }

    else if ( first == player2name )
    {
        return player2name;
    }

}

void TTT_game_is_on ( )
{
    cout << "Enter player1's name: ";
    cin >> player1name;
    cout << "Enter player2's name: ";
    cin >> player2name;

    cout << player1name << " enter an ALPHABETIC character you would like to use on the game board: ";
    cin >> Player1Symbol;
    cout << player2name << " enter an ALPHABETIC character you would like to use on the game board: ";
    cin >> Player2Symbol;

    // Assuming valid char, the characters are not needed to check

    string runningPlayerSpot;

    if ( player1name == who_first ( ) )
    {
        runningPlayerSpot = Player1Symbol;
    }

    else if ( player2name == who_first ( ) )
    {
        runningPlayerSpot = Player2Symbol;
    }

    int x;					// holds returned X coordinate number
    int y;					// holds returned Y coordinate number

    int spot_taken = 0;

    bool isRunning = true;

    while ( isRunning == true )
    {
        display_board ( );

        x = getXcoordinate ( );
        y = getYcoordinate ( );

       if ( check_spot ( x, y, runningPlayerSpot ) == false )
       {
           cout << x << ", " << y << "That spot has been taken." << endl;
       }

       else
       {
           spot_taken = spot_taken + 1;

           if ( who_is_winner ( runningPlayerSpot ) == true )
           {
               if ( runningPlayerSpot == Player1Symbol )
               {
                   cout << "congratulations " << player1name << endl;
                   cout << " You won the game." << endl;

                   isRunning = false;
               }

               else if ( runningPlayerSpot == Player2Symbol )
               {
                   cout << "Congratulations " << player2name << endl;
                   cout << " You won the game." << endl;

                   isRunning = false;
               }
           }

           else if ( spot_taken == 9 )
           {
               cout << "Tie Game." << endl;
               cout << "Thanks you both, " << player1name << " and " << player1name;
               cout << " for a good game." << endl;

               isRunning = false;
           }

           if ( runningPlayerSpot == Player1Symbol )
           {
               cout << player2name << " your turn -> " << endl;

               runningPlayerSpot = Player2Symbol;
           }
           else
           {
               cout << player1name << " your turn -> " << endl;

               runningPlayerSpot = Player1Symbol;
           }
       }
    }
}

bool who_is_winnner ( string runningPlayerSpot )
{
    for ( int i = 0; i < raws; i++ )
    {
        if ( ( board [ i ] [ 0 ] == runningPlayerSpot ) && ( board [ i ] [ 0 ] == board [ i ] [ 1 ] ) &&
             ( board [ i ] [ 1 ] == board [ i ] [ 2 ] )
            )
        {
            return true;
        }
    }

    for ( int i = 0; i < columns; i++ )
    {
        if ( ( board [ 0 ] [ i ] == runningPlayerSpot ) && ( board [ 0 ] [ i ] == board [ 1 ] [ i ] ) &&
             ( board [ 1 ] [ i ] == board [ 2 ] [ i ] )
            )
        {
            return true;
        }
    }

    if ( ( board [ 0 ] [ 0 ] == runningPlayerSpot ) && ( board [ 0 ] [ 0 ] == board [ 1 ] [ 1 ] ) &&
             ( board [ 1 ] [ 1 ] == board [ 2 ] [ 2 ] )
        )
    {
        return true;
    }

    if ( ( board [ 0 ] [ 2 ] == runningPlayerSpot ) && ( board [ 0 ] [ 2 ] == board [ 1 ] [ 1 ] ) &&
             ( board [ 1 ] [ 1 ] == board [ 2 ] [ 0 ] )
            )
    {
        return true;
    }

    return false;
}

bool check_free_spot ( int spotX, int spotY, string runningPlayerSpot )
{
	if ( board [ spotX ] [ spotY ] == blank )
	{
		board [ spotX ] [ spotY ] = runningPlayerSpot;

		return true;
	}

	else
	{
		return false;
		// meaning that -> board [ spotX ] [ spotY ] != blank;
	}

	return false;
}

void reset_board ( )
{
	for ( int i = 0; i < raws; i++ )
	{
		for ( int j = 0; j < columns; j++ )
		{
			board [ i ] [ j ] = blank;			// setting every spot to the 'T'
		}
	}
}

void display_board ( )
{
	cout << "------------------" << endl;
	cout << "R/C | 1  | 2 | 3 | " << endl;
	cout << "------------------" << endl;

		for ( int i = 0; i < 3; i++ )
		{
			cout << i+1 << "	| " << board [ i ] [ 0 ] << " | " << board [ i ] [ 1 ]  << " | " << board [ i ] [ 2 ] << " |"<< endl;
		}
		cout << "------------------" << endl;
}

int getXcoordinate ( )
{
	int x;

	while ( true )
	{
		cout << "Enter a number for X coordinate: ";
		cin >> x;

		if ( x >= 1 && x <= 3 )
		{
			break;
		}
		cout << x << " is out of X coordinate range." << endl;
		cout << "Please, ";
	}

	return x - 1;
}

int getYcoordinate ( )
{
	int y;

	while ( true )
	{
		cout << "Enter a number for Y coordinate: ";
		cin >> y;

		if ( y >= 1 && y <= 3 )
		{
			break;
		}
		cout << y << " is out of Y coordinate range." << endl;
		cout << "Please, ";
	}

	return y - 1;
}
Posted
Updated 22-Apr-17 22:24pm
Comments
[no name] 21-Apr-17 15:20pm    
5 months is plenty long enough to have learned how to ask a question.
Patrice T 21-Apr-17 15:23pm    
Explain problem !
Hamidur R. 21-Apr-17 15:24pm    
when i run this code, i get this

15:05:25 **** Incremental Build of configuration Debug for project StringTTT ****
make all
Building file: ../TTTstring.cpp
Invoking: GCC C++ Compiler
g++ -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"TTTstring.d" -MT"TTTstring.o" -o "TTTstring.o" "../TTTstring.cpp"
../TTTstring.cpp:87:1: warning: control may reach end of non-void function [-Wreturn-type]
}
^
1 warning generated.
Finished building: ../TTTstring.cpp

Building target: StringTTT
Invoking: MacOS X C++ Linker
g++ -o "StringTTT" ./TTTstring.o
Undefined symbols for architecture x86_64:
"check_spot(int, int, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >)", referenced from:
TTT_game_is_on() in TTTstring.o
"who_is_winner(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >)", referenced from:
TTT_game_is_on() in TTTstring.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [StringTTT] Error 1

15:05:25 Build Finished (took 429ms)
Patrice T 21-Apr-17 15:26pm    
Use Improve question to update your question.
So that everyone can pay attention to this information.
Richard MacCutchan 22-Apr-17 4:47am    
You need to learn the difference between build your program, and running it. The above error messages refer to compiler and linker errors in your source code. You need to correct those in order to build your program.

What system is this build running on?

1 solution

1.) Warning: "warning: control may reach end of non-void function [-Wreturn-type]"
First you should correct this warning. It comes most probably from the method "string who_first()" which can be return without a return value.
string who_first ( )
{
    string first;
    cout << "Decide who goes first then enter the name as you used before: ";
    cin >> first;
 
    if ( first == player1name )
    {
        return player1name;
    }
 
    else if ( first == player2name )
    {
        return player2name;
    }

    // Warning: If we come to this point the function does not return anything *1)
}
How to correct? In case the logic is, that you never should come to "*1)" then you should throw an Exception there.

2.) Linker Error
: "Undefined symbols for architecture x86_64: check_spot"
For the implementation you used the name check_free_spot vs. check_spot you used in the Forward declaration. So it would be easy to correct this, yes?

I hope it helps.
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900