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 clear_board();
void player_choice(char player);
void ai_choice(char player);
char check_win();
int check_free_space();
char flag = ' ';
int main(int argc, char *argv[]){
clear_board();
while(check_free_space() != 0){
while(check_free_space() != 0 && flag == ' '){
print_board();
player_choice(PLAYER1);
flag = check_win();
print_board();
player_choice(PLAYER2);
flag = check_win();
}
printf(BOLDWHITE "%c won!" RESET, flag);
return 0;
}
@ -44,7 +53,7 @@ void print_board(){
int r, c;
c = 0;
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);
@ -81,48 +90,69 @@ void clear_board(){
}
void player_choice(char player){
unsigned int time = 30000;
int pc;
unsigned int frametime = 30000;
int chosenCol;
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);
sleep(1);
} else {
pc--; //arrays start with 0
for(int r = 0; board[r][pc] == ' '; ++r){
chosenCol--; //arrays start with 0
for(int r = 0; board[r][chosenCol] == ' '; ++r){
// Check if the next tile is not empty (occupied)
if(board[r+1][pc] != ' '){
board[r][pc] = player;
if(board[r+1][chosenCol] != ' '){
board[r][chosenCol] = player;
break;
}
//check if we've hit the last row
if(r >= COLUMNS){
board[r][pc] = player;
board[r][chosenCol] = player;
break;
}
// Animation
board[r][pc] = player;
// Animation for dropping the piece down
board[r][chosenCol] = player;
print_board();
usleep(time);
board[r][pc] = ' ';
usleep(frametime);
board[r][chosenCol] = ' ';
print_board();
usleep(time);
usleep(frametime);
}
}
}
void ai_choice(char player){
//TODO: Implement AI
//TODO: Implement ""AI""
}
char check_win(){
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;

Loading…
Cancel
Save