Reputation: 3
I made tictactoe! ...almost.
The code:
int main()
{
//initialize
char board[3][3] =
{ '-','-','-',
'-','-','-',
'-','-','-'};
bool gameOver = false;
std::string playerTurn = "player 1";
char chX, chY; //choice x and y
int x = 1;
//game loop
do
{
std::cout << "\n\n\n\n\n\n\n\n\n\n";
std::cout << "Input X, then Y.\np1 = X, p2 = O.";
std::cout << "\n\nTurn: " << playerTurn;
std::cout << "\n\n\n\n\n\n\n\n\n\n";
std::cout << "+---+---+---+" << std::endl;
std::cout << "| " << board[0][0] << " | " << board[0][1] << " | " << board[0][2] << " | " << std::endl;
std::cout << "+---+---+---+" << std::endl;
std::cout << "| " << board[1][0] << " | " << board[1][1] << " | " << board[1][2] << " | " << std::endl;
std::cout << "+---+---+---+" << std::endl;
std::cout << "| " << board[2][0] << " | " << board[2][1] << " | " << board[2][2] << " | " << std::endl;
std::cout << "+---+---+---+" << std::endl;
//get input and change board's value
std::cin >> chX;
std::cin >> chY;
if (playerTurn == "player 1")
board[chX][chY] = 'X';
else if (playerTurn == "player 2")
board[chX][chY] = 'O';
//change turns
if (playerTurn == "player 1")
playerTurn = "player 2";
else if (playerTurn == "player 2")
playerTurn = "player 1";
} while (gameOver == false);
return 0;
}
my problem:
//get input and change board's value
std::cin >> chX;
std::cin >> chY;
if (playerTurn == "player 1")
board[chX][chY] = 'X';
else if (playerTurn == "player 2")
board[chX][chY] = 'O';
This piece is for changing the X and Y coordinates to an X / O depending on who's turn it is. However this doesn't change the board at all, and doesn't return an error.
Also: board[0][0] = 't';
will successfully change the value, and print t
in the [0][0]
spot.
Is there something I'm missing? Maybe the problem is elsewhere in the code? (I apologize in advance if the problem is way too simple -- maybe I have had too much coffee.)
Upvotes: 0
Views: 117
Reputation: 4951
char chX,chY; // valid values -127..128
std::cin >> chX; //gets ascii code of '0', '1' or whichever value you enter
std::cin >> chY;
The problem is cin will return the ascii code of characters you enter; '0'=48; To get the integer value simply subtract the '0':
std::cin >> chX;
std::cin >> chY;
chX -= '0';
chy -= '0';
Upvotes: 0
Reputation: 448
Right change chY and chX from chars to ints cause thats how arrays are indexed.
Then use this for your input code
std::cout << "Enter X corrdinate: ";
std::cin >> chX;
std::cout << "\n";
std::cin.clear();
std::cout << "Enter Y corrdinate: ";
std::cin >> chY;
std::cin.clear();
if (playerTurn == "player 1")
board[chX][chY] = 'X';
else if (playerTurn == "player 2")
board[chX][chY] = 'O';
Id also create a second array board of type int so you can examine whether a player has won easier
Upvotes: 0
Reputation: 30765
It's just a small mistake - you defined chX and chY as char, and if the user enters e.g. '1', it's the ASCII code for '1', not the integer value 1.
Just change your declaration to
int chX, chY;
and it should work.
Upvotes: 3