Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- void print(int board[][3]);
- void choice(int board[][3], char name[99]);
- void check(int board[][3], bool &isEnd);
- char name1[99];
- char name2[99];
- int counter = 0;
- int main()
- {
- cout << "GRA W KOLKO I KRZYZYK" << endl << endl;
- int board[3][3] = { 0 };
- int queue = 0;
- bool isEnd = false;
- cout << "Imie gracza 1: " << endl;
- cin.getline(name1, 99);
- cout << "Imie gracza 2: " << endl;
- cin.getline(name2, 99);
- cout << name1 << " " << name2 << endl;
- while (!isEnd)
- {
- if (queue == 0)
- {
- choice(board, name1);
- print(board);
- check(board, isEnd);
- if (isEnd == true)
- {
- if (counter == 9)
- {
- cout << "REMIS" << endl;
- }
- else
- {
- cout << "Wygral gracz: " << name1 << endl;
- }
- }
- else
- queue++;
- }
- else if (queue == 1)
- {
- choice(board, name2);
- print(board);
- check(board, isEnd);
- if (isEnd == true)
- {
- if (counter == 9)
- {
- cout << "REMIS" << endl;
- }
- else
- {
- cout << "Wygral gracz: " << name2 << endl;
- }
- }
- else
- queue--;
- }
- }
- system("pause");
- }
- void print(int board[][3])
- {
- cout << " 0 1 2" << endl << endl;
- for (int i = 0; i < 3; i++)
- {
- cout << i << " ";
- for (int j = 0; j < 3; j++)
- {
- cout << "|";
- if (board[i][j] == 0) cout << "_";
- else if (board[i][j] == 1) cout << "0";
- else if (board[i][j] == 2) cout << "X";
- }
- cout << "|" << endl;
- }
- cout << counter << endl;
- }
- void choice(int board[][3], char name[99])
- {
- int row;
- int column;
- while (true)
- {
- cout << "Gracz " << name << " wybiera wiersz: ";
- cin >> row;
- cout << "Gracz " << name << " wybiera kolumne: ";
- cin >> column;
- if (row < 3 && column < 3)
- {
- break;
- }
- else
- {
- cout << "Wiersz lub kolumna rozna od 0, 1 lub 2, wybierz ponownie!" << endl;
- }
- }
- if (board[row][column] == 0)
- {
- if(name == name1)
- {
- board[row][column] = 1;
- }
- else if (name == name2)
- {
- board[row][column] = 2;
- }
- counter++;
- }
- else
- {
- cout << "Pole zajete, sprobuj jeszcze raz!" << endl;
- choice(board, name);
- }
- }
- void check(int board[][3], bool &isEnd)
- {
- if (counter == 9)
- {
- isEnd = true;
- return;
- }
- int count0 = 0;
- int countX = 0;
- for (int i = 0; i < 3; i++)
- {
- for (int j = 0; j < 3; j++)
- {
- if (board[i][j] == 1) count0++;
- else if (board[i][j] == 2) countX++;
- if (count0 == 3 || countX == 3)
- {
- isEnd = true;
- return;
- }
- }
- count0 = 0;
- countX = 0;
- }
- for (int i = 0; i < 3; i++)
- {
- for (int j = 0; j < 3; j++)
- {
- if (board[j][i] == 1) count0++;
- else if (board[j][i] == 2) countX++;
- if (count0 == 3 || countX == 3)
- {
- isEnd = true;
- return;
- }
- }
- count0 = 0;
- countX = 0;
- }
- if ((board[0][0] == 1 && board[1][1] == 1 && board[2][2] == 1) || (board[0][2] == 1 && board[1][1] == 1 && board[2][0] == 1))
- {
- isEnd = true;
- return;
- }
- else if ((board[0][0] == 2 && board[1][1] == 2 && board[2][2] == 2) || (board[0][2] == 2 && board[1][1] == 2 && board[2][0] == 2))
- {
- isEnd = true;
- return;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement