#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 java.awt.*import java.awt.event.*
import javax.swing.*
public class Frame
extends JFrame {
JTextField text
JLabel nowBomb, setBomb
int BombNum, BlockNum//禅滑闷 当前雷数,当前方块数
int rightBomb, restBomb, restBlock/贺弯/ 找到的地雷数,剩余雷数,剩余方块数
JButton start = new JButton("让旦 开始 ")
JPanel MenuPamel = new JPanel()
JPanel bombPanel = new JPanel()
Bomb[][] bombButton
JPanel c
BorderLayout borderLayout1 = new BorderLayout()
GridLayout gridLayout1 = new GridLayout()
public Frame() {
try {
setDefaultCloseOperation(EXIT_ON_CLOSE)
jbInit()
}
catch (Exception exception) {
exception.printStackTrace()
}
}
private void jbInit() throws Exception {
c = (JPanel) getContentPane()
setTitle("扫雷")
c.setBackground(Color.WHITE)
MenuPamel.setBackground(Color.GRAY)
c.setLayout(borderLayout1)
setSize(new Dimension(600, 600))
setResizable(false)
BlockNum = 144
BombNum = 10
text = new JTextField("10 ", 3)
nowBomb = new JLabel("当前雷数" + ":" + BombNum)
setBomb = new JLabel("设置地雷数")
start.addActionListener(new Frame1_start_actionAdapter(this))
MenuPamel.add(setBomb)
MenuPamel.add(text)
MenuPamel.add(start)
MenuPamel.add(nowBomb)
c.add(MenuPamel, java.awt.BorderLayout.SOUTH)
bombPanel.setLayout(gridLayout1)
gridLayout1.setColumns( (int) Math.sqrt(BlockNum))
gridLayout1.setRows( (int) Math.sqrt(BlockNum))
bombButton = new Bomb[ (int) Math.sqrt(BlockNum)][ (int) Math.sqrt(BlockNum)]
for (int i = 0i <(int) Math.sqrt(BlockNum)i++) {
for (int j = 0j <(int) Math.sqrt(BlockNum)j++) {
bombButton[i][j] = new Bomb(i, j)
//bombButton[i][j].setSize(10, 10)
bombButton[i][j].setFont(new Font("", Font.PLAIN, 14))//设置字体大小
bombButton[i][j].setForeground(Color.white)
bombButton[i][j].addMouseListener(new Bomb_mouseAdapter(this))
bombButton[i][j].addActionListener(new Bomb_actionAdapter(this))
bombPanel.add(bombButton[i][j])
}
}
c.add(bombPanel, java.awt.BorderLayout.CENTER)
startBomb()
}
/* 开始按钮 */
public void start_actionPerformed(ActionEvent e) {
int num=Integer.parseInt(text.getText().trim())
if (num >= 5 &&num <50) {
BombNum = num
startBomb()
}
else if (num <5) {
JOptionPane.showMessageDialog(null, "您设置的地雷数太少了,请重设!", "错误",
JOptionPane.ERROR_MESSAGE)
num=10
BombNum = num
}
else {
JOptionPane.showMessageDialog(null, "您设置的地雷数太多了,请重设!", "错误",
JOptionPane.ERROR_MESSAGE)
num=10
BombNum = num
}
}
/* 开始,布雷 */
public void startBomb() {
nowBomb.setText("当前雷数" + ":" + BombNum)
for (int i = 0i <(int) Math.sqrt(BlockNum)i++) {
for (int j = 0j <(int) Math.sqrt(BlockNum)j++) {
bombButton[i][j].isBomb = false
bombButton[i][j].isClicked = false
bombButton[i][j].isRight = false
bombButton[i][j].BombFlag = 0
bombButton[i][j].BombRoundCount = 9
bombButton[i][j].setEnabled(true)
bombButton[i][j].setText("")
bombButton[i][j].setFont(new Font("", Font.PLAIN, 14))//设置字体大小
bombButton[i][j].setForeground(Color.BLUE)
rightBomb = 0
restBomb = BombNum
restBlock = BlockNum - BombNum
}
}
for (int i = 0i <BombNum) {
int x = (int) (Math.random() * (int) (Math.sqrt(BlockNum) - 1))
int y = (int) (Math.random() * (int) (Math.sqrt(BlockNum) - 1))
if (bombButton[x][y].isBomb != true) {
bombButton[x][y].isBomb = true
i++
}
}
CountRoundBomb()
}
/* 计算方块周围雷数 */
public void CountRoundBomb() {
for (int i = 0i <(int) Math.sqrt(BlockNum)i++) {
for (int j = 0j <(int) Math.sqrt(BlockNum)j++) {
int count = 0
// 当需要检测的单元格本身无地雷的情况下,统计周围的地雷个数
if (bombButton[i][j].isBomb != true) {
for (int x = i - 1x <i + 2x++) {
for (int y = j - 1y <j + 2y++) {
if ( (x >= 0) &&(y >= 0)
&&(x <( (int) Math.sqrt(BlockNum)))
&&(y <( (int) Math.sqrt(BlockNum)))) {
if (bombButton[x][y].isBomb == true) {
count++
}
}
}
}
bombButton[i][j].BombRoundCount = count
}
}
}
}
/* 是否挖完了所有的雷 */
public void isWin() {
restBlock = BlockNum - BombNum
for (int i = 0i <(int) Math.sqrt(BlockNum)i++) {
for (int j = 0j <(int) Math.sqrt(BlockNum)j++) {
if (bombButton[i][j].isClicked == true) {
restBlock--
}
}
}
if (rightBomb == BombNum || restBlock == 0) {
JOptionPane.showMessageDialog(this, "您挖完了所有的雷,您胜利了!", "胜利",
JOptionPane.INFORMATION_MESSAGE)
startBomb()
}
}
/** 当选中的位置为空,则翻开周围的地图* */
public void isNull(Bomb ClickedButton) {
int i, j
i = ClickedButton.num_x
j = ClickedButton.num_y
for (int x = i - 1x <i + 2x++) {
for (int y = j - 1y <j + 2y++) {
if ( ( (x != i) || (y != j)) &&(x >= 0) &&(y >= 0)
&&(x <( (int) Math.sqrt(BlockNum)))
&&(y <( (int) Math.sqrt(BlockNum)))) {
if (bombButton[x][y].isBomb == false
&&bombButton[x][y].isClicked == false
&&bombButton[x][y].isRight == false) {
turn(bombButton[x][y])
}
}
}
}
}
/* 翻开 */
public void turn(Bomb ClickedButton) {
ClickedButton.setEnabled(false)
ClickedButton.isClicked = true
if (ClickedButton.BombRoundCount >0) {
ClickedButton.setText(ClickedButton.BombRoundCount + "")
}
else {
isNull(ClickedButton)
}
}
/* 左键点击 */
public void actionPerformed(ActionEvent e) {
if ( ( (Bomb) e.getSource()).isClicked == false
&&( (Bomb) e.getSource()).isRight == false) {
if ( ( (Bomb) e.getSource()).isBomb == false) {
turn( ( (Bomb) e.getSource()))
isWin()
}
else {
for (int i = 0i <(int) Math.sqrt(BlockNum)i++) {
for (int j = 0j <(int) Math.sqrt(BlockNum)j++) {
if (bombButton[i][j].isBomb == true) {
bombButton[i][j].setText("b")
}
}
}
( (Bomb) e.getSource()).setForeground(Color.RED)
( (Bomb) e.getSource()).setFont(new Font("", Font.BOLD, 20))
( (Bomb) e.getSource()).setText("X")
JOptionPane.showMessageDialog(this, "你踩到地雷了,按确定重来", "踩到地雷", 2)
startBomb()
}
}
}
/* 右键点击 */
public void mouseClicked(MouseEvent e) {
Bomb bombSource = (Bomb) e.getSource()
boolean right = SwingUtilities.isRightMouseButton(e)
if ( (right == true) &&(bombSource.isClicked == false)) {
bombSource.BombFlag = (bombSource.BombFlag + 1) % 3
if (bombSource.BombFlag == 1) {
if (restBomb >0) {
bombSource.setForeground(Color.RED)
bombSource.setText("F")
bombSource.isRight = true
restBomb--
}
else {
bombSource.BombFlag = 0
}
}
else if (bombSource.BombFlag == 2) {
restBomb++
bombSource.setText("Q")
bombSource.isRight = false
}
else {
bombSource.setText("")
}
if (bombSource.isBomb == true) {
if (bombSource.BombFlag == 1) {
rightBomb++
}
else if (bombSource.BombFlag == 2) {
rightBomb--
}
}
nowBomb.setText("当前雷数" + ":" + restBomb)
isWin()
}
}
public static void main(String[] args) {
Frame frame = new Frame()
frame.setVisible(true)
}
}
class Frame1_start_actionAdapter
implements ActionListener {
private Frame adaptee
Frame1_start_actionAdapter(Frame adaptee) {
this.adaptee = adaptee
}
public void actionPerformed(ActionEvent e) {
adaptee.start_actionPerformed(e)
}
}
////////////////////////////
class Bomb
extends JButton {
int num_x, num_y// 第几号方块
int BombRoundCount// 周围雷数
boolean isBomb// 是否为雷
boolean isClicked// 是否被点击
int BombFlag// 探雷标记
boolean isRight// 是否点击右键
public Bomb(int x, int y) {
num_x = x
num_y = y
BombFlag = 0
BombRoundCount = 9
isBomb = false
isClicked = false
isRight = false
}
}
class Bomb_actionAdapter
implements ActionListener {
private Frame adaptee
Bomb_actionAdapter(Frame adaptee) {
this.adaptee = adaptee
}
public void actionPerformed(ActionEvent e) {
adaptee.actionPerformed(e)
}
}
class Bomb_mouseAdapter
extends MouseAdapter {
private Frame adaptee
Bomb_mouseAdapter(Frame adaptee) {
this.adaptee = adaptee
}
public void mouseClicked(MouseEvent e) {
adaptee.mouseClicked(e)
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)