changes etc

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

102
main.c

@ -3,56 +3,61 @@
#include <time.h>
#include <unistd.h>
#include <assert.h>
// connect four
/* 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 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();
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();
while(check_free_space() != 0){
print_board();
player_choice(PLAYER1);
print_board();
player_choice(PLAYER2);
}
return 0;
}
void print_board(){
system("clear");
printf("\nFree Spaces: %d\n", check_free_space());
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){
//printf(BOLDWHITE "%d\t" RESET, r+1);
for(c = 0; c < COLUMNS; ++c){
if(board[r][c] == 'O'){
//PLAYER 1 COLOURS
if(board[r][c] == PLAYER1){
printf("[ ");
printf(BOLDRED "%c" RESET ,board[r][c]);
printf(" ]");
}
else if(board[r][c] == '0'){
//PLAYER 2 COLOURS
else if(board[r][c] == PLAYER2){
printf("[ ");
printf(BOLDYELLOW "%c" RESET ,board[r][c]);
printf(" ]");
@ -79,45 +84,58 @@ void player_choice(char player){
unsigned int time = 30000;
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);
pc--; //arrays start with 0
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){
// 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);
}
}
}
//TODO: replace these
assert(pc < 6);
assert(pc < 6);
void ai_choice(char player){
//TODO: Implement AI
}
char check_win(){
char flag = ' ';
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;
}
return flag;
// 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;
if(board[r][c] == ' ')
++sp;
}
}

Loading…
Cancel
Save