#include #include #include void setRows(int rows) { _ROWS = rows; } void setCollums(int collums) { _COLLUMS = collums; } void setTokens(char t1, char t2) { _TOKEN1 = t1; _TOKEN2 = t2; } int getIndex(int r, int c) { return r * _COLLUMS + c; } int checkLine(tile_t *board, int pos, int delta) { int count = 1, i; i = pos - delta; while (isValid(i) && board[pos] == board[i]) { count++; i -= delta; } i = pos + delta; while (isValid(i) && board[pos] == board[i]) { count++; i += delta; } return count >= 4; } // checks board for a win // returns player tile_t on win, otherwise 0 int checkWin(tile_t *board, int pos) { int result = checkLine(board, pos, ORIZONTAL); if (result) { return board[pos]; } result = checkLine(board, pos, VERTICAL); if (result) { return board[pos]; } result = checkLine(board, pos, DIAGONAL); if (result) { return board[pos]; } result = checkLine(board, pos, DIAGONAL_INV); if (result) { return board[pos]; } return 0; } int checkWinAll(tile_t *board){ for (int i=0; i <_ROWS *_COLLUMS; i++) { if (checkWin(board, i) != EMPTY) { return checkWin(board, i); } } return EMPTY; } void printBoard(tile_t *board){ for (int i=0; i < _ROWS; i++) { for (int j=0; j < _COLLUMS; j++) { printTile(board[i * _COLLUMS + j]); if (j != _COLLUMS - 1) { printf(" | "); } else { printf("\n"); } } for(int j=0; i != _ROWS - 1 && j < _COLLUMS - 1; j++) { printf("----"); } if (i < _ROWS - 1) { printf("---\n"); } } } void printTile(tile_t t){ switch(t) { case EMPTY: printf(" "); break; case PLAYER1: printf("\033[94m%c\033[39m", _TOKEN1); break; case PLAYER2: printf("\033[91m%c\033[39m", _TOKEN2); break; } } int getHeight(tile_t *board, int col){ int height = _ROWS - 1; while (height-- >= 0 && board[getIndex(height, col)] == EMPTY); return height; }