Znalazłem w sieci taki prosty kod gry konsolowej "Kółko i krzyżyk", ale mam problem z jego całą analizą.
#include <iostream>
#include <stdlib.h>
#include <conio.h>
using namespace std;
char board[3][3];
bool isRunning = true;
void printBoard();
void userInput(char c);
bool checkWin(char c);
bool checkRow(char c, int r);
bool checkColumn(char c, int r);
int main()
{
for(int i = 0; i < 3; i++)
{
for(int j = 0; j < 3; j++)
{
board[i][j] = ' ';
}
}
while(isRunning)
{
printBoard();
userInput('x');
printBoard();
userInput('o');
printBoard();
if(checkWin('x'))
{
cout << "Wygral X";
isRunning = false;
}
if(checkWin('o'))
{
cout << "Wygralo O!";
isRunning = false;
}
}
return 0;
}
void printBoard()
{
system("cls");
for(int i = 0; i < 3; i++)
{
for(int j = 0; j < 3; j++)
{
cout << board[j][i] << " ";
}
cout << endl;
}
}
void userInput(char c)
{
int x = 0, y = 0;
cout << "[" << c << "]Podaj pozycje x: ";
cin >> x;
cout << "[" << c << "]Podaj pozycje y: ";
cin >> y;
x--;
y--;
if(board[x][y] == ' ')
board[x][y] = c;
else
userInput(c);
}
bool checkWin(char c)
{
for(int i = 0; i < 3; i++)
{
bool ret = checkRow(c , i);
if(ret)
return true;
}
for(int i = 0; i < 3; i++)
{
bool ret = checkColumn(c , i);
if(ret)
return true;
}
if(board[0][0] == c && board[1][1] == c && board[2][2] == c)
{
return true;
}
if(board[2][0] == c && board[1][1] == c && board[0][2] == c)
{
return true;
}
return false;
}
bool checkRow(char c, int r)
{
int ret = 0;
for(int i = 0; i < 3; i++)
{
if(board[i][r] == c)
ret++;
}
if(ret == 3)
return true;
return false;
}
bool checkColumn(char c, int r)
{
int ret = 0;
for(int i = 0; i < 3; i++)
{
if(board[r][i] == c)
ret++;
}
if(ret == 3)
return true;
return false;
}
Nie rozumiem tego fragmentu:
bool checkWin(char c)
{
for(int i = 0; i < 3; i++)
{
bool ret = checkRow(c , i);
if(ret)
return true;
}
for(int i = 0; i < 3; i++)
{
bool ret = checkColumn(c , i);
if(ret)
return true;
}
if(board[0][0] == c && board[1][1] == c && board[2][2] == c)
{
return true;
}
if(board[2][0] == c && board[1][1] == c && board[0][2] == c)
{
return true;
}
return false;
}
bool checkRow(char c, int r)
{
int ret = 0;
for(int i = 0; i < 3; i++)
{
if(board[i][r] == c)
ret++;
}
if(ret == 3)
return true;
return false;
}
bool checkColumn(char c, int r)
{
int ret = 0;
for(int i = 0; i < 3; i++)
{
if(board[r][i] == c)
ret++;
}
if(ret == 3)
return true;
return false;
}
Wiem, że te 3 funkcje służą do: sprawdza wygraną, sprawdza czy w kolumnie są trzy takie same znaki i czy w wierszu są trzy takie same znaki. Nie wiem co przechowuje zmienna
```cpp
bool ret
i co do funkcji podaje się w zmiennej int r
. Najbardziej nie rozumiem tego:
```cpp
bool ret = checkRow(c , i);
if(ret)
return true;
i
bool ret = checkColumn(c , i);
if(ret)
return true;