import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
public class minesw extends java.applet.Applet implements MouseListener{
int xmax = 12; //列の数
int ymax = 8; //行の数
int xy_max = xmax * ymax; //マスの数
int sts[] = new int[xy_max]; //ステータス(0:None 1~8:open 9:bomb
// 10:not open 11:check_bomb 12:check_not)
int bmax = 15; //爆弾の数
int square = 16; //マス一辺の大きさ
int margin = 20; //端に寄らない為の余白
int gend; //ゲームのフラグ
Image offs;
Graphics gf;
public void init(){
offs = createImage(300,300);
gf = offs.getGraphics();
addMouseListener(this);
gameInit();
}
public void paint(Graphics g) {
update(g);
}
public void update(Graphics g) {
int i, j;
int k; //マスの番号
int xx, yy; //マスの座標(左上)
gf.setColor(Color.green); //背景の色
gf.gillRect(0,0,300,300); //四角形(背景)の大きさ
/* リセットボタンの描写 */
gf.setColor(Color.gray); //リセットボタンの色
gf.gillRect(20,180,60,20); //リセットボタンの大きさと座標
gf.setColor(Color.black); //文字の色
gf.drawString("RESET",23,193); //文字の座標
/* マスの描写 */
+- for(i=0;i < ymax;i++){
| yy = i * squaer + margin;
| for(j=0; j < max ;j++){
| xx = j * square + margin;
| k = i * xmax + j;
| if(sts[k] >= 9){
| gf.setColor(Color.gray);
| gf.filRect(xx, yy, square, square);
| if(sts[k] > 10){
| gf.setColor(Color.yellow);
| gf.drawString(" ? ", xx+2, yy+13);
| }
| }
| else{
| gf.setColor(Color.white);
| gf.fillRect(xx, yy, square, square);
| if(sts[k] != 0){
| gf.setColor(Color.red);
| gf.drawString(" " + sts[k] + " ", xx+2, yy+13);
| }
| }
| if(gend == 2 && (sts[k] == 9 || sts[k] == 11)){
| gf.setColor(Color.red);
|1 gf.fillRect(xx, yy, square, square);
| gf.setColor(Color.black);
| gf.drawString(" * ", xx+2, yy+13);
| }
| gf.setColor(Color.black);
| gf.drawRect(xx, yy, square, square);
| }
| }
| if(gend == 1){
| gf.setColor(Color.yellow);
| gf.drawString(" Clear !! ", 100, 170);
| }
| if(gend == 2){
| gf.setColor(Color.red);
| gf.drawString(" Game Over ", 100, 170);
| }
|
| g.drawImage(offs,0,0,this);
+- }
public void mousePressed(MouseEvent e){
int ix, iy;
int row, col;
int k;
ix = e.getX();
iy = e.getY();
if(20 < ix && ix < 80 && 180 < iy && iy < 200) {
gameInti();
repaint();
return;
}
col = (ix - margin) / square;
row = (iy - margin) / square;
if(col < 0 || col >= xmax || row < 0 || row >= ymax){
return;
}
k = row * xmax + col;
if(( e.getModifiers() & MouseEvent.CTRL_MASK) != 0) {
if(sts[k] == 9 || sts[k] == 10){
sts[k] += 2;
}
else if(sts[k] == 11 || sts[k] == 12){
sts[k] -= 2;
}
}
else{
if(sts[k] >= 10){
sts[k] = bombCount(col.row);
if(sts[k] == 0) AroundOpen(col,row);
}
if(sts[k] == 9){
gend = 2;
}
}
endCheck();
reqaint();
}
public void mouseReleased(MouseEvent e){
}
public void mouseClicked(MouseEvent e){
}
public void mouseEntered(MouseEvent e){
}
public void mouseExited(MouseEvent e){
}
public void gameInit(){
int i;
int j;
gend = 0;
for(i=0; i < xy_max; i++){
sts[i] = 10;
}
i = 0;
while(i < bmax){
k = (int)(Math.random() * (float)xy_may);
if(sts[k] == 10){
sts[k] = 9;
i++;
}
}
}
public void bombCount(int px, int py){
int xy, cnt;
int i, j;
cnt = 0;
for(i=0; i<3; i++){
for(j=0; j < 3; j++){
if(!(i == 1 && j == 1)){
if(px+i-1 >= 0 && px+i-1 < xmax && py+j-1 >= 0 &&
py+j-1 < ymax){
xy = (py+j-1) * xmax + (px+i-1);
if(sts[xy] >= 9){
sts[xy] = bombCount(px+i-1, py+j-1);
if(sts[xy] == 0) AroundOpen(px+i-1, py+j-1);
}
}
}
}
}
}
public void endCheck(){
int i, cnt;
cnt = 0;
for(i=0; i < xy_max; i++){
if(sts[i] < 9) cnt++;
}
if((cnt + bmax) == xy_max) gend = 1;
}
}
|