C语言程序设计 :扫雷TC语言程序
游戏促进学习——“扫雷”程序提高C语言学习兴趣

1 引言
识o
c 语言是计 算机课 程 中非 常重要 的一 门高级语
言 , 书 写灵 活 , 法 简 单 , 能 强 大 , 且 具 有 低 级 它 语 功 而 语言 的某 些 特点 , 计 算 机 专 业 课 的 教 学 中 , 已经 在 它 越 来 越 占有 非 常 重要 的地 位 。而 且 , 其它 非 计 算 机 在
3 1 二 维数 组存 储 雷 区 .
置 布雷 , i—e e 句判 断 当前 位 置 的值 是 否 为 1 用 f l 语 s , 如果 已布 雷 , 补 充 一 次 布 雷 机 会 , 而就 不会 使 雷 就 从 数 改 变 ( 少 ) 减 。
3 2 用递 归 函数 实现 智 能 “ 雷 ” . 扫 过程
维普资讯
20 0 2年 6月
连 云 港 师范 高 等 专科 学 校 学报
J un lo i y n a gT a h r olg o ra fLa u g n ec esC l e n e
Jn ,0 2 u e2 0
No. 2
第2 期
数 组 ( 括 多维 数 组 ) C语 言 中 的一 个 重 点 内 包 是
容, 同学 们 在学 习 这部 分 内容 时 , 于 一 维 数 组 掌 握 对 的情 况 普 遍 比较 好 , 对 于 二 维 及 以 上 的 数 组 , 少 但 不 同学对 于其 下 标 、 列 引 用 等 容 易 产 生 一 定 的混 淆 , 行 而 且 二 维 数 组 的存 放顺 序 及 与一 维 数 组 的转 换 , 对 相 也 是 一 个 难 点 。在 程序 中 , 实 现 在一 个 区域 ( 面 ) 为 平 内布 雷 , 们 必 须要 用 到 二维 数 组 , 以存 储 地 雷 , 我 用 方 法 是 若 区 域 内某 个 小方 格 中有 雷 , 设 置 值 为 1 以 则 。
C语言扫雷(自带外挂)

void setmine(char n[9][9])
{
int a,b;
srand(time(NULL));
for(int k=0;k<=9;)
{
a=(rand())%9;
printf("You are %d steps away from Success!!!GO on!!!\n",left-10);
if(w-1==10&&e-1==10)
goto read1;//外挂部分,直接跳到胜利条件中间
//-----------------------布雷-----------------------------------------//
if(n[w-1][e-1]=='*')
{
printf("You stepped on a BOMB! GAME OVER!!!!!!!!!\n");//点到地雷,游戏失败
break;
}
else
{
void spread(int r,int c,char n[9][9],int mask[9][9]);
}
goto read;
}
}
}
//----------------------------------------------------------------//
void spread(int r,int c,char n[9][9],int mask[9][9])
C语言实现的打擂台算法的示例

c 复制代码
#include <stdio.h>
// 定义一个函数指针类型 typedef int (*ComparisonFunc)(int, int);
// 比较函数,用于比较两个整数的大小 int compare(int a, int b) {
if (a < b) { return -1;
// 调用打擂台函数,并传入比较函数和数组 int result = champion(compare, arr, size);
printf("冠军是:%d\n", result);
return 0;
}
这个示例中,我们定义了一个比较函数 compare ,用于比较两个整数的大小。然后,我们定 义了一个打擂台函数 champion ,它接受一个比较函数和一个整数数组作为参数,通过遍历 数组找到最大的元素作为冠军。最后,在 main 函数中调用打擂台函数,并输出冠军的值。 请注意,这只是一个简单的示例,你可以根据实际需求进行修改和扩展。
} else if (a > b) { return 1;} else { 来自eturn 0;} }
// 打擂台函数,接受一个比较函数和一个整数数组作为参数 int champion(ComparisonFunc compareFunc, int arr[], int size) {
int champ = arr[0]; // 初始化冠军为数组的第一个元素
for (int i = 1; i < size; i++) { if (compareFunc(arr[i], champ) > 0) { champ = arr[i]; // 如果当前元素比冠军大,则更新冠军 }
扫雷游戏的设计与实现

第37卷第2期2021年2月福建电脑Journal of F ujian ComputerVol.37 No.2Feb.2021扫雷游戏的设计与实现吕橙(北京建筑大学计算机系北京100044)摘要本文根据“自顶向下,逐步求精”的软件设计方法,针对windows自带的扫雷游戏进行了总体设计、详细设计,并 采用C语言在VS2019平台上利用Easyx图形库进行编程实现,游戏运行效果良好。
关键词 Easyx;visual studio2019;扫雷游戏中图法分类号 TP311.52 DOI:10.16707/ki.fjpc.2021.02.027Design and Implementation of Mine Sweeping GameLV Cheng(School of E lectrical and Information Engineering,Beijing University of Civil Engineering and Architecture,Beijing,China, 100044)1引言《扫雷》是一款大众类的益智小游戏,游戏于1992年发行,目标是在最短的时间内根据点击格子出现的数字找出所有非雷格子,同时避免踩雷,踩 到一个雷即全盘皆输。
扫雷游戏容易上手,一方面是为了増强思维能力,提高智力,另一方面是为了训练用户的鼠标左右键操作能力,让这些动作变得非常自然,并培养鼠标移动的速度和准确性,扫雷游戏已在各个游戏平台均有应用。
张锦歌等人用Flash AS3制作了扫雷游戏[1]。
李 丽华等人以扫雷游戏为例,使用控件数组,讲解了 案例教学法在V B中的应用[2]。
俞亮用C#语言对扫雷游戏进行了系统设计与实现[3]。
徐驰按照自顶向下、逐步求精、模块化的结构化程序设计原则利用C语言实现了实现了扫雷游戏的开发[4]。
毛金玲基于Google公司提供的Android系统插件对扫雷游戏进行了总体设计[5]。
C语言实现经典扫雷游戏流程

C语⾔实现经典扫雷游戏流程⽬录扫雷⼩游戏简介⼀、分析与实现1.设计棋盘2.放置雷以及排雷⼆、扫雷⼩游戏演⽰三、源码总结扫雷⼩游戏简介想必很多⼈⼩时候电脑没⽹的时候都玩⼉过这个经典的⼩游戏,也都被它折磨过。
其实这个游戏很简单,通过点击相应位置显⽰的数字来确定周围雷的数量,在避免踩到雷的同时找出所有的雷就能获得胜利。
这次我们⽤C语⾔来实现⼀个简单的扫雷⼩游戏。
⼀、分析与实现1.设计棋盘要玩⼉扫雷游戏,我们⾸先应该有⼀个棋盘。
这个棋盘中的雷应该是在开始玩⼉游戏的时候就已经布置好了,不能随意变化。
但是呢⼜不能给玩家看到雷的位置,所以呢,我们应该有两个棋盘,⼀个显⽰给玩家,⼀个给⽤来给设计者查看。
有了棋盘之后⾸先要进⾏初始化://初始化棋盘void InitChess(char chess[ROWS][COLS], int rows, int cols, char sign){int i = 0;for (i = 0; i < rows; i++){int j = 0;for (j = 0; j < cols; j++){chess[i][j] = sign;}}printf("初始化棋盘成功!\n");}之后呢我们可以将设计好的棋盘打印出来看⼀看是否符合⼼意://打印棋盘void DisplayChess(char chess[ROWS][COLS], int row, int col){int i = 0;printf(" ");for (i = 1; i <= row; i++){printf(" %d ", i);}printf("\n");for (i = 1; i <= row; i++){int j = 0;printf(" ");for (j = 1; j <= col; j++){printf("+---");}printf("+\n");printf(" %d ", i);for (j = 1; j <= col; j++){printf("| %c ", chess[i][j]);}printf("|\n");}int j = 0;printf(" ");for (j = 1; j <= col; j++){printf("+---");}printf("+\n");}这是设计的⼀个简易的9X9的⼩棋盘,*号代表这个位置还没有被探查过,⼤家可以根据⾃⼰的喜好更改棋盘⼤⼩。
c语言扫雷程序代码

if(n==0) {
for(i=0;i<2;i++) for(j=0;j<2;j++) { if(b[x+i][x+j]==10) { b[x+i][y+j]=0; printf_0(b,x+i,y+j); } }
} else b[x][y]=n; } else if(x==0&&y<Map_list-1&&y>0) { n=0; for(i=0;i<2;i++)
for(j=-1;j<2;j++) if(b[x+i][y+j]==11) n++;
if(n!=0) b[x][y]=n;
else for(i=0;i<2;i++)
for(j=-1;j<2;j++) {
if(b[x+i][y+j]==10) {
b[x+i][y+j]=0; printf_0(b,x+i,y+j); } } } else if(x==0&&y==Map_list-1) { n=0; for(i=0;i<2;i++) for(j=-1;j<1;j++) if(b[x+i][y+j]==11) n++; if(n!=0) b[x][y]=n; else for(i=0;i<2;i++) for(j=-1;j<1;j++) { if(b[x+i][y+j]==10) { b[x+i][y+j]=0; printf_0(b,x+i,y+j); } } } else if(y==Map_list-1&&x!=0&x!=Map_line-1) { n=0; for(i=-1;i<2;i++) for(j=-1;j<1;j++) if(b[x+i][y+j]==11) n++; if(n!=0) b[x][y]=n; else for(i=-1;i<2;i++) for(j=-1;j<1;j++) { if(b[x+i][y+j]==10) {
扫雷课程教案设计方案模板

一、课程名称C语言扫雷项目开发二、教学目标1. 让学生掌握C语言编程的基本知识,提高编程能力。
2. 通过扫雷项目的开发,让学生学会如何分析问题、设计算法和实现代码。
3. 培养学生的团队协作能力和项目开发经验。
三、教学对象C语言课程的学生四、教学时间4课时五、教学重点与难点1. 教学重点:扫雷游戏的算法设计、数据结构选择、程序框架搭建。
2. 教学难点:边缘位置的数字统计、棋盘打印时雷与非雷信息区分、排雷和胜利判定逻辑。
六、教学准备1. 计算机实验室,确保每名学生有一台可使用的计算机。
2. C语言编程环境,如Visual Studio、Code::Blocks等。
3. 教案、课件、代码示例等教学资料。
七、教学过程第一课时1. 导入- 介绍扫雷游戏的历史和玩法。
- 引导学生思考如何用C语言实现扫雷游戏。
2. 游戏分析- 分析扫雷游戏的功能和设计要求。
- 讨论数据存储方案,如二维数组。
3. 程序框架搭建- 演示如何使用C语言创建一个基本的扫雷游戏框架。
- 引导学生编写代码,实现游戏初始化和菜单打印。
4. 课后作业- 完成扫雷游戏的基本框架,包括初始化和菜单打印。
第二课时1. 数据存储方案- 讨论如何使用二维数组存储雷和非雷信息。
- 引导学生编写代码,实现布雷逻辑。
2. 边缘位置数字统计- 分析边缘位置的数字统计问题,讲解算法实现。
- 引导学生编写代码,实现边缘位置数字统计功能。
3. 棋盘打印- 讲解如何打印棋盘,区分雷和非雷信息。
- 引导学生编写代码,实现棋盘打印功能。
4. 课后作业- 完成布雷、边缘位置数字统计和棋盘打印功能。
第三课时1. 排雷逻辑- 讲解排雷逻辑,包括用户输入、雷和非雷的判断。
- 引导学生编写代码,实现排雷功能。
2. 胜利判定- 讲解胜利判定逻辑,包括用户是否已排除所有非雷区域。
- 引导学生编写代码,实现胜利判定功能。
3. Print函数实现- 讲解Print函数的设计,演示如何打印棋盘状态。
扫雷游戏代码

扫雷游戏代码Document serial number【UU89WT-UU98YT-UU8CB-UUUT-UUT108】/**/#ifndef BLOCK_H_#define BLOCK_H_#include<QLabel>class QWidget;class Block:public QLabel{Q_OBJECTpublic:explicit Block(bool mine_flag,QWidget*parent=0);void set_number(int number);void turn_over();bool is_mine()const;bool is_turn_over()const;signals:void turn_over(bool is_mine);protected:void mousePressEvent(QMouseEvent*event); private:bool mine_flag_;bool mark_flag_;bool turn_over_flag_;int number_;};#endif#include""#include<QLabel>#include<QMouseEvent>#include<QPixmap>#include<QWidget>Block::Block(bool mine_flag,QWidget*parent) :QLabel(parent){mine_flag_=mine_flag;mark_flag_=false;turn_over_flag_=false;number_=-1;setPixmap(QPixmap(":/images/"));}void Block::set_number(int number){number_=number;}void Block::turn_over(){if(!turn_over_flag_){turn_over_flag_=true;if(mine_flag_)setPixmap(QPixmap(":/images/"));elsesetPixmap(QPixmap(":/images/mine_"+QString("%1").arg(num ber_)+".png"));update();}}bool Block::is_mine()const{return mine_flag_;}bool Block::is_turn_over()const{return turn_over_flag_;}/*鼠标事件的实现*/void Block::mousePressEvent(QMouseEvent*event){if(event->button()==Qt::LeftButton){if(!turn_over_flag_&&!mark_flag_){turn_over_flag_=true;if(mine_flag_==true){setPixmap(QPixmap(":/images/"));update();emit turn_over(true);}else{setPixmap(QPixmap(":/images/mine_"+QString("%1").arg(num ber_)+".png"));update();emit turn_over(false);}}}else if(event->button()==Qt::RightButton){if(!turn_over_flag_){if(!mark_flag_){mark_flag_=true;setPixmap(QPixmap(":/images/"));}else{mark_flag_=false;setPixmap(QPixmap(":/images/"));}update();}}QLabel::mousePressEvent(event);}#ifndef BLOCK_AREA_H_#define BLOCK_AREA_H_#include""#include<QWidget>class QEvent;class QGridLayout;class QObject;class BlockArea:public QWidget{Q_OBJECTpublic:BlockArea(int row,int column,int mine_number,QWidget* parent=0);void set_block_area(int row,int column,intmine_number,int init_flag=false);signals:void game_over(bool is_win);protected:bool eventFilter(QObject*watched,QEvent*event); private slots:void slot_turn_over(bool is_mine);private:int calculate_mines(int x,int y)const;rg(easy_record_time_)),1,1);up_layout->addWidget(new QLabel(easy_record_name_),1,2);up_layout->addWidget(new QLabel(tr("Middle")),2,0);up_layout->addWidget(newQLabel(QString("%1").arg(middle_record_time_)),2,1);up_layout->addWidget(newQLabel(middle_record_name_),2,2);up_layout->addWidget(new QLabel(tr("Hard")),3,0);up_layout->addWidget(newQLabel(QString("%1").arg(hard_record_time_)),3,1);up_layout->addWidget(new QLabel(hard_record_name_),3,2);QPushButton*recount_button=newQPushButton(tr("recount"));QPushButton*close_button=new QPushButton(tr("close"));close_button->setDefault(true);connect(recount_button,SIGNAL(clicked()),&dialog,SLOT(ac cept()));connect(close_button,SIGNAL(clicked()),&dialog,SLOT(reje ct()));QHBoxLayout*bottom_layout=new QHBoxLayout;bottom_layout->addStretch();bottom_layout->addWidget(recount_button);bottom_layout->addWidget(close_button);QVBoxLayout*main_layout=new QVBoxLayout(&dialog);main_layout->addLayout(up_layout);main_layout->addLayout(bottom_layout);if()==QDialog::Accepted){easy_record_time_=middle_record_time_=hard_record_time_= g_no_record_time;easy_record_name_=middle_record_name_=hard_record_name_= g_no_record_name;}}void MainWindow::slot_show_game_toolBar(bool show){if(show)game_toolBar->show();elsegame_toolBar->hide();}void MainWindow::slot_show_statusBar(bool show){if(show)statusBar()->show();elsestatusBar()->hide();}/*游戏的设置容易、中等、困难及自定义*/void MainWindow::slot_standard(QAction*standard_action) {if(standard_action==easy_standard_action){current_standard_=0;row_=9;column_=9;mine_number_=10;}else if(standard_action==middle_standard_action){ current_standard_=1;row_=16;column_=16;mine_number_=40;}else if(standard_action==hard_standard_action){current_standard_=2;row_=16;column_=30;mine_number_=99;}else if(standard_action==custom_standard_action){ QDialog dialog;(tr("set standard"));QSpinBox*row_spinBox=new QSpinBox;row_spinBox->setRange(5,50);row_spinBox->setValue(row_);QSpinBox*column_spinBox=new QSpinBox;column_spinBox->setRange(5,50);column_spinBox->setValue(column_);QSpinBox*mine_spinBox=new QSpinBox;mine_spinBox->setValue(mine_number_);QHBoxLayout*up_layout=new QHBoxLayout;up_layout->addWidget(row_spinBox);up_layout->addWidget(column_spinBox);up_layout->addWidget(mine_spinBox);QDialogButtonBox*dialog_buttonBox=new QDialogButtonBox;dialog_buttonBox->addButton(QDialogButtonBox::Ok);dialog_buttonBox->addButton(QDialogButtonBox::Cancel);connect(dialog_buttonBox,SIGNAL(accepted()),&dialog,SLOT (accept()));connect(dialog_buttonBox,SIGNAL(rejected()),&dialog,SLOT (reject()));QHBoxLayout*bottom_layout=new QHBoxLayout;bottom_layout->addStretch();bottom_layout->addWidget(dialog_buttonBox);QVBoxLayout*main_layout=new QVBoxLayout(&dialog);main_layout->addLayout(up_layout);main_layout->addLayout(bottom_layout);if()==QDialog::Accepted)if(row_spinBox->value()*column_spinBox->value()>mine_spinBox->value()){current_standard_=3;row_=row_spinBox->value();column_=column_spinBox->value();mine_number_=mine_spinBox->value();}}slot_new_game();}/*实现帮助菜单中的关于游戏,及功能*/void MainWindow::slot_about_game(){QString introduction("<h2>"+tr("About Mine Sweepr")+"</h2>"+"<p>"+tr("This game is played by revealing squares of the grid,typically by clicking them with a mouse.If a square containing a mine is revealed,the player loses the game.Otherwise,a digit is revealed in the square,indicating the number of adjacent squares(out of the possible eight)that contain this number is zero then the square appears blank,and the surrounding squares are automatically also revealed.By using logic,the player can in many instances use this information to deduce that certain other squares are mine-free, in which case they may be safely revealed,or mine-filled,in which they can be marked as such(which is effected by right-clicking the square and indicated by a flag graphic).")+"</p>"+"<p>"+tr("This program is free software;you can redistribute it and/or under the terms of the GNU General Public License as published by the Software Foundation;either version3of the License,or(at your option)any later version.")+"</p>"+"<p>"+tr("Please see")+"<a href="+tr("for an overview of GPLv3licensing")+"</p>"+"<br>"+tr("Version:")+g_software_version+"</br>"+"<br>"+tr("Author:")+g_software_author+"</br>");QMessageBoxmessageBox(QMessageBox::Information,tr("About Mine Sweeper"),introduction,QMessageBox::Ok);();}/*游戏的判断,及所给出的提示做出判断*/void MainWindow::slot_game_over(bool is_win){();QString name;if(is_win){switch(current_standard_){case0:if(time_label->text().toInt()<easy_record_time_){name=QInputDialog::getText(this,tr("Please enter your name"),tr("You create a record.Please enter your name"));if(!()){easy_record_time_=time_label->text().toInt();easy_record_name_=name;}}elseQMessageBox::information(this,tr("Result"),tr("You win"));break;case1:if(time_label->text().toInt()<middle_record_time_){name=QInputDialog::getText(this,tr("Please enter your name"),tr("You create a record.Please enter your name"));if(!()){middle_record_time_=time_label->text().toInt();middle_record_name_=name;}}elseQMessageBox::information(this,tr("Result"),tr("You win"));break;case2:if(time_label->text().toInt()<hard_record_time_){name=QInputDialog::getText(this,tr("Please enter your name"),tr("You create a record.Please enter your name"));if(!()){hard_record_time_=time_label->text().toInt();hard_record_name_=name;}}elseQMessageBox::information(this,tr("Result"),tr("You win"));break;default:QMessageBox::information(this,tr("Result"),tr("Y ou win"));}}else{QMessageBox::information(this,tr("Result"),tr("You lose"));}}/*定时器的设置*/void MainWindow::slot_timer(){time_label->setText(QString("%1").arg()/1000));}/*关于菜单栏的设置是否显示游戏工具栏和状态栏*/void MainWindow::read_settings(){QSettings settings;("MainWindow");resize("size").toSize());move("pos").toPoint());bool show_game_toolBar=("showGameToolBar").toBool();show_game_toolBar_action->setChecked(show_game_toolBar);slot_show_game_toolBar(show_game_toolBar);bool show_statusBar=("showStatusBar").toBool();show_statusBar_action->setChecked(show_statusBar);slot_show_statusBar(show_statusBar);();("GameSetting");current_standard_=("current_standard").toInt();switch(current_standard_){case0:easy_standard_action->setChecked(true);break;case1:middle_standard_action->setChecked(true);break;case2:hard_standard_action->setChecked(true);break;case3:custom_standard_action->setChecked(true);break;default:;}row_=("row").toInt()==09:("row").toInt();column_=("column").toInt()==09:("column").toInt();mine_number_=("mine_number").toInt()==010:("mine_number" ).toInt();();("Rank");easy_record_time_=("easy_record_time").toInt()==0g_no_re cord_time:("easy_record_time").toInt();middle_record_time_=("middle_record_time").toInt()==0g_n o_record_time:("middle_record_time").toInt();hard_record_time_=("hard_record_time").toInt()==0g_no_re cord_time:("hard_record_time").toInt();easy_record_name_=("easy_record_name").toString()==""g_n o_record_name:("easy_record_name").toString();middle_record_name_=("middle_record_name").toString()==" "g_no_record_name:("middle_record_name").toString();hard_record_name_=("hard_record_name").toString()==""g_n o_record_name:("hard_record_name").toString();();}void MainWindow::write_settings(){QSettings settings;("MainWindow");("size",size());("pos",pos());("showGameToolBar",show_game_toolBar_action->isChecked());("showStatusBar",show_statusBar_action->isChecked());();("GameSetting");("current_standard",current_standard_);("row",row_);("column",column_);("mine_number",mine_number_);();("Rank");("easy_record_time",easy_record_time_);("middle_record_time",middle_record_time_);("hard_record_time",hard_record_time_);("easy_record_name",easy_record_name_);("middle_record_name",middle_record_name_);("hard_record_name",hard_record_name_);();}/*菜单栏里图片的显示*/void MainWindow::create_actions(){new_game_action=new QAction(QIcon(":/images/"),tr("New Game"),this);new_game_action->setShortcut(QKeySequence::New);connect(new_game_action,SIGNAL(triggered()),this,SLOT(sl ot_new_game()));rank_action=newQAction(QIcon(":/images/"),tr("Rank"),this);connect(rank_action,SIGNAL(triggered()),this,SLOT(slot_r ank()));exit_action=newQAction(QIcon(":/images/"),tr("Exit"),this);exit_action->setShortcut(QKeySequence::Quit);connect(exit_action,SIGNAL(triggered()),this,SLOT(close( )));show_game_toolBar_action=new QAction(tr("Show Game Tool Bar"),this);show_game_toolBar_action->setCheckable(true);connect(show_game_toolBar_action,SIGNAL(toggled(bool)),t his,SLOT(slot_show_game_toolBar(bool)));show_statusBar_action=new QAction(tr("Show Status Bar"),this);show_statusBar_action->setCheckable(true);connect(show_statusBar_action,SIGNAL(toggled(bool)),this ,SLOT(slot_show_statusBar(bool)));easy_standard_action=newQAction(QIcon(":/images/"),tr("Easy"),this);easy_standard_action->setCheckable(true);middle_standard_action=newQAction(QIcon(":/images/"),tr("Middle"),this);middle_standard_action->setCheckable(true);hard_standard_action=newQAction(QIcon(":/images/"),tr("Hard"),this);hard_standard_action->setCheckable(true);custom_standard_action=newQAction(QIcon(":/images/"),tr("Custom"),this);custom_standard_action->setCheckable(true);standard_actionGroup=new QActionGroup(this);standard_actionGroup->addAction(easy_standard_action);standard_actionGroup->addAction(middle_standard_action);standard_actionGroup->addAction(hard_standard_action);standard_actionGroup->addAction(custom_standard_action);connect(standard_actionGroup,SIGNAL(triggered(QAction*)) ,this,SLOT(slot_standard(QAction*)));about_game_action=newQAction(QIcon(":/images/"),tr("About Game"),this);connect(about_game_action,SIGNAL(triggered()),this,SLOT( slot_about_game()));about_qt_action=new QAction(QIcon(":/images/"),tr("About Qt"),this);connect(about_qt_action,SIGNAL(triggered()),qApp,SLOT(ab outQt()));}/*菜单栏的创建*/void MainWindow::create_menus(){game_menu=menuBar()->addMenu(tr("Game"));game_menu->addAction(new_game_action);game_menu->addSeparator();game_menu->addAction(rank_action);game_menu->addSeparator();game_menu->addAction(exit_action);setting_menu=menuBar()->addMenu(tr("Setting"));setting_menu->addAction(show_game_toolBar_action);setting_menu->addAction(show_statusBar_action);setting_menu->addSeparator();setting_menu->addAction(easy_standard_action);setting_menu->addAction(middle_standard_action);setting_menu->addAction(hard_standard_action);setting_menu->addAction(custom_standard_action);help_menu=menuBar()->addMenu(tr("Help"));help_menu->addAction(about_game_action);help_menu->addAction(about_qt_action);}void MainWindow::create_game_toolBar(){game_toolBar=addToolBar(tr("Game Tool Bar"));game_toolBar->setFloatable(false);game_toolBar->setMovable(false);game_toolBar->addAction(new_game_action);game_toolBar->setToolButtonStyle(Qt::ToolButtonTextBesideIcon); }void MainWindow::create_statusBar(){time_label=new QLabel;statusBar()->addPermanentWidget(time_label);statusBar()->addPermanentWidget(newQLabel(tr("second")));}#include""#include<QApplication>#include<QTranslator>int main(int argc,char*argv[]){QApplication app(argc,argv);QTranslator translator;(":/");(&translator);MainWindow window;();return();}。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
} } } } int judge_game(struct Games *g) /*判断游戏是否结束*/ { int sum=0; int i,j; for(i=0;i<x;i++) { for(j=0;j<y;j++) { if((*g).game_char[i][j]=='#') sum+=1; } } return sum; } int openmap(struct Games *g,int i,int j) /*点开地图 */ { system("cls"); int next1,next2; if((*g).game_int[i][j]==9) return 1; else { if((*g).game_int[i][j]!=0) { (*g).game_char[i][j]=(char)((*g).game_int[i][j]+48); return 2; } else { (*g).game_char[i][j]='0'; for(next1=-1;next1<2;next1++) {
C语言程序设计 :扫雷TC语言程序
#include <stdio.h> #include "stdlib.h" # include "time.h" # define s 20 int x; /*棋盘行数*/ int y; /*棋盘列数*/ int b; /*选择游戏难度 1简单2普通3困难4 自定义*/ int h; /*雷的个数*/ struct Games g; struct Games { int game_int[s][s]; char game_char[s][s]; }; void InitialValue_game(struct Games *g) /*棋盘初始化*/ { int i,j; for(i=0;i<x;i++) { for(j=0;j<y;j++) { (*g).game_int[i][j]=0; (*g).game_char[i][j]='#'; } } } void InitialMine_game(struct Games *g) /*随机布雷*/ { int i,j,next1,next2; int sum=0; srand((unsigned)time(NULL));
system("cls"); InitialValue_game(&g); InitialMine_game(&g); print_game(&g,2); NO=1; while(NO) { printf("Please choose:"); scanf("%d,%d",&i,&j); getchar(); if(-1<i-1 && i-1<x && -1<j-1 && j-1<y) { switch(openmap(&g,i-1,j-1)) { case 1: print_game(&g,1); printf("游戏结束!按回车键再来一局\n"); if(getchar()=='\n') begin=1; else begin=0; NO=0; break; case 2: if(judge_game(&g)==b) { printf("win!!\n"); print_game(&g,1); printf("按回车键再来一局\n"); if(getchar()=='\n') begin=1; else begin=0; NO=0; break; } else print_game(&g,2);NO=1;break; default:break; } } else { printf("你输入错误!请重新输入\n");
while(1) { i=rand()%x; j=rand()%y; if((*g).game_int[i][j]==9) continue; (*g).game_int[i][j]=9; ++sum; for(next1=-1;next1<2;next1++) { for(next2=-1;next2<2;next2++) { if( -1<i+next1 && next1+i<x&& -1<j+next2 && j+next2<y ) { if((*g).game_int[i+next1][j+next2]!=9) { (*g).game_int[i+next1][j+next2]+=1; } } } } if(sum==b) break; } }
void print_game(struct Games *g,int Logo) /*打印棋盘*/ { int i,j; int a[s]; for(i=0;i<x;i++) { a[i]=i+1; } if(Logo==1) { printf(" %d",a[0]);
for(i=1;i<x;i++) printf(i<10? " %d" : " %d",a[i]); printf("\n\n"); for(i=0;i<x;i++) { if(i+1<10) { printf("%d ",i+1); for(j=0;j<y;j++) printf("%d ",(*g).game_int[i][j]); printf("\n"); } else { printf("%d ",i+1); for(j=0;j<y;j++) printf("%d ",(*g).game_int[i][j]); printf("\n"); } } } if(Logo==2) { printf(" %d",a[0]); for(i=1;i<x;i++) printf(i<10? " %d" : " %d",a[i]); printf("\n\n"); for(i=0;i<x;i++) { if(i+1<10) { printf("%d ",i+1); for(j=0;j<y;j++)printf("%c ",(*g).game_char[i][j]); printf("\n"); } else { printf("%d ",i+1); for(j=0;j<y;j++)printf("%c ",(*g).game_char[i][j]); printf(mp;&h<5) { switch(h) { case 1: x=10; y=10; b=10; break; case 2: x=15; y=15; b=40; break; case 3: x=20; y=20; b=60; break; case 4: system("cls"); printf("请输入棋盘行列数(小于20且用逗号隔开):"); scanf("%d,%d",&x,&y); getchar(); printf("请输入雷的个数:"); scanf("%d",&b); getchar(); break; } break; } else { printf("输入错误请重新输\n"); } } while(begin) {
for(next2=-1;next2<2;next2++) { if( -1<i+next1 && next1+i<x && -1<j+next2 && j+next2<y) { if((*g).game_char[i+next1][j+next2]=='#') { openmap(g,i+next1,j+next2); } } } } return 2; } } } main() { int i,j; int NO; int begin=1; int h; while(begin) { printf ( "\n\n\n\n\n\n\n\n\t\t\t=================================\n\n" "\t\t\t| 欢迎进入扫雷 |\n\n\n" "\t\t\t| 按回车键继续 |\n\n" "\t\t\t=================================\n\n\n\n" ); getchar(); system("cls"); printf(" \n\n\n\n\n\n\n\n\n 请选择游戏难度:\n\n"); printf(" 1简单难度\n\n"); printf(" 2普通难度\n\n"); printf(" 3困难难度\n\n"); printf(" 4自定义难度\n\n"); scanf("%d",&h); getchar();