三子棋游戏
一、分析

1.创建一个进入游戏让玩家选择的框架
2.创建一个三子棋的棋盘,棋盘内部存放玩家和电脑下的棋子,所以总的来说棋盘是由一个二维数组和棋盘框架构成的
3.对棋盘进行操作
4.判断棋局并做出调整

二、代码

game.h

#define _CRT_SECURE_NO_WARNINGS 1#include #include <string.h>#include #include #define ROW 3#define COL 3//初始化数组void Init(char board[ROW][COL], int row, int col);//打印棋盘void Display(char board[ROW][COL], int row, int col);//玩家下棋void PlayerMove(char board[ROW][COL], int row, int col);//电脑下棋void ComputerMove(char board[ROW][COL], int row, int col);//判断输赢char Is_Win(char board[ROW][COL], int row, int col);

game.c

#define _CRT_SECURE_NO_WARNINGS 1#include "game.h"void Init(char board[ROW][COL], int row, int col){    for (int i = 0; i < row; i++)    {        for (int j = 0; j < col; j++)        {            board[i][j] = ' ';        }    }}//期待打印的棋盘//   |   |   //---|---|---//   |   |   //---|---|---//   |   |   void Display(char board[ROW][COL], int row, int col){    for (int i = 0; i < row; i++)    {        //打印i行        for (int j = 0; j < col; j++)        {            printf(" %c ", board[i][j]);            if (j < col - 1)                printf("|");        }        printf("\n");        if (i < row - 1)        {            for (int j = 0; j < col; j++)            {                printf("---");                if (j < col - 1)                    printf("|");            }            printf("\n");        }    }}//玩家下棋void PlayerMove(char board[ROW][COL], int row, int col){    int x, y;    while (1)    {        printf("Player Move:>");        scanf("%d%d", &x, &y);        if ((x >= 1 && x = 1 && y <= col))        {            if (board[x - 1][y - 1] == ' ')            {                board[x - 1][y - 1] = '*';                break;//退出循环            }            else            {                printf("Location Error\n");            }        }        else        {            printf("Location Error\n");        }    }}//电脑下棋void ComputerMove(char board[ROW][COL], int row, int col){    int x, y;    //随机生成一个x,y的位置    printf("Computer Move\n");    while (1)    {        x = rand() % row;        y = rand() % col;        if (board[x][y] == ' ')        {            board[x][y] = '#';            break;        }    }}int Is_Full(char board[ROW][COL], int row, int col){    int flag = 1;    for (int i = 0; i < row; i++)    {        for (int j = 0; j < col; j++)        {            if (board[i][j] == ' ')            {                flag = 0;                return flag;            }        }    }    return flag;}//判断输赢//如果返回‘*’代表玩家赢//‘#’代表电脑赢//‘D’代表平局//‘C’代表可以继续char Is_Win(char board[ROW][COL], int row, int col){    //玩家赢,电脑赢    //行三个子,列三个子,斜三个子    //    for (int i = 0; i < row; i++)    {        for (int j = 0; j <= col - 3; j++)        {            if ((board[i][j] == board[i][j + 1] )&&( board[i][j + 1] == board[i][j + 2]) &&( board[i][j] != ' '))            {                return board[i][j];            }        }    }    //    for (int j = 0; j < col; j++)    {        for (int i = 0; i <= row - 3; i++)        {            if ((board[i][j] == board[i + 1][j] )&&( board[i+1][j] == board[i + 2][j]) &&( board[i][j] != ' '))            {                return board[i][j];            }        }    }    //正斜    for (int i = 0; i <= row - 3; i++)    {        for (int j = 0; j <= col - 3; j++)        {            if ((board[i][j] == board[i + 1][j+1]) && (board[i + 1][j+1] == board[i + 2][j+2]) && (board[i][j] != ' '))            {                return board[i][j];            }        }    }    //反斜    for (int i = row - 1; i >= 2; i--)    {        for (int j = col - 1; j >= 2; j--)        {            if ((board[i][j-2] == board[i - 1][j - 1]) && (board[i - 1][j - 1] == board[i - 2][j ]) && (board[i-1][j-1] != ' '))            {                return board[i-1][j-1];            }        }    }    //平局    if (Is_Full(board,row,col) == 1)    {        return 'D';    }    else    {        return 'C';    }    //继续}

test.c

#include "game.h"void menu(){    printf("                       \n");    printf("   1.Play     0.Exit   \n");    printf("                       \n");}void game(){    char board[ROW][COL];    //初始化数组    Init(board, ROW, COL);    //打印棋盘    Display(board, ROW, COL);    //开始下棋,是一个循环事件    char ret;    while (1)    {        //玩家下棋        PlayerMove(board,ROW,COL);        Display(board, ROW, COL);        //判断棋局形势        ret=Is_Win(board, ROW, COL);        //printf("%c\n", ret);//用于测试        if (ret != 'C')        {            break;        }        //电脑下子        ComputerMove(board, ROW, COL);        Display(board, ROW, COL);        ret=Is_Win(board, ROW, COL);        //printf("%c\n", ret);//用于测试        if (ret != 'C')        {            break;        }    }    if (ret == '*')        printf("Player Win\n");    else if (ret == '#')        printf("Computer Win\n");    else if (ret == 'D')        printf("Draw\n");}int main(){    //初始化随机数    srand((unsigned int)time(NULL));    //创建一个初始界面    int input;    do    {        menu();        printf("Input Num:>");        scanf("%d", &input);        switch (input)        {        case 1:            //进入游戏            game();            break;        case 0:            printf("Exit\n");            break;        default:            printf("Error Input\n");            break;        }    } while (input);    return 0;}

三、运行结果