C語言實(shí)現(xiàn)掃雷游戲簡易版
本文實(shí)例為大家分享了C語言實(shí)現(xiàn)掃雷游戲的簡易版,供大家參考,具體內(nèi)容如下
game.h
#pragma once #include <stdio.h> #include <string.h> #include <time.h> #include <windows.h> #define ROW 12 #define COL 12 #define NUMS 20 #pragma warning(disable:4996) void Menu(); void Game();
game.c
#include "game.h" void Menu() { printf("###########################\n"); printf("## 1.Play 2. Exit ##\n"); printf("###########################\n"); printf("請(qǐng)輸入# "); } void SetMines(char board[][COL], int row, int col) { int num = NUMS; while (num) { int x = rand() % 10 + 1; int y = rand() % 10 + 1; if (board[x][y] == '0') { board[x][y] = '1'; num--; } } } int GetNums(char board[][COL], int row, int col, int x, int y) { return board[x - 1][y - 1] + board[x - 1][y] + \ board[x - 1][y + 1] + board[x][y + 1] + \ board[x + 1][y + 1] + board[x + 1][y] + \ board[x + 1][y - 1] + board[x][y - 1] - 8 * '0'; } void ShowBoard(char board[][COL], int row, int col) { printf(" "); for (int i = 1; i < col - 1; i++) { printf(" %2d ", i); } printf("\n"); printf("-------------------------------------------\n"); for (int i = 1; i < row - 1; i++) { printf("%2d|", i); for (int j = 1; j < col - 1; j++) { printf(" %c |", board[i][j]); } printf("\n"); printf("-------------------------------------------\n"); } } void Game() { system("cls"); srand((unsigned long)time(NULL)); char show_board[ROW][COL]; char mine_board[ROW][COL]; memset(show_board, '*', sizeof(show_board)); memset(mine_board, '0', sizeof(mine_board)); SetMines(mine_board, ROW, COL); int count = (ROW - 2) * (COL - 2) - NUMS; int x = 0; int y = 0; do { ShowBoard(show_board, ROW, COL); printf("請(qǐng)輸入坐標(biāo)# "); scanf("%d %d", &x, &y); if (x < 1 || x > ROW - 2 || y < 1 || y > COL - 2) { printf("輸入位置越界,請(qǐng)重新輸入!\n"); continue; } if (show_board[x][y] != '*') { printf("該位置已經(jīng)被排除!\n"); continue; } if (mine_board[x][y] == '1') { break; } int num = GetNums(mine_board, ROW, COL, x, y); show_board[x][y] = num + '0'; count--; system("cls"); } while (count > 0); if (count > 0) { printf("你被炸死了!\n"); ShowBoard(mine_board, ROW, COL); } else { printf("恭喜,你通過游戲!\n"); } }
main.c
#include "game.h" void Menu() { printf("###########################\n"); printf("## 1.Play 2. Exit ##\n"); printf("###########################\n"); printf("請(qǐng)輸入# "); } void SetMines(char board[][COL], int row, int col) { int num = NUMS; while (num) { int x = rand() % 10 + 1; int y = rand() % 10 + 1; if (board[x][y] == '0') { board[x][y] = '1'; num--; } } } int GetNums(char board[][COL], int row, int col, int x, int y) { return board[x - 1][y - 1] + board[x - 1][y] + \ board[x - 1][y + 1] + board[x][y + 1] + \ board[x + 1][y + 1] + board[x + 1][y] + \ board[x + 1][y - 1] + board[x][y - 1] - 8 * '0'; } void ShowBoard(char board[][COL], int row, int col) { printf(" "); for (int i = 1; i < col - 1; i++) { printf(" %2d ", i); } printf("\n"); printf("-------------------------------------------\n"); for (int i = 1; i < row - 1; i++) { printf("%2d|", i); for (int j = 1; j < col - 1; j++) { printf(" %c |", board[i][j]); } printf("\n"); printf("-------------------------------------------\n"); } } void Game() { system("cls"); srand((unsigned long)time(NULL)); char show_board[ROW][COL]; char mine_board[ROW][COL]; memset(show_board, '*', sizeof(show_board)); memset(mine_board, '0', sizeof(mine_board)); SetMines(mine_board, ROW, COL); int count = (ROW - 2) * (COL - 2) - NUMS; int x = 0; int y = 0; do { ShowBoard(show_board, ROW, COL); printf("請(qǐng)輸入坐標(biāo)# "); scanf("%d %d", &x, &y); if (x < 1 || x > ROW - 2 || y < 1 || y > COL - 2) { printf("輸入位置越界,請(qǐng)重新輸入!\n"); continue; } if (show_board[x][y] != '*') { printf("該位置已經(jīng)被排除!\n"); continue; } if (mine_board[x][y] == '1') { break; } int num = GetNums(mine_board, ROW, COL, x, y); show_board[x][y] = num + '0'; count--; system("cls"); } while (count > 0); if (count > 0) { printf("你被炸死了!\n"); ShowBoard(mine_board, ROW, COL); } else { printf("恭喜,你通過游戲!\n"); } }
更多有趣的經(jīng)典小游戲?qū)崿F(xiàn)專題,分享給大家:
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C++計(jì)算整數(shù)序列的最長遞增子序列的長度操作
這篇文章主要介紹了C++計(jì)算整數(shù)序列的最長遞增子序列的長度操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-12-12C++實(shí)現(xiàn)查找中位數(shù)的O(N)算法和Kmin算法
這篇文章主要介紹了C++實(shí)現(xiàn)查找中位數(shù)的O(N)算法和Kmin算法,對(duì)于C++程序算法設(shè)計(jì)有一定的借鑒價(jià)值,需要的朋友可以參考下2014-09-09C語言中typedef的用法以及#define區(qū)別詳解
這篇文章主要給大家介紹了關(guān)于C語言中typedef用法以及#define區(qū)別的相關(guān)資料,typedef 是用來定義一種類型的新別名的,它不同于宏(#define),不是簡單的字符串替換。而#define只是簡單的字符串替換(原地?cái)U(kuò)展),需要的朋友可以參考下2021-07-07C語言實(shí)現(xiàn)隨機(jī)讀寫文件的函數(shù)詳解
文件的隨機(jī)讀寫,可以在文件中指定的任意位置讀或者寫。這篇文章主要為大家詳細(xì)介紹了C語言實(shí)現(xiàn)隨機(jī)讀寫文件的3個(gè)函數(shù),感興趣的可以了解一下2023-03-03C++迭代器介紹(iterator、const_iterator、reverse_interator、const_rev
這篇文章主要介紹了C++迭代器介紹(iterator、const_iterator、reverse_interator、const_reverse_interator),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-02-02