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

}

//}}

import javax.swing.*

import java.awt.*

import java.awt.event.*

public class Main

{

public static void main(String[] argus)

{

Landmine Zhang = new Landmine()

}

}

//

// Landmine类 主界面

class Landmine extends JFrame

{

static Resources resources = new Resources()

Playing listener = new Playing(this) //主要监听者,监听地雷面板的动作

Help helpListener = new Help(this) //辅助监听者,监听“帮助”、“关于”

JPanel landminePanel = new JPanel() //创建地雷面板

JPanel topPanel = new JPanel() //创建顶部面板

JPanel lowerPanel = new JPanel() //创建底部面板

public static MyButton [][] lei //主区按钮

public static int numberOfUnflaged //剩余的雷数,显示在topPanel上,用于提示用户

public static int numberOfClicked //已经翻开的格子数,当数字数字到"总格子数—雷数"时,即胜利

public static int usedTime//已用时间

public static JLabel numberOfUnflagedLabel = new JLabel()//创建剩雷数标签

public static JLabel timeLabel = new JLabel()//创建时间标签

public static Timer timer //创建计时

Keylistener keyListener = new Keylistener(this)

public Landmine()

{

super("扫雷__1.2版__小老头") //标题

setBounds(300,90,800,800) //设置窗口位置和大小

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)//最大化、最小化、关闭按钮

BorderLayout ff = new BorderLayout() //创建布局管理器

setLayout(ff) //关联布局管理器

setResizable(false) //禁止改变窗口大小

/*初始化一些数据*/

numberOfClicked = 0

numberOfUnflaged = 40

usedTime = 0

/*设置顶部面板*/

numberOfUnflagedLabel.setText("剩余雷数:"+numberOfUnflaged)//显示剩余雷数

numberOfUnflagedLabel.setFont(resources.fontOne)//设置剩雷数标签字体

numberOfUnflagedLabel.setIcon(resources.bombIconForLabel)//剩雷数标签图标(地雷形)

topPanel.add(numberOfUnflagedLabel) //剩雷数标签加入topPanel

timeLabel.setText("用时:" + usedTime)//显示剩余时间

timeLabel.setFont(resources.fontOne) //设置时间标签字体

timeLabel.setIcon(resources.clockIcon)//设置时间标签图标

topPanel.add(timeLabel) //时间标签加入topPanel

add(topPanel,BorderLayout.NORTH) //加入主面板上部

timer = new Timer(1000,new TimerListener())//计算器注册监听者

/*设置底部面板*/

JButton aboutJB = new JButton("关于") //创建“关于”按钮

JButton helpJB = new JButton("求救") //创建“求救”按钮

helpJB.addActionListener(helpListener) //"求救"按钮加入监听者

aboutJB.addActionListener(helpListener)//"关于"按钮加入监听者

helpJB.addKeyListener(keyListener)

aboutJB.addKeyListener(keyListener) //注册按键监听

lowerPanel.add(aboutJB) //“关于”按钮加入lowerPanel

lowerPanel.add(helpJB) //“帮助”按钮加入lowerPanel

add(lowerPanel,BorderLayout.SOUTH)

/*设置地雷面板*/

GridLayout dd = new GridLayout(16,16)

landminePanel.setLayout(dd) //布局管理

lei = new MyButton[18][18]

for(int i=0i<18++i)

{//创建下标0—17的按钮,18*18矩阵

for(int j=0j<18++j)

{

lei[i][j] = new MyButton(i,j)

}

}

for(int i=1i<17++i)

{//将下标1-16的按钮,加入面板、设置图标、翻开标记为假、加入监听者

for(int j=1j<17++j)

{

landminePanel.add(lei[i][j]) //按钮加入地雷面板

lei[i][j].setIcon(resources.smallIcon) //设置按钮图标

lei[i][j].isClicked = false //翻开标记设置为 假lei[i][j].setIcon(dead)

lei[i][j].addActionListener(listener) //加入监听者

lei[i][j].addMouseListener(listener) //加入鼠标事件监听者

lei[i][j].addKeyListener(keyListener) //按钮注册按键监听,当焦点在按钮上是能监听按键

}

}

add(landminePanel,BorderLayout.CENTER) //landminePanel加入主框架中央

addLandmine() //布雷

timer.start() //启动计时器

setVisible(true)//显示之

}

/*布雷*/

public static void addLandmine()

{//随机将40的按钮的是否为雷的标记isBomb设为真

for(int count = 0count<40/*blank*/)

{

int i = (int)(Math.random()*100 % 16 +1 )

int j = (int)(Math.random()*100 % 16 +1 )

if(lei[i][j].isBomb == false)

{

lei[i][j].isBomb = true

count++

}

}

}

class TimerListener implements ActionListener

{//内部类,时间监听

public void actionPerformed(ActionEvent e)

{

usedTime++

timeLabel.setText("用时:" + usedTime)

}

}

}

//

// Playing类 执行主要游戏 *** 作

class Playing implements ActionListener,MouseListener

{

static Resources resources = new Resources()

public static Landmine gui

public Playing(Landmine in )

{

gui = in

}

public void actionPerformed(ActionEvent event)

{

MyButton receive = (MyButton)event.getSource()

if(receive.isBomb)

{//如果翻到了雷。。

for(int i=1i<17++i)

{//将所有的雷图标设为 “地雷”

for(int j=1j<17++j)

{

if(gui.lei[i][j].isBomb)

gui.lei[i][j].setIcon(resources.bombIcon)

}

}

receive.setIcon(resources.deadIcon)//将踩到的地雷图标设为 “衰”

gui.timer.stop() //停止计时器

JOptionPane.showMessageDialog(null,"小朋友,你挂了…","失败!",

JOptionPane.INFORMATION_MESSAGE,

resources.deadIcon)//提示失败

int yourChose = JOptionPane.showConfirmDialog(null,"你可能是一不小心点错了,再来一局?" )

if(yourChose == JOptionPane.OK_OPTION)

{//点击“是”时

replay()

}

else

{//点击 “否” 或 “取消” 时退出程序

System.exit(0)

}

}

else if(receive.isClicked ==false)

{//未翻到雷

showBombNumber(receive)

}

}

public static void showBombNumber(MyButton in)

{//翻开点击的按钮

int numberOfLandmine = 0//记录雷的个数

in.isClicked = true //翻开标记设为真

/*检测周围8个方块是否为雷*/

if(gui.lei[in.num_x-1][in.num_y-1].isBomb == true) numberOfLandmine++//左上

if(gui.lei[in.num_x][in.num_y-1].isBomb == true) numberOfLandmine++ //上

if(gui.lei[in.num_x+1][in.num_y-1].isBomb == true) numberOfLandmine++//右上

if(gui.lei[in.num_x+1][in.num_y].isBomb == true) numberOfLandmine++ //右

if(gui.lei[in.num_x+1][in.num_y+1].isBomb == true) numberOfLandmine++//右下

if(gui.lei[in.num_x][in.num_y+1].isBomb == true) numberOfLandmine++ //下

if(gui.lei[in.num_x-1][in.num_y+1].isBomb == true) numberOfLandmine++//左下

if(gui.lei[in.num_x-1][in.num_y].isBomb == true) numberOfLandmine++ //左

in.setIcon(new ImageIcon("images/"+numberOfLandmine+".png"))//根据周围的雷数显示数字图标

gui.numberOfClicked++//翻开格子数+1

if(gui.numberOfClicked==216)

{//翻开216个格子时游戏成功,用户选择是否再来一局

int yourChoice = JOptionPane.showConfirmDialog(null,"恭喜你成功了!再来一盘吗?")

if(yourChoice == JOptionPane.OK_OPTION)

replay()

else

System.exit(0)

}

if(numberOfLandmine==0)

{//如果周围无雷,则将周围未翻开格子的全部翻开

if(gui.lei[in.num_x-1][in.num_y-1].isClicked == false)

showBombNumber(gui.lei[in.num_x-1][in.num_y-1])

if(gui.lei[in.num_x][in.num_y-1].isClicked == false)

showBombNumber(gui.lei[in.num_x][in.num_y-1])

if(gui.lei[in.num_x+1][in.num_y-1].isClicked == false)

showBombNumber(gui.lei[in.num_x+1][in.num_y-1])

if(gui.lei[in.num_x+1][in.num_y].isClicked == false)

showBombNumber(gui.lei[in.num_x+1][in.num_y])

if(gui.lei[in.num_x+1][in.num_y+1].isClicked == false)

showBombNumber(gui.lei[in.num_x+1][in.num_y+1])

if(gui.lei[in.num_x][in.num_y+1].isClicked == false)

showBombNumber(gui.lei[in.num_x][in.num_y+1])

if(gui.lei[in.num_x-1][in.num_y+1].isClicked == false)

showBombNumber(gui.lei[in.num_x-1][in.num_y+1])

if(gui.lei[in.num_x-1][in.num_y].isClicked == false)

showBombNumber(gui.lei[in.num_x-1][in.num_y])

}

}

public static void replay()

{//重新开始

gui.dispose() //释放框架资源

gui.timer.stop() //终止计时器

Landmine ff = new Landmine()//重新创建一个主类的实例

//这几条语句实现了重新开始————关闭上一个窗口,重新开启一个

//但是这种方法会造成内存的浪费,一个改进的方法是不关闭当年窗口,而是将当前窗口重新初始化

}

public void mousePressed(MouseEvent e)

{//当鼠标右键点击时自动调用此函数

int mods = e.getModifiers()

MyButton receive = (MyButton)e.getSource()

if((mods &InputEvent.BUTTON3_MASK) != 0)

{//鼠标右键

if(receive.isClicked == false)

{

receive.isRight = receive.isRight ? false : true//改变receive.isRight的值

if(receive.isRight)

{//如果添加标记,则剩余雷数-1,设置标签为“旗帜”

gui.numberOfUnflaged--

receive.setIcon(resources.flagIcon)

}

else

{//如果清除标记,则剩余雷数+1,设置标签为“未翻开”

gui.numberOfUnflaged++

receive.setIcon(resources.smallIcon)

}

gui.numberOfUnflagedLabel.setText("剩余雷数:"+gui.numberOfUnflaged)

//更新剩余雷数标签

}

}

}

public void mouseReleased(MouseEvent e){}

public void mouseExited(MouseEvent e) {}

public void mouseClicked(MouseEvent e){}

public void mouseEntered(MouseEvent e){}

}

//

// Help类,响应“关于”、“求救”

class Help implements ActionListener

{

static Resources resources = new Resources()

public static Landmine gui

public Help(Landmine in)

{

gui = in

}

public void actionPerformed(ActionEvent event)

{

if(event.getActionCommand()=="关于")

JOptionPane.showMessageDialog(null,"扫雷1.2版。。小老头出品")

if(event.getActionCommand()=="求救")

help()

}

public static void help()

{//求救

int stopNumber = (int)(Math.random() * gui.numberOfUnflaged + 1 )

int count = 0

for(int i=1i<17++i )

{

for(int j=1j<17++j)

{

if( gui.lei[i][j].isBomb && !gui.lei[i][j].isClicked &&!gui.lei[i][j].isRight )

{

count++

}

if(count == stopNumber)

{

gui.lei[i][j].setIcon(resources.badIcon)

return

}

}

}

}

}

//

// Keylistener类,响应键盘事件

class Keylistener implements KeyListener

{

static Resources resources = new Resources()

Landmine gui

public Keylistener(Landmine in)

{

gui = in

}

public void keyPressed(KeyEvent e)

{//有键按下时自动执行该方法

if(e.getKeyCode() == KeyEvent.VK_UP)

{//按键为 向上 时,将所有未标记的地雷显示出

for(int i=1i<17++i)

{

for(int j=1j<17++j)

{

if(gui.lei[i][j].isBomb &&!gui.lei[i][j].isRight)

gui.lei[i][j].setIcon(resources.badIcon)

}

}

}

if(e.getKeyCode() == KeyEvent.VK_DOWN)

{//按键为 向下 时,将所有未标记的地雷恢复为未点击的图标

for(int i=1i<17++i)

{

for(int j=1j<17++j)

{

if(gui.lei[i][j].isBomb &&!gui.lei[i][j].isRight)

gui.lei[i][j].setIcon(resources.smallIcon)

}

}

}

}

public void keyReleased(KeyEvent e){}

public void keyTyped(KeyEvent e){}

}

//

// 按钮类 MyBtton

class MyButton extends JButton

{

public int num_x,num_y //第几号方块

public boolean isBomb //是否为雷

public boolean isClicked //是否被点击

public int BombFlag //探雷标记

public boolean isRight //是否点击右键

public MyButton(int x, int y)

{

BombFlag = 0

num_x = x

num_y = y

isBomb = false

isClicked = true

isRight = false

}

}

//

// 资源类 其他类中用到的图标,字体等

class Resources

{

public static ImageIcon deadIcon

public static ImageIcon smallIcon

public static ImageIcon clockIcon

public static ImageIcon bombIcon

public static ImageIcon flagIcon

public static ImageIcon badIcon

public static ImageIcon bombIconForLabel

public static Font fontOne

public Resources()

{

deadIcon = new ImageIcon("images/dead.gif")

smallIcon = new ImageIcon("images/smallIcon.png")

clockIcon = new ImageIcon("images/clock2.png")

bombIcon = new ImageIcon("images/bomb.png")

flagIcon = new ImageIcon("images/flag_2.png")

badIcon = new ImageIcon("images/bad.gif")

bombIconForLabel = new ImageIcon("images/bombForLabel.gif")

fontOne = new Font("null",Font.BOLD,20)

}

}


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

原文地址:https://54852.com/yw/8083685.html

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

发表评论

登录后才能评论

评论列表(0条)

    保存