Why My Tic Tac Toe is not working.

#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

/*void display_board1 ( )
{
for ( int i = 0; i < raws; i++ )
{
cout << " -----------" << endl;
cout << " | ";

for ( int j = 0; j < columns; j++ )
{
board [ i ] [ j ] = blank;

cout << blank << " | ";
}

cout << endl;
}
cout << " ------------" << endl;
}*/

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;
}
closed account (D9T7Djzh)
What are the error messages you are getting?
Topic archived. No new replies allowed.