Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- char matrix[3][3] = { '1', '2', '3' , '4' , '5' , '6', '7' , '8' , '9' };
- char player = 'X';
- void print() {
- system("cls");
- for (int i = 0; i < 3; i++) {
- for ( int j = 0; j < 3; j++) {
- cout << matrix[i][j] << " | " ;
- }
- cout << endl <<"__________"<< endl;
- }
- }
- void play() {
- char pos;
- cout << "Enter you position player ( " << player << " ) ?" << endl;
- cin >> pos;
- for (int i = 0; i < 3; i++) {
- for (int j = 0; j < 3; j++) {
- if (matrix[i][j] == pos)
- matrix[i][j] = player;
- }
- }
- if (player == 'X')
- player = 'O';
- else
- player = 'X';
- }
- char whoWin() {
- int counter = 0;
- int Cx = 0, Co = 0;
- for (int i = 0; i < 3; i++) {
- for (int j = 0; j < 3; j++) {
- if (matrix[i][j] != 'X' && matrix[i][j] != 'O') counter++;
- if (matrix[i][j] == 'X') Cx++;
- else if (matrix[i][j] == 'O') Co++;
- }
- if (Cx == 3 || Co == 3) {
- return(Cx > Co) ? 'X' : 'O';
- }
- Cx = 0;
- Co = 0;
- }
- for (int i = 0; i < 3; i++) {
- for (int j = 0; j < 3; j++) {
- if (matrix[j][i] == 'X') Cx++;
- else if (matrix[j][i] == 'O') Co++;
- }
- if (Cx == 3 || Co == 3) {
- return(Cx > Co) ? 'X' : 'O';
- }
- Cx = 0;
- Co = 0;
- }
- if (matrix[0][0] == 'X' && matrix[1][1] == 'X' && matrix[2][2] == 'X')return 'X';
- else if (matrix[0][0] == 'O' && matrix[1][1] == 'O' && matrix[2][2] == 'O') return 'O';
- if (matrix[0][2] == 'X' && matrix[1][1] == 'X' && matrix[2][0] == 'X')return 'X';
- else if (matrix[0][2] == 'O' && matrix[1][1] == 'O' && matrix[2][0] == 'O') return 'O';
- // Z mean no place in the gray
- if (counter == 0) return 'Z';
- // game is still and no one win
- return '.';
- }
- int main() {
- while (whoWin() == '.') {
- print();
- play();
- }
- if (whoWin() == 'Z')
- cout << "Game is ended without winner *_*";
- else
- cout << "The winner is player (" << whoWin() << ") Congratulations ";
- system("pause");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement