Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdbool.h>
- #include <assert.h>
- #define countof(a) (sizeof(a) / sizeof((a)[0]))
- static char field[3][3];
- static_assert(countof(field) == countof(field[0]), \
- "Game field must be square");
- static bool check_combos(size_t x, size_t y, char mark)
- {
- const size_t size = countof(field[0]);
- // Всегда проверяем текущие строку и столбец.
- bool horz = true;
- bool vert = true;
- // Проверяем диагонали, только если попали на диагональ.
- bool maindiag = (x == y);
- bool antidiag = (size - x - 1 == y);
- for (size_t i = 0; i < size; i++) {
- // Прячем некрасивые if.
- horz = horz && (field[y][i] == mark);
- vert = vert && (field[i][x] == mark);
- maindiag = maindiag && (field[i][i] == mark);
- antidiag = antidiag && (field[i][size - i - 1] == mark);
- }
- if (horz) {
- printf("Player %c completes line %zu!\n", mark, y + 1);
- }
- if (vert) {
- printf("Player %c completes column %zu!\n", mark, x + 1);
- }
- if (maindiag) {
- printf("Player %c completes main diagonal!\n", mark);
- }
- if (antidiag) {
- printf("Player %c completes second diagonal!\n", mark);
- }
- return horz || vert || maindiag || antidiag;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement