I am making a Tic Tac Toe game and i created a function that inserts X or O into my array. I have run into one problem with my design. I call the function to make a move for X, but when it is the next players turn how do i make it call for O?
Is there a way after i put makeMove() i can just call somehow it to take in O turn instead of X. Because as you can see if i do X it will just always ask for X and not O. How can i make it choose to pull in X or O turn.
The problem is i need to only have one function that makes moves.
int main()
{
while(SOME CONDITION HERE)
{
printBoard();
cout << "Player X please choose a position: ";
makeMove('X');
cout << "Player O please choose a position: ";
makeMove('O');
}
}
int makeMove(char marker)
{
int choosePosition = 0;
cin >> choosePosition;
ticTacBoard[choosePosition - 1] = marker;
}
Start with this:
int main()
{
while(SOME CONDITION HERE)
{
printBoard();
cout << "Player X please choose a position: ";
makeMove('X');
cout << "Player O please choose a position: ";
makeMove('O');
}
}
int makeMove(char marker)
{
int choosePosition = 0;
cin >> choosePosition;
ticTacBoard[choosePosition - 1] = marker;
}
Note that you're going to want to change the SOME CONDITION HERE part, but you could quickly replace it by 1 and get the same behavior of your current script (actually, a bit better).
But you'll eventually want to put something there that makes sense -- something that will tell the program to stop prompting the players for positions and, say, declare a winner.
The following is just a more streamlined way of doing the same thing:
int main()
{
while(SOME CONDITION HERE)
{
printBoard();
makeMove('X');
makeMove('O');
}
}
int makeMove(char marker)
{
cout << "Player " << marker << " please choose a position: ";
int choosePosition = 0;
cin >> choosePosition;
ticTacBoard[choosePosition - 1] = marker;
return 0;
}
Note the added return 0 -- if you don't want to return something, you should just make makeMove return void so as not to be confusing.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With