You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
126 lines
2.6 KiB
126 lines
2.6 KiB
2 years ago
|
#include <stdio.h>
|
||
|
#include <stdlib.h>
|
||
|
#include <time.h>
|
||
|
#include <unistd.h>
|
||
|
#include <assert.h>
|
||
|
// connect four
|
||
|
|
||
|
#define ROWS 7
|
||
|
#define COLUMNS 6
|
||
|
#define TIME 0.6
|
||
|
|
||
|
#define BOLDYELLOW "\033[1m\033[33m" /* Bold Yellow */
|
||
|
#define BOLDRED "\033[1m\033[31m" /* Bold Yellow */
|
||
|
#define BOLDWHITE "\033[1m\033[37m" /* Bold White */
|
||
|
#define RESET "\033[0m"
|
||
|
|
||
|
char board[ROWS][COLUMNS];
|
||
|
|
||
|
void print_board();
|
||
|
void clear_board();
|
||
|
void player_choice(char player);
|
||
|
int check_free_space();
|
||
|
|
||
|
int main(int argc, char *argv[]){
|
||
|
clear_board();
|
||
|
|
||
|
print_board();
|
||
|
player_choice('0');
|
||
|
print_board();
|
||
|
player_choice('O');
|
||
|
print_board();
|
||
|
player_choice('0');
|
||
|
print_board();
|
||
|
player_choice('O');
|
||
|
print_board();
|
||
|
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
|
|
||
|
void print_board(){
|
||
|
system("clear");
|
||
|
printf("\nFree Spaces: %d\n", check_free_space());
|
||
|
int r, c;
|
||
|
c = 0;
|
||
|
printf(BOLDWHITE " 1 2 3 4 5 6 \n" RESET);
|
||
|
for(r = 0; r < ROWS; ++r){
|
||
|
//printf(BOLDWHITE "%d\t" RESET, r+1);
|
||
|
for(c = 0; c < COLUMNS; ++c){
|
||
|
if(board[r][c] == 'O'){
|
||
|
printf("[ ");
|
||
|
printf(BOLDRED "%c" RESET ,board[r][c]);
|
||
|
printf(" ]");
|
||
|
}
|
||
|
else if(board[r][c] == '0'){
|
||
|
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 time = 30000;
|
||
|
int pc;
|
||
|
|
||
|
printf(BOLDWHITE "\n[%c]Enter column (1-6)\n> ", player);
|
||
|
scanf("%d", &pc);
|
||
|
pc--; //arrays start with 0
|
||
|
|
||
|
|
||
|
//TODO: replace these
|
||
|
assert(pc < 6);
|
||
|
assert(pc < 6);
|
||
|
|
||
|
|
||
|
for(int r = 0; board[r][pc] == ' '; ++r){
|
||
|
|
||
|
// Check if the next tile is not empty (occupied)
|
||
|
if(board[r+1][pc] != ' '){
|
||
|
board[r][pc] = player;
|
||
|
break;
|
||
|
}
|
||
|
//check if we've hit the last row
|
||
|
if(r >= COLUMNS){
|
||
|
board[r][pc] = player;
|
||
|
break;
|
||
|
}
|
||
|
|
||
|
// Animation
|
||
|
board[r][pc] = player;
|
||
|
print_board();
|
||
|
usleep(time);
|
||
|
board[r][pc] = ' ';
|
||
|
print_board();
|
||
|
usleep(time);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
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;
|
||
|
}
|