プロが教える店舗&オフィスのセキュリティ対策術

周辺の地雷の数を表示させたいのですがなかなかうまくいきません・・・
どうすればいいか教えてください。
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
public class jirai extends JApplet implements ActionListener{
int Width,Masu; //格子の幅(ピクセル)、格子の数
int x,y,ix,iy;
int i,j;
int n,m;
int l;
int count = 0;
int JIRAI[][] = new int[10][10];
JPanel pan1, pan2;
JButton s_btn = new JButton("start");
JTextField txt = new JTextField(23);
public void init(){
setSize(360,400);
Container con=getContentPane();
pan1=new JPanel();
pan2=new JPanel();
con.add(pan1);
con.add(pan2=new JPanel(),"South");
pan1.addMouseListener(new MyMouseListener());
pan1.add(txt);
pan1.setBackground(Color.yellow);
pan1.setForeground(Color.red);
pan1.setSize(300,400);
pan1.setBorder(BorderFactory.createLineBorder(Color.red));
pan2.add(s_btn);
s_btn.setBackground(Color.pink);
s_btn.addActionListener(this);
pan2.setBorder(BorderFactory.createLineBorder(Color.blue));
pan2.setBackground(Color.red);
}
public void actionPerformed(ActionEvent e){
if (e.getActionCommand().equals("start")){
Graphics g=pan1.getGraphics();
Masu=8;
Width=30;
g.setColor(Color.black);
for(x=Width*2;x<=Width*10;x+=Width)
g.drawLine(x,Width*2,x,Width*10); //30ピクセル間隔でY方向の線を描く
for(y=Width*2;y<=Width*10;y+=Width)
g.drawLine(Width*2,y,Width*10,y); //30ピクセル間隔でX方向の線を描く
for(i=1;i<8;i++){
for(j=1;j<8;j++){
JIRAI[i][j] = 0;
}
}
for(n=0;n<15;n++){
do{
for(i=1;i<8;i++){
for(j=1;j<8;j++){
m=(int)(Math.random()*8);
l=(int)(Math.random()*8);
}
}
}while(JIRAI[m][l]!=0);
JIRAI[m][l] = 1;
}
for (int dx=(ix-1)-1;dx<(ix-1)+1;dx++){
for(int dy=(iy-1)-1;dy<(iy-1)+1;dy++){
}
}
g.dispose();
txt.setBackground(Color.pink);
txt.setForeground(Color.black);
txt.setFont( new Font("Serif",Font.ITALIC,18));
txt.setText("地雷の数は15個です!") ;
showStatus(" 地雷の数は15個です!"); //アプレット下の表示
}
}
class MyMouseListener extends MouseAdapter{
public void mouseClicked(MouseEvent me){ //マウスがクリックされた
x=me.getX(); //マウスのクリック位置:x座標
y=me.getY(); //マウスのクリック位置:y座標
ix=(int)(x/Width); //0~11? アプレットの大きさで変わる
iy=(int)(y/Width); //0~11? アプレットの大きさで変わる
if(2<=iy && iy<=Masu+1 && 2<=ix && ix<=Masu+1){ //(2,2)~(9,9)
ix=ix-1;
iy=iy-1;
Graphics g=pan1.getGraphics();
//クリックした升目の地雷の有無を判定して処理
if(JIRAI[ix-1][iy-1] == 1){
g.setColor(Color.red);
g.fillOval(30*(ix-1)+60,30*(iy-1)+60,30,30);
JOptionPane.showMessageDialog(null,"ゲーム-オーバー!",null,JOptionPane.ERROR_MESSAGE);
}
if(JIRAI[ix-1][iy-1] == 0){
g.setColor(Color.blue);
g.fillOval(30*(ix-1)+60,30*(iy-1)+60,30,30);
g.setColor(Color.black);
g.drawString(""+check,30*(ix-1)+70,30*(iy-1)+80);
}
g.setColor(Color.yellow);
g.fillRect(60,305,100,50);
g.setColor(Color.black);
g.setFont( new Font("Serif",Font.ITALIC,18));
g.drawString("("+ix+","+iy+")",60,320);
}
}
}
}

A 回答 (2件)

周囲8ますの地雷の数を表示するのであれば、外周のマスを考慮して、こんな感じ?



class lengthtest {
 static int JIRAI[][] = new int[10][10];

 public static void main(String args[]) {
  for(int i=1;i<8;i++){
   for(int j=1;j<8;j++){
    JIRAI[i][j] = 0;
   }
  }
  int m=0;
  int l=0;
  for(int n=0;n<15;n++){
   do{
    for(int i=1;i<8;i++){
     for(int j=1;j<8;j++){
      m=(int)(Math.random()*8);
      l=(int)(Math.random()*8);
     }
    }
   }while(JIRAI[m][l]!=0);
   JIRAI[m][l] = 1;
  }
  for(int a=0;a<10;a++){
   for(int b=0;b<10;b++){
    if(JIRAI[a][b]==1){
     System.out.print("*");
    }else{
     int numOfMine=0;
     numOfMine += checkJirai(a-1 , b-1);//左上
     numOfMine += checkJirai(a-1 , b+0);//左
     numOfMine += checkJirai(a-1 , b+1);//左下
     numOfMine += checkJirai(a+0 , b-1);// 上
     numOfMine += checkJirai(a+0 , b+1);// 下
     numOfMine += checkJirai(a+1 , b-1);//右上
     numOfMine += checkJirai(a+1 , b+0);//右
     numOfMine += checkJirai(a+1 , b+1);//右下
     System.out.print(""+numOfMine);
     }
   }
   System.out.println("");
  }
  
 }

 private static int checkJirai(int x,int y){
  if ( (x < 0) || (x >= JIRAI.length) || (y<0) || (y >= JIRAI[0].length) ){
   return 0;
  }else{
   if(JIRAI[x][y]==1){
     return 1;
   }else{
    return 0;
   }
  }
 }
}
    • good
    • 0

深く考えてませんが、



public void mouseClicked(){
 if(JIRAI[a][b]==機雷){
  System.out.println("どかーん");
 }else{
  int numOfMine=0;
  if(JIRAI[a][b-1]==機雷)numOfMine++;//上
  if(JIRAI[a-1][b]==機雷)numOfMine++;//左
  if(JIRAI[a][b+1]==機雷)numOfMine++;//下
  if(JIRAI[a+1][b]==機雷)numOfMine++;//右
  print("機雷数は"+numOfMine+"デス");
 }
}

てなカンジじゃない?
    • good
    • 0

お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!