欧美大屁股bbbbxxxx,狼人大香伊蕉国产www亚洲,男ji大巴进入女人的视频小说,男人把ji大巴放进女人免费视频,免费情侣作爱视频

歡迎來到入門教程網(wǎng)!

C語言

當(dāng)前位置:主頁 > 軟件編程 > C語言 >

C語言簡易掃雷游戲

來源:本站原創(chuàng)|時間:2020-01-10|欄目:C語言|點擊: 次

本文實例為大家分享了C語言掃雷游戲的具體代碼,供大家參考,具體內(nèi)容如下

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define MAX_ROW 9
#define MAX_COL 9
#define MINE_C0UNT 10
void menu() {
 printf("************************\n");
 printf("*****   1.play  ****\n");
 printf("*****   0.exit  ****\n");
 printf("************************\n");
}
//1、先初始化兩個地圖,玩家看到的,地雷布局圖。
void Init(char show_map[MAX_ROW][MAX_COL],char mine_map[MAX_ROW][MAX_COL]) {
 //對于玩家看到的地圖,未翻到的都設(shè)為*;
 for (int row = 0; row < MAX_ROW; row++) {
 for (int col = 0; col < MAX_COL; col++) {
  show_map[row][col] = '*';
 }
 }
 //對于地雷布局圖,用0表示沒有地雷,用1表示雷。
 for (int row = 0; row < MAX_ROW; row++) {
 for (int col = 0; col < MAX_COL; col++) {
  mine_map[row][col] = '0';
 }
 }
 //假設(shè)設(shè)置十個地雷
 int n = MINE_C0UNT;
 while (n > 0) {
 int row = rand() % MAX_ROW;
 int col = rand() % MAX_COL;
 if (mine_map == '1') {
  continue;
 }
 mine_map[row][col] = '1';
 --n;
 }
}
void printmap(char map[MAX_ROW][MAX_COL]) {
 //不光能打印出地圖,還能帶坐標(biāo)
//先打印第一行
 printf("  ");
 for (int i = 0; i < MAX_COL; i++) {
 printf("%d ", i);
 }
 printf("\n");
 //打印一個分割線
 for (int col = 0; col < MAX_COL - 2; ++col) {
 printf("---");
 }
 printf("\n");
 //在打印其他行
 for (int row = 0; row < MAX_ROW; row++) {
 printf(" %d| ", row);
 //打印本行的每一列
 for (int col = 0; col < MAX_COL; col++) {
  printf("%c ", map[row][col]);
 }
 printf("\n");
 }
}
void updateshowmap(int row,int col,char show_map[MAX_ROW][MAX_COL], char mine_map[MAX_ROW][MAX_COL]) {
 int count = 0;
 if (row - 1 >= 0 && col - 1 >= 0 && row - 1 < MAX_ROW && col - 1 < MAX_COL && mine_map[row - 1][col - 1] == '1') {
 count++;
 }
 if (row - 1 >= 0 && col >= 0 && row - 1 < MAX_ROW && col < MAX_COL && mine_map[row - 1][col] == '1') {
 count++;
 }
 if (row - 1 >= 0 && col + 1 >= 0 && row - 1 < MAX_ROW && col + 1 < MAX_COL && mine_map[row - 1][col + 1] == '1') {
 count++;
 }
 if (row >= 0 && col - 1 >= 0 && row < MAX_ROW && col - 1 < MAX_COL && mine_map[row][col - 1] == '1') {
 count++;
 }
 if (row >= 0 && col + 1 >= 0 && row < MAX_ROW && col + 1 < MAX_COL && mine_map[row][col + 1] == '1') {
 count++;
 }
 if (row + 1 >= 0 && col - 1 >= 0 && row + 1 < MAX_ROW && col - 1 < MAX_COL && mine_map[row + 1][col - 1] == '1') {
 count++;
 }
 if (row + 1 >= 0 && col + 1 >= 0 && row + 1 < MAX_ROW && col + 1 < MAX_COL && mine_map[row + 1][col + 1] == '1') {
 count++;
 }
 show_map[row][col] = '0' + count;
}
void game() {
 char show_map[MAX_ROW][MAX_COL];
 char mine_map[MAX_ROW][MAX_COL];
 Init(show_map,mine_map);
 while (1) {
 printmap(show_map);
 printf("請玩家輸入一組坐標(biāo):");
 int row, col;
 int blank_count_already_show = 0;
 scanf("%d%d", &row, &col);
 system("cls");
 if (row < 0 || row >= MAX_ROW || col < 0 || col >= MAX_COL) {
  printf("您的輸入不合法,請您重新輸入!\n");
  continue;
 }
 if (show_map[row][col] != '*') {
  printf("您輸入的位置已經(jīng)被占用了,請您重新輸入!\n");
  continue;
 }
 //判斷玩家輸入的坐標(biāo)對應(yīng)的是不是地雷,如果是地雷則游戲就結(jié)束了
 if (mine_map[row][col] == '1') {
  printf("游戲結(jié)束!\n");
  printmap(mine_map);
  break;
 }
 //判斷游戲是否勝利,通過計算已翻開的非雷的格子的個數(shù)
 ++blank_count_already_show;
 if (blank_count_already_show == MAX_ROW * MAX_COL - MINE_C0UNT) {
  printf("游戲勝利 !\n");
  printmap(mine_map);
  break;
 }
 //統(tǒng)計當(dāng)前位置中周圍雷的個數(shù)
 updateshowmap(row, col, show_map, mine_map);
 }
}
int main() {
 srand((unsigned)time(0));
 int input = 0;
 while (1) {
 menu();
 printf("請選擇:");
 scanf("%d", &input);
 if (input == 1) {
  printf("開始游戲!\n");
  game();
 }
 else if (input == 0) {
  printf("退出游戲!\n");
  break;
 }
 else {
  printf("輸入錯誤,請重新輸入!\n");
  continue;
 }
 }
 system("pause");
 return 0;
}

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持我們。

上一篇:C語言實現(xiàn)按行讀寫文件

欄    目:C語言

下一篇:C語言實現(xiàn)萬年歷效果

本文標(biāo)題:C語言簡易掃雷游戲

本文地址:http://mengdiqiu.com.cn/a1/Cyuyan/137.html

網(wǎng)頁制作CMS教程網(wǎng)絡(luò)編程軟件編程腳本語言數(shù)據(jù)庫服務(wù)器

如果侵犯了您的權(quán)利,請與我們聯(lián)系,我們將在24小時內(nèi)進行處理、任何非本站因素導(dǎo)致的法律后果,本站均不負任何責(zé)任。

聯(lián)系QQ:835971066 | 郵箱:835971066#qq.com(#換成@)

Copyright © 2002-2020 腳本教程網(wǎng) 版權(quán)所有