⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 minespanel.java

📁 自己编写的扫雷游戏的源代码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
      if(testBlock.untouched() )      {        testBlock.openArea();        testBlockArroundMines = testBlock.getArroundMines();        if (testBlockArroundMines==0)    openNearArea(posRow-1,posCol+1);      }    }    /* left Block*/    if (posCol>0)    {      testBlockIndex =  posRow*columns + (posCol-1) ;      Block testBlock = ((Block)this.blocks.get(testBlockIndex));      if(testBlock.untouched() )      {        testBlock.openArea();        testBlockArroundMines = testBlock.getArroundMines();        if (testBlockArroundMines==0)    openNearArea(posRow,posCol-1);      }    }    /* right Block*/    if (posCol<columns-1)    {      testBlockIndex = posRow*columns + (posCol+1) ;      Block testBlock = ((Block)this.blocks.get(testBlockIndex));      if(testBlock.untouched() )      {        testBlock.openArea();        testBlockArroundMines = testBlock.getArroundMines();        if (testBlockArroundMines==0)    openNearArea(posRow,posCol+1);      }    }    /* left-bottom Block */    if(posRow<rows-1 && 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,posCol-1);      }    }    /* bottom Block */    if(posRow<rows-1)    {      testBlockIndex = (posRow+1)*columns+ posCol;      Block testBlock = ((Block)this.blocks.get(testBlockIndex));      if(testBlock.untouched() )      {        testBlock.openArea();        testBlockArroundMines = testBlock.getArroundMines();        if (testBlockArroundMines==0)    openNearArea(posRow+1,posCol);      }    }    /* right-bottom Block */    if(posRow<rows-1 && posCol<columns-1)    {      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,posCol+1);      }    }    return true;  }  void jbInit() throws Exception {    this.setBorder(BorderFactory.createLoweredBevelBorder());  }  /////////////////////////////////////////////////////////////////////////////  /*  when the first left mouse button is clicked,      we begin array all the mines      in order to the first clicked position has no mine      @posRow,posCol : the first block's position (row,col)  */  private void arrageMines(int posRow,int posCol)  {    if(!wrongPosition(posRow,posCol))   return ;    int columns = param.getParamWidth();    int rows = param.getParamHeight();     int unConsiderIndex = posRow*columns + posCol;      //Block(posRow,posCol) should not be consider to arrange minee    int blockCount = rows*columns;    int i=0;    boolean[] m;    m = new boolean[blockCount];    while(i<blockCount)    {      m[i] = false;      i++;    }    if(param.getParamMines() < blockCount) /*when the mines is lower than total block count*/    {        i=0;        int arrangedMines = 0;        while(arrangedMines<param.getParamMines())        {          int rand = (int) (Math.random()*blockCount);          if (m[rand]==false && rand!=unConsiderIndex)          {            m[rand]=true;            arrangedMines++;          }          i++;        }    }    i=0;    while(i<blockCount)    {      if(m[i]==true)      {        ((Block)blocks.get(i)).setHasMine(true);      }      i++;    }    int pos=0;    while(pos<blockCount)    {      int row=pos/columns;      int col=pos%columns;        //////////////////////////////////////////////////////////////        /* Define a array to identify which block can be considered */        /*        0   1   2           (row-1,col-1)  (row-1,col)   (row-1,col+1)        3       4            (row,col-1)    (row,col)     (row,col+1)        5   6   7           (row+1,col-1)  (row+1,col)   (row+1,col+1)        */        boolean add[];        add = new boolean[8];        /* consume : all the block are be considered*/        int k=0;        while(k<8)        {          add[k]=true;          k++;        }        if(row==0)        {          add[0]=false; add[1]=false; add[2]=false;        }        else if (row==rows-1)        {          add[5]=false; add[6]=false; add[7]=false;        }        if(col==0)        {          add[0]=false; add[3]=false; add[5]=false;        }        else if(col==columns-1)        {          add[2]=false; add[4]=false; add[7]=false;        }        int arroundMines = 0;        if(add[0])          if(m[(row-1)*columns+(col-1)])     arroundMines++;        if(add[1])          if(m[(row-1)*columns+col])         arroundMines++;        if(add[2])          if(m[(row-1)*columns+(col+1)])     arroundMines++;        if (add[3])          if(m[ row*columns+(col-1)])        arroundMines++;        if (add[4])          if(m[ row*columns+(col+1)])        arroundMines++;       if (add[5])          if(m[ (row+1)*columns+(col-1)])    arroundMines++;       if (add[6])          if(m[ (row+1)*columns+ col ])      arroundMines++;       if (add[7])          if(m[ (row+1)*columns+ (col+1)])  arroundMines++;      //((Block)blocks.get(pos)).setArroundMines(calculateArroundMines(row,col));      ((Block)blocks.get(pos)).setArroundMines(arroundMines);      /* Test Code */      //((Block)blocks.get(pos)).leftButtonClicked();      pos++;    }  }  /*    Count all mines which has been identified--flag symbol    return value: 0~8  have 0~8 flags                  -1   have some flag is wrong identified  */  private int getFlags(int posRow,int posCol)  {    int columns = param.getParamWidth() ;    int rows = param.getParamHeight() ;    int index = posRow*columns+posCol;    int testBlockIndex=-1;    int testBlockContent =0;    int flag = 0;    // left-top Block    if(posRow>0 && posCol>0)    {      testBlockIndex = (posRow-1)*columns+(posCol-1);      Block testBlock = ((Block)this.blocks.get(testBlockIndex));      if (!testBlock.correctlyIdentified()  )  return -1;      if (testBlock.isFlag())      flag++;    }    // top Block    if (posRow>0)    {      testBlockIndex = (posRow-1)*columns+ posCol ;      Block testBlock = ((Block)this.blocks.get(testBlockIndex));      if (!testBlock.correctlyIdentified() )  return -1;      if (testBlock.isFlag())      flag++;    }    // right-top Block    if (posRow>0 && posCol<columns-1)    {      testBlockIndex = (posRow-1)*columns+ (posCol+1) ;      Block testBlock = ((Block)this.blocks.get(testBlockIndex));      if (!testBlock.correctlyIdentified() )  return -1;      if (testBlock.isFlag())      flag++;    }    // left Block    if (posCol>0)    {      testBlockIndex =  posRow*columns +posCol-1 ;      Block testBlock = ((Block)this.blocks.get(testBlockIndex));      if (!testBlock.correctlyIdentified() )  return -1;      if (testBlock.isFlag())      flag++;    }    // right Block    if (posCol<columns-1)    {      testBlockIndex = posRow*columns + (posCol+1) ;      Block testBlock = ((Block)this.blocks.get(testBlockIndex));      if (!testBlock.correctlyIdentified() )  return -1;      if (testBlock.isFlag())      flag++;    }    // left-bottom Block    if(posRow<rows-1 && posCol>0)    {      testBlockIndex = (posRow+1)*columns+(posCol-1);      Block testBlock = ((Block)this.blocks.get(testBlockIndex));      if (!testBlock.correctlyIdentified() )  return -1;      if (testBlock.isFlag())      flag++;    }    // bottom Block //    if(posRow<rows-1)    {      testBlockIndex = (posRow+1)*columns+ posCol;      Block testBlock = ((Block)this.blocks.get(testBlockIndex));      if (!testBlock.correctlyIdentified() )  return -1;      if (testBlock.isFlag())      flag++;    }    // right-bottom Block //    if(posRow<rows-1 && posCol<columns-1)    {      testBlockIndex = (posRow+1)*columns+(posCol+1);      Block testBlock = ((Block)this.blocks.get(testBlockIndex));      if (!testBlock.correctlyIdentified() )  return -1;      if (testBlock.isFlag())      flag++;    }    return flag;  }  private boolean wrongPosition(int posRow, int posCol)  {    if ( posRow<0||posRow>param.getParamHeight() ||          posCol<0||posCol>=param.getParamWidth() )    {      return false;    }    return true;  }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -