返回目录:游戏资讯
呵,我刚好有一个,花了半天时间才20分唉。
package com.game;
import java.awt.GridLayout;
import java.awt.event.*;
import java.util.Random;
import javax.swing.*;
/**
*
* @author ss
*
*/
public class Winmine extends JFrame implements ActionListener {
private static final long serialVersionUID = 1L;
GridLayout gridLayout;
JButton[] btn;
JPanel pMain;
JMenuBar jMenuBar;
JMenu game;
JMenuItem start;
int width, height;
Random random;
int[] mine;
public Winmine(int width, int height) {
random = new Random();
this.width = width;
this.height = height;
jMenuBar = new JMenuBar();
game = new JMenu("游戏");
start = new JMenuItem("开始");
start.addActionListener(this);
jMenuBar.add(game);
game.add(start);
this.setJMenuBar(jMenuBar);
gridLayout = new GridLayout(width, height);
pMain = new JPanel();
pMain.setLayout(gridLayout);
btn = new JButton[width * height];
for (int i = 0; i < width * height; i++) {
btn[i] = new JButton();
btn[i].addActionListener(this);
pMain.add(btn[i]);
}
produceMine(width, height);
this.getContentPane().add(pMain);
this.setSize(width * 50, height * 40);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent event) {
try {
JMenuItem jMenuItem = (JMenuItem) event.getSource();
jMenuItem.getAccelerator();
produceMine(width, height);
replay();
return;
} catch (Exception e) {
}
JButton button = (JButton) event.getSource();
for (int i = 0; i < width * height; i++) {
if (button == btn[i]) {
if (!checkMine(i)) {
JOptionPane.showMessageDialog(this, "对不起,你点到地雷啦!");
loseGame();
} else {
int num = getMineQoh(i);
btn[i].setText(num + "");
btn[i].setEnabled(false);
if (num == 0) {
clickBtn(i);
}
isWinGame();
break;
}
}
}
}
// 输了
public void loseGame() {
for (int i = 0; i < mine.length; i++) {
btn[mine[i]].setText("*");
}
for (int i = 0; i < btn.length; i++) {
btn[i].setEnabled(false);
}
}
// 重新7a64e4b893e5b19e362开始新游戏
public void replay() {
for (int i = 0; i < btn.length; i++) {
btn[i].setText("");
btn[i].setEnabled(true);
}
}
// 是否获胜
public void isWinGame() {
int qoh = 0;
for (int i = 0; i < btn.length; i++) {
if (!btn[i].isEnabled()) {
qoh++;
}
}
if (qoh == width * height - mine.length) {
JOptionPane.showMessageDialog(this, "恭喜您赢了!");
}
}
// 如果得到点击铵钮附近地雷数为0的话
public void clickBtn(int index) {
int[] around = getIndex(index);
for (int i = 0; i < around.length; i++) {
btn[around[i]].doClick();
}
}
// 得到点击铵钮附近的地雷数
public int getMineQoh(int index) {
int num = 0;
int[] around = getIndex(index);
for (int i = 0; i < around.length; i++) {
for (int j = 0; j < mine.length; j++) {
if (around[i] == mine[j]) {
num++;
continue;
}
}
}
return num;
}
// 得到点击铵钮附近的索引号
public int[] getIndex(int index) {
int[] left = new int[height - 2];
for (int i = 0; i < left.length; i++) {
left[i] = (i + 1) * width;
}
int[] right = new int[height - 2];
for (int i = 0; i < right.length; i++) {
right[i] = (i + 2) * width - 1;
}
int[] top = new int[width - 2];
for (int i = 0; i < top.length; i++) {
top[i] = i + 1;
}
int[] floor = new int[width - 2];
for (int i = 0; i < floor.length; i++) {
floor[i] = width * (height - 1) + i + 1;
}
int[] around = new int[3];
if (index == 0) {
around[0] = 1;
around[1] = width;
around[2] = width + 1;
} else if (index == width - 1) {
around[0] = width - 2;
around[1] = 2 * width - 2;
around[2] = 2 * width - 1;
} else if (index == (height - 1) * width) {
around[0] = (height - 2) * width;
around[1] = (height - 2) * width + 1;
around[2] = (height - 1) * width + 1;
} else if (index == height * width - 1) {
around[0] = (height - 1) * width - 2;
around[1] = (height - 1) * width - 1;
around[2] = height * width - 2;
} else {
around = new int[5];
for (int i = 0; i < top.length; i++) {
try {
if (index == top[i]) {
around[0] = top[i] - 1;
around[1] = top[i] + 1;
around[2] = top[i] + width - 1;
around[3] = top[i] + width;
around[4] = top[i] + width + 1;
return around;
} else if (index == floor[i]) {
around[0] = floor[i] - width - 1;
around[1] = floor[i] - width;
around[2] = floor[i] - width + 1;
around[3] = floor[i] - 1;
around[4] = floor[i] + 1;
return around;
} else if (index == left[i]) {
around[0] = left[i] - width;
around[1] = left[i] - width + 1;
around[2] = left[i] + 1;
around[3] = left[i] + width;
around[4] = left[i] + width + 1;
return around;
} else if (index == right[i]) {
around[0] = right[i] - width - 1;
around[1] = right[i] - width;
around[2] = right[i] - 1;
around[3] = right[i] + width - 1;
around[4] = right[i] + width;
return around;
}
} catch (Exception e) {
}
}
around = new int[8];
around[0] = index - width - 1;
around[1] = index - width;
around[2] = index - width + 1;
around[3] = index - 1;
around[4] = index + 1;
around[5] = index + width - 1;
around[6] = index + width;
around[7] = index + width + 1;
}
return around;
}
// 检查点到的铵钮是否是地雷
public boolean checkMine(int btnIndex) {
return checkNum(btnIndex, mine);
}
// 得到不重复数字的方法
public boolean checkNum(int num, int[] arrayNum) {
for (int i = 0; i < arrayNum.length; i++) {
if (arrayNum[i] == num) {
return false;
}
}
return true;
}
// 生成地雷的方法
public void produceMine(int width, int height) {
int qoh = width * height / 5;
int index;
mine = new int[qoh];
for (int i = 0; i < qoh; i++) {
index = random.nextInt(width * height);
if (checkNum(index, mine)) {
mine[i] = index;
} else {
i--;
}
}
}
public static void main(String[] args) {
new Winmine(16, 16);
}
}
感觉还行
这是字符界面的扫雷:e799bee5baa6e79fa5e98193e58685e5aeb9336(如果要MFC程序源码,给我邮箱)
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <windows.h>
#include <conio.h>
// defines
#define KEY_UP 0xE048
#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 = x;cd.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 = Width; poolHeight = 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=0; y<=Height+1; ++y)
{
for(int x=0; x<=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=1;y<=poolHeight;++y)
{
CConsoleWnd::GotoXY(1, y);
for(int x=1;x<=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=-1;Y<=1;++Y)
for(int X=-1;X<=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=-1;Y<=1;++Y)
for(int X=-1;X<=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: Win; otherwise: 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=-1;y<=1;++y)
for(int x=-1;x<=1;++x)
{
if(bm_gamepool[curY+y][curX+x] & GMARK_MARK)
--nb;
}
if(nb==0)
{
for(int y=-1;y<=1;++y)
for(int x=-1;x<=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;
}
//}}
求采纳为满意回答。
简易“推箱子”游戏C代码:
#include <stdio.h>
#include <conio.h>
#include<stdlib.h>
#include<windows.h>
int m =0; //m代表第几关
struct maps{short a[9][11]; };
struct maps map[5]={ 0,0,0,0,0,0,0,0,0,0,0, //共5关,每关9行
0,1,1,1,1,1,1,1,0,0,0,
0,1,0,0,0,0,0,1,1,1,0,
1,1,4,1,1,1,0,0,0,1,0, //0空地,1墙
1,5,0,0,4,0,0,4,0,1,0, //4是箱子,5是人
1,0,3,3,1,0,4,0,1,1,0, //3是目的地
1,1,3,3,1,0,0,0,1,0,0, //7是箱子在目的地(4+3)
0,1,1,1,1,1,1,1,1,0,0, //8是人在目的地(5+3)
0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,
0,0,1,1,1,1,0,0,0,0,0,
0,0,1,5,0,1,1,1,0,0,0,
0,0,1,0,4,0,0,1,0,0,0,
0,1,1,1,0,1,0,1,1,0,0,
0,1,3,1,0,1,0,0,1,0,0,
0,1,3,4,0,0,1,0,1,0,0,
0,1,3,0,0,0,4,0,1,0,0,
0,1,1,1,1,1,1,1,1,0,0,
0,0,0,0,0,0,0,0,0,0,0,
0,0,0,1,1,1,1,1,1,1,0,
0,0,1,1,0,0,1,0,5,1,0,
0,0,1,0,0,0,1,0,0,1,0,
0,0,1,4,0,4,0,4,0,1,0,
0,0,1,0,4,1,1,0,0,1,0,
1,1,1,0,4,0,1,0,1,1,0,
1,3,3,3,3,3,0,0,1,0,0,
1,1,1,1,1,1,1,1,1,0,0,
0,1,1,1,1,1,1,1,1,1,0,
0,1,0,0,1,1,0,0,0,1,0,
0,1,0,0,0,4,0,0,0,1,0,
0,1,4,0,1,1,1,0,4,1,0,
0,1,0,1,3,3,3,1,0,1,0,
1,1,0,1,3,3,3,1,0,1,1,
1,0,4,0,0,4,0,0,4,0,1,
1,0,0,0,0,0,1,0,5,0,1,
1,1,1,1,1,1,1,1,1,1,1,
0,0,0,0,0,0,0,0,0,0,0,
0,0,0,1,1,1,1,1,1,0,0,
0,1,1,1,0,0,0,0,1,0,0,
1,1,3,0,4,1,1,0,1,1,0,
1,3,3,4,0,4,0,0,5,1,0,
1,3,3,0,4,0,4,0,1,1,0,
1,1,1,1,1,1,0,0,1,0,0,
0,0,0,0,0,1,1,1,1,0,0,
0,0,0,0,0,0,0,0,0,0,0 };
void DrMap( ) //绘制地图
{ CONSOLE_CURSOR_INFO cursor_info={1,0}; //隐藏光标的设置
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE),&cursor_info);
printf("\n\n \t\t\b推箱子");
printf("\n \t");
for (int i = 0; i < 9; i++)
{for (int j = 0; j < 11; j++)
{switch (map[m].a[i][j])
{case 0: printf(" "); break;
case 1: printf("■"); break;
case 3: printf("◎e799bee5baa6e997aee7ad94e59b9ee7ad94331");break;
case 4: printf("□"); break;
case 5: printf("♀"); break; //5是人
case 7: printf("□"); break; //4 + 3箱子在目的地中
case 8: printf("♀");break; // 5 + 3人在目的地中
}
}
printf("\n\t");
}
}
void gtxy(int x, int y) //控制光标位置的函数
{ COORD coord;
coord.X = x;
coord.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}
void start( ) //开始游戏
{ int r, c; //存储人的下标
for (int i = 0; i < 9; i++)
{ for (int j = 0; j < 11; j++)
{if (map[m].a[i][j] == 5||map[m].a[i][j]==8) { r = i; c = j; } } //i j 人的下标
}
char key;
key = getch( );
switch (key)
{case 'W':
case 'w':
case 72:
if (map[m]. a[r - 1][c] == 0|| map[m]. a [r - 1][c] == 3)
{ gtxy(2*c+8,r-1+3); printf("♀"); // gtxy(2*c+8,r-1+3)是到指定位置输出字符
if(map[m]. a[r ][c] == 5){gtxy(2*c+8,r+3); printf(" "); }
if(map[m]. a[r ][c] == 8){gtxy(2*c+8,r+3); printf("◎");}
map[m]. a [r - 1][c] += 5; map[m]. a [r][c] -= 5; }
else if (map[m]. a [r - 1][c] == 4 || map[m]. a [r - 1][c] == 7)
{ if (map[m]. a [r - 2][c] == 0 || map[m]. a [r - 2][c] == 3)
{ gtxy(2*c+8,r-2+3); printf("□"); gtxy(2*c+8,r-1+3); printf("♀");
if(map[m]. a[r ][c] == 5){gtxy(2*c+8,r+3); printf(" "); }
if(map[m]. a[r ][c] == 8){gtxy(2*c+8,r+3); printf("◎");}
map[m]. a [r - 2][c] += 4; map[m]. a [r - 1][c] += 1;
map[m]. a [r][c] -= 5; }
} break;
case 'S':
case 's':
case 80:
if (map[m]. a [r + 1][c] == 0 || map[m]. a [r + 1][c] == 3)
{ gtxy(2*c+8,r+1+3); printf("♀");
if(map[m]. a[r ][c] == 5){gtxy(2*c+8,r+3); printf(" "); }
if(map[m]. a[r ][c] == 8){gtxy(2*c+8,r+3); printf("◎");}
map[m]. a [r + 1][c] += 5; map[m]. a [r][c] -= 5; }
else if (map[m]. a [r + 1][c] == 4 || map[m]. a [r+ 1][c] == 7)
{ if (map[m]. a [r + 2][c] == 0 || map[m]. a [r + 2][c] == 3)
{ gtxy(2*c+8,r+2+3); printf("□"); gtxy(2*c+8,r+1+3); printf("♀");
if(map[m]. a[r ][c] == 5){gtxy(2*c+8,r+3); printf(" "); }
if(map[m]. a[r ][c] == 8){gtxy(2*c+8,r+3); printf("◎");}
map[m]. a [r + 2][c] += 4; map[m]. a [r + 1][c] += 1;
map[m]. a [r][c] -= 5; }
}break;
case 'A':
case 'a':
case 75:
if (map[m]. a [r ][c - 1] == 0 || map[m]. a [r ][c - 1] == 3)
{ gtxy(2*(c-1)+8,r+3); printf("♀");
if(map[m]. a[r ][c] == 5){gtxy(2*c+8,r+3); printf(" "); }
if(map[m]. a[r ][c] == 8){gtxy(2*c+8,r+3); printf("◎");}
map[m]. a [r ][c - 1] += 5; map[m]. a [r][c] -= 5; }
else if (map[m]. a [r][c - 1] == 4 || map[m]. a [r][c - 1] == 7)
{if (map[m]. a [r ][c - 2] == 0 || map[m]. a [r ][c - 2] == 3)
{ gtxy(2*(c-2)+8,r+3); printf("□"); gtxy(2*(c-1)+8,r+3); printf("♀");
if(map[m]. a[r ][c] == 5){gtxy(2*c+8,r+3); printf(" "); }
if(map[m]. a[r ][c] == 8){gtxy(2*c+8,r+3); printf("◎");}
map[m]. a [r ][c - 2] += 4; map[m]. a [r ][c - 1] += 1;
map[m]. a [r][c] -= 5; }
}break;
case 'D':
case 'd':
case 77:
if (map[m]. a [r][c + 1] == 0 || map[m]. a [r][c + 1] == 3)
{ gtxy(2*(c+1)+8,r+3); printf("♀");
if(map[m]. a[r ][c] == 5){gtxy(2*c+8,r+3); printf(" "); }
if(map[m]. a[r ][c] == 8) {gtxy(2*c+8,r+3); printf("◎");}
map[m]. a [r][c + 1] += 5; map[m]. a [r][c] -= 5; }
else if (map[m]. a [r][c + 1] == 4 || map[m]. a [r][c + 1] == 7)
{ if (map[m]. a [r][c + 2] == 0 || map[m]. a [r][c + 2] == 3)
{ gtxy(2*(c+2)+8,r+3); printf("□"); gtxy(2*(c+1)+8,r+3); printf("♀");
if(map[m]. a[r ][c] == 5){gtxy(2*c+8,r+3); printf(" "); }
if(map[m]. a[r ][c] == 8){gtxy(2*c+8,r+3); printf("◎");}
map[m]. a [r][c + 2] += 4; map[m]. a [r][c + 1] += 1;
map[m]. a [r][c] -= 5; }
}break;
}
}
int ifwan( ) //是否完成(1是0否)
{ if(m==0){if(map[m].a[5][2]==7&& map[m].a[5][3]==7&&
map[m].a[6][2]==7&& map[m].a[6][3]==7) return 1;}
if(m==1){if(map[m].a[5][2]==7&& map[m].a[6][2]==7&&
map[m].a[7][2]==7) return 1;}
if(m==2){if(map[m].a[7][1]==7&& map[m].a[7][2]==7&& map[m].a[7][3]==7&&
map[m].a[7][4]==7&& map[m].a[7][5]==7) return 1;}
if(m==3){if(map[m].a[4][4]==7&& map[m].a[4][5]==7&& map[m].a[4][6]==7&&
map[m].a[5][4]==7&& map[m].a[5][5]==7&& map[m].a[5][6]==7) return 1;}
if(m==4){if(map[m].a[3][2]==7&& map[m].a[4][1]==7&& map[m].a[4][2]==7&&
map[m].a[5][1]==7&& map[m].a[5][2]==7) return 1;}
return 0;
}
int main( ) //主函数
{ while (1)
{ system("cls");
DrMap( );
while (1)
{ start( );
if(ifwan()){printf("\007");break;} //完成后响铃
}
m+=1;
}
return 0;
}
#include”7a686964616fe59b9ee7ad94332stdio.h”#include”conio.h”#include”stdlib.
h”#include”dos.h”#include”time.h”
int a[16+1][30+1];/*数组用来存放地雷和数字的
*/多出一个是用来控制边界的
int flag1,flag2;/*长宽调整标志*/
int color_flag=1,lei_number=10,high=8,width=8,face_
flag=1;
struct time1/*调用时间函数*/
{unsigned char ti_min; unsigned char ti_hour; unsigned char
ti_hund; unsigned char ti_sec;}t;
void rect()
{ /*本模块的功能是在C界面上画边框,以增加C的
友好性和可观性*/
int i; textcolor(14); textbackground(color_flag);
clrscr();/*设置字体和背景颜色*/
gotoxy(1,2);putchar(201);/*左上角*/
for(i=1;i<79;i++)
{gotoxy(i+1,2);putchar(205);}/*直线*/
putchar(187);/*右上角*/
for(i=2;i<23;i++)
{gotoxy(80,i+1);putchar(186);}/*直线*/
gotoxy(80,24);putchar(188);/*右下角*/
for(i=1;i<79;i++)
{gotoxy(i+1,24);putchar(205);}/*竖线*/
gotoxy(1,24);putchar(200);/*右上角*/
for(i=2;i<23;i++)
{gotoxy(1,i+1);putchar(186);}/*竖线*/
gotoxy(30,1);cprintf(“WELCOMETO SAOLEI!”);
gotoxy(28,25);cprintf(“COPYRIGHT---LANGCY--
-2002”);}
void rect_s()
{ int i;/*根据长宽调整标志画边框*/
gotoxy(flag2-1,flag1-1);putchar(218);
gotoxy(flag2+width*2-1,flag1-1);putchar(191);
gotoxy(flag2-1,flag1+high);putchar(192);
gotoxy(flag2+width*2-1,flag1+high);putchar(217);
for(i=flag2;i<flag2+width*2-1;i++)
{gotoxy(i,flag1-1);putchar(196);
gotoxy(i,flag1+high);putchar(196);}
for(i=flag1;i<flag1+high;i++)
{gotoxy(flag2-1,i);putchar(179);
gotoxy(flag2+width*2-1,i);putchar(179);}}
void color()/*颜色选择*/
{ char ch; rect();
gotoxy(25,6);cprintf(”This is the color table!”);
gotoxy(30,8);cprintf(”1-black”); gotoxy(30,9);cprintf(”
2-blue”);
gotoxy(30,10);cprintf(”3-green”); gotoxy(30,11);cprintf
(”4-bluesky”);
gotoxy(30,12);cprintf(”5-red”); gotoxy(30,13);cprintf(”
6-pink”);
gotoxy(30,14); cprintf(”7-yellow”); gotoxy(30,15);
cprintf(”8-gray”);
gotoxy(30,16);cprintf(”ESC-main menu”);
gotoxy(25,18);cprintf(”Please choose the color numble:”);
while(1)
{ch=getch();
switch(ch)
{case’1’: color_flag=0;cprintf(”black!”);break;
case’2’: color_flag=1;cprintf(”blue!”);break;
case’3’: color_flag=2;cprintf(”green!”);break;
case’4’: color_flag=3;cprintf(”bluesky!”);break;
case’5’: color_flag=4;cprintf(”red!”);break;
case’6’: color_flag=5;cprintf(”pink!”);break;
case’7’: color_flag=6;cprintf(”yellow!”);break;
case’8’: color_flag=7;cprintf(”gray!”);break;
case 27: main();break;
case 13: main();break;
default :break;}}}
void size()/*选择大小*/
{ char ch; rect();
gotoxy(25,8);cprintf(”This is the lei_size table!”);
gotoxy(30,10);cprintf(”1--high=8,width=8”); gotoxy
(30,11);cprintf(”2--high=8,width=16”);
gotoxy(30,12);cprintf(”3--high=16,width=16”); go-
toxy(30,13);cprintf(”4--high=16,width=20”);
gotoxy(30,14);cprintf(”5--high=16,width=25”); go-
toxy(30,15);cprintf(”6--high=16,width=30”);
gotoxy(30,16);cprintf(”ESC-main menu”);
gotoxy(25,18);cprintf(”Please choose the lei_numble:”);
while(1)
{ch=getch();
switch(ch)
{ case’1’: high=8;width=8;cprintf(”high=8,width=
8”);break;
case’2’: high=8;width=16;cprintf(”high=8,width=
16”);break;
case’3’: high=16;width=16;cprintf(”high=16,width=
16”);break;
case’4’: high=16;width=20;cprintf(”high=16,width=
20”);break;
case’5’: high=16;width=25;cprintf(”high=16,width=
25”);break;
case’6’: high=16;width=30;cprintf(”high=16,width=
30”);break;
case 27: main();break;
case 13: main();break;
default :break; }}}
void lei_num()/*选择雷的数目*/
{ char ch; rect();
gotoxy(25,8);cprintf(”This is the lei_number table!”);
gotoxy(30,10);cprintf(”1-10”); gotoxy(30,11);cprintf(”
2-20”); gotoxy(30,12);cprintf(”3-40”);
gotoxy(30,13);cprintf(”4-50”); gotoxy(30,14);cprintf(”
5-80”); gotoxy(30,15);cprintf(”6-100”);
gotoxy(30,16);cprintf(”ESC-main menu”);
gotoxy(25,18);cprintf(”Please choose the lei_numble:”);
while(1)
{ch=getch();
switch(ch)
{ case’1’: lei_number=10;cprintf(”10”);break;
case’2’: lei_number=20;cprintf(”20”);break;
case’3’: lei_number=40;cprintf(”40”);break;
case’4’: lei_number=50;cprintf(”50”);break;
case’5’: lei_number=80;cprintf(”80”);break;
case’6’: lei_number=100;cprintf(”100”);break;
case 27: main();break;
case 13: main();break;
default :break; }}}
void face()/*选择图标*/
{ char ch; rect();
gotoxy(25,8);cprintf(”This is the face table!”);
gotoxy(30,10);cprintf(”1--smail face”); gotoxy(30,
11);cprintf(”2--heart”);
gotoxy(30,12);cprintf(”3--rectangle”); gotoxy(30,13);
cprintf(”4--flower”);
gotoxy(30,14);cprintf(”5--peach”); gotoxy(30,15);
cprintf(”6--music”);
gotoxy(30,16);cprintf(”ESC-main menu”);
gotoxy(25,18);cprintf(”Please choose the face:”);
while(1)
{ch=getch();
switch(ch)
{ case’1’:face_flag=1;printf(”smail face!”);break;
case’2’: face_flag=3;printf(”heart!”);break;
case’3’: face_flag=4;printf(”rectangle!”);break;
case’4’: face_flag=5;printf(”flower!”);break;
case’5’: face_flag=6;printf(”peach!”);break;
case’6’: face_flag=14;printf(”music!”);break;
case 27: main();break; case 13: main();break; default :
break;}}}
void fail()/*没有扫成功,重新再来*/
{ int i,j,t3,t4; char ch; flag1=(23-high)/2+2; flag2=
(80-2*width)/2;
for(i=1,t3=flag1;i<high+1;i++,t3++)
for(j=1,t4=flag2;j<width+1;j++,t4+=2)
{ gotoxy(t4,t3); if(a[i][j]==-1) putchar(15); else
printf(”%d”,a[i][j]); }
gotoxy(25,23);printf(”Do you want to continue? <y/n
>”);
ch=getch(); if(ch==’y’||ch==’Y’) saolei(); else
main();}
void succeed()/*扫雷成功,再来否*/
{ char ch; gotoxy(30,22);cprintf(”Congradulation! You
win!”);
gotoxy(25,23);printf(”Do you want to continue? <y/n
>”); ch=getch();
if(ch==’y’||ch==’Y’) saolei(); else main();}
saolei()/*扫雷函数*/
{ int i,j,countlei,left=lei_number,time_flag1,time_flag2;
int t1=1,t2=1,lei_flag=0,t3,t4; char ch; rect();
flag1=(23-high)/2+2; flag2=(80-2*width)/2;
for(i=0;i<high+1;i++)
for(j=0;j<width+1;j++)
a[i][j]=0;/*初始值*/
while(lei_flag<=lei_number)
{ t1=1+random(high);t2=1+random(width);
if(a[t1][t2]! =-1) { a[t1][t2]=-1; lei_flag++;}
}/*随机赋地雷*/
for(i=1;i<high+1;i++)
for(j=1;j<width+1;j++)
{ if(a[i][j]! =-1)
{ countlei=0;/*由于上面已经考虑了边界,这里不需
判断边界*/
if(a[i-1][j-1]==-1) countlei++; if(a[i-1][j]=
=-1) countlei++;
if(a[i-1][j+1]==-1) countlei++;if(a[i][j-1]=
=-1) countlei++;
if(a[i][j+1]==-1) countlei++; if(a[i+1][j-1]=
=-1) countlei++;
if(a[i+1][j]==-1) countlei++; if(a[i+1][j+1]=
=-1) countlei++;
a[i][j]=countlei; } }/*计算各个地区周围的地雷的
数目*/
rect_s();/*画小边框*/
for(i=1,t3=flag1;i<high+1;i++,t3++)
for(j=1,t4=flag2;j<width+1;j++,t4+=2)
{gotoxy(t4,t3);putchar(face_flag);}
gotoxy(4,3);cprintf(”Left:%3d”,left); gotoxy(70,3);
cprintf(”Time:000”);
gettime(&t);/*从系统中取出时间*/
time_flag1=(int)t.ti_min*60+(int)t.ti_sec; i=1;j=1;
while(1)/*键盘操作*/
{ /*72-up,75-left,77-right,80-down,27-esc,13
-enter,32-space*/
gettime(&t);
time_flag2=(int)t.ti_min*60+(int)t.ti_sec-time_
flag1;
gotoxy(68,3);cprintf(”Time:%3d”,time_flag2);
gotoxy(flag2,flag1); ch=getch();
switch(ch) { case 72 : i--;flag1--; if(i<=0) {i+
+;flag1++;} /*判断越界*/
gotoxy(flag2,flag1); break;
case 75 : j--;flag2-=2; if(j<=0) {j++;flag2+=
2;} gotoxy(flag2,flag1); break; case 77 : j++;flag2+=
2;if(j>width) {j--;flag2-=2;} gotoxy(flag2,flag1);
break;
case 80 : i++;flag1++; if(i>high) {i--;flag1-
-;} gotoxy(flag2,flag1); break;
case 32 : if(a[i][j]==-1)
{ putchar(15);left--; if(left==0) succeed();go-
toxy(4,3);cprintf(”Left:%3d”,left);
gotoxy(flag2,flag1); } else fail(); break;
case 13 : if(a[i][j]! =-1)
{ printf(”%d”,a[i][j]); gotoxy(flag2,flag1); }
if(a[i][j]==-1) fail(); break;
case 27 : main();break;
default : break;}}}
main()/*主函数*/
{ char ch; rect();
gotoxy(25,7);printf(”This is the main menu!”);
gotoxy(30,9);printf(”Enter-start!”); gotoxy(30,10);
printf(”1--Choose the color!”);
gotoxy(30,11);printf(”2--Choose the size!”); gotoxy
(30,12);printf(”3--Choose the lei_number!”);
gotoxy(30,13);printf(”4--Choose the face!”); gotoxy
(30,14);printf(”ESC--exit!”);
gotoxy(25,16);printf(”Please select what you want!”);
while(1)
{ch=getch();
switch(ch)
{case’1’: color();break;
case’2’: size();break;
case’3’: lei_num();break;
case’4’: face();break;
case 27: rect();
gotoxy(30,13);
printf(”Thank you ! Good Bye!”);
getch();
exit(0);break;
case 13:
saolei();break;
default :break;}}}