博客
关于我
c语言学习小结_05_扫雷小游戏和三子棋小游戏
阅读量:84 次
发布时间:2019-02-25

本文共 5091 字,大约阅读时间需要 16 分钟。

??????????????????

  • ????

    ????????????????????main.c????????????????saolei.c????????????????????????????

  • ??????main.c?

    • ????

      ?????Menu()??????????????????????

      void Menu() {    printf("************************\n");    printf("****1?Play  2 Exit*****\n");    printf("please input your select:");}
    • ????

      ????????????????????????????????

      int main() {    int quit = 0;    int select = 0;    while (!quit) {        Menu();        scanf("%d", &select);        switch (select) {            case 1: Game(); printf("????????\n"); break;            case 2: quit = 1; break;            default: printf("???????????"); break;        }    }    printf("ByeBye!\n");    system("pause");    return 0;}
    1. ???????saolei.c?
      • ?????

        ???????????????show_board?mine_board????????????

        void Game() {    char show_board[ROW][COL];    char mine_board[ROW][COL];    memset(show_board, '*', sizeof(show_board));    memset(mine_board, '0', sizeof(mine_board));    srand((unsigned long)time(NULL));    SetMines(mine_board, ROW, COL);}
      • ????

        ??SetMines??????????????????????

        void SetMines(char mine_board[ROW][COL], int row, int col) {    int count = NUMS;    while (count) {        int x = rand() % 5 + 1;        int y = rand() % 5 + 1;        if (mine_board[x][y] == '0') {            mine_board[x][y] = '1';            count--;        }    }}
      • ??????

        ???Game????????????????????????????????????

        int GetMines(char mine_board[ROW][COL], int row, int col, int x, int y) {    return mine_board[x - 1][y - 1] + mine_board[x - 1][y] + mine_board[x - 1][y + 1] +        mine_board[x][y - 1] + mine_board[x][y + 1] + mine_board[x + 1][y - 1] +        mine_board[x + 1][y] + mine_board[x + 1][y + 1] - 8 * '0';}
      • ??????

        ???????????????????count???????????????

        int count = (ROW - 2)*(COL - 2) - NUMS;int x = 0;int y = 0;do {    ShowBoard(show_board, ROW, COL);    printf("??????");    scanf("%d %d", &x, &y);    if (x < 1 || x > 10 || y < 1 || y > 10) {        printf("??????????\n");        continue;    }    if (show_board[x][y] != '*') {        printf("??????????????????\n");        continue;    }    if (mine_board[x][y] == '1') {        break;    }    num = GetMines(mine_board, ROW, COL, x, y);    show_board[x][y] = num + '0';    count--;    system("cls");} while (count > 0);
      • ????

        ???????????count?????????????????????

        if (count > 0) {    printf("??????\n");} else {    printf("???!\n");}printf("????????!\n");ShowBoard(mine_board, ROW, COL);
      1. ??????????japplet.c?
        • ?????

          ??japplet.c?????3x3???????????

          #define ROW 3#define COL 3#define PLAYER_CHESS 'X'#define COMPUTER_CHESS 'O'#define PLAYING 'P'#define DRAW 'D'
        • ??????

          ??????PlayerMove???????????

          static void PlayerMove(char board[ROW][COL], int row, int col) {    int x = 0;    int y = 0;    int quit = 0;    while (!quit) {        printf("?????????????");        scanf("%d %d", &x, &y);        if (x < 1 || x > 3 || y < 1 || y > 3) {            printf("?????????????");            continue;        }        if (board[x - 1][y - 1] != ' ') {            printf("???????????????????");            continue;        }        board[x - 1][y - 1] = PLAYER_CHESS;        quit = 1;    }}
        • ??????

          ??Judge????????????????????????

          static char Judge(char board[ROW][COL], int row, int col) {    for (int i = 0; i < row; i++) {        if (board[i][0] == board[i][1] && board[i][1] == board[i][2] && board[i][0] != ' ') {            return board[i][0];        }    }    for (int i = 0; i < col; i++) {        if (board[0][i] == board[1][i] && board[1][i] == board[2][i] && board[0][i] != ' ') {            return board[0][i];        }    }    if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[0][0] != ' ') {        return board[0][0];    }    if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[1][1] != ' ') {        return board[0][2];    }    for (int i = 0; i < row; i++) {        for (int j = 0; j < col; j++) {            if (board[i][j] == ' ') {                return PLAYING;            }        }    }    return DRAW;}
        • ???????

          ?????ComputerMove??????????????????

          static void ComputerMove(char board[ROW][COL], int row, int col) {    while (1) {        int x = rand() % row;        int y = rand() % col;        if (board[x][y] == ' ') {            board[x][y] = COMPUTER_CHESS;            break;        }    }}
        • ??????

          ?Game????????result??????????????????

          void Game() {    srand((unsigned long)time(NULL));    char board[ROW][COL];    memset(board, ' ', sizeof(board));    char result = 'x';    do {        ShowBoard(board, ROW, COL);        PlayerMove(board, ROW, COL);        result = Judge(board, ROW, COL);        if (result != PLAYING) {            break;        }        ComputerMove(board, ROW, COL);        result = Judge(board, ROW, COL);        if (result != PLAYING) {            break;        }    } while (1);    if (PLAYER_CHESS == result) {        printf("????????\n");    } else if (COMPUTER_CHESS == result) {        printf("??????!\n");    } else {        printf("???\n");    }    printf("????????????????????????\n");    ShowBoard(board, ROW, COL);}
        1. ??????
          ???????????????????????????????????????????????????????????????????????

    转载地址:http://rfp.baihongyu.com/

    你可能感兴趣的文章
    OAuth2.0_环境介绍_授权服务和资源服务_Spring Security OAuth2.0认证授权---springcloud工作笔记138
    查看>>
    OAuth2.0_环境搭建_Spring Security OAuth2.0认证授权---springcloud工作笔记139
    查看>>
    oauth2.0协议介绍,核心概念和角色,工作流程,概念和用途
    查看>>
    OAuth2授权码模式详细流程(一)——站在OAuth2设计者的角度来理解code
    查看>>
    OAuth2:项目演示-模拟微信授权登录京东
    查看>>
    OA系统多少钱?OA办公系统中的价格选型
    查看>>
    OA系统选型:选择好的工作流引擎
    查看>>
    OA让企业业务流程管理科学有“据”
    查看>>
    OA项目之我的会议(会议排座&送审)
    查看>>
    OA项目之我的会议(查询)
    查看>>
    Object c将一个double值转换为时间格式
    查看>>
    object detection之Win10配置
    查看>>
    object detection训练自己数据
    查看>>
    object detection错误Message type "object_detection.protos.SsdFeatureExtractor" has no field named "bat
    查看>>
    object detection错误之Could not create cudnn handle: CUDNN_STATUS_INTERNAL_ERROR
    查看>>
    object detection错误之no module named nets
    查看>>
    Object of type 'ndarray' is not JSON serializable
    查看>>
    Object Oriented Programming in JavaScript
    查看>>
    object references an unsaved transient instance - save the transient instance before flushing
    查看>>
    Object.keys()的详解和用法
    查看>>