package screen;
import java.awt.*;
import javax.swing.*;
import java.util.ArrayList;
import com.borland.jbcl.layout.*;
import findmines.SettingParameters;
/**
* Title:
* Description:
* Copyright: Copyright (c) 2004
* Company:
* @author
* @version 1.0
*/
public class MinesPanel extends JPanel {
MainScreen parentPanel;
ArrayList blocks = new ArrayList();
int panelWidth;
int panelHeight;
SettingParameters param;
boolean firstDown =false; //@ property begin --when the first block is clicked by left mouse button,
// we can arrang all the mines in the panel
int flagCount = 0;
public MinesPanel(MainScreen screen,SettingParameters pa){
parentPanel = screen;
param = pa;
try {
jbInit();
}
catch(Exception ex) {
ex.printStackTrace();
}
setAllBlock(param.getParamWidth(),param.getParamHeight());
}
public void gameReady()
{
this.firstDown = false;
this.flagCount = 0;
int i=0;
while(i<param.getParamHeight()*param.getParamWidth())
{
((Block)blocks.get(i)).reset();
i++;
}
}
/*
public void gameOver()
called by : one block which has mine is touched
inform MainScreen that game has been over
*/
public void gameOver()
{
parentPanel.gameOver();
int i=0;
while(i<param.getParamHeight()*param.getParamWidth())
{
((Block)blocks.get(i)).gameOver();
i++;
}
}
/* the method should be called by blocks */
public void leftButtonClicked(int posRow,int posCol)
{
if (!firstDown)
{
firstDown = true;
this.arrageMines(posRow,posCol);
this.parentPanel.gameStart();
}
}
/* methord: one right Button Clicked
the identified mines should be add or delete
called by: class Block
@param add : true, add a flag
false, remove a flag
*/
public void addIdentifiedMines(boolean add)
{
if(add)
{
flagCount++;
}
else
{
flagCount--;
}
this.parentPanel.flagChanged(flagCount);
}
public void setAllBlock(int width,int height)
{
int i=0;
blocks.clear();
this.removeAll();
this.setLayout(new GridLayout(height,width));
while(i<width*height)
{
Block block = new Block(this);
int row = i/width;
int col = i%width;
block.setPosition(row,col);
blocks.add(i,block);
this.add(block,"null");
i=i+1;
}
panelHeight = height*param.mineWidth+2*2;
panelWidth = width*param.mineWidth+2*2;
Dimension d = new Dimension(panelWidth,panelHeight);
this.setMaximumSize(d);
this.setMinimumSize(d);
this.setPreferredSize(d);
this.repaint();
/* arrangeMines is canceled here
it will happen when the first block is clicked by left mouse button
*/
//arrageMines();
}
/*
public void doubleButtonPressed(int posRow, int posCol)
{
int index = posRow*param.getParamWidth()+posCol;
int testBlockIndex=-1;
int columns = param.getParamWidth() ;
int rows = param.getParamHeight() ;
// left-top Block
if(posRow>0 && posCol>0)
{
testBlockIndex = (posRow-1)*columns+(posCol-1);
((Block)this.blocks.get(testBlockIndex)).setBorder(BorderFactory.createLineBorder(Color.gray));
}
// top Block
if (posRow>0)
{
testBlockIndex = (posRow-1)*columns+ posCol ;
((Block)this.blocks.get(testBlockIndex)).setBorder(BorderFactory.createLineBorder(Color.gray));
}
// right-top Block
if (posRow>0 && posCol<columns-1)
{
testBlockIndex = (posRow-1)*columns+ (posCol+1) ;
((Block)this.blocks.get(testBlockIndex)).setBorder(BorderFactory.createLineBorder(Color.gray));
}
// left Block
if (posCol>0)
{
testBlockIndex = posRow*columns +posCol-1 ;
((Block)this.blocks.get(testBlockIndex)).setBorder(BorderFactory.createLineBorder(Color.gray));
}
// right Block
if (posCol<columns-1)
{
testBlockIndex = posRow*columns + (posCol+1) ;
((Block)this.blocks.get(testBlockIndex)).setBorder(BorderFactory.createLineBorder(Color.gray));
}
// left-bottom Block
if(posRow<rows-1 && posCol>0)
{
testBlockIndex = (posRow+1)*columns+(posCol-1);
((Block)this.blocks.get(testBlockIndex)).setBorder(BorderFactory.createLineBorder(Color.gray));
}
// bottom Block
if(posRow<rows-1)
{
testBlockIndex = (posRow+1)*columns+ posCol;
((Block)this.blocks.get(testBlockIndex)).setBorder(BorderFactory.createLineBorder(Color.gray));
}
// right-bottom Block
if(posRow<rows-1 && posCol<columns-1)
{
testBlockIndex = (posRow+1)*columns+(posCol+1);
((Block)this.blocks.get(testBlockIndex)).setBorder(BorderFactory.createLineBorder(Color.gray));
}
}
*/
/*
public void doubleButtonRelease(int posRow, int posCol)
{
int index = posRow*param.getParamWidth()+posCol;
int testBlockIndex=-1;
int columns = param.getParamWidth() ;
int rows = param.getParamHeight() ;
// left-top Block
if(posRow>0 && posCol>0)
{
testBlockIndex = (posRow-1)*columns+(posCol-1);
((Block)this.blocks.get(testBlockIndex)).setBorder(BorderFactory.createRaisedBevelBorder());
}
// top Block
if (posRow>0)
{
testBlockIndex = (posRow-1)*columns+ posCol ;
((Block)this.blocks.get(testBlockIndex)).setBorder(BorderFactory.createRaisedBevelBorder());
}
// right-top Block
if (posRow>0 && posCol<columns-1)
{
testBlockIndex = (posRow-1)*columns+ (posCol+1) ;
((Block)this.blocks.get(testBlockIndex)).setBorder(BorderFactory.createRaisedBevelBorder());
}
// left Block
if (posCol>0)
{
testBlockIndex = posRow*columns +posCol-1 ;
((Block)this.blocks.get(testBlockIndex)).setBorder(BorderFactory.createRaisedBevelBorder());
}
// right Block
if (posCol<columns-1)
{
testBlockIndex = posRow*columns + (posCol+1) ;
((Block)this.blocks.get(testBlockIndex)).setBorder(BorderFactory.createRaisedBevelBorder());
}
// left-bottom Block
if(posRow<rows-1 && posCol>0)
{
testBlockIndex = (posRow+1)*columns+(posCol-1);
((Block)this.blocks.get(testBlockIndex)).setBorder(BorderFactory.createRaisedBevelBorder());
}
// bottom Block //
if(posRow<rows-1)
{
testBlockIndex = (posRow+1)*columns+ posCol;
((Block)this.blocks.get(testBlockIndex)).setBorder(BorderFactory.createRaisedBevelBorder());
}
// right-bottom Block //
if(posRow<rows-1 && posCol<columns-1)
{
testBlockIndex = (posRow+1)*columns+(posCol+1);
((Block)this.blocks.get(testBlockIndex)).setBorder(BorderFactory.createRaisedBevelBorder());
}
}
*/
public boolean openNearArea(int posRow, int posCol)
{
if(!wrongPosition(posRow,posCol)) return false;
int flags = this.getFlags(posRow,posCol);
//System.out.println(" Row "+String.valueOf(posRow)+" col:"+String.valueOf(posCol)+" flags"+String.valueOf(flags));
if (flags == -1)
{
this.gameOver() ;
return false;
}
int index = posRow*param.getParamWidth()+posCol;
int existMines = ((Block)blocks.get(index)).getArroundMines() ;
if(flags != existMines && existMines !=0 ) return false;
///////////////////////////////////////////
/* when flags are equal to existed Mines */
int testBlockIndex=-1;
int testBlockArroundMines = 0;
int columns = param.getParamWidth() ;
int rows = param.getParamHeight() ;
/* left-top Block */
if(posRow>0 && posCol>0)
{
testBlockIndex = (posRow-1)*columns+(posCol-1);
Block testBlock = ((Block)this.blocks.get(testBlockIndex));
if(testBlock.untouched() )
{
testBlock.openArea();
testBlockArroundMines = testBlock.getArroundMines();
if (testBlockArroundMines==0) openNearArea(posRow-1,
src.rar_Java 扫雷 代码_java扫雷
版权申诉
61 浏览量
2022-09-24
12:10:30
上传
评论
收藏 12KB RAR 举报
weixin_42653672
- 粉丝: 93
- 资源: 1万+
最新资源
- keil2 + proteus + 8051.exe
- 1961ee27df03bd4595d28e24b00dde4e_744c805f7e4fb4d40fa3f695bfbab035_8(1).c
- mediapipe-0.9.0.1-cp37-cp37m-win-amd64.whl.zip
- windows注册表编辑工具
- mediapipe-0.9.0.1-cp37-cp37m-win-amd64.whl.zip
- 校园通行码预约管理系统20240522075502
- 车类型数据集6250张VOC+YOLO格式.zip
- The PyTorch implementation of STGCN.STGCN-main.zip
- 092300108.cpp
- 车类型数据集6000张VOC+YOLO格式.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈