世界视讯!程序:三子棋游戏

2023-01-06 18:31:24 来源:51CTO博客


(相关资料图)

头文件game.h

#define _CRT_SECURE_NO_WARNINGS//头文件#include#include #include //定义行列#define ROW 3#define COL 3//函数声明void menu();void InitBoard(char board[ROW][COL], int row, int col);void DisplayBoard(char board[ROW][COL], int row, int col);void player(char board[ROW][COL], int row, int col);void computer(char board[ROW][COL], int row, int col);int is_win(char board[ROW][COL], int row, int col);

游戏的实现game.c

#include"game.h"void menu(){     printf("*******************\n");    printf("******1.start******\n");    printf("*******************\n");    printf("******0.exit*******\n");    printf("*******************\n");    printf("请输入你选择的游戏模式\n");}void InitBoard(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; j++)        {            board[i][j] = " ";        }    }}void DisplayBoard(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; j++)        {            printf(" %c ", board[i][j]);            if (j < col - 1)            {                printf("|");            }        }        printf("\n");        if (i < row - 1)        {            for (j = 0; j < col; j++)            {                printf("---");                if (j < col - 1)                {                    printf("|");                }            }            printf("\n");        }    }}void player(char board[ROW][COL], int row, int col)//玩家下棋{    int x = 0;    int y = 0;    printf("选择你要下的坐标\n");    while (1)    {        loop:            scanf("%d%d", &x, &y);            printf("\n");            if (x >= 1 && x <= 3 && y >= 1 && y <= 3)            {                if (board[x - 1][y - 1] == " ")                {                    board[x - 1][y - 1] = "*";                    break;                }                else                {                    printf("输入的坐标被占用,请重新输入\n");                    goto loop;                }            }            else            {                printf("输入的坐标有误,请重新输入\n");            }    }}void computer(char board[ROW][COL], int row, int col)//电脑下棋{    while (1)    {        int x = rand() % row;        int y = rand() % col;//只输出0 1 2        if (board[x][y] == " ")        {            board[x][y] = "#";            break;        }    }}int is_win(char board[ROW][COL], int row, int col)//判断输赢{    int i = 0;    int j = 0;    for (i = 0; i < row; i++)//判断按行3个获胜    {        if (board[i][0] == board[i][1] && board[i][0] == board[i][2] && board[i][0] != " ")        {            if (board[i][0] == "*")            {                printf("玩家获胜\n");                return 2;            }            else if (board[i][0] == "#")            {                printf("电脑获胜\n");                return 1;            }        }    }    for (j = 0; j < col; j++)//判断按列3个获胜    {        if (board[0][j] == board[1][j] && board[0][j] == board[2][j] && board[0][j] != " ")        {            if (board[i][0] == "*")            {                printf("玩家获胜\n");                return 2;            }            else if (board[i][0] == "#")            {                printf("电脑获胜\n");                return 1;            }        }    }    if (((board[0][0] == board[1][1] && board[0][0] == board[2][2]) || (board[0][2] == board[1][1] && board[0][2] == board[2][0]))&&board[1][1]!=" ")    {//判断按对角线获胜        if (board[1][1] == "*")        {            printf("玩家获胜\n");            return 2;        }        else        {            printf("电脑获胜\n");            return 1;        }    }    int ret2=is_full(board, ROW, COL);//判断是否平局    if (ret2 == 1)    {        printf("平局了\n");        return 0;    }    else    {        return 3;    }}int is_full(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; j++)        {            if (board[i][j] == " ")            {                return 0;//没下满            }        }    }    return 1;//下满了}

游戏的测试的主程序

#include"game.h"void game(){  char board[ROW][COL] = { 0 };  InitBoard(board, ROW, COL);//初始化棋盘  DisplayBoard(board, ROW, COL);//打印棋盘  while (1)  {    player(board, ROW, COL);    system("cls");    int ret=is_win(board, ROW, COL);    if (ret == 2)    {      break;    }    else if (ret == 1)    {      break;    }    else if (ret == 0)    {      break;    }        computer(board, ROW, COL);    DisplayBoard(board, ROW, COL);    ret=is_win(board, ROW, COL);    if (ret == 2)    {      break;    }    else if (ret == 1)    {      break;    }    else if (ret == 0)    {      break;    }  }}void test(){  int input = 0;  do  {      menu();    scanf("%d", &input);//1.开始游戏0.退出游戏    system("cls");    switch (input)    {    case 1:      game();      break;    case 0:      printf("游戏结束退出程序");      break;    default:      printf("输入数据有误,请重新输入");      break;    }  } while (input);}int main(){  srand((unsigned int)time(NULL));  test();  return 0;}

标签: 游戏结束 游戏模式 开始游戏

上一篇:即时焦点:Linux用户及权限详解
下一篇:世界聚焦:嵌入式:ARM定时器