#include #include #include #include #include /* Game Specific defines */ #define ROWS 7 #define COLUMNS 6 #define TIME 0.6 #define PLAYER1 '0' #define PLAYER2 'X' /* Printf colour defines */ #define BOLDYELLOW "\033[1m\033[33m" /* Bold Yellow */ #define BOLDRED "\033[1m\033[31m" /* Bold Red */ #define BOLDWHITE "\033[1m\033[37m" /* Bold White */ #define RESET "\033[0m" /* Global board, saves passing 2D array pointers */ 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 && 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; } void print_board(){ int r, c; c = 0; system("clear"); //printf("\nFree Spaces: %d\n", check_free_space()); printf(BOLDWHITE " 1 2 3 4 5 6 \n" RESET); for(r = 0; r < ROWS; ++r){ for(c = 0; c < COLUMNS; ++c){ //PLAYER 1 COLOURS if(board[r][c] == PLAYER1){ printf("[ "); printf(BOLDRED "%c" RESET ,board[r][c]); printf(" ]"); } //PLAYER 2 COLOURS else if(board[r][c] == PLAYER2){ printf("[ "); printf(BOLDYELLOW "%c" RESET ,board[r][c]); printf(" ]"); } else{ printf("[ %c ]", board[r][c]); } } printf("\n"); } } void clear_board(){ int r, c; for(r = 0; r < ROWS; ++r){ for(c = 0; c < COLUMNS; ++c){ board[r][c] = ' '; } } } void player_choice(char player){ unsigned int frametime = 30000; int chosenCol; printf(BOLDWHITE "\n[%c] Enter column (1-6)\n> " RESET, player); scanf("%d", &chosenCol); if(chosenCol > 6){ printf(BOLDRED "\n[%c] Cannot play here, skipping...\n" RESET, player); sleep(1); } else { 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][chosenCol] != ' '){ board[r][chosenCol] = player; break; } //check if we've hit the last row if(r >= COLUMNS){ board[r][chosenCol] = player; break; } // Animation for dropping the piece down board[r][chosenCol] = player; print_board(); usleep(frametime); board[r][chosenCol] = ' '; print_board(); usleep(frametime); } } } void ai_choice(char player){ //TODO: Implement ""AI"" } char check_win(){ char flag = ' '; //no i know there is a better way to check for a win cond. //buuttt ill do what i wanna //and repeat all the checks twice (player 1 and player 2)! //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 for(int i = 0; i < ROWS; ++i){ for(int j = 0; j < COLUMNS; ++j) { if(board[i][j] == PLAYER1 && board[i-1][j+1] == PLAYER1 && board[i-2][j+2] == PLAYER1 && board[i-3][j+3] == PLAYER1){ flag = PLAYER1; } } } //check diagleft for(int i = 0; i < ROWS; ++i){ for(int j = 0; j < COLUMNS; ++j) { if(board[i][j] == PLAYER1 && board[i-1][j-1] == PLAYER1 && board[i-2][j-2] == PLAYER1 && board[i-3][j-3] == PLAYER1){ flag = PLAYER1; } } } //check vert for(int i = 0; i < COLUMNS; ++i) { for(int j = 0; j < ROWS; ++j) { if(board[i][j] == PLAYER2 && board[i][j+1] == PLAYER2 && board[i][j+2] == PLAYER2 && board[i][j+3] == PLAYER2){ flag = PLAYER2; } } } //check horiz for(int i = 0; i < ROWS; ++i) { for(int j = 0; j < COLUMNS; ++j) { if(board[i][j] == PLAYER2 && board[i+1][j] == PLAYER2 && board[i+2][j] == PLAYER2 && board[i+3][j] == PLAYER2){ flag = PLAYER2; } } } //check diagright for(int i = 0; i < ROWS; ++i){ for(int j = 0; j < COLUMNS; ++j) { if(board[i][j] == PLAYER2 && board[i-1][j+1] == PLAYER2 && board[i-2][j+2] == PLAYER2 && board[i-3][j+3] == PLAYER2){ flag = PLAYER2; } } } //check diagleft for(int i = 0; i < ROWS; ++i){ for(int j = 0; j < COLUMNS; ++j) { if(board[i][j] == PLAYER2 && board[i-1][j-1] == PLAYER2 && board[i-2][j-2] == PLAYER2 && board[i-3][j-3] == PLAYER2){ flag = PLAYER2; } } } return flag; } int check_free_space(){ int r, c; int sp = 0; for(r = 0; r < ROWS; ++r){ for(c = 0; c < COLUMNS; ++c){ if(board[r][c] == ' ') ++sp; } } return sp; }