プログラムのダウンロード
ダウンロード開始
ソースコード
●ObakeMuraPanel.java
package jp.tuyano;
import java.awt.*;
import java.awt.event.*;
import java.net.URL;
import java.util.*;
import javax.swing.*;
public class ObakeMuraPanel extends JPanel {
int x,y,w,h,num;
int[][] arr;
Image house,house2,house3,jimen,obake;
boolean gameflg;
public ObakeMuraPanel(int m,int n){
super();
gameflg = false;
w = 50; // image width
h = 50; // image height
x = m;
y = n;
arr = new int[x][y];
URL url1 = this.getClass().getResource("jimen.gif");
URL url2 = this.getClass().getResource("house.gif");
URL url3 = this.getClass().getResource("house2.gif");
URL url4 = this.getClass().getResource("house3.gif");
URL url5 = this.getClass().getResource("obake.gif");
jimen = Toolkit.getDefaultToolkit().getImage(url1);
house = Toolkit.getDefaultToolkit().getImage(url2);
house2 = Toolkit.getDefaultToolkit().getImage(url3);
house3 = Toolkit.getDefaultToolkit().getImage(url4);
obake = Toolkit.getDefaultToolkit().getImage(url5);
this.addMouseListener(new MyMouseAdapter());
this.setSize(new Dimension(x * w,y * h));
this.setPreferredSize(new Dimension(x * w,y * h));
initArr(0);
}
public void newGame(){
num = 1;
initArr(num);
gameflg = true;
}
public void initArr(int n){
Random rnd = new Random(new Date().getTime());
for(int i = 0;i < x;i++)
for(int j = 0;j < y;j++)
arr[i][j] = 0;
int n0 = 0;
while (n0 != n){
int x1 = rnd.nextInt(x);
int y1 = rnd.nextInt(y);
if (arr[x1][y1] == 0){
arr[x1][y1] = 1;
n0++;
}
}
repaint();
}
public void resetStage(){
for(int i = 0;i < x;i++)
for(int j = 0;j < y;j++)
arr[i][j] = arr[i][j] == -1 ? 0 : arr[i][j] > 0 ? 1 : 0;
repaint();
}
public void renewStage(){
initArr(num);
repaint();
}
public boolean checkClear(){
int count = 0;
for(int i = 0;i < x;i++)
for(int j = 0;j < y;j++){
if (arr[i][j] > 0){
int n = 0;
n += this.getData(i-1,j-1) == -1 ? 1 : 0;
n += this.getData(i,j-1) == -1 ? 1 : 0;
n += this.getData(i+1,j-1) == -1 ? 1 : 0;
n += this.getData(i-1,j) == -1 ? 1 : 0;
n += this.getData(i+1,j) == -1 ? 1 : 0;
n += this.getData(i-1,j+1) == -1 ? 1 : 0;
n += this.getData(i,j+1) == -1 ? 1 : 0;
n += this.getData(i+1,j+1) == -1 ? 1 : 0;
if (n != 3) count++;
arr[i][j] = n == 3 ? 2 : n > 3 ? 3 : 1;
}
}
return count == 0 ? true : false;
}
public int getData(int x0,int y0){
return (x0 < 0 || x0 >= x || y0 < 0 || y0 >= y) ? 0 : arr[x0][y0];
}
public void paintComponent(Graphics g){
super.paintComponent(g);
g.setColor(Color.WHITE);
g.fillRect(0,0,x * w,y * h);
for(int i = 0;i < x;i++)
for(int j = 0;j < y;j++){
g.drawImage(jimen,i * w,j * h,this);
if (arr[i][j] == 1)
g.drawImage(house,i * w,j * h,this);
if (arr[i][j] == 2)
g.drawImage(house2,i * w,j * h,this);
if (arr[i][j] == 3)
g.drawImage(house3,i * w,j * h,this);
if (arr[i][j] == -1)
g.drawImage(obake,i * w,j * h,this);
}
}
class MyMouseAdapter extends MouseAdapter {
public void mouseClicked(MouseEvent ev){
if (gameflg == false) return;
int x0 = ev.getX() / w;
int y0 = ev.getY() / h;
if (arr[x0][y0] == 0) arr[x0][y0] = -1;
repaint();
if (checkClear()){
JOptionPane.showMessageDialog(getParent().getParent(),"制圧完了!");
initArr(++num);
repaint();
}
}
}
}