changes etc

main
simonkellet 3 years ago
parent 374bc97b5b
commit 3eeec01e9f
  1. 66
      main.c

@ -3,56 +3,61 @@
#include <time.h> #include <time.h>
#include <unistd.h> #include <unistd.h>
#include <assert.h> #include <assert.h>
// connect four
/* Game Specific defines */
#define ROWS 7 #define ROWS 7
#define COLUMNS 6 #define COLUMNS 6
#define TIME 0.6 #define TIME 0.6
#define PLAYER1 '0'
#define PLAYER2 'X'
/* Printf colour defines */
#define BOLDYELLOW "\033[1m\033[33m" /* Bold Yellow */ #define BOLDYELLOW "\033[1m\033[33m" /* Bold Yellow */
#define BOLDRED "\033[1m\033[31m" /* Bold Yellow */ #define BOLDRED "\033[1m\033[31m" /* Bold Red */
#define BOLDWHITE "\033[1m\033[37m" /* Bold White */ #define BOLDWHITE "\033[1m\033[37m" /* Bold White */
#define RESET "\033[0m" #define RESET "\033[0m"
/* Global board, saves passing 2D array pointers */
char board[ROWS][COLUMNS]; 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);
char check_win();
int check_free_space(); int check_free_space();
int main(int argc, char *argv[]){ int main(int argc, char *argv[]){
clear_board(); clear_board();
while(check_free_space() != 0){
print_board();
player_choice('0');
print_board();
player_choice('O');
print_board();
player_choice('0');
print_board(); print_board();
player_choice('O'); player_choice(PLAYER1);
print_board(); print_board();
player_choice(PLAYER2);
}
return 0; return 0;
} }
void print_board(){ void print_board(){
system("clear");
printf("\nFree Spaces: %d\n", check_free_space());
int r, c; int r, c;
c = 0; c = 0;
system("clear");
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);
for(r = 0; r < ROWS; ++r){ for(r = 0; r < ROWS; ++r){
//printf(BOLDWHITE "%d\t" RESET, r+1);
for(c = 0; c < COLUMNS; ++c){ for(c = 0; c < COLUMNS; ++c){
if(board[r][c] == 'O'){ //PLAYER 1 COLOURS
if(board[r][c] == PLAYER1){
printf("[ "); printf("[ ");
printf(BOLDRED "%c" RESET ,board[r][c]); printf(BOLDRED "%c" RESET ,board[r][c]);
printf(" ]"); printf(" ]");
} }
else if(board[r][c] == '0'){ //PLAYER 2 COLOURS
else if(board[r][c] == PLAYER2){
printf("[ "); printf("[ ");
printf(BOLDYELLOW "%c" RESET ,board[r][c]); printf(BOLDYELLOW "%c" RESET ,board[r][c]);
printf(" ]"); printf(" ]");
@ -79,16 +84,14 @@ void player_choice(char player){
unsigned int time = 30000; unsigned int time = 30000;
int pc; int pc;
printf(BOLDWHITE "\n[%c]Enter column (1-6)\n> ", player); printf(BOLDWHITE "\n[%c] Enter column (1-6)\n> " RESET, player);
scanf("%d", &pc); scanf("%d", &pc);
pc--; //arrays start with 0
//TODO: replace these
assert(pc < 6);
assert(pc < 6);
if(pc > 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){ for(int r = 0; board[r][pc] == ' '; ++r){
// Check if the next tile is not empty (occupied) // Check if the next tile is not empty (occupied)
@ -111,13 +114,28 @@ void player_choice(char player){
usleep(time); usleep(time);
} }
} }
}
void ai_choice(char player){
//TODO: Implement AI
}
char check_win(){
char flag = ' ';
return flag;
}
int check_free_space(){ int check_free_space(){
int r, c; int r, c;
int sp = 0; int sp = 0;
for(r = 0; r < ROWS; ++r){ for(r = 0; r < ROWS; ++r){
for(c = 0; c < COLUMNS; ++c){ for(c = 0; c < COLUMNS; ++c){
if(board[r][c] == ' ') ++sp; if(board[r][c] == ' ')
++sp;
} }
} }

Loading…
Cancel
Save