printboard
This commit is contained in:
parent
f586aa739a
commit
bd08db380d
|
@ -14,12 +14,17 @@
|
|||
static int _ROWS;
|
||||
static int _COLLUMS;
|
||||
|
||||
static char _TOKEN1;
|
||||
static char _TOKEN2;
|
||||
|
||||
void setCollums(int collums);
|
||||
void setRows(int rows);
|
||||
int getIndex(int i, int j);
|
||||
|
||||
int checkWin(tile_t *board, int pos, int rows, int collums);
|
||||
int checkWinAll(tile_t *board, int rows, int collums);
|
||||
int checkWin(tile_t *board, int pos);
|
||||
int checkWinAll(tile_t *board);
|
||||
|
||||
void printBoard(tile_t *board);
|
||||
void printTile(tile_t t);
|
||||
|
||||
#endif
|
50
src/forza4.c
50
src/forza4.c
|
@ -11,10 +11,16 @@ 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;
|
||||
|
@ -33,7 +39,7 @@ int checkLine(tile_t *board, int pos, int delta) {
|
|||
|
||||
// checks board for a win
|
||||
// returns player tile_t on win, otherwise 0
|
||||
int checkWin(tile_t *board, int pos, int rows, int collums) {
|
||||
int checkWin(tile_t *board, int pos) {
|
||||
int result = checkLine(board, pos, ORIZONTAL);
|
||||
if (result) {
|
||||
return board[pos];
|
||||
|
@ -57,12 +63,46 @@ int checkWin(tile_t *board, int pos, int rows, int collums) {
|
|||
return 0;
|
||||
}
|
||||
|
||||
int checkWinAll(tile_t *board, int rows, int collums){
|
||||
for (int i=0; i<rows*collums; i++) {
|
||||
if (checkWin(board, i, rows, collums) != EMPTY) {
|
||||
return checkWin(board, i, rows, collums);
|
||||
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;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue