#include #include #include #include void setRows(int rows) { _ROWS = rows; } void setCollums(int collums) { _COLLUMS = collums; } void setDimension(int rows, int collums) { setRows(rows); setCollums(collums); } void setTokens(char t1, char t2) { _TOKEN1 = t1; _TOKEN2 = t2; } int getIndex(int i, int j) { return i * _COLLUMS + j; } 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[getIndex(i, 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 checkMove(tile_t *board, int collums) { if (collums < 0 || collums > _COLLUMS) { char buf[100]; sprintf(buf, "the collums must be between 1 and %d", _COLLUMS); warningMsg(buf); return -1; } if (board[collums] != EMPTY) { char buf[100]; sprintf(buf, "the collum %d is full", collums + 1); warningMsg(buf); return -1; } int pos = collums; while (isValid(pos + VERTICAL) && board[pos + VERTICAL] == EMPTY) { pos += VERTICAL; } return pos; } void insertCoin() { }