C語言數組應用實現掃雷游戲
更新時間:2022年06月07日 16:56:38 作者:xxzaa
這篇文章主要為大家詳細介紹了C語言數組應用實現掃雷游戲,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了C語言數組應用實現掃雷游戲的具體代碼,供大家參考,具體內容如下
掃雷游戲(10×10的面板):
1.定義兩個12×12的字符型數組;
2.show_board[][]:初始化全為*
mine_board[][]:隨機賦值字符’0’或’1’
3.統(tǒng)計雷的個數:周圍8個位置的值累加-8×’0’
代碼:
game.h
#ifndef _GAME_H_ #define _GAME_H_ #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(); #endif
game.c
#include "game.h"
void Menu()
{
?? ?printf("########################\n");
?? ?printf("#####1.Play ?2.Exit#####\n");
?? ?printf("########################\n");
?? ?printf("請輸入:");
}
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("請輸入坐標:");
?? ??? ?scanf("%d %d", &x, &y);
?? ??? ?if (x<1 || x>ROW - 2 || y<1 || y>COL - 2){
?? ??? ??? ?printf("輸入位置越界,請重新輸入!\n");
?? ??? ??? ?continue;
?? ??? ?}
?? ??? ?if (show_board[x][y] != '*'){
?? ??? ??? ?printf("該位置已被排除,請重新輸入!\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"
int main()
{
?? ?int quit = 0;
?? ?int select = 0;
?? ?while (!quit){
?? ??? ?Menu();
?? ??? ?scanf("%d", &select);
?? ??? ?switch (select){
?? ??? ?case 1:
?? ??? ??? ?Game();
?? ??? ??? ?break;
?? ??? ?case 2:
?? ??? ??? ?quit = 1;
?? ??? ??? ?break;
?? ??? ?default:
?? ??? ??? ?printf("輸入有誤,請重新輸入!\n ");
?? ??? ??? ?break;
?? ??? ?}
?? ?}
?? ?printf("ByeBye!\n");
?? ?system("pause");
?? ?return 0;
}運行結果


以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
C++實現LeetCode(80.有序數組中去除重復項之二)
這篇文章主要介紹了C++實現LeetCode(80.有序數組中去除重復項之二),本篇文章通過簡要的案例,講解了該項技術的了解與使用,以下就是詳細內容,需要的朋友可以參考下2021-07-07

