added checks for horiz and vert

main
simonkellet 2 years ago
parent 3eeec01e9f
commit 0635907995
  1. 64
      main.c

@ -22,20 +22,29 @@ char board[ROWS][COLUMNS];
void print_board(); void print_board();
void clear_board(); void clear_board();
void player_choice(char player); void player_choice(char player);
void ai_choice(char player); void ai_choice(char player);
char check_win(); char check_win();
int check_free_space(); int check_free_space();
char flag = ' ';
int main(int argc, char *argv[]){ int main(int argc, char *argv[]){
clear_board(); clear_board();
while(check_free_space() != 0){ while(check_free_space() != 0 && flag == ' '){
print_board(); print_board();
player_choice(PLAYER1); player_choice(PLAYER1);
flag = check_win();
print_board(); print_board();
player_choice(PLAYER2); player_choice(PLAYER2);
flag = check_win();
} }
printf(BOLDWHITE "%c won!" RESET, flag);
return 0; return 0;
} }
@ -44,7 +53,7 @@ void print_board(){
int r, c; int r, c;
c = 0; c = 0;
system("clear"); system("clear");
printf("\nFree Spaces: %d\n", check_free_space()); //printf("\nFree Spaces: %d\n", check_free_space());
printf(BOLDWHITE " 1 2 3 4 5 6 \n" RESET); printf(BOLDWHITE " 1 2 3 4 5 6 \n" RESET);
@ -81,48 +90,69 @@ void clear_board(){
} }
void player_choice(char player){ void player_choice(char player){
unsigned int time = 30000; unsigned int frametime = 30000;
int pc; int chosenCol;
printf(BOLDWHITE "\n[%c] Enter column (1-6)\n> " RESET, player); printf(BOLDWHITE "\n[%c] Enter column (1-6)\n> " RESET, player);
scanf("%d", &pc); scanf("%d", &chosenCol);
if(pc > 6){ if(chosenCol > 6){
printf(BOLDRED "\n[%c] Cannot play here, skipping...\n" RESET, player); printf(BOLDRED "\n[%c] Cannot play here, skipping...\n" RESET, player);
sleep(1); sleep(1);
} else { } else {
pc--; //arrays start with 0 chosenCol--; //arrays start with 0
for(int r = 0; board[r][pc] == ' '; ++r){ for(int r = 0; board[r][chosenCol] == ' '; ++r){
// Check if the next tile is not empty (occupied) // Check if the next tile is not empty (occupied)
if(board[r+1][pc] != ' '){ if(board[r+1][chosenCol] != ' '){
board[r][pc] = player; board[r][chosenCol] = player;
break; break;
} }
//check if we've hit the last row //check if we've hit the last row
if(r >= COLUMNS){ if(r >= COLUMNS){
board[r][pc] = player; board[r][chosenCol] = player;
break; break;
} }
// Animation // Animation for dropping the piece down
board[r][pc] = player; board[r][chosenCol] = player;
print_board(); print_board();
usleep(time); usleep(frametime);
board[r][pc] = ' '; board[r][chosenCol] = ' ';
print_board(); print_board();
usleep(time); usleep(frametime);
} }
} }
} }
void ai_choice(char player){ void ai_choice(char player){
//TODO: Implement AI //TODO: Implement ""AI""
} }
char check_win(){ char check_win(){
char flag = ' '; char flag = ' ';
//check vert
for (int i = 0; i < COLUMNS; ++i) {
for (int j = 0; j < ROWS; ++j) {
if(board[i][j] == PLAYER1 && board[i][j+1] == PLAYER1 && board[i][j+2] == PLAYER1 && board[i][j+3] == PLAYER1){
flag = PLAYER1;
}
}
}
//check horiz
for (int i = 0; i < ROWS; ++i) {
for (int j = 0; j < COLUMNS; ++j) {
if(board[i][j] == PLAYER1 && board[i+1][j] == PLAYER1 && board[i+2][j] == PLAYER1 && board[i+3][j] == PLAYER1){
flag = PLAYER1;
}
}
}
//check diagright
//check diagleft
return flag; return flag;

Loading…
Cancel
Save