DropsBrowse Pastes
Login with GitHub

connect-4.c

December 22nd, 2021Views: 20(0 unique)C
#include <stdio.h>
#include <stdbool.h>

char board[12][25] = {"                         ",
                      "                         ",
                      "                         ",
                      "      +-+-+-+-+-+-+      ",
                      "      +-+-+-+-+-+-+      ",
                      "      +-+-+-+-+-+-+      ",
                      "      +-+-+-+-+-+-+      ",
                      "      +-+-+-+-+-+-+      ",
                      "      +-+-+-+-+-+-+      ",
                      "                         ",
                      "                         ",
                      "                         "};

struct
{
    int x, y;
} lastStep;

bool isUnfinished(void);
void printBoard(void);
int placeStone(int, int);
int realColumnNumCalc(int);
char toMark(int);
bool winGame(void);

int main(void)
{
    while (isUnfinished())
    {
        int columnNum;

        printBoard();
        do
        {
            printf("Player A, please enter a column number: ");
            scanf("%d", &columnNum);
        } while (placeStone(columnNum, 0));
        if (winGame())
        {
            printBoard();
            printf("Congratulations! A wins the game.\n");
            return 0;
        }

        printBoard();
        do
        {
            printf("Player B, please enter a column number: ");
            scanf("%d", &columnNum);
        } while (placeStone(columnNum, 1));
        if (winGame())
        {
            printBoard();
            printf("Congratulations! B wins the game.\n");
            return 0;
        }
    }
    printBoard();
    printf("Oh no, the board is full!\n");
    return 0;
}

bool isUnfinished(void)
{
    bool isEmpty = false;
    for (int i = 3; i < 9; i++)
    {
        for (int j = 6; j < 19; j += 2)
        {
            if (board[i][j] == '+')
            {
                isEmpty = true;
                break;
            }
        }
    }
    if (isEmpty)
    {
        return true;
    }
    return false;
}

void printBoard(void)
{
    for (int i = 8; i >= 3; i--)
    {
        for (int j = 6; j < 19; j++)
        {
            putchar(board[i][j]);
        }
        putchar('\n');
    }
}

int placeStone(int column, int player)
{
    int i;
    for (i = 3; i < 9; i++)
    {
        int realColumnNum = realColumnNumCalc(column);
        if (realColumnNum == -1)
        {
            return 2;
        }
        if (board[i][realColumnNum] == '+')
        {
            board[i][realColumnNum] = toMark(player);
            lastStep.x = i;
            lastStep.y = realColumnNum;
            break;
        }
    }
    if (i == 9)
    {
        return 1;
    }
    return 0;
}

int realColumnNumCalc(int column)
{
    switch (column)
    {
    case 1:
        return 6;
    case 2:
        return 8;
    case 3:
        return 10;
    case 4:
        return 12;
    case 5:
        return 14;
    case 6:
        return 16;
    case 7:
        return 18;
    default:
        return -1;
    }
}

char toMark(int player)
{
    return player ? 'X' : 'O';
}

bool winGame(void)
{
    char currentMark = board[lastStep.x][lastStep.y];

    for (int i = lastStep.y - 6; i <= lastStep.y; i += 2)
    {
        if (board[lastStep.x][i] == currentMark && board[lastStep.x][i + 2] == currentMark &&
            board[lastStep.x][i + 4] == currentMark && board[lastStep.x][i + 6] == currentMark)
        {
            return true;
        }
    }

    for (int i = lastStep.x - 3; i <= lastStep.x; i++)
    {
        if (board[i][lastStep.y] == currentMark && board[i + 1][lastStep.y] == currentMark &&
            board[i + 2][lastStep.y] == currentMark && board[i + 3][lastStep.y] == currentMark)
        {
            return true;
        }
    }

    for (int i = lastStep.x + 3, j = lastStep.y - 6; j <= lastStep.y; i--, j += 2)
    {
        if (board[i][j] == currentMark && board[i - 1][j + 2] == currentMark &&
            board[i - 2][j + 4] == currentMark && board[i - 3][j + 6] == currentMark)
        {
            return true;
        }
    }

    for (int i = lastStep.x - 3, j = lastStep.y - 6; j <= lastStep.y; i++, j += 2)
    {
        if (board[i][j] == currentMark && board[i + 1][j + 2] == currentMark &&
            board[i + 2][j + 4] == currentMark && board[i + 3][j + 6] == currentMark)
        {
            return true;
        }
    }

    return false;
}