五子棋(简易人机对战)

五子棋基本要求:棋盘大小,玩家下棋,人机下棋,判断输赢,判断平局。

一.头文件

#include
#include
#include

二.棋盘大小(宏定义确定棋盘大小)

这里棋盘的实现比较低级,请多多包含。

#define ROW 10
#define COL 10

void printboard(char board[ROW][COL])
{
    int row = 0;
    int s = 0;
    printf("\t  1   2   3   4   5   6   7   8   9   10\n");
    printf("\t+---+---+---+---+---+---+---+---+---+---+\n");
    for (row=0,s= 0; row < ROW; row++,s++)
    {
        printf("%7d\t| %c | %c | %c | %c | %c | %c | %c | %c | %c | %c |\n",s+1, board[row][0], board[row][1], board[row][2],board[row][3],board[row][4],board[row][5],board[row][6],board[row][7],board[row][8],board[row][9] );
        printf("\t+---+---+---+---+---+---+---+---+---+---+\n");
    }
}

初始化棋盘(数组里的位置里的数都化为0)

void csh(char board[ROW][COL])
{
    int row = 0, col = 0;
    for (row = 0; row < ROW; row++)
    {
        for (col = 0; col < COL; col++)
        { 
            board[row][col] = ' ';
        }
    }
}

三.主函数及游戏菜单

int main()
{
    int input = 0;
    srand((unsigned int)time(0));
    do 
    {
        printf("\n");
        printf("****************************\n");
        printf("****** 1.play  0.exit ******\n");
        printf("****************************\n");

        printf("please input a number:>");
        scanf("%d", &input);
        switch (input)
        {
        case 1:
            printf("\n");
            game();
            input=0;
            break;
        case 0:
            printf("exit!\n");
            break;
        default:
            printf("input error!\n");
            break;
        }
    } while (input);
    getch();
}

四.game函数

void game()
{
    char ret = 0;
    char board[ROW][COL] = { 0 };
    csh(board);
    printboard(board);
    while (1)
    {
        playermove(board,ROW,COL);
        system("cls");
        printboard(board);
        ret = whowin(board, ROW, COL);
        if (ret != 'c')
        {
            break;
        }
        computermove(board, ROW, COL);
        system("cls");
        printboard(board);
        ret = whowin(board, ROW, COL);
        if (ret != 'c')
        {
            break;
        }
    }
    if (ret == 'o')
    {
        printf("player win\n");
    }
    else if (ret == 'x')
    {
        printf("computer win\n");
    }
    else
    {
        printf("draw\n");
    }
    
}

五.玩家下棋(使用o图案)

void playermove(char board[ROW][COL],int row,int col)
{
    int x = 0;
    int y = 0;
    printf("player go:>\n");
    while (1)
    {
        printf("\n");
        printf("please input a coord:>");
        scanf("%d %d", &x, &y);
        if (x >= 1 && x <= row && y >= 1 && y <= col)
        {
            if (board[x - 1][y - 1] == ' ')
            {
                board[x - 1][y - 1] = 'o';
                break;
            }
            else
            {
                printf("already have\n");
            }
        }
        else
        {
            printf("input error!\n");
        }
    }
}

六.人机随机下棋(生成随机值为坐标)

void computermove(char board[ROW][COL], int row, int col)
{
    int x = 0, y = 0;
    printf("computermove:>\n");
    while (1)
    {
        x = rand() % row;
        y = rand() % col;
        if (board[x][y] == ' ')
        {
            board[x][y] = 'x';
            break;
        }
    }
}

七.判断胜负

char whowin(char board[ROW][COL], int row, int col)
{
    int i = 0;
    int j = 0;

    for (i = 0; i < row; i++)
    {
        for (j = 0; j <= col - 4; j++)
        {
            if (board[i][j]==board[i][j+1] 
                &&board[i][j+1]==board[i][j+2]
                &&board[i][j+2]==board[i][j+3]
                &&board[i][j+3]==board[i][j+4]
                &&board[i][j]!=' ')
            {
                return board[i][j];
            }
        }
    }
    for (j = 0; j < col; j++)
    {
        for (i = 0; i <= row - 4; i++)
        {
            if (board[i][j]== board[i+1][j]
                && board[i+1][j]== board[i+2][j]
                && board[i+2][j]== board[i+3][j]
                && board[i+3][j]== board[i+4][j]
                &&board[i][j]!= ' ')
            {
                return board[i][j];
            }
        }
    }

    for (i = 0; i < row - 4; i++)
    {
        if (board[i][i] == board[i + 1][i + 1]
            && board[i + 1][i + 1] == board[i + 2][i + 2] 
            && board[i + 2][i + 2] == board[i + 3][i + 3] 
            && board[i + 3][i + 3]==board[i+4][i+4]
            &&board[i][i]!=' ')
        {
            return board[i][i];
        }
        if (board[i][i+4] == board[i+1][i+3] 
            && board[i+1][i+3] == board[i+2][i+2] 
            && board[i+2][i+2] == board[i+3][i+1]
            &&board[i+3][i+1]==board[i+4][i]
            &&board[i][i+4]!=' ')
        {
            return board[i][i+4];
        }
    }

    if (draw(board, ROW, COL) == 1)
    {
        return 'q';
    }
    return 'c';
}

八.平局

int draw(char board[ROW][COL], int row, int col)
{
    int i = 0, j = 0;
    for (i = 0; i < row; i++)
        for (j = 0; j < col; j++)
        {
            if (board[i][j] == ' ')
            {
                return 0;
            }
        }
    return 1;
}

完整代码

#define ROW 10
#define COL 10
#include//改表格换多少行多少列;玩家下棋图案,电脑下棋图案;设置成双玩家;
#include
#include

void csh(char board[ROW][COL])
{
    int row = 0, col = 0;
    for (row = 0; row < ROW; row++)
    {
        for (col = 0; col < COL; col++)
        { 
            board[row][col] = ' ';
        }
    }
}

void printboard(char board[ROW][COL])
{
    int row = 0;
    int s = 0;
    printf("\t  1   2   3   4   5   6   7   8   9   10\n");
    printf("\t+---+---+---+---+---+---+---+---+---+---+\n");
    for (row=0,s= 0; row < ROW; row++,s++)
    {
        printf("%7d\t| %c | %c | %c | %c | %c | %c | %c | %c | %c | %c |\n",s+1, board[row][0], board[row][1], board[row][2],board[row][3],board[row][4],board[row][5],board[row][6],board[row][7],board[row][8],board[row][9] );
        printf("\t+---+---+---+---+---+---+---+---+---+---+\n");
    }
    
}

void playermove(char board[ROW][COL],int row,int col)
{
    int x = 0;
    int y = 0;
    printf("player go:>\n");
    while (1)
    {
        printf("\n");
        printf("please input a coord:>");
        scanf("%d %d", &x, &y);
        if (x >= 1 && x <= row && y >= 1 && y <= col)
        {
            if (board[x - 1][y - 1] == ' ')
            {
                board[x - 1][y - 1] = 'o';
                break;
            }
            else
            {
                printf("already have\n");
            }
        }
        else
        {
            printf("input error!\n");
        }
    }
}

void computermove(char board[ROW][COL], int row, int col)
{
    int x = 0, y = 0;
    printf("computermove:>\n");
    while (1)
    {
        x = rand() % row;
        y = rand() % col;
        if (board[x][y] == ' ')
        {
            board[x][y] = 'x';
            break;
        }
    }
}

int draw(char board[ROW][COL], int row, int col)
{
    int i = 0, j = 0;
    for (i = 0; i < row; i++)
        for (j = 0; j < col; j++)
        {
            if (board[i][j] == ' ')
            {
                return 0;
            }
        }
    return 1;
}

char whowin(char board[ROW][COL], int row, int col)
{
    int i = 0;
    int j = 0;

    for (i = 0; i < row; i++)
    {
        for (j = 0; j <= col - 4; j++)
        {
            if (board[i][j]==board[i][j+1] 
                &&board[i][j+1]==board[i][j+2]
                &&board[i][j+2]==board[i][j+3]
                &&board[i][j+3]==board[i][j+4]
                &&board[i][j]!=' ')
            {
                return board[i][j];
            }
        }
    }
    for (j = 0; j < col; j++)
    {
        for (i = 0; i <= row - 4; i++)
        {
            if (board[i][j]== board[i+1][j]
                && board[i+1][j]== board[i+2][j]
                && board[i+2][j]== board[i+3][j]
                && board[i+3][j]== board[i+4][j]
                &&board[i][j]!= ' ')
            {
                return board[i][j];
            }
        }
    }

    for (i = 0; i < row - 4; i++)
    {
        if (board[i][i] == board[i + 1][i + 1]
            && board[i + 1][i + 1] == board[i + 2][i + 2] 
            && board[i + 2][i + 2] == board[i + 3][i + 3] 
            && board[i + 3][i + 3]==board[i+4][i+4]
            &&board[i][i]!=' ')
        {
            return board[i][i];
        }
        if (board[i][i+4] == board[i+1][i+3] 
            && board[i+1][i+3] == board[i+2][i+2] 
            && board[i+2][i+2] == board[i+3][i+1]
            &&board[i+3][i+1]==board[i+4][i]
            &&board[i][i+4]!=' ')
        {
            return board[i][i+4];
        }
    }

    if (draw(board, ROW, COL) == 1)
    {
        return 'q';
    }
    return 'c';
}

void game()
{
    char ret = 0;
    char board[ROW][COL] = { 0 };
    csh(board);
    printboard(board);
    while (1)
    {
        playermove(board,ROW,COL);
        system("cls");
        printboard(board);
        ret = whowin(board, ROW, COL);
        if (ret != 'c')
        {
            break;
        }
        computermove(board, ROW, COL);
        system("cls");
        printboard(board);
        ret = whowin(board, ROW, COL);
        if (ret != 'c')
        {
            break;
        }
    }
    if (ret == 'o')
    {
        printf("player win\n");
    }
    else if (ret == 'x')
    {
        printf("computer win\n");
    }
    else
    {
        printf("draw\n");
    }
    
}

int main()
{
    int input = 0;
    srand((unsigned int)time(0));
    do 
    {
        printf("\n");
        printf("****************************\n");
        printf("****** 1.play  0.exit ******\n");
        printf("****************************\n");

        printf("please input a number:>");
        scanf("%d", &input);
        switch (input)
        {
        case 1:
            printf("\n");
            game();
            input=0;
            break;
        case 0:
            printf("exit!\n");
            break;
        default:
            printf("input error!\n");
            break;
        }
    } while (input);
    getch();
}
五子棋(简易人机对战)_第1张图片

你可能感兴趣的:(小游戏,c语言,游戏)