Hello guys,
I'm writing a simple tic-tac-toe game just to get some practice with function (for those that read my previous post), and I have encounterd a problem which I've been trying to solve for sometime.
The problem is that if an invalid position has been entered and the lop goes back to the prompt again and a new CORRECT position has been entered it simply says it is wrong again.
Below I'm posting the two functions that handle that process.If anyone could help I would greaty appreciate it!
and by the way I don't know how to use arrays therefore it is all functions.
axon
Code://function switching the coordinates to the global vars
int switchCoordinatesToPosition(char letterPosition, int numberPosition, int&badMove)
{
if( (letterPosition == 'A') && (numberPosition == 1) )
{
if(p1 != ' ') {badMove = -1;}
else{ return 1; }
}
else if( (letterPosition == 'A') && (numberPosition == 2) )
{
if(p2 != ' ') {badMove = -1;}
else{ return 2; }
}
else if( (letterPosition == 'A') && (numberPosition == 3) )
{
if(p3 != ' ') {badMove = -1;}
else{ return 3; }
}
else if( (letterPosition == 'B') && (numberPosition == 1) )
{
if(p4 != ' ') {badMove = -1;}
else{ return 4; }
}
else if( (letterPosition == 'B') && (numberPosition == 2) )
{
if(p5 != ' ') { badMove = -1;}
else{ return 5; }
}
else if( (letterPosition == 'B') && (numberPosition == 3) )
{
if(p6 != ' ') { badMove = -1;}
else{ return 6; }
}
else if( (letterPosition == 'C') && (numberPosition == 1) )
{
if(p7 != ' ') {badMove = -1;}
else{ return 7; }
}
else if( (letterPosition == 'C') && (numberPosition == 2) )
{
if(p8 != ' ') { badMove = -1;}
else{ return 8; }
}
else if( (letterPosition == 'C') && (numberPosition == 3) )
{
if(p9 != ' ') { badMove = -1;}
else{ return 9; }
}
else { cout << "\nInvalid command...exiting!"; exit(-1);} //default escape
}
//=================================================================================
//function displaying the 'to move' prompt
void displayMovePrompt(char& playerValue, char& letterPosition, int& numberPosition,
int& positionNumberOnBoard, int badMove)
{
do
{
switch(playerValue){
case 'X': cout << "\n\nPlayer X type in a move position: ";
break;
case 'O': cout << "\n\nPlayer O type in a move position: ";
break;
}
cin >> letterPosition >> numberPosition;
letterPosition = toupper(letterPosition); //convert to CAPS
//switch to position number
positionNumberOnBoard = switchCoordinatesToPosition(letterPosition, numberPosition, badMove);
//check for bad imput
if(badMove == -1)
{
cout << "That is an invalid move!";
badMove = -1;
}
}
while(badMove == -1);
}