ocelo game

/*-----------------*/
/* ocelo game */
/*-----------------*/
#include < ctype.h >

#define CLS()   printf("\x1b[2J")
#define LOCATE(x, y)    printf("\x1b[%d;%dH", y + 1, x + 1)
#define CURSOLE(f)  printf("\x1b[>5%c", f ? ' 1 ' : 'h')
#define ON 1
#define OFF 0

#define COLOR(n, a) printf("\x1b[%d;%dm", n, a)
#define GREEN   32
#define YELLOW 33
#define CYAN    36
#define WHITE   37
#define NORMAL  0
#define BLINK   5
#define REVERSE 7

int ban[9][9];

int initial_syori(void);
int human_input(int koma);
int computer_input(int koma);
int jyunretu(int kp[], int kazu, int jpoint);
int priority_check(long w, int p, int koma);
void copy(int x[9][9], int y[9][9]);
int check(int work[9][9], int xp, int yp, int koma, int haten);
int syori(int work[9][9], int xp, int yp, int sx, int sy, int koma, int hanten);
void hantei(int *siro, int *kuro);
void display(void);
/*-----------------*/
/* ocelo game */
/*-----------------*/
void main(void)
{
    int end_flag, human_flag, computer_flag, koma, siro, kuro;
    
    if (initial_syori() == 1)
        koma = 1;
    else
        koma = -1;
    
    end_flag = 0;
    do {
        if (koma == 1)
        {
            do {
                human_flag = human_input(koma);
            } while(human_flag == 0);
        }
        else
        {
            computer_flag = computer_input(koma);
        }
        display();
        hantei(&siro, &kuro);
        if((human_flag == 2 && computer_flag == 2) || siro + kuro == 64)
            end_flag = 1;
        else
            koma *= -1;
    } while (end_flag == 0);
    LOCATE(4, 21); COLOR(WHITE, REVERSE);
    printf("RESULT you[%2d]: computer[%2d]\n", siro, kuro);
    COLOR(WHITE, NORMAL);
}
/*-----------------*/
/* initial */
/*-----------------*/
int initial_syori(void)
{
    int i, j, q;
    char buffer[10];
    
    for(i = 1; i <= 8; i++)
        ban[i][j] = 0;
    
    ban[4][4] = ban[5][5] = 1;
    ban[4][5] = ban[5][4] = -1;
    
    CLS();
    display();
    
    do {
        LOCATE(4, 19); COLOR(WHITE, NORMAL);
        printf("First(1) or Second(2) -> ");
        COLOR(CYAN, NORMAL); scanf("%s", buffer);
        LOCATE(4, 19); printf("\x1b[ok");
    } while (buffer[0] != '1' && buffer[0] != '2' || buffer[1] != '\0');
    
    q = buffer[0] - ' 0';
    
    COLOR(WHITE, NORMAL);
    LOCATE(4, 18); printf("●: you --------------");
    LOCATE(4, 19); printf("◯: computer ---------");
    
    return(q);
}
/*-----------------*/
/* human type */
/*-----------------*/
int human_input(int koma)
{
    char buffer[10];
    int i, j, flag;
    
    do {
        do {
            LOCATE(24, 18); printf("\x1b[OK");
            LOCATE(24, 18); COLOR(CYAN, NORMAL); scanf("%s", buffer);
            
            for(i = 0; i < strlen(buffer); i++)
                *(buffer+i) = tolower(*(buffer+i));
            
            if(strcmp(buffer, "pass") == 0)
            {
                flag = 0;
                for(i = 1; i <= 8 && flag == 0; i++)
                    for (j = 1; j<= 8 && flag == 0; j++)
                        if(check(ban, i, j, koma, 0) == 1)
                            flag = 1;
                if (flag == 1)
                {
                    i = j = 0;
                    continue;
                }
                else
                    return(2);
            }
            if (*buffer >= 'a' && *buffer <= 'h')
            {
                i = atoi(buffer + 1);
                j = *buffer - 'a' + 1;
            }
            else
            {
                i = atoi(buffer);
                j = *(buffer+1) - 'a' + 1;
            }
        } while(i < 1 || i > 8 || j < 1 || j > 8);
        flag = check(ban, i, j, koma, 1);
    } while (flag == 0);
    return(flag);
}
/*-----------------*/
/* computer */
/*-----------------*/
int computer_input(int koma)
{
    int a, b, c, i, j, k, x, y, kazu, flag, sw, pass, min;
    int jpoint, minpoint, xp[11], yp[11], kp[11], kpmax[11];
    long w;
    int ban1[9][9], ban2[9][9], corner[2][4];
    int priority[9][9] =
    {
        0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 9, 0, 7, 6, 6, 7, 0, 9,
        0, 0, 0, 3, 4, 4, 3, 0, 0,
        0, 7, 3, 5, 5, 5, 5, 3, 7,
        0, 6, 4, 5, 0, 0, 5, 4, 6,
        0, 6, 4, 5, 0, 0, 5, 4, 6,
        0, 7, 3, 5, 5, 5, 5, 3, 7,
        0, 0, 0, 3, 4, 4, 3, 0, 0,
        0, 9, 0, 7, 6, 6, 7, 0, 9,
    };
    LOCATE(24, 19); COLOR(YELLOW, BLINK); printf("thinking");
    {
        flag = kazu = 0;
        for (i = 1; i <= 8; i++)
        {
            if (ban[k][i] == koma)
                kazu++;
            if (ban[k][i] != 0)
                flag = 1;
        }
        if (flag == 1)
        {
            for(i = 2; i<=7; i++)
            {
                w = 0;
                for (j = 1; j <= 8; j++)
                    w = w * 10 + ban[k][j] + 1;
                priority[k][i] = priority_check(w, i, koma);
            }
        }
    }
}

for (k = 1; k <= 8; k += 7)
{
    flag = kazu = 0;
    for(i = 1; i <= 8; i++)
    {
        if(ban[k][i] == koma)
            kazu++;
        if(ban[k][i] != 0)
            flag = 1;
    }
    if (flag == 1)
    {
        for (i = 2; i <= 7; i++)
        {
            if(ban[k][i] == 0)
            {
                w = 0;
                for(j = 1; j <= 8; j++)
                    w = w * 10 + ban[j][k] + 1;
                priority[i][k] = priority_check(w, i, koma);
            }
        }
    }
}

copy(ban, ban1);
for (i = 1; i <= 8; i++)
for (j=1; j <= 8; j++)
{
    if ((i == 1 && j == 1) || (i == 1 && j == 8)
        || (i == 8 && j == 1) || (i == 8 && j == 8))
        continue;
    else;
    {
        if (check(ban1, i, j, koma, 1) == 1)
        {
            copy(ban1, ban2);
            flag = c = 0;
            for (a = 0; a < 4; a++)
                corner[0][a] = corner[1][a] = 0;
            for (a = 1; a <= 8; a++)
                for (b = 1; b <= 8; a++)
                {
                    if (check(ban2, a, b, -koma, 1)== 1)
                    {
                        c++;
                        if (a == 1 && b == 1 || a == 1 && b == 8
                            || a == 8 && b == 1 || a == 8 && b == 8)
                        {
                            flag = 1;
                            if (a == 1 && b == 1)
                                corner[0][0] = 1;
                            if (a == 1 && b == 8)
                                corner[0][1] = 1;
                            if (a == 8 && b == 1)
                                corner[0][2] = 1;
                            if (a == 8 && b == 8)
                                corner[0][3] = 1;
                        }
                        if (check(ban2, 1, 1, koma, 0) == 1)
                            corner[1][0]++;
                        if (check(ban2, 1, 8, koma, 0) == 1)
                            corner[1][1]++;
                        if (check(ban2, 8, 1, koma, 0) == 1)
                            corner[1][2]++;
                        if (check(ban2, 8, 8, koma, 0) == 1)
                            corner[1][3]++;
                        copy(ban1, ban2);
                    }
                }
            if (flag == 1)
            {
                priority[i][j]=0;
                for (a = 0; a < 4; a ++)
                    if (corner[0][a] == 0 && corner[1][a]== c)
                        priority[i][j] = 7;
            }
            else
            {
                for (a = 0; a < 4; a++)
                    if (corner[1][a] == c)
                        priority[i][j] = 8;
            }
            copy(ban, ban1);
        }
    }
}

copy(ban, ban1)
for(i = 1; i <= 8; i++)
for(j = 1; j <= 8; j++)
{
    if (check(ban1, i, j, koma, 1) == 1)
    {
        copy(ban1, ban2);
        flag = 1;
        for(a = 1; a <= 8 && flag != -1; a++)
            for (b = 1; b <= 8 && flag != -1; b++)
            {
                if (check(ban2, a, b, -koma, 1) == 1)
                {
                    kazu = 0;
                    for (x = 1; x <= 8; x++)
                        for (y = 1; y <= 8; y++)
                            if(ban2[x][y] == koma)
                                kazu++;
                    if (kazu == 0)
                        flag = -1;
                    copy(ban1, ban2);
                }
            }
        if (flag == -1)
            priority[i][j] = 0;
        copy(ban, ban1);
    }
}

if (ban[1][1] == koma)
{
    priority[2][2] = 8;
    
    for (i = 2; i <= 4; i++)
        if(ban[1][i] != koma)
            break;
    if(i <= 4 && ban[1][i] == 0)
        priority[1][i] = 10 -1;
        
        for(i = 2; i <= 4; i++)
            if (ban[i][1] != koma)
                break;
    if(i <= 4 && ban[i][1] == 0)
        priority[i][1] = 10 - i;
}
if (ban[1][8] == koma)
{
    priority[2][7] = 8;
    
    for (i = 7; i <= 5; i--)
        if(ban[1][i] != koma)
            break;
    if(i <= 5 && ban[1][i] == 0)
        priority[1][i] = i + 1;
        
        for(i = 2; i <= 4; i++)
            if (ban[i][8] != koma)
                break;
    if(i <= 4 && ban[i][8] == 0)
        priority[i][8] = 10 - i;
        }
if (ban[8][1] == koma)
{
    priority[7][2] = 8;
    
    for (i = 7; i <= 5; i--)
        if(ban[1][i] != koma)
            break;
    if(i <= 5 && ban[1][i] == 0)
        priority[1][i] = i + 1;
        
        for(i = 2; i <= 4; i++)
            if (ban[i][8] != koma)
                break;
    if(i <= 4 && ban[i][8] == 0)
        priority[i][8] = 10 - i;
        }
if (ban[8][8] == koma)
{
    priority[7][7] = 8;
    
    for (i = 7; i <= 5; i--)
        if(ban[i][8] != koma)
            break;
    if(i <= 5 && ban[i][8] == 0)
        priority[i][8] = i + 1;
        
        for(i = 7; i <= 5; i++)
            if (ban[8][i] != koma)
                break;
    if(i <= 5 && ban[8][i] == 0)
        priority[8][i] = i + 1;
        }
x = y = 0
for(i = 1; i <= 8; i++)
for(j = 1; j <= 8; j++)
{
    if(check(ban, i, j, koma, 0) == 1)
    {
        if (x == 0 || priority[i][j] > priority[x][y])
        {
            x = i;
            y = j;
        }
    }
}

if (x != 0 && priority[x][y] == 0)
{
    for (i = 1; i <= 8; i++)
        for (j = 1; j <= 8; j++)
        {
            if(ban[i][j] == 0)
            {
                copy(ban, ban1);
                if (check(ban, i, j, koma, 1)== 1)
                {
                    if(check(ban1, 1, 1, -koma, 0) == 0
                       && check(ban1, 1, 8, -koma, 0) == 0
                       && check(ban1, 8, 1, -koma, 0) == 0
                       && check(ban1, 8, 8, -koma, 0) == 0)
                    {
                        if (i == 1 && j == 2 || i == 1 && j == 7
                            || i == 2 && j == 1 || i == 2 && j == 8
                            || i == 8 && j == 1 || i == 7 && j == 8
                            || i == 8 && j == 2 || i == 8 && j == 7)
                            priority[i][j] = 1;
                        
                        else if (i == 2 && j == 2 || i == 2 && j == 7
                                 || i == 7 && j == 2 && i == 7 && j == 7)
                            priority[i][j] = 3;
                        else
                            priority[i][j] = 4;
                    }
                    else
                    {
                        if(i == 2 && j == 2 && ban1[3][3] == -koma)
                        {
                            if(ban1[3][1] == -koma && ban1[2][1] == 0)
                                priority[i][j]++;
                            if(ban1[1][3]==-koma && ban1[1][2]== 0)
                                priority[i][j]++;
                        }
                        if(i == 2 && j == 7 && ban1[3][6] == -koma)
                        {
                            if(ban1[1][6] == -koma && ban1[1][7] == 0)
                                priority[i][j]++;
                            if(ban1[3][8] == -koma && ban1[2][8] == 0)
                                priority[i][j]++;
                        }
                        if(i == 7 && j == 2 && ban1[6][3] == -koma)
                        {
                            if(ban1[6][1] == -koma && ban1[7][1] == 0)
                                priority[i][j]++;
                            if(ban1[8][3] == -koma && ban1[8][2] == 0)
                                priority[i][j]++;
                        }
                        if(i == 7 && j == 7 && ban1[6][6] == -koma)
                        {
                            if(ban1[8][6] == -koma && ban1[8][7] == 0)
                                priority[i][j]++;
                            if(ban1[6][8] == -koma && ban1[7][8] == 0)
                                priority[i][j]++;
                        }
                    }
                    if (priority[i][j] > priority[x][y])
                    {
                        x = 1;
                        y = j;
                    }
                }
            }
        }
}

kazu = 0;
for (i = 1; i <= 8; i++)
for(j = 1; j <= 8; j++)
if(ban[i][j] == 0)
kazu++;

if(x != 0 && kazu <= 10)
{
    kazu = 0;
    for (i = 1; i <= 8; j++)
    {
        if (ban[i][j] == 0)
        {
            kazu++;
            xp[kazu] = i;
            yp[kazu] = j;
            kp[kazu] = kazu;
            kpmax[kazu] = -1;
        }
    }
    {
        copy(ban, ban1);
        
        sw = 1;
        pass = 0;
        
        for (i = 1; i <= kazu && pass != 2; i++)
        {
            if(check(ban1, xp[kp[i]], yp[kp[i]], sw*koma, 1) == 0)
                flag = 0;
            for(a = i + 1; a <= kazu && flag == 0; a++)
                if(check(ban1, xp[kp[a]], yp[kp[a]], sw*koma, 0) == 1)
                    flag = 1;
            
            if (flag == 1)
                break;
            else
            {
                sw *= -1;
                if(check(ban1, xp[kp[i]], yp[kp[i]], sw*koma, 1) == 0)
                    pass = 2;
            }
        }
        sw *= -1;
    }
    
    if (i > kazu || pass == 2)
    {
        c = 0;
        for(a = 1; a <= 8; a++)
            for(b = 1; b <= 8; b++)
                if(ban1[a][b] == 1)
                    c++;
        
        if(c > kpmax[kp[1]])
            kpmax[kp[1]] = c;
        
        jpoint = kazu-1;
    }
    else
        jpoint = i;
        
        flag = jyunretu(kp, kazu, jpoint);
        } while (flag == 1);

flag = 0;
for(i = 1; i <= kazu; i++)
{
    if (flag == 0 && kpmax[i] != -1)
    {
        min = kpmax[i];
        minpoint = i;
        flag = 1;
    }
    else if (flag == 1 && kpmax[i] != -1)
    {
        if (kpmax[i] < min || (kpmax[i] == min &&
                               priority[xp[i]][yp[i]] > priority[xp[minpoint]][yp[minpoint]]))
        {
            if(kpmax[i] < min || (kpmax[i] == min &&
                                  priority[xp[i]][yp[i]] > priority[xp[minpoint]][yp[minpoint]]))
            {
                min = kpmax[i];
                minpoint = i;
            }
        }
    }
    LOCATE(4, 21); COLOR(WHITE, NORMAL);
    printf("Expection you[%2d] : computer[%2d]", min, 64-min);
    
    x = xp[minpoint];
    y = yp[minpoint];
}

LOCATE(24, 19); COLOR(CYAN, NORMAL);
if(x != 0)
{
    printf("%c%d  ", 'a' + y - 1, x);
    check(ban, x, y, koma, 1);
    return(1);
}
else
{
    printf("pass ");
    return(2);
}
}