00001
00006 import java.applet.Applet;
00007 import java.util.Random;
00008 import java.awt.*;
00009
00011 public class minezj extends java.applet.Applet
00012 {
00013 private Image buffer;
00014 private Graphics screen;
00015
00016 private byte[] pground;
00017 private Image[] img;
00018 private boolean fl_done;
00019 private int screen_x;
00020 private int screen_y;
00021 private int bgcolor = 0xE0E0E0;
00022 private int nr_mines = 85;
00023 private int size_x = 30;
00024 private int size_y = 15;
00025 private int nr_uncovered = 0;
00026
00027 private final byte F_MINE = 9;
00028 private final byte B_UNCOVERED = 0x10;
00029 private final byte B_MARKED = 0x20;
00030 private final byte COUNT_MASK = 0x0F;
00031 private final byte C_SIZE = 15;
00032
00034 public String getAppletInfo ()
00035 {
00036 return "minezj 1.0\r\nOndra Havel <xohavel@fd.cvut.cz>\r\n";
00037 }
00038
00040 public void init ()
00041 {
00042 String opt;
00043
00044 screen_x = getSize().width;
00045 screen_y = getSize().height;
00046
00047 try {
00048 buffer = createImage (screen_x, screen_y);
00049 screen = buffer.getGraphics ();
00050 } catch (Exception e) {
00051 screen = null;
00052 }
00053
00054 pground = new byte[size_x * size_y];
00055
00056 opt = getParameter("bgcolor");
00057 if(opt!=null)
00058 bgcolor = Integer.parseInt(opt);
00059
00060 img = new Image[13];
00061 for (int i = 0; i < 13; i++)
00062 img[i] = getImage (getCodeBase (), "./gfx/" + (i) + ".gif");
00063 }
00064
00067 public void reset_game ()
00068 {
00069 Random a;
00070 int todo, fields, mns = nr_mines, i, j, r, cnt;
00071
00072 fl_done = false;
00073 nr_uncovered = 0;
00074
00075 a = new Random ();
00076 todo = fields = size_y * size_x;
00077
00078
00079 for (i = 0; i < todo; i++)
00080 pground[i] = 0;
00081
00082 while(mns>0) {
00083 mns--;
00084 r = (int)(todo * a.nextDouble());
00085 i = 0;
00086 cnt = 0;
00087 while (i<fields) {
00088 if(pground[i]==0) {
00089 if(cnt == r) {
00090 pground[i] = F_MINE;
00091 break;
00092 }
00093 cnt++;
00094 }
00095 i++;
00096 }
00097 }
00098
00099
00100 for(i=0;i<fields;i++) {
00101 if(pground[i]==0) {
00102 int y=i/size_x, x=i % size_x;
00103
00104 if(y!=0) {
00105 if(pground[i-size_x] == F_MINE)
00106 pground[i]++;
00107
00108 if(x-1>=0 && pground[i - size_x - 1] == F_MINE)
00109 pground[i]++;
00110
00111 if(x+1<size_x && pground[i-size_x+1] == F_MINE)
00112 pground[i]++;
00113 }
00114
00115
00116 if(y!=size_y-1) {
00117 if(pground[i+size_x]==F_MINE)
00118 pground[i]++;
00119
00120 if(x-1>=0 && pground[i+size_x-1] == F_MINE)
00121 pground[i]++;
00122
00123 if(x+1<size_x && pground[i+size_x+1]==F_MINE)
00124 pground[i]++;
00125 }
00126
00127
00128 if(x!=0 && pground[i-1]==F_MINE)
00129 pground[i]++;
00130
00131
00132 if(x!=size_x-1&&pground[i+1]==F_MINE)
00133 pground[i]++;
00134 }
00135 }
00136 }
00137
00139 public void start ()
00140 {
00141 reset_game ();
00142 repaint ();
00143 }
00144
00149 private boolean is_empty(int x, int y)
00150 {
00151 return(pground[x + y * size_x] == 0);
00152 }
00153
00158 private void revealcount(int x, int y)
00159 {
00160 if((pground[x+y*size_x] & B_UNCOVERED)==0 && (pground[x+y*size_x] & COUNT_MASK)!=0) {
00161 nr_uncovered++;
00162 pground[x+y*size_x] |= B_UNCOVERED;
00163 }
00164 }
00165
00170 private void zeroafter909(int x, int y)
00171 {
00172 pground[x+y*size_x] = B_UNCOVERED;
00173 nr_uncovered++;
00174 if(x>0) {
00175
00176 if(is_empty(x - 1,y)) {
00177 zeroafter909(x - 1,y);
00178 } else {
00179 revealcount(x-1,y);
00180 if(y>0)
00181 revealcount(x-1,y-1);
00182 if(y<size_y-1)
00183 revealcount(x-1,y+1);
00184 }
00185 }
00186 if(x<size_x-1) {
00187
00188 if(is_empty(x+1,y)) {
00189 zeroafter909(x+1,y);
00190 } else {
00191 revealcount(x+1,y);
00192 if(y>0)
00193 revealcount(x+1,y-1);
00194 if(y<size_y-1)
00195 revealcount(x+1,y+1);
00196 }
00197 }
00198 if(y>0) {
00199
00200 if(is_empty(x,y-1)) {
00201 zeroafter909(x,y-1);
00202 } else {
00203 revealcount(x,y-1);
00204 }
00205 }
00206 if(y<size_y-1) {
00207
00208 if(is_empty(x,y+1)) {
00209 zeroafter909(x,y+1);
00210 } else {
00211 revealcount(x,y+1);
00212 }
00213 }
00214 }
00215
00221 public boolean mouseDown (Event e, int x, int y)
00222 {
00223 int cell_x = x / C_SIZE, cell_y = y / C_SIZE;
00224 int off = cell_x + cell_y * size_x;
00225
00226
00227 if (fl_done) {
00228 reset_game ();
00229 repaint ();
00230 return (true);
00231 }
00232
00233
00234 if(cell_x > size_x || cell_y > size_y)
00235 return (true);
00236
00237
00238 if((pground[off] & B_UNCOVERED)!=0)
00239 return (true);
00240
00241
00242 if ((e.modifiers & Event.META_MASK) == Event.META_MASK) {
00243 pground[off] ^= B_MARKED;
00244 repaint();
00245 return (true);
00246 }
00247
00248
00249
00250
00251 if(pground[off]==0) {
00252 zeroafter909(cell_x, cell_y);
00253 repaint();
00254 return (true);
00255 }
00256
00257
00258 if((pground[off] & COUNT_MASK)==F_MINE) {
00259 fl_done = true;
00260 } else {
00261 pground[off] |= B_UNCOVERED;
00262 nr_uncovered++;
00263 if (nr_mines == size_x * size_y - nr_uncovered)
00264 fl_done = true;
00265 }
00266
00267 repaint ();
00268
00269 return (true);
00270 }
00271
00275 public void paint (Graphics g)
00276 {
00277 update (g);
00278 }
00279
00283 public synchronized void update (Graphics g)
00284 {
00285 if (screen != null) {
00286 do_draw (screen);
00287 g.drawImage (buffer, 0, 0, this);
00288 } else {
00289 do_draw (g);
00290 }
00291 }
00292
00296 private void do_draw (Graphics g)
00297 {
00298 int i, pic_nr, val, count;
00299
00300 g.setColor (new Color (bgcolor));
00301 g.fillRect (0, 0, screen_x, screen_y);
00302
00303 for (i = 0; i < size_x * size_y; i++) {
00304 val = pground[i];
00305 count = val & COUNT_MASK;
00306
00307
00308 if((val & B_UNCOVERED)!=0 && count==F_MINE)
00309 fl_done = true;
00310
00311
00312 if (fl_done) {
00313 if ((val & B_MARKED)!=0) {
00314 if(count!=F_MINE) {
00315 pic_nr = 12;
00316 } else {
00317 pic_nr = 11;
00318 }
00319 } else {
00320 pic_nr = count;
00321 }
00322 } else {
00323 if((val & B_UNCOVERED) == 0) {
00324 if((val & B_MARKED)!=0) {
00325 pic_nr = 11;
00326 } else {
00327 pic_nr = 10;
00328 }
00329 } else {
00330 pic_nr = count;
00331 }
00332 }
00333
00334 if (img[pic_nr] != null)
00335 g.drawImage (img[pic_nr], (i % size_x) * C_SIZE,
00336 (i / size_x) * C_SIZE, this);
00337 }
00338
00339
00340 g.setColor (Color.black);
00341 g.drawString ("nr_uncovered=" + nr_uncovered, 0, size_y * C_SIZE + 16);
00342
00343 if(fl_done) {
00344 if (nr_mines == size_x * size_y - nr_uncovered) {
00345 g.drawString ("You have won!", (size_x * C_SIZE) >> 1, size_y * C_SIZE + 16);
00346 } else {
00347 g.drawString ("You have lost the match!", (size_x * C_SIZE) >> 1, size_y * C_SIZE + 16);
00348 }
00349 }
00350 }
00351 }
00352