Is there a way to store the x and y entered by the user into a location for the array.
I have this:
if (x == 1 && y == 1)
{
board[0][0] = playerMarker;
}
I want to make it so the x and y are stored into a variable that matches the name of a spot in an array.
But I want to try and make it more like this:
if (currentMove == '1' && squareOne == '1')
squareOne = playerMarker;
This is a code I have seen above. The code bellow is my own.
void playerMove(char* CurrentPlayer, char (&board)[3][3], char &playerMarker)
{
bool bValidMove;
int x,y;
// Prompt the player for a move
cout << "Player " << CurrentPlayer << "'s move:" << endl;
// Loop until we get a valid move
do
{
cout << "Please enter the row number for the place you wish to mark: " << endl;
cin >> x;
cout << "Please enter the column number for the place you wish to mark" << endl;
cin >> y;
bValidMove = true;
// Check for a valid move
if (x == 1 && y == 1)
{
board[0][0] = playerMarker;
}
else if (x == 1 && y == 2)
{
board[0][1] = playerMarker;
}
else if (x == 1 && y == 3)
{
board[0][2] = playerMarker;
}
else if (x == 2 && y == 1)
{
board[1][0] = playerMarker;
}
else if (x == 2 && y == 2)
{
board[1][1] = playerMarker;
}
else if (x == 2 && y == 3)
{
board[1][2] = playerMarker;
}
else if (x == 3 && y == 1)
{
board[2][0] = playerMarker;
}
else if (x == 3 && y == 2)
{
board[2][1] = playerMarker;
}
else if (x == 3 && y == 3)
{
board[2][2] = playerMarker;
}
else
{
cout << "Invalid Move. Try again." << endl;
bValidMove = false;
}
}
while (!bValidMove);
}
This is the code I am trying to make my own into.
// Ask current player for a move
cout << currentPlayer << "'s move: ";
bool validMove;
// Loop until a valid move is chosen
do
{
char currentMove;
cin >> currentMove;
validMove = true;
// Check for a valid move
if (currentMove == '1' && squareOne == '1')
squareOne = playerMarker;
else if (currentMove == '2' && squareTwo == '2')
squareTwo = playerMarker;
else if (currentMove == '3' && squareThree == '3')
squareThree = playerMarker;
else if (currentMove == '4' && squareFour == '4')
squareFour = playerMarker;
else if (currentMove == '5' && squareFive == '5')
squareFive = playerMarker;
else if (currentMove == '6' && squareSix == '6')
squareSix = playerMarker;
else if (currentMove == '7' && squareSeven == '7')
squareSeven = playerMarker;
else if (currentMove == '8' && squareEight == '8')
squareEight = playerMarker;
else if (currentMove == '9' && squareNine == '9')
squareNine = playerMarker;
else
{
cout << "Invalid Move. Try again: ";
validMove = false;
}
} while ( !validMove );