Witam,
Mam problem z zapisaniem funkcji odpowiadającej za wykrycie remisu w grze. Próbuję na różne sposoby, ale nadal nic.
W przypadku zapełnienia planszy, program nadal oczekuje wprowadzenia współrzędnych x oraz y. Wykrywa, że dane pole jest zajęte i ponownie oczekuje na wprowadzenie współrzędnych.
Mój kod:
```#include <iostream>
#include <ctime>
char map[3][3];
bool gameover = false;
// functions
void Graphics(char s);
void ClearMap();
int RandPlayer();
void GetCoordinates(char sign);
void CheckWin(char sign);
bool DrawGame();
//-------------------------------------------
int main()
{
srand(time(NULL));
int start;
char sign;
start = RandPlayer();
if (start == 1)
sign = 'X';
else
sign = 'O';
ClearMap();
while(gameover == false)
{
GetCoordinates(sign);
CheckWin(sign);
if (gameover == true)
{
Graphics(sign);
std::cout << "Player " << sign << " won";
}
else
{
bool d = DrawGame();
if (d == true)
std::cout << "Draw";
}
if (gameover == false)
{
if (sign == 'X')
sign = 'O';
else
sign = 'X';
}
}
std::cout << std::endl;
system("pause");
return 0;
}
//------------------------------------------
void Graphics(char s)
{
system("cls");
std::cout << "Description of the coordinates\n";
std::cout << "00|01|02\n10|11|12\n20|21|22\n\n";
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
std::cout << map[i][j] << "|";
if (j == 2)
std::cout << std::endl;
}
}
std::cout << "Player " << s << " your move\n";
}
void ClearMap()
{
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++)
map[i][j] = ' ';
}
int RandPlayer()
{
return rand() % 2 + 1;
}
void GetCoordinates(char sign)
{
int x, y;
bool check;
Graphics(sign);
do{
check = true;
do {
std::cout << "Enter the value x [from 0 to 2]: ";
std::cin >> x;
} while (x < 0 || x > 2);
do {
std::cout << "Enter the value y [from 0 to 2]: ";
std::cin >> y;
} while (y < 0 || y > 2);
if ((x >= 0 || x < 3) && (y >= 0 || y < 3) && (map[x][y] == ' '))
map[x][y] = sign;
else if ((x >= 0 || x < 3) && (y >= 0 || y < 3) && (map[x][y] != ' '))
check = false;
} while (check == false);
}
void CheckWin(char sign)
{
for (int i = 0; i < 3; i++)
{
if ((map[i][0] == sign) && (map[i][1] == sign) && (map[i][2] == sign)) // row
gameover = true;
else if ((map[0][i] == sign) && (map[1][i] == sign) && (map[2][i] == sign)) // column
gameover = true;
}
if ((map[0][0] == sign) && (map[1][1] == sign) && (map[2][2] == sign))
gameover = true;
if ((map[0][2] == sign) && (map[1][1] == sign) && (map[2][0] == sign))
gameover = true;
}
bool DrawGame()
{
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++)
if (map[i][j] == ' ')
return false;
return true;
}