C语言扫雷游戏源代码

C语言扫雷游戏源代码,第1张

"扫雷"小游戏C代码

#include<stdio.h>

#include<math.h>

#include<time.h>

#include<stdlib.h>

main( )

{char a[102][102],b[102][102],c[102][102],w

int i,j /*循环变量*/

int x,y,z[999] /*雷的位置*/

int t,s /*标记*/

int m,n,lei /*计数*/

int u,v /*输入*/

int hang,lie,ge,mo /*自定义变量*/

srand((int)time(NULL)) /*启动随机数发生器*/

leb1:  /*选择模式*/

printf("\n   请选择模式:\n   1.标准  2.自定义\n")

scanf("%d",&mo)

if(mo==2)  /*若选择自定义模式,要输入三个参数*/

{do

{t=0printf("请输入\n行数 列数 雷的个数\n")

scanf("%d%d%d",&hang,&lie,&ge)

if(hang<2){printf("行数太少\n")t=1}

if(hang>100){printf("行数太多\n")t=1}

if(lie<2){printf("列数太少\n")t=1}

if(lie>100){printf("列数太多\n")t=1}

if(ge<1){printf("至少要有一个雷\n")t=1}

if(ge>=(hang*lie)){printf("雷太多了\n")t=1}

}while(t==1)

}

else{hang=10,lie=10,ge=10}  /*否则就是选择了标准模式(默认参数)*/

for(i=1i<=gei=i+1)  /*确定雷的位置*/

{do

{t=0z[i]=rand( )%(hang*lie)

for(j=1j<ij=j+1){if(z[i]==z[j]) t=1}

}while(t==1)

}

for(i=0i<=hang+1i=i+1)  /*初始化a,b,c*/

{for(j=0j<=lie+1j=j+1) {a[i][j]='1'b[i][j]='1'c[i][j]='0'} }

for(i=1i<=hangi=i+1)

{for(j=1j<=liej=j+1) {a[i][j]='+'} }

for(i=1i<=gei=i+1)  /*把雷放入c*/

{x=z[i]/lie+1y=z[i]%lie+1c[x][y]='#'}

for(i=1i<=hangi=i+1)  /*计算b中数字*/

{for(j=1j<=liej=j+1)

{m=48

if(c[i-1][j-1]=='#')m=m+1if(c[i][j-1]=='#')m=m+1

if(c[i-1][j]=='#')m=m+1 if(c[i+1][j+1]=='#')m=m+1

if(c[i][j+1]=='#')m=m+1 if(c[i+1][j]=='#')m=m+1

if(c[i+1][j-1]=='#')m=m+1if(c[i-1][j+1]=='#')m=m+1

b[i][j]=m

}

}

for(i=1i<=gei=i+1)  /*把雷放入b中*/

{x=z[i]/lie+1y=z[i]%lie+1b[x][y]='#'}

lei=ge /*以下是游戏设计*/

do

{leb2:  /*输出*/

system("cls")printf("\n\n\n\n")

printf("    ")

for(i=1i<=liei=i+1)

{w=(i-1)/10+48printf("%c",w)

w=(i-1)%10+48printf("%c  ",w)

}

printf("\n   |")

for(i=1i<=liei=i+1){printf("---|")}

printf("\n")

for(i=1i<=hangi=i+1)

{w=(i-1)/10+48printf("%c",w)

w=(i-1)%10+48printf("%c |",w)

for(j=1j<=liej=j+1)

{if(a[i][j]=='0')printf("   |")

else printf(" %c |",a[i][j])

}

if(i==2)printf(" 剩余雷个数")

if(i==3)printf(" %d",lei)

printf("\n   |")

for(j=1j<=liej=j+1){printf("---|")}

printf("\n")

}

scanf("%d%c%d",&u,&w,&v) /*输入*/

u=u+1,v=v+1

if(w!='#'&&a[u][v]=='@')

goto leb2

if(w=='#')

{if(a[u][v]=='+'){a[u][v]='@'lei=lei-1}

else if(a[u][v]=='@'){a[u][v]='?'lei=lei+1}

else if(a[u][v]=='?'){a[u][v]='+'}

goto leb2

}

a[u][v]=b[u][v]

leb3:  /*打开0区*/

t=0

if(a[u][v]=='0')

{for(i=1i<=hangi=i+1)

{for(j=1j<=liej=j+1)

{s=0

if(a[i-1][j-1]=='0')s=1if(a[i-1][j+1]=='0')s=1

if(a[i-1][j]=='0')s=1 if(a[i+1][j-1]=='0')s=1

if(a[i+1][j+1]=='0')s=1if(a[i+1][j]=='0')s=1

if(a[i][j-1]=='0')s=1 if(a[i][j+1]=='0')s=1

if(s==1)a[i][j]=b[i][j]

}

}

for(i=1i<=hangi=i+1)

{for(j=liej>=1j=j-1)

{s=0

if(a[i-1][j-1]=='0')s=1if(a[i-1][j+1]=='0')s=1

if(a[i-1][j]=='0')s=1 if(a[i+1][j-1]=='0')s=1

if(a[i+1][j+1]=='0')s=1if(a[i+1][j]=='0')s=1

if(a[i][j-1]=='0')s=1   if(a[i][j+1]=='0')s=1

if(s==1)a[i][j]=b[i][j]

}

}

for(i=hangi>=1i=i-1)

{for(j=1j<=liej=j+1)

{s=0

if(a[i-1][j-1]=='0')s=1if(a[i-1][j+1]=='0')s=1

if(a[i-1][j]=='0')s=1 if(a[i+1][j-1]=='0')s=1

if(a[i+1][j+1]=='0')s=1if(a[i+1][j]=='0')s=1

if(a[i][j-1]=='0')s=1 if(a[i][j+1]=='0')s=1

if(s==1)a[i][j]=b[i][j]

}

}

for(i=hangi>=1i=i-1)

{for(j=liej>=1j=j-1)

{s=0

if(a[i-1][j-1]=='0')s=1if(a[i-1][j+1]=='0')s=1

if(a[i-1][j]=='0')s=1 if(a[i+1][j-1]=='0')s=1

if(a[i+1][j+1]=='0')s=1if(a[i+1][j]=='0')s=1

if(a[i][j-1]=='0')s=1  if(a[i][j+1]=='0')s=1

if(s==1)a[i][j]=b[i][j]

}

}

for(i=1i<=hangi=i+1)  /*检测0区*/

{for(j=1j<=liej=j+1)

{if(a[i][j]=='0')

{if(a[i-1][j-1]=='+'||a[i-1][j-1]=='@'||a[i-1][j-1]=='?')t=1

if(a[i-1][j+1]=='+'||a[i-1][j+1]=='@'||a[i-1][j+1]=='?')t=1

if(a[i+1][j-1]=='+'||a[i+1][j-1]=='@'||a[i+1][j-1]=='?')t=1

if(a[i+1][j+1]=='+'||a[i+1][j+1]=='@'||a[i+1][j+1]=='?')t=1

if(a[i+1][j]=='+'||a[i+1][j]=='@'||a[i+1][j]=='?')t=1

if(a[i][j+1]=='+'||a[i][j+1]=='@'||a[i][j+1]=='?')t=1

if(a[i][j-1]=='+'||a[i][j-1]=='@'||a[i][j-1]=='?')t=1

if(a[i-1][j]=='+'||a[i-1][j]=='@'||a[i-1][j]=='?')t=1

}

}

}

if(t==1)goto leb3

}

n=0 /*检查结束*/

for(i=1i<=hangi=i+1)

{for(j=1j<=liej=j+1)

{if(a[i][j]!='+'&&a[i][j]!='@'&&a[i][j]!='?')n=n+1}

}

}

while(a[u][v]!='#'&&n!=(hang*lie-ge))

for(i=1i<=gei=i+1)  /*游戏结束*/

{x=z[i]/lie+1y=z[i]%lie+1a[x][y]='#'}

printf("    ")

for(i=1i<=liei=i+1)

{w=(i-1)/10+48printf("%c",w)

w=(i-1)%10+48printf("%c  ",w)

}

printf("\n   |")

for(i=1i<=liei=i+1){printf("---|")}

printf("\n")

for(i=1i<=hangi=i+1)

{w=(i-1)/10+48printf("%c",w)

w=(i-1)%10+48printf("%c |",w)

for(j=1j<=liej=j+1)

{if(a[i][j]=='0')printf(" |")

else  printf(" %c |",a[i][j])

}

if(i==2)printf(" 剩余雷个数")

if(i==3)printf(" %d",lei)printf("\n   |")

for(j=1j<=liej=j+1) {printf("---|")}

printf("\n")

}

if(n==(hang*lie-ge)) printf("你成功了!\n")

else printf("    游戏结束!\n")

printf("    重玩请输入1\n")

t=0

scanf("%d",&t)

if(t==1)goto leb1

}

/*注:在DEV c++上运行通过。行号和列号都从0开始,比如要确定第0行第9列不是“雷”,就在0和9中间加入一个字母,可以输入【0a9】三个字符再按回车键。3行7列不是雷,则输入【3a7】回车;第8行第5列是雷,就输入【8#5】回车,9行0列是雷则输入【9#0】并回车*/

这是字符界面的扫雷:

#include <iostream>

#include <cstdlib>

#include <ctime>

#include <windows.h>

#include <conio.h>

// defines

#define KEY_UP0xE048

#define KEY_DOWN 0xE050

#define KEY_LEFT 0xE04B

#define KEY_RIGHT 0xE04D

#define KEY_ESC 0x001B

#define KEY_1 '1'

#define KEY_2 '2'

#define KEY_3 '3'

#define GAME_MAX_WIDTH 100

#define GAME_MAX_HEIGHT 100

// Strings Resource

#define STR_GAMETITLE "ArrowKey:MoveCursor Key1:Open \

Key2:Mark Key3:OpenNeighbors"

#define STR_GAMEWIN "Congratulations! You Win! Thank you for playing!\n"

#define STR_GAMEOVER "Game Over, thank you for playing!\n"

#define STR_GAMEEND "Presented by yzfy . Press ESC to exit\n"

//-------------------------------------------------------------

// Base class

class CConsoleWnd

{

public:

static int TextOut(const char*)

static int GotoXY(int, int)

static int CharOut(int, int, const int)

static int TextOut(int, int, const char*)

static int GetKey()

public:

}

//{{// class CConsoleWnd

//

// int CConsoleWnd::GetKey()

// Wait for standard input and return the KeyCode

//

int CConsoleWnd::GetKey()

{

int nkey=getch(),nk=0

if(nkey>=128||nkey==0)nk=getch()

return nk>0?nkey*256+nk:nkey

}

//

// int CConsoleWnd::GotoXY(int x, int y)

// Move cursor to (x,y)

// Only Console Application

//

int CConsoleWnd::GotoXY(int x, int y)

{

COORD cd

cd.X = xcd.Y = y

return SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),cd)

}

//

// int CConsoleWnd::TextOut(const char* pstr)

// Output a string at current position

//

int CConsoleWnd::TextOut(const char* pstr)

{

for(*pstr++pstr)putchar(*pstr)

return 0

}

//

// int CConsoleWnd::CharOut(int x, int y, const int pstr)

// Output a char at (x,y)

//

int CConsoleWnd::CharOut(int x, int y, const int pstr)

{

GotoXY(x, y)

return putchar(pstr)

}

//

// int CConsoleWnd::TextOut(const char* pstr)

// Output a string at (x,y)

//

int CConsoleWnd::TextOut(int x, int y, const char* pstr)

{

GotoXY(x, y)

return TextOut(pstr)

}

//}}

//-------------------------------------------------------------

//Application class

class CSLGame:public CConsoleWnd

{

private:

private:

int curX,curY

int poolWidth,poolHeight

int bm_gamepool[GAME_MAX_HEIGHT+2][GAME_MAX_WIDTH+2]

public:

CSLGame():curX(0),curY(0){poolWidth=poolHeight=0}

int InitPool(int, int, int)

int MoveCursor(){return CConsoleWnd::GotoXY(curX, curY)}

int DrawPool(int)

int WaitMessage()

int GetShowNum(int, int)

int TryOpen(int, int)

private:

int DFSShowNum(int, int)

private:

const static int GMARK_BOOM

const static int GMARK_EMPTY

const static int GMARK_MARK

}

const int CSLGame::GMARK_BOOM = 0x10

const int CSLGame::GMARK_EMPTY= 0x100

const int CSLGame::GMARK_MARK = 0x200

//{{// class CSLGame:public CConsoleWnd

//

// int CSLGame::InitPool(int Width, int Height, int nBoom)

// Initialize the game pool.

// If Width*Height <= nBoom, or nBoom<=0,

// or Width and Height exceed limit , then return 1

// otherwise return 0

//

int CSLGame::InitPool(int Width, int Height, int nBoom)

{

poolWidth = WidthpoolHeight = Height

if(nBoom<=0 || nBoom>=Width*Height

|| Width <=0 || Width >GAME_MAX_WIDTH

|| Height<=0 || Height>GAME_MAX_HEIGHT

){

return 1

}

// zero memory

for(int y=0y<=Height+1++y)

{

for(int x=0x<=Width+1++x)

{

bm_gamepool[y][x]=0

}

}

// init seed

srand(time(NULL))

// init Booms

while(nBoom)

{

int x = rand()%Width + 1, y = rand()%Height + 1

if(bm_gamepool[y][x]==0)

{

bm_gamepool[y][x] = GMARK_BOOM

--nBoom

}

}

// init cursor position

curX = curY = 1

MoveCursor()

return 0

}

//

// int CSLGame::DrawPool(int bDrawBoom = 0)

// Draw game pool to Console window

//

int CSLGame::DrawPool(int bDrawBoom = 0)

{

for(int y=1y<=poolHeight++y)

{

CConsoleWnd::GotoXY(1, y)

for(int x=1x<=poolWidth++x)

{

if(bm_gamepool[y][x]==0)

{

putchar('.')

}

else if(bm_gamepool[y][x]==GMARK_EMPTY)

{

putchar(' ')

}

else if(bm_gamepool[y][x]>0 &&bm_gamepool[y][x]<=8)

{

putchar('0'+bm_gamepool[y][x])

}

else if(bDrawBoom==0 &&(bm_gamepool[y][x] &GMARK_MARK))

{

putchar('#')

}

else if(bm_gamepool[y][x] &GMARK_BOOM)

{

if(bDrawBoom)

putchar('*')

else

putchar('.')

}

}

}

return 0

}

//

// int CSLGame::GetShowNum(int x, int y)

// return ShowNum at (x, y)

//

int CSLGame::GetShowNum(int x, int y)

{

int nCount = 0

for(int Y=-1Y<=1++Y)

for(int X=-1X<=1++X)

{

if(bm_gamepool[y+Y][x+X] &GMARK_BOOM)++nCount

}

return nCount

}

//

// int CSLGame::TryOpen(int x, int y)

// Try open (x, y) and show the number

// If there is a boom, then return EOF

//

int CSLGame::TryOpen(int x, int y)

{

int nRT = 0

if(bm_gamepool[y][x] &GMARK_BOOM)

{

nRT = EOF

}

else

{

int nCount = GetShowNum(x,y)

if(nCount==0)

{

DFSShowNum(x, y)

}

else bm_gamepool[y][x] = nCount

}

return nRT

}

//

// int CSLGame::DFSShowNum(int x, int y)

// Private function, no comment

//

int CSLGame::DFSShowNum(int x, int y)

{

if((0<x &&x<=poolWidth) &&

(0<y &&y<=poolHeight) &&

(bm_gamepool[y][x]==0))

{

int nCount = GetShowNum(x, y)

if(nCount==0)

{

bm_gamepool[y][x] = GMARK_EMPTY

for(int Y=-1Y<=1++Y)

for(int X=-1X<=1++X)

{

DFSShowNum(x+X,y+Y)

}

}

else bm_gamepool[y][x] = nCount

}

return 0

}

//

// int CSLGame::WaitMessage()

// Game loop, wait and process an input message

// return: 0: not end 1: Winotherwise: Lose

//

int CSLGame::WaitMessage()

{

int nKey = CConsoleWnd::GetKey()

int nRT = 0, nArrow = 0

switch (nKey)

{

case KEY_UP:

{

if(curY>1)--curY

nArrow=1

}break

case KEY_DOWN:

{

if(curY<poolHeight)++curY

nArrow=1

}break

case KEY_LEFT:

{

if(curX>1)--curX

nArrow=1

}break

case KEY_RIGHT:

{

if(curX<poolWidth)++curX

nArrow=1

}break

case KEY_1:

{

nRT = TryOpen(curX, curY)

}break

case KEY_2:

{

if((bm_gamepool[curY][curX]

&~(GMARK_MARK|GMARK_BOOM))==0)

{

bm_gamepool[curY][curX] ^= GMARK_MARK

}

}break

case KEY_3:

{

if(bm_gamepool[curY][curX] &0xF)

{

int nb = bm_gamepool[curY][curX] &0xF

for(int y=-1y<=1++y)

for(int x=-1x<=1++x)

{

if(bm_gamepool[curY+y][curX+x] &GMARK_MARK)

--nb

}

if(nb==0)

{

for(int y=-1y<=1++y)

for(int x=-1x<=1++x)

{

if((bm_gamepool[curY+y][curX+x]

&(0xF|GMARK_MARK)) == 0)

{

nRT |= TryOpen(curX+x, curY+y)

}

}

}

}

}break

case KEY_ESC:

{

nRT = EOF

}break

}

if(nKey == KEY_1 || nKey == KEY_3)

{

int y=1

for(y<=poolHeight++y)

{

int x=1

for(x<=poolWidth++x)

{

if(bm_gamepool[y][x]==0)break

}

if(x<=poolWidth) break

}

if(! (y<=poolHeight))

{

nRT = 1

}

}

if(nArrow==0)

{

DrawPool()

}

MoveCursor()

return nRT

}

//}}

//-------------------------------------------------------------

//{{

//

// main function

//

int main(void)

{

int x=50, y=20, b=100,n// define width &height &n_booms

CSLGame slGame

// Init Game

{

CConsoleWnd::GotoXY(0,0)

CConsoleWnd::TextOut(STR_GAMETITLE)

slGame.InitPool(x,y,b)

slGame.DrawPool()

slGame.MoveCursor()

}

while((n=slGame.WaitMessage())==0) // Game Message Loop

// End of the Game

{

slGame.DrawPool(1)

CConsoleWnd::TextOut("\n")

if(n==1)

{

CConsoleWnd::TextOut(STR_GAMEWIN)

}

else

{

CConsoleWnd::TextOut(STR_GAMEOVER)

}

CConsoleWnd::TextOut(STR_GAMEEND)

}

while(CConsoleWnd::GetKey()!=KEY_ESC)

return 0

}

//}}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/10989578.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-12
下一篇 2023-05-12

发表评论

登录后才能评论

评论列表(0条)

保存