博客
关于我
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/

    你可能感兴趣的文章
    npm包管理深度探索:从基础到进阶全面教程!
    查看>>
    npm升级以及使用淘宝npm镜像
    查看>>
    npm发布包--所遇到的问题
    查看>>
    npm发布自己的组件UI包(详细步骤,图文并茂)
    查看>>
    npm和package.json那些不为常人所知的小秘密
    查看>>
    npm和yarn清理缓存命令
    查看>>
    npm和yarn的使用对比
    查看>>
    npm如何清空缓存并重新打包?
    查看>>
    npm学习(十一)之package-lock.json
    查看>>
    npm安装 出现 npm ERR! code ETIMEDOUT npm ERR! syscall connect npm ERR! errno ETIMEDOUT npm ERR! 解决方法
    查看>>
    npm安装crypto-js 如何安装crypto-js, python爬虫安装加解密插件 找不到模块crypto-js python报错解决丢失crypto-js模块
    查看>>
    npm安装教程
    查看>>
    npm报错Cannot find module ‘webpack‘ Require stack
    查看>>
    npm报错Failed at the node-sass@4.14.1 postinstall script
    查看>>
    npm报错fatal: Could not read from remote repository
    查看>>
    npm报错File to import not found or unreadable: @/assets/styles/global.scss.
    查看>>
    npm报错unable to access ‘https://github.com/sohee-lee7/Squire.git/‘
    查看>>
    npm淘宝镜像过期npm ERR! request to https://registry.npm.taobao.org/vuex failed, reason: certificate has ex
    查看>>
    npm版本过高问题
    查看>>
    npm的“--force“和“--legacy-peer-deps“参数
    查看>>