Internet Start Page  TOP  
 Road of Developers
 
 



DownLoad Cluster.lzh(ソースコード付き)


		

///////////////////////////////////////////////////////////////////////////////
// 3D 3目×2 並べ / I-ichirow Suzuki
// いわゆる5目並べの 3x3にするゲーム 
// cluster.java
import java.applet.Applet;
import java.applet.AppletContext;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.io.IOException;
import java.io.InputStream;
import sun.audio.*;
//----------------------------------------------------------------------------
public class cluster extends Applet implements Runnable, MouseListener, MouseMotionListener
{
	//--------------------------------------------------------------------
    int i;
    int j;
    int k;
    int l;
    int m;
    int n;
    int x;
    int y;
    int lastX;
    int lastY;
    int gameMode;
    int ballnum;
    int tempball;
    int ballMidX;
    int ballMidY;
    int ballR;
    int dragOffsX;
    int dragOffsY;
    int busycount;
    int playing;
    int ip;
    int jp;
    int kp;
    int lp;
    int is;
    int js;
    int ks;
    int fronts;
    int backs;
    int id;
    int jd;
    int kd;
    int ipt;
    int idc;
    int mi;
    int mj;
    int mk;
    int wi;
    int dragging;
    boolean busy;
    boolean drawing;
    double ap;
    double bp;
    double cp;
    double as;
    double an;
    double amd;
    double adc;
    double bdc;
    double cdc;
    double apt;
    double bpt;
    double cpt;
    double dpt;
    double dragScale;
    double cornX[];
    double cornY[];
    double cornZ[];
    double projX[];
    double projY[];
    double projZ[];
    double pntX[];
    double pntY[];
    double pntZ[];
    double scrX[];
    double scrY[];
    double scrZ[];
    double frontZ[];
    double backZ[];
    final double xa[] = {
        1.0D, 0, 0
    };
    final double ya[] = {
        0, 1.0D, 0
    };
    final double za[] = {
        0, 0, 1.0D
    };
    double xaxis[];
    double yaxis[];
    double zaxis[];
    double ut[];
    double vt[];
    double wt[];
    double frontDir[];
    double boxpos[];
    double ballX[];
    double ballY[];
    double ballZ[];
    final double scale = 11D;
    final double ballsize = 0.34100000000000003D;
    long lastClick;
    long nextTime;
    int faces[] = {
        6, 4, 2, 7, 3, 5, 0, 4, 1, 6, 
        2, 7, 2, 0, 3, 6, 7, 4
    };
    int bbx[] = {
        0, 28, 58, 90, 124, 160, 198, 90, 46, 0, 
        210, 180, 148, 114, 78, 40, 0, 132, 174, 218
    };
    int bby[] = {
        34, 34, 34, 34, 34, 34, 34, 70, 67, 64, 
        120, 118, 118, 114, 113, 111, 110, 72, 74, 74
    };
    int linefaces[];
    int cubedata[];
    int balltype[];
    int backOrder[];
    int frontOrder[];
    int ballOrder[];
    int lFrom[];
    int lTo[];
    Image b1;
    Image b2;
    Image background;
    Image bballs[];
    Graphics bG;
    Graphics bsG;
    ImageProducer improd;
    MediaTracker mt;
    Color gCol[];
    InputStream sounds[];
    byte sndData[][];
    right rightApplet;
    bottom bottomApplet;
    Thread runner;
    //------------------------------------------------------------
	public cluster()
    {
        playing = -1;
        busy = false;
        dragScale = 0.012D;
        cornX = new double[8];
        cornY = new double[8];
        cornZ = new double[8];
        projX = new double[8];
        projY = new double[8];
        projZ = new double[8];
        pntX = new double[32];
        pntY = new double[32];
        pntZ = new double[32];
        scrX = new double[32];
        scrY = new double[32];
        scrZ = new double[32];
        frontZ = new double[36];
        backZ = new double[36];
        xaxis = new double[3];
        yaxis = new double[3];
        zaxis = new double[3];
        ut = new double[3];
        vt = new double[3];
        wt = new double[3];
        frontDir = new double[7];
        boxpos = new double[81];
        ballX = new double[27];
        ballY = new double[27];
        ballZ = new double[27];
        linefaces = new int[72];
        cubedata = new int[27];
        balltype = new int[27];
        backOrder = new int[36];
        frontOrder = new int[36];
        ballOrder = new int[27];
        lFrom = new int[36];
        lTo = new int[36];
        bballs = new Image[20];
        gCol = new Color[36];
        sounds = new AudioDataStream[2];
        sndData = new byte[2][];
    }
	//--------------------------------------------------------------------
	public void init()
    {
        mt = new MediaTracker(this);
        byte abyte0[] = new byte[28];
        for(i = 0; i < 2; i++)
        {
            InputStream inputstream1 = getClass().getResourceAsStream("sound" + i + ".au");
            try
            {
                j = inputstream1.available() - 28;
                sndData[i] = new byte[j];
                for(k = 0; k < 28; k += inputstream1.read(abyte0, k, 28 - k));
                inputstream1.read(sndData[i]);
                sounds[i] = new AudioDataStream(new AudioData(sndData[i]));
                sounds[i].mark(j);
            }
            catch(IOException _ex) { }
        }
        InputStream inputstream = getClass().getResourceAsStream("balls1.gif");
        try
        {
            byte abyte1[] = new byte[inputstream.available()];
            inputstream.read(abyte1);
            b1 = Toolkit.getDefaultToolkit().createImage(abyte1);
        }
        catch(IOException _ex)
        {
            b1 = createImage(1, 1);
        }
        mt.addImage(b1, 0);
        inputstream = getClass().getResourceAsStream("balls2.gif");
        try
        {
            byte abyte2[] = new byte[inputstream.available()];
            inputstream.read(abyte2);
            b2 = Toolkit.getDefaultToolkit().createImage(abyte2);
        }
        catch(IOException _ex)
        {
            b2 = createImage(1, 1);
        }
        mt.addImage(b2, 0);
        try
        {
            mt.waitForID(0);
        }
        catch(InterruptedException _ex) { }
        improd = b1.getSource();
        j = 0;
        k = 30;
        for(i = 0; i < 10; i++)
        {
            bballs[i] = createImage(new FilteredImageSource(improd, new CropImageFilter(j, 0, k, k)));
            mt.addImage(bballs[i], 1);
            j += k;
            k += 2;
        }
        improd = b2.getSource();
        j = 0;
        k = 30;
        for(i = 0; i < 10; i++)
        {
            bballs[i + 10] = createImage(new FilteredImageSource(improd, new CropImageFilter(j, 0, k, k)));
            mt.addImage(bballs[i + 10], 1);
            j += k;
            k += 2;
        }
        try
        {
            mt.waitForID(1);
        }
        catch(InterruptedException _ex) { }
        frontDir[6] = -1D;
        background = createImage(190, 190);
        bG = background.getGraphics();
        buildCoordinates();
        buildGrays();
        reset();
        addMouseListener(this);
        addMouseMotionListener(this);
    }
	//--------------------------------------------------------------------
    public void reset()
    {
        for(int i1 = 0; i1 < 3; i1++)
        {
            xaxis[i1] = xa[i1];
            yaxis[i1] = ya[i1];
            zaxis[i1] = za[i1];
        }
        busy = false;
        for(int j1 = 0; j1 < 27; j1++)
            cubedata[j1] = 0;
        for(; rightApplet == null || bottomApplet == null; bottomApplet = (bottom)getAppletContext().getApplet("bottom"))
            rightApplet = (right)getAppletContext().getApplet("right");
        tempball = -1;
        rightApplet.clearBoard();
        bottomApplet.setNewMessage(0);
        drawing = false;
        performDrawing();
    }
	//--------------------------------------------------------------------
    public void buildCoordinates()
    {
        for(i = 0; i < 2; i++)
            for(j = 0; j < 2; j++)
                for(k = 0; k < 2; k++)
                {
                    cornX[i * 4 + j * 2 + k] = 1.0D - 2D * (double)(k & 0x1);
                    cornY[i * 4 + j * 2 + k] = 1.0D - 2D * (double)(j & 0x1);
                    cornZ[i * 4 + j * 2 + k] = 1.0D - 2D * (double)(i & 0x1);
                }
        m = 0;
        for(i = 0; i < 8; i++)
        {
            pntX[i * 4] = cornX[i];
            pntY[i * 4] = cornY[i];
            pntZ[i * 4] = cornZ[i];
            for(j = 0; j < 3; j++)
                if((i & 1 << j) == 0)
                {
                    lFrom[m] = i * 4;
                    lTo[m++] = (i + (1 << j)) * 4;
                }
            for(j = 0; j < 3; j++)
            {
                k = (i & 7 - (1 << j)) + ((1 << j) - (i & 1 << j));
                pntX[i * 4 + 1 + j] = (cornX[i] * 2D + cornX[k]) / 3D;
                pntY[i * 4 + 1 + j] = (cornY[i] * 2D + cornY[k]) / 3D;
                pntZ[i * 4 + 1 + j] = (cornZ[i] * 2D + cornZ[k]) / 3D;
                for(k = 0; k < 3; k++)
                    if(k != j && (i & 1 << k) == 0)
                    {
                        lFrom[m] = i * 4 + 1 + j;
                        lTo[m++] = (i + (1 << k)) * 4 + 1 + j;
                    }
            }
        }
        for(i = 0; i < 36; i++)
        {
            linefaces[i * 2 + 1] = 6;
            l = i * 2;
            j = lFrom[i];
            k = lTo[i];
            if(Math.abs(pntX[j] - pntX[k]) < 0.01D)
                if(pntX[j] > 0.98999999999999999D)
                    linefaces[l++] = 0;
                else
                if(pntX[j] < -0.98999999999999999D)
                    linefaces[l++] = 1;
            if(Math.abs(pntY[j] - pntY[k]) < 0.01D)
                if(pntY[j] > 0.98999999999999999D)
                    linefaces[l++] = 2;
                else
                if(pntY[j] < -0.98999999999999999D)
                    linefaces[l++] = 3;
            if(Math.abs(pntZ[j] - pntZ[k]) < 0.01D)
                if(pntZ[j] > 0.98999999999999999D)
                    linefaces[l++] = 4;
                else
                if(pntZ[j] < -0.98999999999999999D)
                    linefaces[l++] = 5;
        }
        for(i = 0; i < 3; i++)
            for(j = 0; j < 3; j++)
                for(k = 0; k < 3; k++)
                {
                    boxpos[i * 27 + j * 9 + k * 3] = 0.66666999999999998D * (double)(1 - k);
                    boxpos[i * 27 + j * 9 + k * 3 + 1] = 0.66666999999999998D * (double)(1 - j);
                    boxpos[i * 27 + j * 9 + k * 3 + 2] = 0.66666999999999998D * (double)(1 - i);
                }
    }
	//--------------------------------------------------------------------
    public void project()
    {
        for(ip = 0; ip < 32; ip++)
        {
            scrZ[ip] = scalProd(pntX[ip], pntY[ip], pntZ[ip], zaxis[0], zaxis[1], zaxis[2]);
            ap = (5D + scrZ[ip]) * 11D;
            bp = scalProd(pntX[ip], pntY[ip], pntZ[ip], xaxis[0], xaxis[1], xaxis[2]);
            cp = scalProd(pntX[ip], pntY[ip], pntZ[ip], yaxis[0], yaxis[1], yaxis[2]);
            scrX[ip] = 95D + ap * bp;
            scrY[ip] = 95D + ap * cp;
            if((ip & 0x3) == 0)
            {
                jp = ip >> 2;
                projX[jp] = (bp * ap) / 50D;
                projY[jp] = (cp * ap) / 50D;
                projZ[jp] = scrZ[ip];
            }
        }
        for(ip = 0; ip < 6; ip++)
        {
            jp = faces[ip * 3];
            kp = faces[ip * 3 + 1];
            lp = faces[ip * 3 + 2];
            ut[0] = projX[kp] - projX[jp];
            ut[1] = projY[kp] - projY[jp];
            ut[2] = projZ[kp] - projZ[jp];
            vt[0] = projX[lp] - projX[jp];
            vt[1] = projY[lp] - projY[jp];
            vt[2] = projZ[lp] - projZ[jp];
            crossProd(ut, vt, wt);
            frontDir[ip] = wt[2];
        }
        ballnum = 0;
        for(ip = 0; ip < 27; ip++)
            if(cubedata[ip] > 0)
            {
                balltype[ballnum] = cubedata[ip];
                ballOrder[ballnum] = ballnum;
                ballZ[ballnum] = 11D * (5D + scalProd(boxpos[ip * 3], boxpos[ip * 3 + 1], boxpos[ip * 3 + 2], zaxis[0], zaxis[1], zaxis[2]));
                ballX[ballnum] = 95D + ballZ[ballnum] * scalProd(boxpos[ip * 3], boxpos[ip * 3 + 1], boxpos[ip * 3 + 2], xaxis[0], xaxis[1], xaxis[2]);
                ballY[ballnum] = 95D + ballZ[ballnum++] * scalProd(boxpos[ip * 3], boxpos[ip * 3 + 1], boxpos[ip * 3 + 2], yaxis[0], yaxis[1], yaxis[2]);
            }
    }
	//--------------------------------------------------------------------
    public void sortStuff()
    {
        fronts = 0;
        backs = 0;
        for(is = 0; is < 36; is++)
            if(frontDir[linefaces[is * 2]] > 0.0D || frontDir[linefaces[is * 2 + 1]] > 0.0D)
            {
                frontZ[fronts] = scrZ[lFrom[is]] + scrZ[lTo[is]];
                frontOrder[fronts++] = is;
            } else
            {
                backZ[backs] = scrZ[lFrom[is]] + scrZ[lTo[is]];
                backOrder[backs++] = is;
            }
        for(is = 0; is < fronts - 1; is++)
            for(js = is + 1; js < fronts; js++)
                if(frontZ[is] > frontZ[js])
                {
                    ks = frontOrder[is];
                    frontOrder[is] = frontOrder[js];
                    frontOrder[js] = ks;
                    as = frontZ[is];
                    frontZ[is] = frontZ[js];
                    frontZ[js] = as;
                }
        for(is = 0; is < backs - 1; is++)
            for(js = is + 1; js < backs; js++)
                if(backZ[is] > backZ[js])
                {
                    ks = backOrder[is];
                    backOrder[is] = backOrder[js];
                    backOrder[js] = ks;
                    as = backZ[is];
                    backZ[is] = backZ[js];
                    backZ[js] = as;
                }
        for(is = 0; is < ballnum - 1; is++)
            for(js = is + 1; js < ballnum; js++)
                if(ballZ[is] > ballZ[js])
                {
                    ks = ballOrder[is];
                    ballOrder[is] = ballOrder[js];
                    ballOrder[js] = ks;
                    as = ballZ[is];
                    ballZ[is] = ballZ[js];
                    ballZ[js] = as;
                }
    }
	//--------------------------------------------------------------------
    public void drawCube()
    {
        int l1 = 0;
        boolean flag = false;
        bG.setColor(Color.black);
        bG.fillRect(0, 0, 190, 190);
        for(int i1 = 0; i1 < backs; i1++)
        {
            bG.setColor(gCol[l1++]);
            bG.drawLine((int)scrX[lFrom[backOrder[i1]]], (int)scrY[lFrom[backOrder[i1]]], (int)scrX[lTo[backOrder[i1]]], (int)scrY[lTo[backOrder[i1]]]);
        }
        for(int j1 = 0; j1 < ballnum; j1++)
        {
            kd = ballOrder[j1];
            adc = 0.34100000000000003D * ballZ[j1];
            int i2 = (int)adc;
            switch(balltype[kd])
            {
            case 2: // '\002'
            case 10: // '\n'
                bG.drawImage(bballs[i2 - 14], (int)ballX[kd] - i2, (int)ballY[kd] - i2, this);
                break;
            case 4: // '\004'
            case 12: // '\f'
                bG.drawImage(bballs[i2 - 4], (int)ballX[kd] - i2, (int)ballY[kd] - i2, this);
                break;
            default:
                ballMidX = (int)ballX[kd];
                ballMidY = (int)ballY[kd];
                ballR = i2;
                bG.setColor(gCol[(int)(adc * 3D - 39D)]);
                bG.fillOval(ballMidX - i2, ballMidY - i2, i2 * 2, i2 * 2);
                break;
            }
        }
        for(int k1 = 0; k1 < fronts; k1++)
        {
            bG.setColor(gCol[l1++]);
            bG.drawLine((int)scrX[lFrom[frontOrder[k1]]], (int)scrY[lFrom[frontOrder[k1]]], (int)scrX[lTo[frontOrder[k1]]], (int)scrY[lTo[frontOrder[k1]]]);
        }
    }
	//--------------------------------------------------------------------
    public void placeTemp(int i1, int j1)
    {
        if(tempball >= 0)
            cubedata[tempball] = 0;
        tempball = -1;
        dpt = 10000D;
        for(ipt = 0; ipt < 27; ipt++)
            if(cubedata[ipt] == 0)
            {
                apt = 11D * (5D + scalProd(boxpos[ipt * 3], boxpos[ipt * 3 + 1], boxpos[ipt * 3 + 2], zaxis[0], zaxis[1], zaxis[2]));
                bpt = 95D + apt * scalProd(boxpos[ipt * 3], boxpos[ipt * 3 + 1], boxpos[ipt * 3 + 2], xaxis[0], xaxis[1], xaxis[2]);
                cpt = 95D + apt * scalProd(boxpos[ipt * 3], boxpos[ipt * 3 + 1], boxpos[ipt * 3 + 2], yaxis[0], yaxis[1], yaxis[2]);
                apt *= 0.34100000000000003D;
                bpt = (bpt - (double)i1) * (bpt - (double)i1);
                cpt = (cpt - (double)j1) * (cpt - (double)j1);
                if(bpt + cpt < apt * apt && bpt + cpt < dpt)
                {
                    tempball = ipt;
                    dpt = bpt + cpt;
                }
            }
        if(tempball >= 0)
            cubedata[tempball] = 3;
    }
	//--------------------------------------------------------------------
    public void performDrawing()
    {
        if(!drawing)
        {
            drawing = true;
            project();
            sortStuff();
            drawCube();
            drawing = false;
        }
    }
	//--------------------------------------------------------------------
    public void placeComputerBall(int i1)
    {
        cubedata[i1] = 12;
        performDrawing();
        repaint();
        busycount = 10;
    }
	//--------------------------------------------------------------------
    public void computerWins(long l1)
    {
        long l2 = l1;
        for(wi = 0; wi < 27; wi++)
        {
            if((l2 & 3L) > 0L)
                cubedata[wi] = 12;
            l2 >>>= 2;
        }
        bottomApplet.setNewMessage(3);
    }
	//--------------------------------------------------------------------
    public void playerWins(long l1)
    {
        long l2 = l1;
        for(wi = 0; wi < 27; wi++)
        {
            if((l2 & 3L) > 0L)
                cubedata[wi] = 10;
            l2 >>>= 2;
        }
        bottomApplet.setNewMessage(2);
    }
	//--------------------------------------------------------------------
    public void buildGrays()
    {
        for(i = 0; i < 36; i++)
            gCol[i] = new Color(45 + 6 * i, 45 + 6 * i, 45 + 6 * i);
    }
	//--------------------------------------------------------------------
    public void start()
    {
        if(runner == null)
        {
            runner = new Thread(this);
            runner.start();
        }
    }
	//--------------------------------------------------------------------
    public void stop()
    {
        if(runner != null && runner.isAlive())
            runner.stop();
        runner = null;
    }
	//--------------------------------------------------------------------
    public void run()
    {
        while(runner != null) 
        {
            try
            {
                Thread.sleep(Math.max(25L, nextTime - System.currentTimeMillis()));
            }
            catch(InterruptedException _ex) { }
            nextTime = System.currentTimeMillis() + 120L;
            if(busy)
            {
                int k1 = 0;
                for(int i1 = 0; i1 < 27; i1++)
                    if((cubedata[i1] & 0x8) > 0)
                    {
                        cubedata[i1] ^= 0x1;
                        k1 = cubedata[i1];
                    }
                if(busycount > 0)
                {
                    busycount--;
                    if(busycount == 0)
                    {
                        for(int j1 = 0; j1 < 27; j1++)
                            cubedata[j1] &= 0x6;
                        busy = false;
                        bottomApplet.setNewMessage(0);
                    } else
                    if(busycount == 5)
                        playSound(1);
                }
                if(k1 > 0)
                {
                    performDrawing();
                    repaint();
                }
            }
        }
    }
	//--------------------------------------------------------------------
    public void mouseDragged(MouseEvent mouseevent)
    {
        x = mouseevent.getX();
        y = mouseevent.getY();
        if(dragging == 1)
        {
            amd = (double)(lastX - x) * dragScale;
            zaxis[0] += amd * xaxis[0];
            zaxis[1] += amd * xaxis[1];
            zaxis[2] += amd * xaxis[2];
            normalize(zaxis);
            crossProd(yaxis, zaxis, xaxis);
            amd = (double)(lastY - y) * dragScale;
            zaxis[0] += amd * yaxis[0];
            zaxis[1] += amd * yaxis[1];
            zaxis[2] += amd * yaxis[2];
            normalize(zaxis);
            crossProd(zaxis, xaxis, yaxis);
            lastX = x;
            lastY = y;
            performDrawing();
            repaint();
            return;
        }
        if(dragging == 2)
        {
            placeTemp(x - dragOffsX, y - dragOffsY);
            performDrawing();
            repaint();
        }
    }
	//--------------------------------------------------------------------
    public void mouseMoved(MouseEvent mouseevent)
    {
    }
    public void mouseEntered(MouseEvent mouseevent)
    {
    }
    public void mouseExited(MouseEvent mouseevent)
    {
    }
    public void mouseReleased(MouseEvent mouseevent)
    {
        dragging = 0;
    }
    public void mouseClicked(MouseEvent mouseevent)
    {
        if(System.currentTimeMillis() - lastClick < 250L && tempball >= 0)
        {
            cubedata[tempball] = 2;
            mi = tempball;
            tempball = -1;
            performDrawing();
            repaint();
            busycount = -1;
            busy = true;
            mk = 0;
            for(mj = 0; mj < 27; mj++)
                if(cubedata[mj] > 0)
                    mk++;
            if(mk < 27)
            {
                bottomApplet.setNewMessage(1);
                playSound(0);
                rightApplet.registerMove(mi);
                return;
            } else
            {
                bottomApplet.setNewMessage(4);
                return;
            }
        } else
        {
            lastClick = System.currentTimeMillis();
            return;
        }
    }
    public void mousePressed(MouseEvent mouseevent)
    {
        x = mouseevent.getX();
        y = mouseevent.getY();
        lastX = x;
        lastY = y;
        if(!busy)
        {
            if(tempball < 0)
            {
                placeTemp(x, y);
                if(tempball < 0)
                {
                    dragging = 1;
                    return;
                } else
                {
                    dragOffsX = 0;
                    dragOffsY = 0;
                    dragging = 2;
                    performDrawing();
                    repaint();
                    return;
                }
            }
            if((x - ballMidX) * (x - ballMidX) + (y - ballMidY) * (y - ballMidY) < ballR * ballR)
            {
                dragOffsX = x - ballMidX;
                dragOffsY = y - ballMidY;
                dragging = 2;
                return;
            } else
            {
                dragging = 1;
                return;
            }
        } else
        {
            dragging = 1;
            return;
        }
    }
	//--------------------------------------------------------------------
    public double scalProd(double d, double d1, double d2, double d3, double d4, double d5)
    {
        return d * d3 + d1 * d4 + d2 * d5;
    }
	//--------------------------------------------------------------------
    public void normalize(double ad[])
    {
        an = 1.0D / Math.sqrt(ad[0] * ad[0] + ad[1] * ad[1] + ad[2] * ad[2]);
        ad[0] *= an;
        ad[1] *= an;
        ad[2] *= an;
    }
	//--------------------------------------------------------------------
    public void crossProd(double ad[], double ad1[], double ad2[])
    {
        ad2[0] = ad[1] * ad1[2] - ad[2] * ad1[1];
        ad2[1] = ad[2] * ad1[0] - ad[0] * ad1[2];
        ad2[2] = ad[0] * ad1[1] - ad[1] * ad1[0];
    }
	//--------------------------------------------------------------------
    public void playSound(int i1)
    {
        if(playing >= 0)
            AudioPlayer.player.stop(sounds[playing]);
        try
        {
            sounds[i1].reset();
        }
        catch(IOException _ex) { }
        AudioPlayer.player.start(sounds[i1]);
        playing = i1;
    }
	//--------------------------------------------------------------------
    public void update(Graphics g)
    {
        g.drawImage(background, 0, 0, this);
    }
	//--------------------------------------------------------------------
    public void paint(Graphics g)
    {
        g.drawImage(background, 0, 0, this);
    }
}
		
		
///////////////////////////////////////////////////////////////////////////////
// right.java
//
import java.applet.Applet;
import java.applet.AppletContext;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.*;
import java.io.IOException;
import java.io.InputStream;
//------------------------------------------------------------------------------
public class right extends Applet implements Runnable, MouseListener
{
    int i;
    int j;
    int k;
    int l;
    int m;
    int n;
    int o;
    int p;
    int x;
    int y;
    int dbi;
    int mpi;
    int mpj;
    int counter;
    int leaves;
    int numchecks;
    int seed;
    long playerMasks[];
    long computerMasks[];
    long tl;
    long lbase;
    final long pmask = 0x555555555555555L;
    final long cmask = 0xaaaaaaaaaaaaaaaL;
    long posMasks[];
    long currentBoard;
    long li;
    long lj;
    int xdirs[];
    int ydirs[];
    int zdirs[];
    int skill;
    boolean computerToMove;
    Image background;
    Image collection;
    Image but1;
    Image but2;
    Graphics bg;
    long nextTime;
    MediaTracker mt;
    Thread runner;
    ImageProducer improd;
    cluster clusterApplet;
	//----------------------------------------------------------------------
    public right()
    {
        lbase = 3L;
        posMasks = new long[27];
        xdirs = new int[27];
        ydirs = new int[27];
        zdirs = new int[27];
        skill = 2;
    }
	//----------------------------------------------------------------------
    public void init()
    {
        mt = new MediaTracker(this);
        InputStream inputstream = getClass().getResourceAsStream("row2c.gif");
        try
        {
            byte abyte0[] = new byte[inputstream.available()];
            inputstream.read(abyte0);
            collection = Toolkit.getDefaultToolkit().createImage(abyte0);
        }
        catch(IOException _ex) { }
        mt.addImage(collection, 0);
        try
        {
            mt.waitForID(0);
        }
        catch(InterruptedException _ex) { }
        background = createImage(51, 126);
        bg = background.getGraphics();
        improd = collection.getSource();
        but1 = createImage(new FilteredImageSource(improd, new CropImageFilter(0, 126, 30, 19)));
        mt.addImage(but1, 1);
        but2 = createImage(new FilteredImageSource(improd, new CropImageFilter(0, 145, 30, 19)));
        mt.addImage(but2, 1);
        try
        {
            mt.waitForID(1);
        }
        catch(InterruptedException _ex) { }
        bg.drawImage(collection, 0, 0, this);
        drawButtons();
        seed = 1 + (int)(System.currentTimeMillis() & 16383L);
        l = 0;
        for(i = -1; i < 2; i++)
            for(j = -1; j < 2; j++)
                for(k = -1; k < 2; k++)
                {
                    xdirs[l] = i;
                    ydirs[l] = j;
                    zdirs[l++] = k;
                }
        long al[] = new long[70];
        numchecks = 0;
        for(i = 0; i < 3; i++)
            for(j = 0; j < 3; j++)
                for(k = 0; k < 3; k++)
                    for(l = 0; l < 27; l++)
                        if(i + 2 * xdirs[l] >= 0 && i + 2 * xdirs[l] < 3 && j + 2 * ydirs[l] >= 0 && j + 2 * ydirs[l] < 3 && k + 2 * zdirs[l] >= 0 && k + 2 * zdirs[l] < 3 && l != 13)
                            for(m = l + 1; m < 27; m++)
                                if(i + 2 * xdirs[m] >= 0 && i + 2 * xdirs[m] < 3 && j + 2 * ydirs[m] >= 0 && j + 2 * ydirs[m] < 3 && k + 2 * zdirs[m] >= 0 && k + 2 * zdirs[m] < 3 && xdirs[l] * xdirs[m] + ydirs[l] * ydirs[m] + zdirs[l] * zdirs[m] == 0 && m != 13)
                                {
                                    tl = lbase << 2 * (i + 3 * j + 9 * k);
                                    tl |= lbase << 2 * (i + xdirs[l] + 3 * (j + ydirs[l]) + 9 * (k + zdirs[l]));
                                    tl |= lbase << 2 * (i + 2 * xdirs[l] + 3 * (j + 2 * ydirs[l]) + 9 * (k + 2 * zdirs[l]));
                                    tl |= lbase << 2 * (i + xdirs[m] + 3 * (j + ydirs[m]) + 9 * (k + zdirs[m]));
                                    tl |= lbase << 2 * (i + 2 * xdirs[m] + 3 * (j + 2 * ydirs[m]) + 9 * (k + 2 * zdirs[m]));
                                    al[numchecks++] = tl;
                                }
        playerMasks = new long[numchecks];
        computerMasks = new long[numchecks];
        for(i = 0; i < numchecks; i++)
        {
            playerMasks[i] = al[i] & 0x555555555555555L;
            computerMasks[i] = al[i] & 0xaaaaaaaaaaaaaaaL;
        }
        tl = 3L;
        for(i = 0; i < 27; i++)
        {
            posMasks[i] = tl;
            tl = tl << 2;
        }
        clearBoard();
        addMouseListener(this);
    }
	//----------------------------------------------------------------------
    public void drawButtons()
    {
        for(dbi = 0; dbi < 4; dbi++)
            if(dbi + 1 == skill)
                bg.drawImage(but2, 21, 107 - dbi * 27, this);
            else
                bg.drawImage(but1, 21, 107 - dbi * 27, this);
    }
	//----------------------------------------------------------------------
    public void start()
    {
        if(runner == null)
        {
            runner = new Thread(this);
            runner.start();
        }
    }
	//----------------------------------------------------------------------
    public void stop()
    {
        if(runner != null && runner.isAlive())
            runner.stop();
        runner = null;
    }
	//----------------------------------------------------------------------
    public void run()
    {
        while(runner != null) 
        {
            try
            {
                Thread.sleep(Math.max(25L, nextTime - System.currentTimeMillis()));
            }
            catch(InterruptedException _ex) { }
            nextTime = System.currentTimeMillis() + 100L;
            if(computerToMove)
            {
                computerToMove = false;
                makeComputerMove();
            }
        }
    }
	//----------------------------------------------------------------------
    public void registerMove(int i1)
    {
        int k1 = -1;
        if(clusterApplet == null)
            clusterApplet = (cluster)getAppletContext().getApplet("cluster");
        currentBoard |= posMasks[i1] & 0x555555555555555L;
        for(int j1 = 0; j1 < numchecks; j1++)
            if((currentBoard & playerMasks[j1]) == playerMasks[j1])
                k1 = j1;
        if(k1 >= 0)
        {
            clusterApplet.playerWins(playerMasks[k1]);
            return;
        } else
        {
            computerToMove = true;
            return;
        }
    }
	//----------------------------------------------------------------------
    public void clearBoard()
    {
        currentBoard = 0L;
        computerToMove = false;
    }
	//----------------------------------------------------------------------
    public void makeComputerMove()
    {
        int j2 = -2000;
        int k2 = 0;
        leaves = 0;
        int i1 = randi() % 27;
        for(int i2 = 0; i2 < 27; i2++)
        {
            if((currentBoard & posMasks[i1]) == 0L)
            {
                int k1 = evaluateMove(currentBoard | posMasks[i1] & 0xaaaaaaaaaaaaaaaL, 1, j2);
                if(k1 > j2)
                {
                    j2 = k1;
                    k2 = i1;
                }
            }
            i1 = (i1 + 1) % 27;
        }
        currentBoard |= posMasks[k2] & 0xaaaaaaaaaaaaaaaL;
        int l1 = -1;
        for(int j1 = 0; j1 < numchecks; j1++)
            if((currentBoard & computerMasks[j1]) == computerMasks[j1])
                l1 = j1;
        if(l1 >= 0)
        {
            clusterApplet.computerWins(computerMasks[l1]);
            return;
        } else
        {
            clusterApplet.placeComputerBall(k2);
            return;
        }
    }
	//----------------------------------------------------------------------
    public int evaluateMove(long l1, int i1, int j1)
    {
        if(i1 >= skill)
        {
            leaves++;
            int k2 = 0;
            for(int k1 = 0; k1 < numchecks; k1++)
            {
                if((l1 & playerMasks[k1]) == 0L)
                    k2++;
                else
                if((l1 & playerMasks[k1]) == playerMasks[k1])
                    k2 -= 1000;
                if((l1 & computerMasks[k1]) == 0L)
                    k2--;
                else
                if((l1 & computerMasks[k1]) == computerMasks[k1])
                    k2 += 1000;
            }
            return k2;
        }
        if((i1 & 0x1) == 1)
        {
            int j3 = 2000;
            for(int i2 = 0; i2 < 27; i2++)
                if((l1 & posMasks[i2]) == 0L)
                {
                    int l2 = evaluateMove(l1 | posMasks[i2] & 0x555555555555555L, i1 + 1, j3);
                    if(l2 < j3)
                    {
                        j3 = l2;
                        if(l2 <= j1)
                            i2 = 27;
                    }
                }
            return j3;
        }
        int k3 = -2000;
        for(int j2 = 0; j2 < 27; j2++)
            if((l1 & posMasks[j2]) == 0L)
            {
                int i3 = evaluateMove(l1 | posMasks[j2] & 0xaaaaaaaaaaaaaaaL, i1 + 1, k3);
                if(i3 > k3)
                {
                    k3 = i3;
                    if(i3 >= j1)
                        j2 = 27;
                }
            }
        return k3;
    }
	//----------------------------------------------------------------------
    public void mouseEntered(MouseEvent mouseevent)
    {
    }
    public void mouseExited(MouseEvent mouseevent)
    {
    }
    public void mouseReleased(MouseEvent mouseevent)
    {
    }
    public void mouseClicked(MouseEvent mouseevent)
    {
    }
    public void mousePressed(MouseEvent mouseevent)
    {
        x = mouseevent.getX();
        y = mouseevent.getY();
        mpj = -1;
        for(mpi = 0; mpi < 4; mpi++)
            if(y > 109 - 27 * mpi && y < 122 - 27 * mpi && x > 25 && x < 45)
                mpj = mpi;
        if(mpj >= 0)
        {
            skill = mpj + 1;
            drawButtons();
            repaint();
        }
    }
	//----------------------------------------------------------------------
    public int randi()
    {
        seed = (seed * 171) % 30269;
        return seed;
    }
	//----------------------------------------------------------------------
    public void update(Graphics g)
    {
        g.drawImage(background, 0, 0, this);
    }
	//----------------------------------------------------------------------
    public void paint(Graphics g)
    {
        g.drawImage(background, 0, 0, this);
    }
}

///////////////////////////////////////////////////////////////////////////////////
// bottom.java
// 
import java.applet.Applet;
import java.applet.AppletContext;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.IOException;
import java.io.InputStream;
//-------------------------------------------------------------------
public class bottom extends Applet implements Runnable, MouseListener
{
    int i;
    int j;
    int k;
    int l;
    int x;
    int y;
    int grayCount;
    int currMes;
    int newMes;
    final String messages[] = {
        "Your turn", "Computer's turn", "You win!", "Computer wins", "Tied game"
    };
    Image background;
    Graphics bg;
    long nextTime;
    MediaTracker mt;
    Thread runner;
    Color grays[];
    Font f;
    FontMetrics fm;
    cluster clusterApplet;
	//------------------------------------------------------------
    public bottom()
    {
        currMes = -1;
        newMes = -1;
        grays = new Color[5];
    }
	//------------------------------------------------------------
    public void init()
    {
        mt = new MediaTracker(this);
        InputStream inputstream = getClass().getResourceAsStream("row4b.gif");
        try
        {
            byte abyte0[] = new byte[inputstream.available()];
            inputstream.read(abyte0);
            background = Toolkit.getDefaultToolkit().createImage(abyte0);
        }
        catch(IOException _ex)
        {
            background = createImage(176, 21);
        }
        mt.addImage(background, 0);
        try
        {
            mt.waitForID(0);
        }
        catch(InterruptedException _ex) { }
        for(i = 0; i < 5; i++)
            grays[i] = new Color((i + 1) * 51, (i + 1) * 51, (i + 1) * 51);
        f = new Font("SansSerif", 1, 12);
        fm = getFontMetrics(f);
        setFont(f);
        addMouseListener(this);
    }
	//------------------------------------------------------------
    public void start()
    {
        if(runner == null)
        {
            runner = new Thread(this);
            runner.start();
        }
    }
	//------------------------------------------------------------
    public void stop()
    {
        if(runner != null && runner.isAlive())
            runner.stop();
        runner = null;
    }
	//------------------------------------------------------------
    public void run()
    {
        while(runner != null) 
        {
            try
            {
                Thread.sleep(Math.max(25L, nextTime - System.currentTimeMillis()));
            }
            catch(InterruptedException _ex) { }
            nextTime = System.currentTimeMillis() + 80L;
            if(newMes >= 0)
            {
                if(grayCount > 0)
                {
                    grayCount--;
                } else
                {
                    currMes = newMes;
                    newMes = -1;
                }
                repaint();
            } else
            if(grayCount < 4 && currMes >= 0)
            {
                grayCount++;
                repaint();
            }
        }
    }
	//------------------------------------------------------------
    public void setNewMessage(int i1)
    {
        newMes = i1;
    }
	//------------------------------------------------------------
    public void update(Graphics g)
    {
        g.drawImage(background, 0, 0, this);
        if(currMes >= 0)
        {
            g.setColor(grays[grayCount]);
            g.drawString(messages[currMes], 100 - fm.stringWidth(messages[currMes]) / 2, 17);
        }
    }
	//------------------------------------------------------------
    public void paint(Graphics g)
    {
        update(g);
    }
	//------------------------------------------------------------
    public void mouseEntered(MouseEvent mouseevent)
    {
    }
    public void mouseExited(MouseEvent mouseevent)
    {
    }
    public void mouseReleased(MouseEvent mouseevent)
    {
    }
    public void mouseClicked(MouseEvent mouseevent)
    {
    }
    public void mousePressed(MouseEvent mouseevent)
    {
        x = mouseevent.getX();
        y = mouseevent.getY();
        if((x - 12) * (x - 12) + (y - 11 * y - 11) < 122)
        {
            if(clusterApplet == null)
                clusterApplet = (cluster)getAppletContext().getApplet("cluster");
            clusterApplet.reset();
            clusterApplet.repaint();
        }
    }
}

インターネットスタートページ 鈴木維一郎 石橋三重子
         
               
                   


©2000 kg-group Inc.