This Java code is converted into HTML using JAVA_Colorizator v1.0 (c) 2001 by Alexander Yanuar Koentjara (lexzeus@hotmail.com)
/* Created by : Alexander Yanuar KOENTJARA - LexZEUS, 28-Jun-2000 E-Mail : lexzeus@hotmail.com Homepage : http://lexzeus.tripod.com To work properly, this class needs mines.class in its folder/directory. As a open source program, you can use my "lexSweeper v1.0" in your web site, modify, or distribute it as your wish, but you must include my name as the author in the source code. Also, I make the code with a nice indents, so even a beginner can learn to know how my code works. You know, it's a bad habbit for some of programmers to be so selfish to write their codes within very awful indents, it won't help other people to debug or modify if there's something error inside, unless their code is for private used. Any comment or question, just contact me via my e-mail ... */ import java.applet.Applet; import java.awt.*; import java.awt.event.*; import java.awt.image.BufferedImage; public class lexSweeper extends Applet implements MouseListener, KeyListener { private Font dummyFont; private Color CBack = new Color(85,85,85); private int wd,hg,TH_ctr, lx, ly, l2x, l2y, bombs; private boolean gameOver,restartGame,youWin; private boolean doubleFlagR, doubleFlagL; private mines mymines; Image myBuff; public void init() { dummyFont = new Font("Halvetica", Font.BOLD, 10 ); lx=10; ly=10; l2x=10; l2y=10; bombs=30; wd=15; hg=15; doubleFlagR=false; doubleFlagL=false; addMouseListener(this); addKeyListener(this); myBuff = createImage(wd*15+50,hg*15+100); mymines = new mines(wd,hg,bombs); } public void keyReleased(KeyEvent me) {} public void keyTyped(KeyEvent me) {} public void keyPressed(KeyEvent e) { if (e.getKeyCode()==KeyEvent.VK_R) { youWin=false; gameOver=false; mymines.newBombs(); repaint(); } } public String getAppletInfo() { String t = " ************************************* \n" + " \n" + " LexZEUS MineSweeper Ver 1.0 \n" + " \n" + " also known as \n" + " \n" + " lexSweeper Ver 1.0 \n" + " \n" + " ------------------------------------- \n" + " \n" + " (C) by Alexander Yanuar Koentjara \n" + " by LexZEUS, 2000 \n" + " \n" + " Term 'LexZEUS' refers to my alias \n" + " name which is also used in every \n" + " orher applications I made. \n" + " \n" + " ************************************* \n"; return t; } public void mouseEntered(MouseEvent me) {} public void mouseExited(MouseEvent me) {} public void mouseClicked(MouseEvent me) {} public void mousePressed(MouseEvent me) { if ((me.getModifiers() & MouseEvent.BUTTON1_MASK )!=0) doubleFlagL=true; if (me.getModifiers()==4) doubleFlagR=true; } private boolean doubleClick(int px, int py) { int ct = mymines.getValue(px,py); int sum = 0, all = 0; for(int y=py-1;y<=py+1;y++) for(int x=px-1;x<=px+1;x++) if ((x>=0)&&(x<wd)&&(y>=0)&&(y<hg)) if (mymines.getFlag(x,y)) { all++; if (mymines.getValue(x,y)==99) sum++; } if (all!=ct) return false; // not enough flag is placed for(int y=py-1;y<=py+1;y++) for(int x=px-1;x<=px+1;x++) if ((x>=0)&&(x<wd)&&(y>=0)&&(y<hg)) if ( (!mymines.getFlag(x,y)) && (!mymines.isOpen(x,y)) ) mymines.setOpen(x,y); if (sum!=ct) gameOver=true; // false flagging the wrong mines return true; } public void mouseReleased(MouseEvent me) { if (youWin||gameOver) return; int px=me.getX()-lx-l2x; int py=me.getY()-ly-l2y; px=(int) Math.floor(px/15); py=(int) Math.floor(py/15); if ((px<0)||(py<0)||(px>=wd)||(py>=hg)) return; if ((me.getModifiers() & MouseEvent.BUTTON1_MASK )!=0) { // button 1 was pressed doubleFlagL=false; if ((doubleFlagR) && (mymines.isOpen(px,py)) && (mymines.getValue(px,py)>0)) { if (doubleClick(px,py)) { repaint(); return; } else return; } if (mymines.getFlag(px,py)) mymines.setFlag(px,py); if (mymines.setOpen(px,py)) gameOver=true; } if (me.getModifiers()==4) { // button 2 was pressed doubleFlagR=false; if ((doubleFlagL) && (mymines.isOpen(px,py)) && (mymines.getValue(px,py)>0)) { if (doubleClick(px,py)) { repaint(); return; } else return; } if (mymines.isOpen(px,py)) return; mymines.setFlag(px,py); if (mymines.getCountFlags()==0) { int ctr=0; for (int y=0;y<hg;y++) for (int x=0;x<hg;x++) if ((mymines.getFlag(x,y))&&(mymines.getValue(x,y)==99)) ctr++; if (ctr==bombs) youWin=true; } } repaint(); } public void start() { youWin=false; gameOver=false; restartGame=false; doubleFlagL=false; doubleFlagR=false; setBackground(Color.white); } public void update(Graphics scGR) { paint(scGR); } private void openSquare(Graphics gr,int x,int y,int xx, int yy) { gr.setColor(CBack); gr.fillRect(xx+x*15+1,yy+y*15+1,15-1,15-1); gr.setFont(dummyFont); int v=mymines.getValue(x,y); if (v==0) return; if (v<99) { gr.setColor(Color.white); gr.drawString(""+v, xx+x*15+5, yy+y*15+12); } else { gr.setColor(Color.red); gr.drawString("o", xx+x*15+5, yy+y*15+12); } } private void littleSquare(Graphics gr,int x,int y,int xx, int yy) { if (mymines.getFlag(x,y)) gr.setColor(Color.red); else gr.setColor(Color.gray); gr.fillRect(xx+x*15+1,yy+y*15+1,15-1,15-1); gr.setColor(Color.black); gr.fillRect(xx+x*15+1,yy+y*15+1,2,15-1); gr.fillRect(xx+x*15+1,yy+y*15+13,15-1,2); gr.setColor(Color.lightGray); gr.drawLine(xx+x*15+14,yy+y*15+1,xx+x*15+14,yy+y*15+15); gr.drawLine(xx+x*15+1,yy+y*15+1,xx+x*15+15,yy+y*15+1); } public void drawAll(Graphics gr, int xx, int yy) { for (int y=0;y<hg;y++) for (int x=0;x<wd;x++) { if (!mymines.isOpen(x,y)) littleSquare(gr,x,y,xx,yy); else openSquare(gr,x,y,xx,yy); } } public void paint(Graphics scGR) { Graphics Buff = myBuff.getGraphics(); Buff.setColor(Color.white); Buff.fillRect(0,0,wd*15+50,hg*15+100); Buff.setColor(Color.lightGray); Buff.fillRect(l2x,l2y,wd*15,hg*15); drawAll(Buff,10,10); Buff.setFont(dummyFont); Buff.setColor(Color.red); Buff.drawString("(C) LexZEUS, 28/Jun/2000", 10, hg*16 + 10); Buff.drawString("(C) Alexander Yanuar Koentjara", 10, hg*16 + 25); Buff.setColor(Color.black); if (youWin) Buff.drawString("Great !! You save the world !" , 10, hg*16 + 50); else if (gameOver) Buff.drawString("Oh poor .. try again ..." , 10, hg*16 + 50); else Buff.drawString(" Mines : " + mymines.getCountFlags(), 10, hg*16 + 50); if (youWin||gameOver) Buff.drawString("Press 'R' to restart", 10, hg*16 + 65); scGR.drawImage(myBuff,lx,ly,this); } } /* Created by : Alexander Yanuar KOENTJARA - LexZEUS, 28-Jun-2000 E-Mail : lexzeus@hotmail.com Homepage : http://lexzeus.tripod.com To work properly, this class needs lexSweeper.class in its folder/directory. This class acts not as the main applet. As a open source program, you can use my "lexSweeper v1.0" in your web site, modify, or distribute it as your wish, but you must include my name as the author in the source code. Any comment or question, just contact me via my e-mail ... */ class mines { private int tile[][]; private boolean Btile[][],Ftile[][]; private int width, height, maxbombs, flags; mines(int wd, int hg, int bombs) { new java.util.Random(); width = wd; height = hg; tile = new int[wd][hg]; Btile = new boolean[wd][hg]; Ftile = new boolean[wd][hg]; maxbombs=bombs; newBombs(); } private int check(int x,int y) { if ( (x>=0) && (x<width) && (y>=0) && (y<height) ) if (tile[x][y]==99) return 1; return 0; } void newBombs() { flags=maxbombs; int curbombs=0, x, y; for (y=0;y<height;y++) for (x=0;x<width;x++) { tile[x][y]=0; Btile[x][y]=false; Ftile[x][y]=false; } while (curbombs<maxbombs) { x=(int) (Math.random()*width); y=(int) (Math.random()*height); if (tile[x][y]==0) { curbombs++; tile[x][y]=99; } // 99 is code for bombs } for (y=0;y<height;y++) for (x=0;x<width;x++) if (tile[x][y]==0) tile[x][y]= check(x-1,y-1) + check(x,y-1) + check(x+1,y-1) + check(x-1,y) + check(x+1,y) + check(x-1,y+1) + check(x,y+1) + check(x+1,y+1); } int getValue(int x,int y) { return tile[x][y]; } boolean isOpen(int x,int y) { return Btile[x][y]; } int getCountFlags() { return flags; } boolean getFlag(int x,int y) { return Ftile[x][y]; } void setFlag(int x,int y) { if (!Ftile[x][y]) flags--; else flags++; Ftile[x][y]=!Ftile[x][y]; } boolean setOpen(int x,int y) { Btile[x][y]=true; if (tile[x][y]==99) return true; if (tile[x][y]==0) explore(); return false; } private boolean lit_expl(int x,int y) { if ( (x>=0) && (x<width) && (y>=0) && (y<height) ) if (!Btile[x][y]) { if (Ftile[x][y]) return false; Btile[x][y]=true; return true; } return false; } private void explore() { boolean chg=true; while(chg) { chg=false; for (int y=0;y<height;y++) for (int x=0;x<width;x++) if ((Btile[x][y])&&(tile[x][y]==0)) chg = lit_expl(x-1,y-1)|lit_expl(x,y-1)|lit_expl(x+1,y-1)| lit_expl(x-1,y) |lit_expl(x+1,y)|chg| lit_expl(x-1,y+1)|lit_expl(x,y+1)|lit_expl(x+1,y+1); } } };