im trying to make a program that would print 1 or 2 in a grid of 0. this is not my full code so it might look confusing but here is my problem :
this program is supposed to print 1 or 2 (int this case 1) where the user wants to put it. but the output is always (no matter the input):
020000000
000000000
000000000
000000000
000000000
000000000
000000000
000000000
000000000
i dont know why it always prints 2 when it should print 1 or why its always in this position.
can anyone help me and explain why this is happening ?
Code:
#include <iostream>
int main()
{
enum Players { P1, P2 };
enum FieldFillers { EMPTY, X_FILLED, O_FILLED };
int gridSize = 8;
int playerOnTurn = P1;
int fields[10][10];
for (int i = 0; i <= gridSize; i++)
{
for (int j = 0; j <= gridSize; j++)
{
fields[i][j] = EMPTY;
}
}
int cordinates[2];
enum CordinatesDimentions { X_CORDINATE, Y_CORDINATE };
while (1)
{
std::cout << "\tEnter X cordinates (number only)\n";
std::cin >> cordinates[X_CORDINATE];
cordinates[X_CORDINATE]--;
std::cout << cordinates[X_CORDINATE];
std::cout << "\tEnter Y cordinates (number only)\n";
std::cin >> cordinates[Y_CORDINATE];
cordinates[Y_CORDINATE]--;
std::cout << cordinates[Y_CORDINATE];
if (cordinates[X_CORDINATE] <= gridSize &&
cordinates[Y_CORDINATE] <= gridSize &&
fields[X_CORDINATE][Y_CORDINATE] == EMPTY)
{
if (playerOnTurn = P1)
{
fields[X_CORDINATE][Y_CORDINATE] = X_FILLED;
break;
}
else
{
fields[X_CORDINATE][Y_CORDINATE] = O_FILLED;
break;
}
}
}
for (int x = 0; x <= gridSize; x++)
{
for (int y = 0; y <= gridSize; y++)
{
std::cout << fields[x][y];
}
std::cout << '\n';
}
}