【C语言】五字棋/三子棋代码(含任意多子)

【C语言】五字棋/三子棋代码(含任意多子)_第1张图片

 五子棋代码c语言简单实现

(pvc版和pvp版都有)

人与电脑下,人与人下都实现了

案例演示:

三子棋

【C语言】五字棋/三子棋代码(含任意多子)_第2张图片

 

案例演示:

五子棋

【C语言】五字棋/三子棋代码(含任意多子)_第3张图片

代码展示:

#define _CRT_SECURE_NO_WARNINGS 1

#define X 10 //行
#define Y 15 //列
#define NUM 5//几子赢


//三子棋: X=3 Y=3 NUM=3
//五子棋: X>=10 Y>=10 NUM=5
//多子棋:(N子棋)NUM=N


#include 
#include 
#include 

void menu();
void startgame();
void gamePvC();
void gamePvP();
void initboard(char board[X][Y]);
void printboard(char board[X][Y]);
void playermove1(char board[X][Y]);
void playermove2(char board[X][Y]);
void PCmove(char board[X][Y]);
char iswin(char board[X][Y], int x, int y);
int Full(char board[X][Y]);

char flag = 'a';

int main()
{
    startgame();
    //将游戏完全封装起来
    //实现一个函数对应一个功能
    return 0; 
}

//菜单栏
void menu()
{
    printf(" ------------------------\n");
    printf("|       0.退出游戏       |\n");
    printf("|     1.PvC人机对局      |\n");
    printf("|     2.PvP双人对局      |\n");
    printf(" ------------------------\n");
}

//游戏开始
void startgame()
{
    int ret = 0;
    do
    {
        menu();
        scanf("%d", &ret);
        switch (ret)
        {
        case 0:
            printf("game over\n");
            break;
        case 1:
            gamePvC();
            break;
        case 2:
            gamePvP();
            break;
        default:
            printf("输入错误,请重新输入!\n");
            break;
        }

    } while (ret);
}

//人机对局游戏
void gamePvC()
{
    char board[X][Y] = { '\0' };
    initboard(board);
    printboard(board);

    while (1)
    {
        playermove1(board);
        if (flag == '*') break;
        if (Full(board) == 1) { flag = '='; break; }

        PCmove(board);
        if (flag == '#') break;
        if (Full(board) == 1) { flag = '='; break; }
    }

    if (flag == '*') printf("玩家胜利\n");
    if (flag == '#') printf("电脑胜利\n");
    if (flag == '=') printf("平局\n");

}

//双人对局游戏
void gamePvP()
{
    char board[X][Y] = { '\0' };
    initboard(board);
    printboard(board);

    while (1)
    {
        playermove1(board);
        if (flag == '*') break;
        if (Full(board) == 1) { flag = '='; break; }

        playermove2(board);
        if (flag == '@') break;
        if (Full(board) == 1) { flag = '='; break; }
    }

    if (flag == '*') printf("玩家1胜利\n");
    if (flag == '@') printf("玩家2胜利\n");
    if (flag == '=') printf("平局\n");

}

//遍历棋盘
void initboard(char board[X][Y])
{
    for (int i = 0; i < X; i++)
    {
        for (int j = 0; j < Y; j++)
        {
            board[i][j] = ' ';
        }
    }
}

//打印棋盘
void printboard(char board[X][Y])
{
    for (int i = 0; i < X; i++)
    {
        if (i == 0)
        {
            printf("    ");
            for (int k = 1; k <= Y; k++)
            {
                printf("%2d列", k);
                if (k == Y)printf("\n    ");
            }
            for (int k = 0; k < Y; k++)
            {
                printf("+---");
                if (k == Y - 1)printf("+\n");
            }
        }

        for (int j = 0; j < Y; j++)
        {
            if (j == 0) printf("%2d行", i + 1);
            printf("|");
            printf(" %c ", board[i][j]);

            if (j == Y - 1)printf("|\n    ");
        }

        for (int k = 0; k < Y; k++)
        {
            printf("+---");
            if (k == Y - 1)printf("+\n");
        }


    }
}

//玩家1下棋 *
void playermove1(char board[X][Y])
{
    while (1)
    {
        int x = 0, y = 0;
        printf("玩家1,(*玩家)请输入行数和列数以此下棋\n");
        scanf("%d %d", &x, &y);
        if (x <= X && x >= 1 && y <= Y && y >= 1 && (board[x - 1][y - 1] == ' '))
        {
            board[x - 1][y - 1] = '*';
            flag = iswin(board, x, y);
            break;
        }
        else
        {
            printf("输入错误,请重新输入\n");
        }
    }

}

//玩家2下棋 @
void playermove2(char board[X][Y])
{
    while (1)
    {
        int x = 0, y = 0;
        printf("玩家2,(@玩家)请输入行数和列数以此下棋\n");
        scanf("%d %d", &x, &y);
        if (x <= X && x >= 1 && y <= Y && y >= 1 && (board[x - 1][y - 1] == ' '))
        {
            board[x - 1][y - 1] = '@';
            flag = iswin(board, x, y);
            break;
        }
        else
        {
            printf("输入错误,请重新输入\n");
        }
    }

}

//电脑下棋 #
void PCmove(char board[X][Y])
{
    srand((unsigned)time(NULL));
    while (1)
    {
        int x = 0;
        int y = 0;

        x = (rand() % X) + 1;
        y = (rand() % Y) + 1;
        if (board[x - 1][y - 1] == ' ')
        {
            board[x - 1][y - 1] = '#';
            flag = iswin(board, x, y);
            break;
        }
    }
}

//棋盘是否满了
int Full(char board[X][Y])
{
    for (int i = 0; i < X; i++)
    {
        for (int j = 0; j < Y; j++)
        {
            if (board[i][j] == ' ') return 0;
        }
    }
    return 1;
}

//判断胜负
char iswin(char board[X][Y], int x, int y)
{
    int arr[X][Y] = { 0 };
    int way[4] = { 1,1,1,1 };
    char c = board[x - 1][y - 1];
    system("cls");
    printboard(board);
    printf("上一步%c下在了%d行%d列\n", c, x, y);
    //将该种类型棋子变成0 1混合的int类型数组,1表示该种类型的棋子分布
    for (int i = 0; i < X; i++)
    {
        for (int j = 0; j < Y; j++)
        {
            if (board[i][j] == c)
            {
                arr[i][j] = 1;
            }
        }
    }

    //校准行列数
    x--;
    y--;

    for (int i = 1; i < NUM; i++)
        if (x - i >= 0 && arr[x - i][y] == 1) way[0]++;
    for (int i = 1; i < NUM; i++)
        if (x + i < X && arr[x + i][y] == 1) way[0]++;
    for (int i = 1; i < NUM; i++)
        if (y - i >= 0 && arr[x][y - i] == 1) way[1]++;
    for (int i = 1; i < NUM; i++)
        if (y + i < Y && arr[x][y + i] == 1) way[1]++;
    for (int i = 1; i < NUM; i++)
        if (x - i >= 0 && y - i >= 0 && arr[x - i][y - i] == 1) way[2]++;
    for (int i = 1; i < NUM; i++)
        if (x + i < X && y + i < Y && arr[x + i][y + i] == 1) way[2]++;
    for (int i = 1; i < NUM; i++)
        if (x - i >= 0 && y + i < Y && arr[x - i][y + i] == 1) way[3]++;
    for (int i = 1; i < NUM; i++)
        if (x + i < X && y - i >= 0 && arr[x + i][y - i] == 1) way[3]++;

    if (way[0] == NUM) printf(" ↑↓方向有了%d个棋子\n", way[0]);
    if (way[1] == NUM) printf(" ←→方向有了%d个棋子\n", way[1]);
    if (way[2] == NUM) printf(" ↖↘方向有了%d个棋子\n", way[2]);
    if (way[3] == NUM) printf(" ↙↗方向有了%d个棋子\n", way[3]);
    if (way[0] == NUM || way[1] == NUM || way[2] == NUM || way[3] == NUM)
    {
        printf("达成%d子连珠!\n", NUM);
        return board[x][y];
    }

    return 'a';
}

 

你可能感兴趣的:(c语言,算法,开发语言)