The first program with more than a hundred lines-Three Men Chess

The trio game can be broken down into the following processes:
1. Set up a game selection interface: the
The first program with more than a hundred lines-Three Men Chess
interface source code is as follows:
void menu()
{
while (1)
{
printf("Welcome to visit\n");
printf("1.play 2.exit\n");
printf("Please select:");
int i;
scanf_s("%d", &i);
if (i == 1)
game();
if (i == 2)
break;
}
}
2. Player placement and system placement, and display the post-placement interface.
The first program with more than a hundred lines-Three Men Chess
Drop source code:
void play(char arr[][3])
{
int x, y;
while (1)
{
printf("Please select the number of lines you want to
download :"); scanf_s("%d", &x) ;
printf("Please select the number of columns you want:");
scanf_s("%d", &y);
if (x> 3 || y> 3|| x <1|| y <1)
{
printf (" Your board is the wrong size!
\n"); continue;
}
else
if (arr[x - 1][y - 1] != ' ')
{
printf("该格已被占用");
continue;
}
else
arr[x - 1][y - 1] = player;
break;
}
}
void pc(char arr[][3])
{
void srand(unsigned int seed);
int x, y;

while (1)
{
    x = rand() % 4;
    y = rand() % 4;

    if (arr[x - 1][y - 1] != ' ')
    {
        continue;
    }
    else
        arr[x - 1][y - 1] = computer;
    break;
}

}
3. Determine the winner after the placement is completed.
The first program with more than a hundred lines-Three Men Chess
Judgment source code:
char judge(char arr[][3])
{
char flag = 0;
for (int i = 0; i <3; i++)
{
if (arr[0][i] == arr[1] [i] && \
arr[1][i] == arr[2][i] && \
arr[1][i] != '')
{
flag = arr[1][i];
break;

    }
}
for (int j = 0; j < 3; j++)
{
    if (arr[j][0] == arr[j][1] && \
        arr[j][2] == arr[j][1] && \
        arr[j][0] != ' ')
    {
        flag = arr[j][0];
        break;
    }

}if (arr[0][0] == arr[1][1] && \
    arr[2][2] == arr[1][1] && \
    arr[0][0] != ' ')
{
    flag = arr[0][0];

}
if (arr[0][2] == arr[1][1] && \
    arr[1][1] == arr[2][0] && \
    arr[1][1] != ' ')
{
    flag = arr[0][2];
}
if (flag == 0)
{
    for (int i = 0; i < 3; i++)
        for (int j = 0; j < 3; j++)
        {
            if (arr[i][j] == ' ')
            {
                flag = 2;
                break;
            }
        }

}
return flag;

}































if (arr[x - 1][y - 1] != ' ')
{
printf("该格已被占用");
continue;
}
else
arr[x - 1][y - 1] = player;
break;
}
}
void pc(char arr[][3])
{
void srand(unsigned int seed);
int x, y;

while (1)
{
    x = rand() % 4;
    y = rand() % 4;

    if (arr[x - 1][y - 1] != ' ')
    {
        continue;
    }
    else
        arr[x - 1][y - 1] = computer;
    break;
}

}
char judge(char arr[][3])
{
char flag = 0;
for (int i = 0; i < 3; i++)
{
if (arr[0][i] == arr[1][i] && \
arr[1][i] == arr[2][i] && \
arr[1][i] != ' ')
{
flag = arr[1][i];
break;

    }
}
for (int j = 0; j < 3; j++)
{
    if (arr[j][0] == arr[j][1] && \
        arr[j][2] == arr[j][1] && \
        arr[j][0] != ' ')
    {
        flag = arr[j][0];
        break;
    }

}if (arr[0][0] == arr[1][1] && \
    arr[2][2] == arr[1][1] && \
    arr[0][0] != ' ')
{
    flag = arr[0][0];

}
if (arr[0][2] == arr[1][1] && \
    arr[1][1] == arr[2][0] && \
    arr[1][1] != ' ')
{
    flag = arr[0][2];
}
if (flag == 0)
{
    for (int i = 0; i < 3; i++)
        for (int j = 0; j < 3; j++)
        {
            if (arr[i][j] == ' ')
            {
                flag = 2;
                break;
            }
        }

}
return flag;

}

void game()
{
char arr[3][3];
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++)
arr[i][j] = ' ';
while (1)
{
play(arr);
printf("*\n");
show(arr);
char i = judge(arr);
if (i != 2 && i != 'o' && i != 'x')
{
printf("和棋");
break;
}
if (i == 'o')
{
printf("你胜了!");
break;
}
if (i == 'x')
{
printf("你输了!");
break;
}

    pc(arr);
    printf("*\n");
    show(arr);
    i = judge(arr);
    if (i != 2 && i != 'o' && i != 'x')
    {
        printf("和棋");
        break;
    }
    if (i == 'o')
    {
        printf("你胜了!");
        break;
    }
    if (i == 'x')
    {
        printf("你输了!");
        break;
    }
}

}
void menu()
{
while (1)
{
printf("欢迎访问\n");
printf("1.play 2.exit\n");
printf("请选择:");
int i;
scanf_s("%d", &i);
if (i == 1)
game();
if (i == 2)
break;
}
}
int main()
{
menu();
return 0;
}

**

Guess you like

Origin blog.51cto.com/14946388/2543589