advertisement
javaboutique
Search Tips
Articles  |   Tutorials  |   Reviews  |   Tools  |   by Category  |   by Date  |   by Name  |   Submit  |   Source  |   Forums  |  
javaboutique
Browse DevX


Partners & Affiliates











advertisement

Eliminator


Java Source:

/**********************************************************
   Eliminator.java - My Second Java Game
**********************************************************/

import java.applet.*;
import java.awt.*;
import java.awt.image.*;
import java.net.*;
import java.util.*;

/******** TEST ********/

public class Eliminator extends Applet implements Runnable
{
  static int MAX_X=9, MAX_Y=10, MAX_LVL=100, MAX_IMG=11, MAX_HIST=100;
  static int X_0 = 5, Y_0 = 5;

  Thread game0 = null;

  private Image[] xImg = new Image[MAX_IMG+1];
  private int xPos, yPos, gLevel, mvPos, maxMove;
  private byte[][] currCanvas = new byte[MAX_Y+1][MAX_X+1];
  private byte[] xMove = new byte[MAX_HIST];
  private byte firstTime;

  private Button btn1  = new Button("X");
  private Button btnN  = new Button(">");
  private Button btnP  = new Button("<");

  private static String stGameEnd = " [GAME END]";
  private static String stYouLost = " [YOU LOST]";

  private static String mCode = " #    *.oO@+,123456789abcdefghij";
  private static int xCoord[][] =
    {{  0, 0,20,20}, { 20, 0,20,20}, { 40, 0,20,20}, { 60, 0,20,20}, { 80, 0,20,20}, {100, 0,20,20},
     {100,20,20,20}, {  0,20,20,20}, { 20,20,20,20}, { 40,20,20,20}, { 60,20,20,20}, { 80,20,20,20}};

  private static String biDat[] = {
    ", #7, #o.o.o.#, # 5#, # # # #1, # # 3#, # 4#1, #6,",
    ",, 1#5, 1#.* 1#, 1# 3#, 1#. 2#, 1# 1.#1, 1#5,",
    ", 2#3, 1#1 .#1, 1# 3#, 1#. o #, 1# 1# #, 1# 1. #, 1#1o #1, 2#3,",
    ",, 1#5, 1# 1. #, 1#1 1#1, 1# 2.#, 1#. 1#1, 1#4,",
    ", 1#2, #1 #2, # . 1#1, #* . 1#1, # 1* . #, # # 1#2, #5,",
    ", #7, #o 3o#, # # 1# #, # 1. 2#, #1. . #1, # 1o . #, #7,",
    ", 1#5, #1 . *#1, # 4.#, # 5#, #. 4#, #1 3#1, 1#1* #1, 2#3,",
    ", #6, # 3.#, #. 2#1, # 1* 1#, # 3#1, #2.#1, 2#2,",
    ",,#9,# 1. 2* #,#1 1*. 1#1, # *. * #, #. 3.#, #7,",
    ", 2#4, 1#1 . #1, #1 4#, #. 3.#, # 2#1*#, #7,",
    ", 4#2, #4.#1, #. 4#, #1 2* #, 1#1 # #1, 2# #2, 2# 1.#, 2#4,",
    ", #6, # 4#, #1 .* #, #. 3#, #* 2.#, # 2* #, #6,",
    " #7, # 1. 2#,#1 1*# 1#,# 2.# 1#,#1 #1 2#, # 1* 2#, # 1. 2#, # 5#, # 5#, #7,",
    ", #6, # .# o#1, #1 3o#, 1# 3#1, 1#* 2.#, 1#.o 1#1, 1#5,",
    ", 1#5, #1. 2#, # 3.#, #1 * #1, # 4#, #1 2.#, 1#5,",
    ", 2#2, 1#1 #3, #1 4#, # 5#, # .o#.o#, #1o#2.#, 1#2 #2,",
    ", 3#2, #3O#1, # o. 1#, # # o.#2,#1 4O*#,# 5o#1,#1 O# 1#1, #6,",
    " 4#2, 2#2 #1, 1#1. 2#, 1# 2o#1, #2 . 1#, # 1o .#1, #1 1o#1, 1#4,",
    ", 1#5, #1 1o #1, #o 3.#, #. 2* #, #1 . 2#, 1#3 #1, 4#2,",
    ", #4, #. 1#1, # 3#1, # o o.#, #2. 1#, 2# 2#, 2#4,",
    ", 2#4, 1#1 O #, #1 3#1,#1.O 3#,# 2o 1#1,# 1#2.#,# O o 1#,#7,",
    " #2,#1 #4,#. 2O #,# 2#1.#1,# 1Oo# 1#,# o# 2#1,# 2. o #,#O 1#1 #1,#7,",
    " 1#2, 1# #2, 1# 1o#1, 1#*. o#1, 1# #O o#, #1 2# #, # . 2O#, #1O 2#1, 1#5,",
    "#8,# Oo.oO #,# 2o 2#,# 2O 2#,# 2o 2#,# o . O #,#1 1# 1#1, #6,",
    " 1#4, #1 # #1, # .# 1#1, #1 #. 1#, # 5#, #1 * 1.#, 1#1 2#1, 2#1 #1, 3#2,",
    " 1#5, #1 1# #1, #.*o.*.#1, # 6#, # #1 # 1#,#1 2#1 1#,# o 4#1,#1 4#1, #6,",
    " 1#2, 1# #, 1# #3, #1 3#1, # 1# . #, # . # 1#, #1 #. #1, 1#1 # #, 2#2 #, 4#2,",
    ", #5, # # 1#1, # 1. 1#,#2. * #1,# 3#. #,#1 2#3, #2 #, 2#2,",
    " #3,#1 1#3,# 2. 1#,#1 .#.#1, # 4#, # #o#o#, #1 1o #, 1#1 2#, 2#1 #1, 3#2,",
    ", #5, # 1o.#1, # . #o#1, # * 3#, #o 4#, #1 2.#1, 1#1 1#1, 2#3,",
    ", 1#5, 1# # .#1, 1# 1o 1#, #2. * #, #. 3#1, #1o 2#, 1# 3#, 1#5,",
    ", 4#2, 3#1*#, 2#1. #, 1#1. 1#1, #1 4#, # 4.#, #o 1* o#, #7,",
    "#7,# .oO* #,#1 4#1,#O . 3#,#o# 3#1,# #1 2#1,# 1o 3#,#. #1O. #,#5 1#, 4#3,",
    ", 1#5, 1#. 2#1, #1 # O #, # 1Oo# #, # 2Oo #, #1 o# 1#, 1# 2.#1, 1#5,",
    ", #5, # 1o #1, # O# o#, #o 1O.#1, # 5#, #1. 2O#, 1#6,",
    " #5, # O .#1, #1 2O#1, 1# o#. #, #1 3o#, #O #O 1#, # o 3#, #1 #4, 1#2,",
    " 1#4, #1O 1#, # o 1#1, #1. 2#, # O.o*#, #1 2.#1, 1# 2o #, 1#1 3#, 2#5,",
    " 1#6, 1# 1. o#, #1 o# 1#, # 2# 1#, #1 3#1, 1#. 2#1, 1# 2o #, 1#1 .# #, 2#5,",
    ", 1#4, #1 1.#1, # . # #1, #1 . 1o#, #2o *.#, # . * o#, #7,",
    " #7,#1 o 1O #,# 4o#1,#1 5#,# 1O 1.#1,#1. 2*#, #1o 2#, # . 1O#, #6,",
  };

  private static int aMove[] =
    {4,4,4,4,4,4,4,4,4,4,6,6,6,6,6,6,6,6,6,6,7,7,7,7,8,8,8,8,8,8,9,9,9,9,9,9,9,9,9,10,};

  private Image mainImg;
  private Graphics g;
  private TextField txt1;

  public void init()
  {
    String param;
    int i;

    if (g==null) g = getGraphics();

    URL url = getCodeBase();

    String str = "";
    int b = 0;

    MediaTracker tracker = new MediaTracker(this);
    mainImg = getImage(url, "Balls.gif");
    tracker.addImage(mainImg,0);

    for (i=0; i<=MAX_IMG; i++)
      {
        xImg[i] = extractImage(xCoord[i]);
        tracker.addImage(xImg[i], i+1);
      }

    try { tracker.waitForAll(); }
    catch (InterruptedException e) {}

    setLayout(new BorderLayout());

    txt1 = new TextField(17);
    txt1.setForeground(new Color(255,255,204));
    txt1.setBackground(new Color(102,51,51));
    txt1.setEditable(false);

    Panel p = new Panel();

    p.add(txt1);
    p.add(btnP);
    p.add(btnN);
    p.add(btn1);

    add("South", p);

    gLevel = 0;
    dat2Canvas(0);
    firstTime = 1;
  }

  public void start()
  {
    if (game0==null)
     {
       game0 = new Thread(this);
       game0.start();
     }
  }

  public void stop()
  {
    if (game0 != null)
     {
       game0.stop();
       game0 = null;
     }
  }

  public void run()
  {
  }

  public boolean dat2Canvas(int dlvl)
  {
    int i,x,y,tmp0;
    byte z = 0, oz = 0;
    String param;

    try
     {
       gLevel += dlvl;
       if (gLevel<0) gLevel = 0;

       try { i = Integer.parseInt(getParameter("maxmove"+gLevel)); }
       catch (NumberFormatException e) { i = 0; }

       param = getParameter("data"+gLevel);
       if (i>0) maxMove = i;

       if (param==null || i<=0)
          { param = biDat[gLevel]; maxMove = aMove[gLevel]; }
     }
    catch (ArrayIndexOutOfBoundsException exc)
     {
       gLevel -= dlvl;
       return false;
     }

    for (y=0; y<=MAX_Y; y++)
      { for (x=0; x<=MAX_X; x++)
          { currCanvas[y][x] = 0; }
      }

    x = y = 0;
    for (i=0; i<param.length(); i++)
      {
        z = (byte)(mCode.indexOf(param.charAt(i)));
        if (z>MAX_IMG)
         {
           if ((tmp0 = (z-MAX_IMG-1))>0)
            {
              for (int j=0; j<tmp0; j++)
                {
                  try { currCanvas[y][x++] = oz; }
                  catch (ArrayIndexOutOfBoundsException exc) { j = tmp0; }
                }
            }
           else
            {
              x = 0; y++;
              if (y>MAX_Y) break;
            }
         }
        else if (x<=MAX_X)
         {
           if (z<0) z = 0;
           currCanvas[y][x++] = z;
           oz = z;
         }
      }
    for (i=0; i<MAX_HIST; i++) { xMove[i] = 0; }
    mvPos = 0;

    txt1.setText(" Level "+gLevel+" ("+maxMove+" step max.)");
    if (firstTime==1) firstTime = 0;

    return true;
  }

  public boolean action(Event e, Object arg)
  {
    int h = -1;

    if      (e.target==btnP)  h = 44;
    else if (e.target==btnN)  h = 46;
    else if (e.target==btn1)  h = 27;

    if (h>0) keyDown(e, h);

    return true;
  }

  public boolean keyDown(Event e, int c)
  {
    int dx = 0;
    int dy = 0;
    int ch = 0;
    int h  = 0;

    switch (c)
    {
      case e.UP:    dy = -1; h = 1; break;
      case e.DOWN:  dy =  1; h = 3; break;
      case e.LEFT:  dx = -1; h = 5; break;
      case e.RIGHT: dx =  1; h = 7; break;
      case 27:
        if (mvPos==0 && firstTime==0) { firstTime = 1; paint(null); return true; }
        h = -100; break;
      case 44: h = -101; break;
      case 46: h =  -99; break;
      case 14: h =  -90; break;
      case 16: h = -110; break;

      default:
        txt1.setText(" Use ^P,<,>,^N,^R, or BS key.");
        break;
    }

    if (dx==0 && dy==0)
     {
       if (h<0) { if (dat2Canvas(h+100)) paint(null); }
       return true;
     }

    if (mvPos>=maxMove)
       { txt1.setText(stYouLost); return true; }

    h = 1; ch = 0;
    while(h>0)
     {
       if ((h = moveBall(dx,dy))>0)
          { ch = 1; h = checkBoard(); }
     }

    if (ch==1) { mvPos++; txt1.setText(" Have "+(maxMove-mvPos)+" more step."); }
       else return true;

    h = GameEnd();
    if (h==1) { txt1.setText(stGameEnd); h = -98; }
    else if (h==-1 || mvPos>=maxMove)
            { txt1.setText(stYouLost); h = -99; }

    if (h<=-98)
     {
       try { game0.sleep(1500); }
       catch (InterruptedException exc) {}
       if (dat2Canvas(h+99)) paint(null);
     }

    return true;
  }

  public int moveBall(int dx, int dy)
  {
    int x,y,lx,ly,x2,y2;
    byte z0,z1;
    int h = 0;

    if (dx != 0)
     {
       ly = Y_0;
       for (y=0; y<=MAX_Y; y++)
       {
         if (dx==1) x = MAX_X-1; else x = 1;
         while (x>0 && x<MAX_X)
         {
           if ((z0 = currCanvas[y][x])>=6)
            {
              x2 = x; lx = X_0+20*x;
              while (x2>0 && x2<MAX_X && (z1=currCanvas[y][x2+dx])==0)
              {
                try { game0.sleep(10); }
                catch (InterruptedException e) {}
                h++;
                currCanvas[y][x2] = 0;
                g.drawImage(xImg[0],lx,ly,this);
                x2 += dx; lx += 20*dx;
                currCanvas[y][x2] = z0;
                g.drawImage(xImg[z0],lx,ly,this);
              }
            }
           x -= dx;
         }
         ly += 20;
       }
     }

    if (dy != 0)
     {
       lx = X_0;
       for (x=0; x<=MAX_X; x++)
       {
         if (dy==1) y = MAX_Y-1; else y = 1;
         while (y>0 && y<MAX_Y)
         {
           if ((z0 = currCanvas[y][x])>=6)
            {
              y2 = y; ly = Y_0+20*y;
              while (y2>0 && y2<MAX_Y && (z1=currCanvas[y2+dy][x])==0)
              {
                try { game0.sleep(10); }
                catch (InterruptedException e) {}
                h++;
                currCanvas[y2][x] = 0;
                g.drawImage(xImg[0],lx,ly,this);
                y2 += dy; ly += 20*dy;
                currCanvas[y2][x] = z0;
                g.drawImage(xImg[z0],lx,ly,this);
              }
            }
           y -= dy;
         }
         lx += 20;
       }
     }

    return h;
  }

  public int checkBoard()
  {
    byte z0,z1,z2;
    int  h  = 0;
    int  h2 = 0;

    z1 = 100;
    for (int y=0; y<MAX_Y; y++)
    {
      for (int x=0; x<MAX_X; x++)
      {
        z0 = (byte)(currCanvas[y][x] % z1);
        if (z0>=7)
         {
           z2 = (byte)(z0+z1);
           if (z0==(byte)(currCanvas[y][x+1] % z1))
            {
              currCanvas[y][x] =
              currCanvas[y][x+1] = z2;
              h++;
            }
           if (z0==(byte)(currCanvas[y+1][x] % z1))
            {
              currCanvas[y][x] =
              currCanvas[y+1][x] = z2;
              h++;
            }
         }
      }
    }

    if (h==0) return 0;

    for (int z=2; z<=6; z++)
    {
      try { game0.sleep(100); }
      catch (InterruptedException e) {}

      int ly = Y_0;
      for (int y=0; y<MAX_Y; y++)
      {
        int lx = X_0;
        for (int x=0; x<MAX_X; x++)
        {
          if ((z0 = currCanvas[y][x])>MAX_IMG)
           {
             if (z<6) g.drawImage(xImg[z],lx,ly,this);
             else
              {
                currCanvas[y][x] = 0;
                g.drawImage(xImg[0],lx,ly,this);
              }
           }
          lx += 20;
        }
        ly += 20;
      }
    }
    return h;
  }

  public int GameEnd()
  {
    int x,y;
    int h = 1;
    int h0[] = new int[MAX_IMG+1];

    for (y=0; y<=MAX_IMG; y++) h0[y] = 0;

    for (y=0; y<=MAX_Y; y++)
    {
      for (x=0; x<=MAX_X; x++)
      {
        h0[currCanvas[y][x]]++;
      }
    }

    y = 7;
    while (y<=MAX_IMG)
    {
      x = h0[y];
      if (x==1) { h = -1; y = MAX_IMG+1; }
      else if (h==1 && x>1) h = 0;
      y++;
    }

    return h;
  }

  private Image extractImage(int[] xyCoord)
  {
    Image newImage;
    ImageFilter filter;
    ImageProducer producer;

    filter = new CropImageFilter(xyCoord[0],xyCoord[1],xyCoord[2],xyCoord[3]);
    producer = new FilteredImageSource(mainImg.getSource(), filter);
    newImage = createImage(producer);
    return newImage;
   }

  public void paint(Graphics g)
  {
    if (g==null) g = getGraphics();
    int y2 = Y_0;
    for (int y=0; y<=MAX_Y; y++)
      {
        int x2 = X_0;
        for (int x=0; x<=MAX_X; x++)
          {
            byte z = currCanvas[y][x];
            if (firstTime==1 || z>MAX_IMG) z = 0;
            g.drawImage(xImg[z],x2,y2,this);
            x2 += 20;
          }
        y2 += 20;
      }
    if (firstTime==1) doFirstTime();
  }

  public void doFirstTime()
  {
    int y = 20, x = 15;
    g.setColor(new Color(255,255,204));
    String str = "* Eli mi nator *";
    g.drawString(str,15,y);
    txt1.setText(str);
    y += 20; g.drawString("(Java Applet version) 1999 by Riza PN",15,y);
    y += 20; g.drawString("The goal is to eliminate all balls. Press",15,y);
    y += 15; g.drawString("any arrow key to move the ball. Balls",15,y);
    y += 15; g.drawString("will be eliminated if there are more",15,y);
    y += 15; g.drawString("than 1 ball in a horizontal or vertical",15,y);
    y += 15; g.drawString("direction. The game will be finished if",15,y);
    y += 15; g.drawString("all balls are eliminated (YOU WIN), or if",15,y);
    y += 15; g.drawString("there is no more possibility to eliminate",15,y);
    y += 15; g.drawString("them (YOU LOST).",15,y);
    y += 30; g.drawString("The idea is picked from my",15,y);
    y += 15; g.drawString("game pocket (they call it IQ-Puzzle)",15,y);
  }

}

Back to Eliminator

How to Add Java Applets to Your Site

New on the Java Boutique:

New Review:

Time Management Made Easy with the Quartz Enterprise Job Scheduler
Why not just use the Java timer API? This open source scheduling API boasts simplicity, ease-of-integration, a well-rounded feature set, and it's free!

New Applet:

Reverse Complement
Reverse Complement is a simple applet that converts DNA or RNA sequences into three useful formats.

Elsewhere on internet.com:

WebDeveloper Java
Lots of Java information on webdeveloper.com

WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.

ScriptSearch Java
Hundreds of free Java code files to download.

jGuru: Your View of the Java Universe
Customizable portal with online training, FAQs, regular news updates, and tutorials.

 DevX Skillbuilding from IBM developerWorks
 RIA Run Contest: Build Next-Gen Apps in Microsoft Silverlight 2
 Avaya DevConnect Center
 Intel Go Parallel Portal
 Internet.com eBook Library
 Microsoft RIA Development Center
 Destination .NET
XML error: not well-formed (invalid token) at line 53
advertisement
Receive Articles via our XML/RSS feed
Receive Articles via our XML/RSS feed

JavaBytes
Internet Cyclone
This powerful, easy-to-use, internet optimizer is for Windows 95, 98, ME, NT, 2000 and XP. It's designed to automatically optimize your Windows settings, boosting your Internet connection up to 200%.

SaaS Tool Offers Custom Database Development
Microsoft’s Automated Agent: Can We Talk?
Borland Finally Sells CodeGear
Red Hat Heads For The JON 2.0
Out with the Old, in with the New at JavaOne
Trolltech Expands WebKit Footprint
Oracle: Eating its Own Open Source Food
Big Money and Open Source May Not Compute
Open Source Embrace Gives Sun New Fans
NetBeans, OpenSolaris Also in Spotlight at JavaOne

Taming Trees: Building Branching Structures
Clean Up Function Syntax Mess with decltype
Sutter Speaks: The Future of Concurrency
INTEL SCAVENGER HUNT, LENOVO X300 AND APPLE IPOD TOUCH GIVEAWAY (the "Giveaway")
Comparing Multi-Core Processors for Server Virtualization
Intel® Desktop Business Computing Solutions
Intel: What Downturn?
Managing the Evolving Data Center
Implement Drag and Drop in Your Windows Forms Applications
Processing Linked Web Data with XSLT

Advertising Info  |   Member Services  |   Contact Us  |   Help  |   Feedback  |   Site Map  |   Network Map  |   About



JupiterOnlineMedia

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info


Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

Solutions
Whitepapers and eBooks
Microsoft Article: HyperV-The Killer Feature in WinServer ‘08
Avaya Article: How to Feed Data into the Avaya Event Processor
Microsoft Article: Install What You Need with Win Server ‘08
HP eBook: Putting the Green into IT
Whitepaper: HP Integrated Citrix XenServer for HP ProLiant Servers
Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 1
Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 2--The Future of Concurrency
Avaya Article: Setting Up a SIP A/S Development Environment
IBM Article: How Cool Is Your Data Center?
Microsoft Article: Managing Virtual Machines with Microsoft System Center
HP eBook: Storage Networking , Part 1
Microsoft Article: Solving Data Center Complexity with Microsoft System Center Configuration Manager 2007
MORE WHITEPAPERS, EBOOKS, AND ARTICLES
Webcasts
Intel Video: Are Multi-core Processors Here to Stay?
On-Demand Webcast: Five Virtualization Trends to Watch
HP Video: Page Cost Calculator
Intel Video: APIs for Parallel Programming
HP Webcast: Storage Is Changing Fast - Be Ready or Be Left Behind
Microsoft Silverlight Video: Creating Fading Controls with Expression Design and Expression Blend 2
MORE WEBCASTS, PODCASTS, AND VIDEOS
Downloads and eKits
Sun Download: Solaris 8 Migration Assistant
Sybase Download: SQL Anywhere Developer Edition
Red Gate Download: SQL Backup Pro and free DBA Best Practices eBook
Red Gate Download: SQL Compare Pro 6
Iron Speed Designer Application Generator
MORE DOWNLOADS, EKITS, AND FREE TRIALS
Tutorials and Demos
How-to-Article: Preparing for Hyper-Threading Technology and Dual Core Technology
eTouch PDF: Conquering the Tyranny of E-Mail and Word Processors
IBM Article: Collaborating in the High-Performance Workplace
HP Demo: StorageWorks EVA4400
Intel Featured Algorhythm: Intel Threading Building Blocks--The Pipeline Class
Microsoft How-to Article: Get Going with Silverlight and Windows Live
MORE TUTORIALS, DEMOS AND STEP-BY-STEP GUIDES