Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- const int fieldSize{ 3 };
- char checkWin(char arr[fieldSize][fieldSize])
- {
- char same{ arr[0][0] }; // compare symbol for horizontal and vertical
- char sameD{ arr[0][0] }; // compare symbol for main and reverse diagonal
- int cnt{ 0 }; // how much symbols was the same for horizontal and vertical
- int cntD{ 0 }; // how much symbols was the same for main and reverse diagonal
- int x{ 0 };
- int y{ 0 };
- // check if three symbols are the same for vertical and main diagonal
- for (x = 0; x < fieldSize; ++x)
- {
- for (y = 0, cnt = 0, same = arr[y][x]; y < fieldSize; ++y)
- {
- (arr[y][x] != same) ? cnt = 0 : ++cnt;
- if (x == y)
- {
- (arr[y][x] != sameD) ? cntD = 0 : ++cntD;
- }
- }
- if (cnt == fieldSize and same != ' ') { return same; };
- }
- if (cntD == fieldSize and sameD != ' ') { return sameD; };
- // check if three symbols are the same for horizontal and reverse diagonal
- sameD = arr[0][fieldSize-1];
- cntD = 0;
- for (y = 0; y < fieldSize; ++y)
- {
- for (x = 0, cnt = 0, same = arr[y][x]; x < fieldSize; ++x)
- {
- (arr[y][x] != same) ? cnt = 0 : ++cnt;
- if (x + y == fieldSize-1)
- {
- (arr[y][x] != sameD) ? cntD = 0 : ++cntD;
- }
- }
- if (cnt == fieldSize and same != ' ') { return same; };
- }
- if (cntD == fieldSize and sameD != ' ') { return sameD; };
- return 'z';
- }
- void printBoard(char arr[fieldSize][fieldSize])
- {
- char symbol{ ' ' };
- int numY{ 0 };
- int numX{ 0 };
- int elY{ 0 };
- int elX{ 0 };
- for (int y{ 0 }; y <= (fieldSize + fieldSize - 1); ++y)
- {
- for (int x{ 0 }; x <= (fieldSize * 3 + fieldSize - 1); ++x)
- {
- symbol = ' ';
- if (y == 0 and (x % 2 == 0) and (x % 4)) { symbol = '1' + numX++; };
- if (x == 0 and y % 2) { symbol = '1' + numY++; };
- if (x and y)
- {
- if (x % 4 == 0) { symbol = '|'; };
- if (y % 2 == 0) { symbol = ((x % 4 == 0) ? '+' : '-'); };
- if (x % 2 == 0 and (x % 4 and y % 2)) { symbol = arr[elY][elX++]; };
- }
- std::cout << symbol;
- }
- std::cout << '\n';
- elX = 0;
- if (y and y % 2 == 0) { ++elY; };
- }
- }
- void makeMove(char arr[fieldSize][fieldSize], char who)
- {
- int y{ 0 };
- int x{ 0 };
- while (true)
- {
- std::cout << "Put " << who << " to X Y\n";
- std::cin >> x >> y;
- x--;
- y--;
- if ((x < 0 and x > fieldSize-1) or (y < 0 or y > fieldSize-1))
- {
- std::cout << "Out of game field! Try again!\n";
- continue;
- }
- if (arr[y][x] == ' ') { arr[y][x] = who; break; }
- else { std::cout << "Occuped! Choose another place!\n"; };
- }
- }
- void initBoard(char arr[fieldSize][fieldSize])
- {
- for (int y{ 0 }; y < fieldSize; ++y)
- {
- for (int x{ 0 }; x < fieldSize; ++x)
- {
- arr[y][x] = ' ';
- }
- }
- }
- int main()
- {
- char field[fieldSize][fieldSize];
- char winner{'z'};
- int moves{ 0 };
- char who{ 'o' };
- initBoard(field);
- printBoard(field);
- for (int moves{0}; winner == 'z' and moves < fieldSize * fieldSize; ++moves)
- {
- who = (who == 'o') ? 'x' : 'o';
- makeMove(field, who);
- printBoard(field);
- winner = checkWin(field);
- }
- if (winner == 'z')
- {
- std::cout << "Nobody wins! HaHa!\n";
- }
- else
- {
- std::cout << "The winner is " << winner << " congratulation!!!\n";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement