Member Avatar for 111100/11000
111100/11000
//compiled with cmd command:gcc main.c -std=c11 -o main.exe
//this is only file

#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <string.h>

//user changeable variables
int boardSize = 9;//board hight and width are of equal size

#define NUMBER_OF_GRID_CHOICES 7//this has to be an odd number(increase it by two if you add another pair of ASCII characters to the beginning of emptySquare array)
char emptySquare[NUMBER_OF_GRID_CHOICES][2] = { {(int)43, (int)255}, {(int)176, (int)177}, {(int)95, (int)176}, //
                                                {(int)43, (int)255}, {(int)176, (int)177}, {(int)95, (int)176}, {(int)206, (int)206} };

char blackPiece = (int)254;
char whitePiece = (int)79;
int P1 = 1;//used as boolean

//functions that shouldn't be changed
void printGridWithoutText(char board[][boardSize]);
void printGrid(char board[][boardSize]);
int squareNotOccupied(char board[][boardSize], int gridChoice, int squareX, int squareY);
#define BIG 32765
int checkIfStoneIsSorrounded(char board[boardSize*boardSize], int gridChoice, int pos, int placesVisited[BIG]);
//global variables that shouldn't be changed
int singlePlayer;//used as boolean

int main(void){
    system("COLOR 60");
    printf("This program is a GO game engine made to work in DOS(console)\n\n");
    int gridChoice;//reused variable that allows user to set grid size, visual look of the grid

    printf("Enter 1 if you want first player's stone to be:%c\n", blackPiece);
    printf("or enter 2 if you want first player's stone to be:%c\n", whitePiece);
    scanf("%i", &gridChoice);
    if(gridChoice == 2) {
        blackPiece = (int)79;
        whitePiece = (int)254;
    }

    printf("Enter number of players (1(game against AI) or 2):");
    scanf("%i", &gridChoice);
    gridChoice == 1 ? (singlePlayer = 1) : (singlePlayer = 0);

    printf("Enter board size (i.e.:9,13,19,or any other number):");
    scanf("%i", &gridChoice);
    boardSize = gridChoice;

    char board[boardSize][boardSize];//contains char values of emptySquare,blackPiece or whitePiece
    //print all possible grid types
    for(gridChoice = 0; gridChoice<NUMBER_OF_GRID_CHOICES; gridChoice++) {

        printf("grid %i:\n", gridChoice+1);

        for(int i=0; i<boardSize; i++) {
            for(int j=0; j<boardSize; j++) {

                if(gridChoice < (NUMBER_OF_GRID_CHOICES-1)/2) {
                    if(i%2==0)//every other vertical row
                        board[j][i] = emptySquare[gridChoice][0];
                    else{
                        board[j][i] = emptySquare[gridChoice][1];
                    }
                }
                else {
                    if(i%2==0 || j%2==0)//every other row and every even square i think
                        board[j][i] = emptySquare[gridChoice][0];
                    else{
                        board[j][i] = emptySquare[gridChoice][1];
                    }
                }

            }
        }

        printGridWithoutText(board);    
        printf("\n");

    }

    printf("Choose grid type by entering it's number:");
    scanf("%i", &gridChoice);
    gridChoice--;

    printf("Chosen grid:\n");
    for(int i=0; i<boardSize; i++) {
        for(int j=0; j<boardSize; j++) {

            if(gridChoice < (NUMBER_OF_GRID_CHOICES-1)/2) {
                if(i%2==0)
                    board[j][i] = emptySquare[gridChoice][0];
                else{
                    board[j][i] = emptySquare[gridChoice][1];
                }
            }
            else {
                if(i%2==0 || j%2==0)
                    board[j][i] = emptySquare[gridChoice][0];
                else{
                    board[j][i] = emptySquare[gridChoice][1];
                }
            }

        }
    }//after this point gridChoice should not be changed because it's value will be needed

    printf("\n");
    printGridWithoutText(board);    
    printf("\n");
    printf("\n");

    int x = 0, y = 0;

    char temp[] = "sixEle";

    while(1) {//game loop

        do
        {

            printf("Enter x and y coordinates separated by space to put piece or type pass to pass:\n\n");

            printGrid(board);
            printf("\n");

            scanf(" %[^\n]s", temp);//get up to 6 characters(ASCII number values)

            for(int i=0; i<3; i++) {

                if(i == 0) {
                    x = 10 * (((int)temp[i])-48);
                    if(temp[i+1] != 32) {
                        x += (int)temp[i+1];
                        x -= 48;
                    }
                    else {
                        x /= 10;
                    }
                }
                if(temp[i] == 32) {//if space
                    y = 10 * (((int)temp[i+1])-48);//add first number after space to y
                    if(temp[i+2] != 0) {//if second parameter consists of two numbers(i.e.:there is a second number after space)
                        y += (int)temp[i+2];//add second number after space to y
                        y -= 48;//ASCII to it's number value
                    }
                    else {
                        y /= 10;
                    }
                }

            }
            system("cls");

        }while(squareNotOccupied(board, gridChoice, x-1, y-1) != 1);

        x--;y--;//decrement x and y because board starts from 0 0 and not from 1 1 (upper left corner)

        if(P1) {
            board[x][y] = blackPiece;
        }
        else {
            board[x][y] = whitePiece;
        }

        //create 1d array from 2d array board
        char board1D[boardSize*boardSize];

        for(int i=0; i<boardSize; i++) {
            for(int j=0; j<boardSize; j++) {
                board1D[j+((boardSize+1)*i)] = board[j][i];//move 2D array into 1D array
            }
        }

        int boolTemp;
        int largeArrayOfZeros[BIG];
        for(int i=0; i<BIG; i++) {
            largeArrayOfZeros[i] = 0;
        }

        for(int i=0; i<boardSize*boardSize; i++) {
            boolTemp = checkIfStoneIsSorrounded(board1D, gridChoice, i, largeArrayOfZeros);//THIS FUNCTION DOESN'T SEEM TO WORK
            if(boolTemp == 1) {
                system("pause");
                printf("element %i surrounded:%i", i, boolTemp);//debugging
            }
        }

        for(int i=0; i<boardSize; i++) {
            for(int j=0; j<boardSize; j++) {
                board[j][i] = board1D[j+((boardSize+1)*i)];//move 1D array back into 2D array
            }
        }

        if(P1) {//give turn to the other player
            P1 = 0;
        }
        else {
            P1 = 1;
        }
        system("cls");

    }//end of game loop
    system("pause");
    return 0;
}

void printGridWithoutText(char board[][boardSize])
{   
    for(int i=0; i<boardSize; i++) {
        for(int j=0; j<boardSize; j++) {
            printf("%c", board[j][i]);
        }
        printf("\n");
    }
}

void printGrid(char board[][boardSize])
{
    system("COLOR 03");
    if(singlePlayer == 0 && P1 == 1) {
        printf("player one's move\n");
    }
    else {
        printf("\n");
    }

    printf("  ");
    for(int i=0; i<boardSize; i++) {//prints numbers at the top of the board
        printf("%i", i+1);
    }
    printf("\n");

    for(int i=0; i<boardSize; i++) {
        for(int j=0; j<boardSize; j++) {
            if(j == 0) {
                printf("%i:%c", i+1, board[j][i]);//prints numbers at the left side of the board
            }
            else {
                printf("%c", board[j][i]);
            }
        }
        printf("\n");
    }

    if(singlePlayer == 0 && P1 == 0){
        printf("player two's move\n");
    }
}

int squareNotOccupied(char board[][boardSize], int gridChoice, int squareX, int squareY)//returns true(1) if square is not occupied
{
    if((board[squareX][squareY] == blackPiece) || (board[squareX][squareY] == whitePiece)) {
        return 0;
    }
    return (board[squareX][squareY] == emptySquare[gridChoice][0] || board[squareX][squareY] == emptySquare[gridChoice][1]) ? 1 : 0;
}

int checkIfStoneIsSorrounded(char board[boardSize*boardSize], int gridChoice, int pos, int placesVisited[BIG])
{//returns 1 if stone is surrounded

    if(board[pos] == whitePiece) {//if checking for white pieces

        if(board[pos-1] == whitePiece) {
                if(placesVisited[pos-boardSize] == 0) {
                    placesVisited[pos-boardSize] = 1;
                    if(checkIfStoneIsSorrounded(board, gridChoice, pos-1, placesVisited) == 0) {
                        return 0;
                    }
                }
        }
        if(board[pos+1] == whitePiece) {
                if(placesVisited[pos-boardSize] == 0) {
                    placesVisited[pos-boardSize] = 1;
                    if(checkIfStoneIsSorrounded(board, gridChoice, pos+1, placesVisited) == 0) {
                        return 0;
                    }
                }
        }
        if(pos+boardSize < boardSize*boardSize) {
            if(board[pos+boardSize] == whitePiece) {
                if(placesVisited[pos-boardSize] == 0) {
                    placesVisited[pos-boardSize] = 1;
                    if(checkIfStoneIsSorrounded(board, gridChoice, pos+boardSize, placesVisited) == 0) {
                        return 0;
                    }
                }
            }
        }
        if(pos-boardSize >= 0) {
            if(board[pos-boardSize] == whitePiece) {
                if(placesVisited[pos-boardSize] == 0) {
                    placesVisited[pos-boardSize] = 1;
                    if(checkIfStoneIsSorrounded(board, gridChoice, pos-boardSize, placesVisited) == 0) {
                        return 0;
                    }
                }
            }
        }

        if( ((board[pos+1] == blackPiece) || (board[pos+1] == whitePiece)) 
        &&  ((board[pos-1] == blackPiece) || (board[pos-1] == whitePiece)) 
        &&  ((board[pos+boardSize] == blackPiece) || (board[pos+boardSize] == whitePiece)) 
        &&  ((board[pos-boardSize] == blackPiece) || (board[pos-boardSize] == whitePiece)) ) {//if surrounded on all sides by either pieces(black or white)
            return 1;
        }

    }
    else {//if checking for black pieces

        if(board[pos-1] == blackPiece) {
                if(placesVisited[pos-boardSize] == 0) {
                    placesVisited[pos-boardSize] = 1;
                    if(checkIfStoneIsSorrounded(board, gridChoice, pos-1, placesVisited) == 0) {
                        return 0;
                    }
                }
        }
        if(board[pos+1] == blackPiece) {
                if(placesVisited[pos-boardSize] == 0) {
                    placesVisited[pos-boardSize] = 1;
                    if(checkIfStoneIsSorrounded(board, gridChoice, pos+1, placesVisited) == 0) {
                        return 0;
                    }
                }
        }
        if(pos+boardSize < boardSize*boardSize) {
            if(board[pos+boardSize] == blackPiece) {
                if(placesVisited[pos-boardSize] == 0) {
                    placesVisited[pos-boardSize] = 1;
                    if(checkIfStoneIsSorrounded(board, gridChoice, pos+boardSize, placesVisited) == 0) {
                        return 0;
                    }
                }
            }
        }
        if(pos-boardSize >= 0) {
            if(board[pos-boardSize] == blackPiece) {
                if(placesVisited[pos-boardSize] == 0) {
                    placesVisited[pos-boardSize] = 1;
                    if(checkIfStoneIsSorrounded(board, gridChoice, pos-boardSize, placesVisited) == 0) {
                        return 0;
                    }
                }
            }
        }

        if( ((board[pos+1] == blackPiece) || (board[pos+1] == whitePiece)) 
        &&  ((board[pos-1] == blackPiece) || (board[pos-1] == whitePiece)) 
        &&  ((board[pos+boardSize] == blackPiece) || (board[pos+boardSize] == whitePiece)) 
        &&  ((board[pos-boardSize] == blackPiece) || (board[pos-boardSize] == whitePiece)) ) {//if surrounded on all sides by either pieces(black or white)
            return 1;
        }

    }
    return 0;//will return 0 only if it finds empty square adjacent to it
}

stones are not disappearing

if(boolTemp == 1) {
                    system("pause");
                    printf("element %i surrounded:%i", i, boolTemp);//debugging
                }

this part should get triggered but it doesn't

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.