Files
c-connect-four/main.c
T

226 lines
5.7 KiB
C
Raw Normal View History

2022-05-23 15:36:13 +01:00
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#include <assert.h>
2022-05-23 18:55:23 +01:00
/* Game Specific defines */
2022-05-23 15:36:13 +01:00
#define ROWS 7
#define COLUMNS 6
#define TIME 0.6
2022-05-23 18:55:23 +01:00
#define PLAYER1 '0'
#define PLAYER2 'X'
2022-05-23 15:36:13 +01:00
2022-05-23 18:55:23 +01:00
/* Printf colour defines */
2022-05-23 15:36:13 +01:00
#define BOLDYELLOW "\033[1m\033[33m" /* Bold Yellow */
2022-05-23 18:55:23 +01:00
#define BOLDRED "\033[1m\033[31m" /* Bold Red */
2022-05-23 15:36:13 +01:00
#define BOLDWHITE "\033[1m\033[37m" /* Bold White */
#define RESET "\033[0m"
2022-05-23 18:55:23 +01:00
/* Global board, saves passing 2D array pointers */
2022-05-23 15:36:13 +01:00
char board[ROWS][COLUMNS];
void print_board();
void clear_board();
2023-05-25 13:56:22 +01:00
2022-05-23 15:36:13 +01:00
void player_choice(char player);
2022-05-23 18:55:23 +01:00
void ai_choice(char player);
2023-05-25 13:56:22 +01:00
2022-05-23 18:55:23 +01:00
char check_win();
2022-05-23 15:36:13 +01:00
int check_free_space();
2023-05-25 13:56:22 +01:00
char flag = ' ';
2022-05-23 15:36:13 +01:00
int main(int argc, char *argv[]){
clear_board();
2023-05-25 13:56:22 +01:00
while(check_free_space() != 0 && flag == ' '){
2022-05-23 18:55:23 +01:00
print_board();
player_choice(PLAYER1);
2023-05-25 13:56:22 +01:00
flag = check_win();
2022-05-23 18:55:23 +01:00
print_board();
player_choice(PLAYER2);
2023-05-25 13:56:22 +01:00
flag = check_win();
2022-05-23 18:55:23 +01:00
}
2022-05-23 15:36:13 +01:00
2023-05-25 13:56:22 +01:00
printf(BOLDWHITE "%c won!" RESET, flag);
2022-05-23 15:36:13 +01:00
return 0;
}
void print_board(){
int r, c;
c = 0;
2022-05-23 18:55:23 +01:00
system("clear");
2023-05-25 13:56:22 +01:00
//printf("\nFree Spaces: %d\n", check_free_space());
2022-05-23 18:55:23 +01:00
2022-05-23 15:36:13 +01:00
printf(BOLDWHITE " 1 2 3 4 5 6 \n" RESET);
2022-05-23 18:55:23 +01:00
2022-05-23 15:36:13 +01:00
for(r = 0; r < ROWS; ++r){
for(c = 0; c < COLUMNS; ++c){
2022-05-23 18:55:23 +01:00
//PLAYER 1 COLOURS
if(board[r][c] == PLAYER1){
2022-05-23 15:36:13 +01:00
printf("[ ");
printf(BOLDRED "%c" RESET ,board[r][c]);
printf(" ]");
}
2022-05-23 18:55:23 +01:00
//PLAYER 2 COLOURS
else if(board[r][c] == PLAYER2){
2022-05-23 15:36:13 +01:00
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){
2023-05-25 13:56:22 +01:00
unsigned int frametime = 30000;
int chosenCol;
2022-05-23 15:36:13 +01:00
2022-05-23 18:55:23 +01:00
printf(BOLDWHITE "\n[%c] Enter column (1-6)\n> " RESET, player);
2023-05-25 13:56:22 +01:00
scanf("%d", &chosenCol);
2022-05-23 15:36:13 +01:00
2023-05-25 13:56:22 +01:00
if(chosenCol > 6){
2022-05-23 18:55:23 +01:00
printf(BOLDRED "\n[%c] Cannot play here, skipping...\n" RESET, player);
sleep(1);
} else {
2023-05-25 13:56:22 +01:00
chosenCol--; //arrays start with 0
for(int r = 0; board[r][chosenCol] == ' '; ++r){
2022-05-23 15:36:13 +01:00
2022-05-23 18:55:23 +01:00
// Check if the next tile is not empty (occupied)
2023-05-25 13:56:22 +01:00
if(board[r+1][chosenCol] != ' '){
board[r][chosenCol] = player;
2022-05-23 18:55:23 +01:00
break;
}
//check if we've hit the last row
if(r >= COLUMNS){
2023-05-25 13:56:22 +01:00
board[r][chosenCol] = player;
2022-05-23 18:55:23 +01:00
break;
}
2022-05-23 15:36:13 +01:00
2023-05-25 13:56:22 +01:00
// Animation for dropping the piece down
board[r][chosenCol] = player;
2022-05-23 18:55:23 +01:00
print_board();
2023-05-25 13:56:22 +01:00
usleep(frametime);
board[r][chosenCol] = ' ';
2022-05-23 18:55:23 +01:00
print_board();
2023-05-25 13:56:22 +01:00
usleep(frametime);
2022-05-23 15:36:13 +01:00
}
}
}
2022-05-23 18:55:23 +01:00
void ai_choice(char player){
2023-05-25 13:56:22 +01:00
//TODO: Implement ""AI""
2022-05-23 18:55:23 +01:00
}
char check_win(){
char flag = ' ';
2023-05-25 17:13:02 +01:00
//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)!
2023-05-25 13:56:22 +01:00
//check vert
2023-05-25 17:13:02 +01:00
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;
2023-05-25 13:56:22 +01:00
}
}
}
//check horiz
2023-05-25 17:13:02 +01:00
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;
2023-05-25 13:56:22 +01:00
}
}
}
//check diagright
2023-05-25 17:13:02 +01:00
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;
}
}
}
2023-05-25 13:56:22 +01:00
//check diagleft
2023-05-25 17:13:02 +01:00
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;
}
}
}
2023-05-25 13:56:22 +01:00
2023-05-25 17:13:02 +01:00
//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;
}
}
}
2022-05-23 18:55:23 +01:00
return flag;
}
2022-05-23 15:36:13 +01:00
int check_free_space(){
int r, c;
int sp = 0;
2022-05-23 18:55:23 +01:00
2022-05-23 15:36:13 +01:00
for(r = 0; r < ROWS; ++r){
for(c = 0; c < COLUMNS; ++c){
2022-05-23 18:55:23 +01:00
if(board[r][c] == ' ')
++sp;
2022-05-23 15:36:13 +01:00
}
}
return sp;
}